Beispiel #1
0
    def set_option(self, optname, value, action=None, optdict=None):
        """overridden from configuration.OptionsProviderMixin to handle some
        special options
        """
        if optname in self._options_methods or optname in self._bw_options_methods:
            if value:
                try:
                    meth = self._options_methods[optname]
                except KeyError:
                    meth = self._bw_options_methods[optname]
                    warn("%s is deprecated, replace it by %s" % (optname, optname.split("-")[0]), DeprecationWarning)
                value = check_csv(None, optname, value)
                if isinstance(value, (list, tuple)):
                    for _id in value:
                        meth(_id)
                else:
                    meth(value)
        elif optname == "output-format":
            if value.lower() in REPORTER_OPT_MAP:
                self.set_reporter(REPORTER_OPT_MAP[value.lower()]())
            else:
                module = load_module_from_name(get_module_part(value))
                class_name = value.split(".")[-1]
                reporter_class = getattr(module, class_name)
                self.set_reporter(reporter_class())

        try:
            BaseRawChecker.set_option(self, optname, value, action, optdict)
        except UnsupportedAction:
            print >> sys.stderr, "option %s can't be read from config file" % optname
 def visit_from(self, node):
     """visit an astng.From node
     
     resolve module dependencies
     """
     basename = node.modname
     context_file = node.root().file
     if context_file is not None:
         relative = is_relative(basename, context_file)
     else:
         relative = False
     for name in node.names:
         if name[0] == '*':
             fullname = basename
         else:
             fullname = '%s.%s' % (basename, name[0])
             if fullname.find('.') > -1:
                 try:
                     # XXX: don't use get_module_part, missing package precedence
                     mod_fullname = get_module_part(fullname,context_file)
                     if(mod_fullname == fullname):
                         self.from_modname_imports+=1;
                         node.modname_import = True;
                     fullname = mod_fullname
                 except ImportError:
                     print "Unresolved From -", node.as_string(),"File -",node.root().name,"Lineno - ",node.fromlineno 
                     self.bad_from_imports+=1
                     continue
         self._imported_module(node, fullname, relative)
 def visit_from(self, node):
     """triggered when a from statement is seen"""
     basename = node.modname
     if self._module_not_exists(node, basename):
         return
     if basename == '__future__':
         # check if this is the first non-docstring statement in the module
         prev = node.previous_sibling()
         if prev:
             # consecutive future statements are possible
             if not (isinstance(prev, astng.From)
                    and prev.modname == '__future__'):
                 self.add_message('W0410', node=node)
     self._check_deprecated(node, basename)
     level = node.level
     if level > 0: # explicit relative import (leading dots)
         relative = True
     else:
         relative = self._check_relative(node, basename)
     for name, _ in node.names:
         if name == '*':
             self.add_message('W0401', args=basename, node=node)
             continue
         # handle reimport
         self._check_reimport(node, name, basename, level)
         # analyze dependencies
         fullname = '.' * level + '%s.%s' % (basename, name)
         fullname = get_module_part(fullname,context_file=node.root().file)
         self._imported_module(node, fullname, relative)
Beispiel #4
0
    def set_option(self, optname, value, action=None, optdict=None):
        """overridden from configuration.OptionsProviderMixin to handle some
        special options
        """
        if optname in self._options_methods or optname in self._bw_options_methods:
            if value:
                try:
                    meth = self._options_methods[optname]
                except KeyError:
                    meth = self._bw_options_methods[optname]
                    warn(
                        '%s is deprecated, replace it by %s' %
                        (optname, optname.split('-')[0]), DeprecationWarning)
                value = check_csv(None, optname, value)
                if isinstance(value, (list, tuple)):
                    for _id in value:
                        meth(_id)
                else:
                    meth(value)
        elif optname == 'output-format':
            if value.lower() in REPORTER_OPT_MAP:
                self.set_reporter(REPORTER_OPT_MAP[value.lower()]())
            else:
                module = load_module_from_name(get_module_part(value))
                class_name = value.split('.')[-1]
                reporter_class = getattr(module, class_name)
                self.set_reporter(reporter_class())

        try:
            BaseRawChecker.set_option(self, optname, value, action, optdict)
        except UnsupportedAction:
            print >> sys.stderr, 'option %s can\'t be read from config file' % \
                  optname
Beispiel #5
0
 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())
Beispiel #6
0
 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())
Beispiel #7
0
 def visit_from(self, node):
     """triggered when an import statement is seen"""
     basename = node.modname
     for name, alias in node.names:
         fullname = '%s.%s' % (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, ex:
                 # this is checked elsewhere in pylint (F0401)
                 continue
         if alias == None:
             alias = fullname         
         self.imported_modules.update({alias: fullname})
Beispiel #8
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})
Beispiel #9
0
 def _add_imported_module(self, node, importedmodname):
     """notify an imported module, used to analyze dependencies"""
     importedmodname = get_module_part(importedmodname)
     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)
Beispiel #10
0
 def visit_from(self, node):
     """visit an astng.From node
     
     resolve module dependencies
     """
     basename = node.modname
     context_file = node.root().file
     if context_file is not None:
         relative = 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:
                 # XXX: don't use get_module_part, missing package precedence
                 fullname = get_module_part(fullname)
             except ImportError:
                 continue
         if fullname != basename:
             self._imported_module(node, fullname, relative)
Beispiel #11
0
 def visit_from(self, node):
     """visit an astng.From node
     
     resolve module dependencies
     """
     basename = node.modname
     context_file = node.root().file
     if context_file is not None:
         relative = 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:
                 # XXX: don't use get_module_part, missing package precedence
                 fullname = get_module_part(fullname)
             except ImportError:
                 continue
         if fullname != basename:
             self._imported_module(node, fullname, relative)
Beispiel #12
0
 def test_knownValues_get_module_part_3(self):
     """relative import from given file"""
     self.assertEqual(modutils.get_module_part('interface.Interface',
                                               modutils.__file__), 'interface')
Beispiel #13
0
 def test_knownValues_get_module_part_3(self):
     """relative import from given file"""
     self.assertEqual(
         modutils.get_module_part('interface.Interface', modutils.__file__),
         'interface')
Beispiel #14
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')
Beispiel #15
0
 def test_knownValues_get_module_part_2(self):
     self.assertEqual(modutils.get_module_part('logilab.common.modutils.get_module_part'),
                      'logilab.common.modutils')
Beispiel #16
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')
Beispiel #17
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')
 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")
 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")
 def test_knownValues_get_module_part_3(self):
     self.assertEqual(modutils.get_module_part("db.get_connexion", modutils.__file__), "db")
 def test_knownValues_get_module_part_1(self):
     self.assertEqual(modutils.get_module_part("logilab.common.modutils"), "logilab.common.modutils")
Beispiel #22
0
 def test_knownValues_get_module_part_2(self):
     self.assertEqual(
         modutils.get_module_part(
             'logilab.common.modutils.get_module_part'),
         'logilab.common.modutils')
Beispiel #23
0
 def test_knownValues_get_module_part_3(self):
     self.assertEqual(
         modutils.get_module_part('db.get_connexion', modutils.__file__),
         'db')
Beispiel #24
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')