コード例 #1
0
    def _add_imported_module(self, node, importedmodname):
        """notify an imported module, used to analyze dependencies"""
        module_file = node.root().file
        context_name = node.root().name
        base = os.path.splitext(os.path.basename(module_file))[0]

        try:
            importedmodname = get_module_part(importedmodname, module_file)
        except ImportError:
            pass

        if context_name == importedmodname:
            self.add_message("import-self", node=node)

        elif not is_standard_module(importedmodname):
            # if this is not a package __init__ module
            if base != "__init__" and context_name not in self._module_pkg:
                # record the module's parent, or the module itself if this is
                # a top level module, as the package it belongs to
                self._module_pkg[context_name] = context_name.rsplit(".", 1)[0]

            # handle dependencies
            importedmodnames = self.stats["dependencies"].setdefault(
                importedmodname, set()
            )
            if context_name not in importedmodnames:
                importedmodnames.add(context_name)

            # update import graph
            self.import_graph[context_name].add(importedmodname)
            if not self.linter.is_message_enabled("cyclic-import", line=node.lineno):
                self._excluded_edges[context_name].add(importedmodname)
コード例 #2
0
ファイル: inspector.py プロジェクト: BadRushdeep/ECellWeb2k18
    def visit_importfrom(self, node):
        """visit an astroid.ImportFrom node

        resolve module dependencies
        """
        basename = node.modname
        context_file = node.root().file
        if context_file is not None:
            relative = modutils.is_relative(basename, context_file)
        else:
            relative = False
        for name in node.names:
            if name[0] == '*':
                continue
            # analyze dependencies
            fullname = '%s.%s' % (basename, name[0])
            if fullname.find('.') > -1:
                try:
                    # TODO: don't use get_module_part,
                    # missing package precedence
                    fullname = modutils.get_module_part(fullname, context_file)
                except ImportError:
                    continue
            if fullname != basename:
                self._imported_module(node, fullname, relative)
コード例 #3
0
ファイル: inspector.py プロジェクト: DonJayamanne/pylint
    def visit_importfrom(self, node):
        """visit an astroid.From node

        resolve module dependencies
        """
        basename = node.modname
        context_file = node.root().file
        if context_file is not None:
            relative = modutils.is_relative(basename, context_file)
        else:
            relative = False
        for name in node.names:
            if name[0] == '*':
                continue
            # analyze dependencies
            fullname = '%s.%s' % (basename, name[0])
            if fullname.find('.') > -1:
                try:
                    # TODO: don't use get_module_part,
                    # missing package precedence
                    fullname = modutils.get_module_part(fullname,
                                                        context_file)
                except ImportError:
                    continue
            if fullname != basename:
                self._imported_module(node, fullname, relative)
コード例 #4
0
ファイル: imports.py プロジェクト: shahmatwu/appstrength
    def _add_imported_module(self, node, importedmodname):
        """notify an imported module, used to analyze dependencies"""
        module_file = node.root().file
        context_name = node.root().name
        base = os.path.splitext(os.path.basename(module_file))[0]

        # Determine if we have a `from .something import` in a package's
        # __init__. This means the module will never be able to import
        # itself using this condition (the level will be bigger or
        # if the same module is named as the package, it will be different
        # anyway).
        if isinstance(node, astroid.ImportFrom):
            if node.level and node.level > 0 and base == "__init__":
                return

        try:
            importedmodname = get_module_part(importedmodname, module_file)
        except ImportError:
            pass

        if context_name == importedmodname:
            self.add_message("import-self", node=node)
        elif not is_standard_module(importedmodname):
            # handle dependencies
            importedmodnames = self.stats["dependencies"].setdefault(importedmodname, set())
            if context_name not in importedmodnames:
                importedmodnames.add(context_name)
            # update import graph
            mgraph = self.import_graph[context_name]
            if importedmodname not in mgraph:
                mgraph.add(importedmodname)
コード例 #5
0
ファイル: imports.py プロジェクト: slavfox/pylint
    def _add_imported_module(self, node, importedmodname):
        """notify an imported module, used to analyze dependencies"""
        module_file = node.root().file
        context_name = node.root().name
        base = os.path.splitext(os.path.basename(module_file))[0]

        try:
            importedmodname = modutils.get_module_part(importedmodname,
                                                       module_file)
        except ImportError:
            pass

        if context_name == importedmodname:
            self.add_message("import-self", node=node)

        elif not modutils.is_standard_module(importedmodname):
            # if this is not a package __init__ module
            if base != "__init__" and context_name not in self._module_pkg:
                # record the module's parent, or the module itself if this is
                # a top level module, as the package it belongs to
                self._module_pkg[context_name] = context_name.rsplit(".", 1)[0]

            # handle dependencies
            importedmodnames = self.stats["dependencies"].setdefault(
                importedmodname, set())
            if context_name not in importedmodnames:
                importedmodnames.add(context_name)

            # update import graph
            self.import_graph[context_name].add(importedmodname)
            if not self.linter.is_message_enabled("cyclic-import",
                                                  line=node.lineno):
                self._excluded_edges[context_name].add(importedmodname)
コード例 #6
0
ファイル: imports.py プロジェクト: mar-chi-pan/pylint
    def _add_imported_module(self, node, importedmodname):
        """notify an imported module, used to analyze dependencies"""
        module_file = node.root().file
        context_name = node.root().name
        base = os.path.splitext(os.path.basename(module_file))[0]

        # Determine if we have a `from .something import` in a package's
        # __init__. This means the module will never be able to import
        # itself using this condition (the level will be bigger or
        # if the same module is named as the package, it will be different
        # anyway).
        if isinstance(node, astroid.ImportFrom):
            if node.level and node.level > 0 and base == '__init__':
                return

        try:
            importedmodname = get_module_part(importedmodname,
                                              module_file)
        except ImportError:
            pass

        if context_name == importedmodname:
            self.add_message('import-self', node=node)
        elif not is_standard_module(importedmodname):
            # handle dependencies
            importedmodnames = self.stats['dependencies'].setdefault(
                importedmodname, set())
            if context_name not in importedmodnames:
                importedmodnames.add(context_name)

            # update import graph
            self.import_graph[context_name].add(importedmodname)
            if not self.linter.is_message_enabled('cyclic-import', line=node.lineno):
                self._excluded_edges[context_name].add(importedmodname)
コード例 #7
0
ファイル: pylinter.py プロジェクト: wtracy/pylint
 def _load_reporter_class(self):
     qname = self._reporter_name
     module = modutils.load_module_from_name(
         modutils.get_module_part(qname))
     class_name = qname.split(".")[-1]
     reporter_class = getattr(module, class_name)
     return reporter_class
コード例 #8
0
 def test_knownValues_get_module_part_3(self):
     """relative import from given file"""
     self.assertEqual(
         modutils.get_module_part("node_classes.AssName",
                                  modutils.__file__),
         "node_classes",
     )
コード例 #9
0
ファイル: lint.py プロジェクト: MaraKovalcik/Flask
 def _load_reporter(self):
     name = self._reporter_name.lower()
     if name in self._reporters:
         self.set_reporter(self._reporters[name]())
     else:
         qname = self._reporter_name
         module = load_module_from_name(get_module_part(qname))
         class_name = qname.split('.')[-1]
         reporter_class = getattr(module, class_name)
         self.set_reporter(reporter_class())
コード例 #10
0
ファイル: lint.py プロジェクト: CoherentLabs/depot_tools
 def _load_reporter(self):
     name = self._reporter_name.lower()
     if name in self._reporters:
         self.set_reporter(self._reporters[name]())
     else:
         qname = self._reporter_name
         module = load_module_from_name(get_module_part(qname))
         class_name = qname.split('.')[-1]
         reporter_class = getattr(module, class_name)
         self.set_reporter(reporter_class())
コード例 #11
0
 def visit_from(self, node):
     """triggered when an import statement is seen"""
     basename = node.modname
     for name, alias in node.names:
         fullname = '{0}.{1}'.format(basename, name)
         self._check_verboten_import(node, fullname)
         if fullname.find('.') > -1:
             try:
                 fullname = get_module_part(fullname,
                                            context_file=node.root().file)
             except ImportError as ex:
                 # this is checked elsewhere in pylint (F0401)
                 continue
         if alias == None:
             alias = fullname         
         self.imported_modules.update({alias: fullname})
コード例 #12
0
ファイル: imports.py プロジェクト: 173210/depot_tools
 def _add_imported_module(self, node, importedmodname):
     """notify an imported module, used to analyze dependencies"""
     try:
         importedmodname = get_module_part(importedmodname)
     except ImportError:
         pass
     context_name = node.root().name
     if context_name == importedmodname:
         # module importing itself !
         self.add_message('import-self', node=node)
     elif not is_standard_module(importedmodname):
         # handle dependencies
         importedmodnames = self.stats['dependencies'].setdefault(
             importedmodname, set())
         if not context_name in importedmodnames:
             importedmodnames.add(context_name)
         # update import graph
         mgraph = self.import_graph[context_name]
         if importedmodname not in mgraph:
             mgraph.add(importedmodname)
コード例 #13
0
 def _add_imported_module(self, node, importedmodname):
     """notify an imported module, used to analyze dependencies"""
     try:
         importedmodname = get_module_part(importedmodname)
     except ImportError:
         pass
     context_name = node.root().name
     if context_name == importedmodname:
         # module importing itself !
         self.add_message('import-self', node=node)
     elif not is_standard_module(importedmodname):
         # handle dependencies
         importedmodnames = self.stats['dependencies'].setdefault(
             importedmodname, set())
         if not context_name in importedmodnames:
             importedmodnames.add(context_name)
         # update import graph
         mgraph = self.import_graph.setdefault(context_name, set())
         if not importedmodname in mgraph:
             mgraph.add(importedmodname)
コード例 #14
0
    def visit_importfrom(self, node):
        """visit an astroid.ImportFrom node

        resolve module dependencies
        """
        basename = node.modname
        context_file = node.root().file
        if context_file is not None:
            relative = modutils.is_relative(basename, context_file)
        else:
            relative = False
        for name in node.names:
            if name[0] == "*":
                continue
            # analyze dependencies
            fullname = "%s.%s" % (basename, name[0])
            if fullname.find(".") > -1:
                try:
                    fullname = modutils.get_module_part(fullname, context_file)
                except ImportError:
                    continue
            if fullname != basename:
                self._imported_module(node, fullname, relative)
コード例 #15
0
 def test_knownValues_get_module_part_2(self):
     self.assertEqual(modutils.get_module_part('astroid.modutils.get_module_part'),
                      'astroid.modutils')
コード例 #16
0
 def test_knownValues_get_module_part_1(self):
     self.assertEqual(
         modutils.get_module_part("astroid.modutils"), "astroid.modutils"
     )
コード例 #17
0
 def test_knownValues_get_builtin_module_part(self):
     self.assertEqual(modutils.get_module_part('sys.path'), 'sys')
     self.assertEqual(modutils.get_module_part('sys.path', '__file__'), 'sys')
コード例 #18
0
 def test_knownValues_get_compiled_module_part(self):
     self.assertEqual(modutils.get_module_part('math.log10'), 'math')
     self.assertEqual(modutils.get_module_part('math.log10', __file__), 'math')
コード例 #19
0
 def test_knownValues_get_compiled_module_part(self):
     self.assertEqual(modutils.get_module_part('math.log10'), 'math')
     self.assertEqual(modutils.get_module_part('math.log10', __file__), 'math')
コード例 #20
0
 def test_knownValues_get_builtin_module_part(self):
     self.assertEqual(modutils.get_module_part('sys.path'), 'sys')
     self.assertEqual(modutils.get_module_part('sys.path', '__file__'), 'sys')
コード例 #21
0
 def test_knownValues_get_module_part_3(self):
     """relative import from given file"""
     self.assertEqual(modutils.get_module_part('node_classes.AssName',
                                               modutils.__file__), 'node_classes')
コード例 #22
0
 def test_knownValues_get_module_part_2(self):
     self.assertEqual(modutils.get_module_part('astroid.modutils.get_module_part'),
                      'astroid.modutils')
コード例 #23
0
 def test_known_values_get_compiled_module_part(self) -> None:
     self.assertEqual(modutils.get_module_part("math.log10"), "math")
     self.assertEqual(modutils.get_module_part("math.log10", __file__),
                      "math")
コード例 #24
0
 def test_known_values_get_module_part_2(self) -> None:
     self.assertEqual(
         modutils.get_module_part("astroid.modutils.get_module_part"),
         "astroid.modutils",
     )
コード例 #25
0
 def test_known_values_get_builtin_module_part(self) -> None:
     self.assertEqual(modutils.get_module_part("sys.path"), "sys")
     self.assertEqual(modutils.get_module_part("sys.path", "__file__"),
                      "sys")