Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def make_resolver(self, filename, module_name):
module = resolve.Local(filename, module_name, self.py_fs)
return resolve.Resolver(self.path, module)
def testResolveSystemRelative(self):
with utils.Tempdir() as d:
os_fs = fs.OSFileSystem(d.path)
fspath = [os_fs]
d.create_file("foo/x.py")
d.create_file("foo/y.py")
imp = parsepy.ImportStatement(".y")
module = resolve.System(d["foo/x.py"], "foo.x")
r = resolve.Resolver(fspath, module)
f = r.resolve_import(imp)
self.assertEqual(f.path, d["foo/y.py"])
self.assertTrue(isinstance(f, resolve.System))
self.assertEqual(f.module_name, "foo.y")
def patch_resolve_import(self):
"""Patch resolve_import to always return a System file."""
resolve_import = resolve.Resolver.resolve_import
def mock_resolve_import(resolver_self, item):
resolved_file = resolve_import(resolver_self, item)
return resolve.System(resolved_file.path, resolved_file.module_name)
resolve.Resolver.resolve_import = mock_resolve_import
yield
resolve.Resolver.resolve_import = resolve_import
def testResolveRelativeFromInitFileWithModule(self):
parent = resolve.Direct("baz/__init__.py", "baz")
imp = parsepy.ImportStatement(".f")
r = resolve.Resolver(self.path, parent)
f = r.resolve_import(imp)
self.assertTrue(isinstance(f, resolve.Local))
self.assertEqual(f.path, "baz/f.py")
self.assertEqual(f.module_name, "baz.f")
def get_file_deps(self, filename):
r = resolve.Resolver(self.path, filename)
resolved = []
unresolved = []
provenance = {}
for imp in parsepy.get_imports(filename, self.env.python_version):
try:
f = r.resolve_import(imp)
if not f.is_extension():
full_path = os.path.abspath(f.path)
resolved.append(full_path)
provenance[full_path] = f
except resolve.ImportException:
unresolved.append(imp)
return (resolved, unresolved, provenance)
def get_file_deps(self, filename):
resolved = []
unresolved = []
parent = self.provenance[filename]
r = resolve.Resolver(self.path, parent)
for imp in parsepy.get_imports(filename, self.env.python_version):
try:
f = r.resolve_import(imp)
if f.is_extension():
continue
full_path = os.path.abspath(f.path)
resolved.append(full_path)
self.provenance[full_path] = f
except resolve.ImportException:
unresolved.append(imp)
return (resolved, unresolved)