How to use the importlab.resolve.System function in importlab

To help you get started, we’ve selected a few importlab examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github google / importlab / tests / test_graph.py View on Github external
deps.append((k, sorted(v)))
        return list(sorted(deps))

    def ordered_sorted_source_files(self):
        return [list(sorted(x)) for x in self.sorted_source_files()]


# Deps = { file : ([resolved deps], [broken deps], {dep_file:provenance}) }

SIMPLE_DEPS = {
        "a.py": (["b.py", "c.py"], [],
                 {"b.py": resolve.Local("b.py", "b", "fs1"),
                  "c.py": resolve.Local("c.py", "c", "fs2")
                  }),
        "b.py": (["d.py"], ["e"],
                 {"d.py": resolve.System("d.py", "d")})
}

SIMPLE_CYCLIC_DEPS = {
        "a.py": (["b.py", "c.py"], ["e"], {}),
        "b.py": (["d.py", "a.py"], ["f"], {}),
}


class TestDependencyGraph(unittest.TestCase):
    """Tests for DependencyGraph."""

    def check_order(self, xs, *args):
        """Checks that args form an increasing sequence within xs."""
        indices = [xs.index(arg) for arg in args]
        for i in range(1, len(indices)):
            self.assertTrue(indices[i - 1] < indices[i],
github google / importlab / tests / test_graph.py View on Github external
deps.append((k, sorted(v)))
        return list(sorted(deps))

    def ordered_sorted_source_files(self):
        return [list(sorted(x)) for x in self.sorted_source_files()]


# Deps = { file : ([resolved deps], [broken deps], {dep_file:provenance}) }

SIMPLE_DEPS = {
        "a.py": (["b.py", "c.py"], [],
                 {"b.py": resolve.Local("b.py", "b", "fs1"),
                  "c.py": resolve.Local("c.py", "c", "fs2")
                  }),
        "b.py": (["d.py"], ["e"],
                 {"d.py": resolve.System("d.py", "d")})
}

SIMPLE_NONPY_DEPS = {"a.pyi": (["b.py"], [], {})}

SIMPLE_CYCLIC_DEPS = {
        "a.py": (["b.py", "c.py"], ["e"], {}),
        "b.py": (["d.py", "a.py"], ["f"], {}),
}

SIMPLE_SYSTEM_DEPS = {
        "a.py": (["b.py"], [], {"b.py": resolve.System("b.py", "b")}),
        "b.py": (["c.py"], [], {"c.py": resolve.System("c.py", "c")}),
}


class TestDependencyGraph(unittest.TestCase):
github google / importlab / tests / test_graph.py View on Github external
"c.py": resolve.Local("c.py", "c", "fs2")
                  }),
        "b.py": (["d.py"], ["e"],
                 {"d.py": resolve.System("d.py", "d")})
}

SIMPLE_NONPY_DEPS = {"a.pyi": (["b.py"], [], {})}

SIMPLE_CYCLIC_DEPS = {
        "a.py": (["b.py", "c.py"], ["e"], {}),
        "b.py": (["d.py", "a.py"], ["f"], {}),
}

SIMPLE_SYSTEM_DEPS = {
        "a.py": (["b.py"], [], {"b.py": resolve.System("b.py", "b")}),
        "b.py": (["c.py"], [], {"c.py": resolve.System("c.py", "c")}),
}


class TestDependencyGraph(unittest.TestCase):
    """Tests for DependencyGraph."""

    def check_order(self, xs, *args):
        """Checks that args form an increasing sequence within xs."""
        indices = [xs.index(arg) for arg in args]
        for i in range(1, len(indices)):
            self.assertTrue(indices[i - 1] < indices[i],
                            "%s comes before %s" % (args[i], args[i - 1]))

    def test_simple(self):
        g = FakeImportGraph(SIMPLE_DEPS)
        g.add_file_recursive("a.py")
github google / importlab / tests / test_resolve.py View on Github external
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")
github google / importlab / tests / test_resolve.py View on Github external
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")
github google / importlab / tests / test_graph.py View on Github external
def mock_resolve_import(resolver_self, item):
            resolved_file = resolve_import(resolver_self, item)
            return resolve.System(resolved_file.path, resolved_file.module_name)
github google / importlab / importlab / resolve.py View on Github external
# If the module isn't found in the explicit pythonpath, see if python
        # itself resolved it.
        if item.source:
            prefix, ext = os.path.splitext(item.source)
            mod_name = name
            # We need to check for importing a symbol here too.
            if short_name:
                mod = prefix.replace(os.path.sep, '.')
                mod = utils.strip_suffix(mod, '.__init__')
                if not mod.endswith(name) and mod.endswith(short_name):
                    mod_name = short_name

            if ext == '.pyc':
                pyfile = prefix + '.py'
                if os.path.exists(pyfile):
                    return System(pyfile, mod_name)
            elif not ext:
                pyfile = os.path.join(prefix, "__init__.py")
                if os.path.exists(pyfile):
                    return System(pyfile, mod_name)
            return System(item.source, mod_name)

        raise ImportException(name)
github google / importlab / importlab / resolve.py View on Github external
# We need to check for importing a symbol here too.
            if short_name:
                mod = prefix.replace(os.path.sep, '.')
                mod = utils.strip_suffix(mod, '.__init__')
                if not mod.endswith(name) and mod.endswith(short_name):
                    mod_name = short_name

            if ext == '.pyc':
                pyfile = prefix + '.py'
                if os.path.exists(pyfile):
                    return System(pyfile, mod_name)
            elif not ext:
                pyfile = os.path.join(prefix, "__init__.py")
                if os.path.exists(pyfile):
                    return System(pyfile, mod_name)
            return System(item.source, mod_name)

        raise ImportException(name)
github google / importlab / importlab / resolve.py View on Github external
mod_name = name
            # We need to check for importing a symbol here too.
            if short_name:
                mod = prefix.replace(os.path.sep, '.')
                mod = utils.strip_suffix(mod, '.__init__')
                if not mod.endswith(name) and mod.endswith(short_name):
                    mod_name = short_name

            if ext == '.pyc':
                pyfile = prefix + '.py'
                if os.path.exists(pyfile):
                    return System(pyfile, mod_name)
            elif not ext:
                pyfile = os.path.join(prefix, "__init__.py")
                if os.path.exists(pyfile):
                    return System(pyfile, mod_name)
            return System(item.source, mod_name)

        raise ImportException(name)
github google / importlab / importlab / resolve.py View on Github external
short_filename = os.path.dirname(filename)
            files.append((short_name, short_filename))

        for module_name, path in files:
            for fs in self.fs_path:
                f = self._find_file(fs, path)
                if not f or f == self.current_module.path:
                    # We cannot import a file from itself.
                    continue
                if item.is_relative():
                    package_name = self.current_module.package_name
                    if package_name is None:
                        # Relative import in non-package
                        raise ImportException(name)
                    module_name = get_absolute_name(package_name, module_name)
                    if isinstance(self.current_module, System):
                        return System(f, module_name)
                return Local(f, module_name, fs)

        # If the module isn't found in the explicit pythonpath, see if python
        # itself resolved it.
        if item.source:
            prefix, ext = os.path.splitext(item.source)
            mod_name = name
            # We need to check for importing a symbol here too.
            if short_name:
                mod = prefix.replace(os.path.sep, '.')
                mod = utils.strip_suffix(mod, '.__init__')
                if not mod.endswith(name) and mod.endswith(short_name):
                    mod_name = short_name

            if ext == '.pyc':