def set_option(self, opt_name, value, action=None, opt_dict=None): """overridden from configuration.OptionsProviderMixin to handle some special options """ if opt_name in self._options_methods: if value: meth = self._options_methods[opt_name] value = check_csv(None, opt_name, value) if isinstance(value, (list, tuple)): for _id in value : meth(_id) else : meth(value) elif opt_name == 'cache-size': MANAGER.set_cache_size(int(value)) elif opt_name == 'output-format': self.set_reporter(REPORTER_OPT_MAP[value.lower()]()) elif opt_name in ('enable-checker', 'disable-checker'): if not value: return checkerids = [v.lower() for v in check_csv(None, opt_name, value)] self.enable_checkers(checkerids, opt_name == 'enable-checker') try: BaseRawChecker.set_option(self, opt_name, value, action, opt_dict) except UnsupportedAction: print >>sys.stderr, 'option %s can\'t be read from config file' % opt_name
def import_module(self, modname, relative_only=False, level=None): """import the given module considering self as context""" try: absmodname = self.absolute_modname(modname, level) return MANAGER.astng_from_module_name(absmodname) except ASTNGBuildingException: # we only want to import a sub module or package of this module, # skip here if relative_only: raise module = MANAGER.astng_from_module_name(modname) return module
def register(linter): if PYLINT == 0: if hasattr(MANAGER, 'register_transformer'): MANAGER.register_transformer(ssl_transform) else: safe = builder.ASTNGBuilder.string_build def _string_build(self, data, modname='', path=None): if modname == 'ssl': data += '\n\nPROTOCOL_SSLv23 = 0\nPROTOCOL_TLSv1 = 0' return safe(self, data, modname, path) builder.ASTNGBuilder.string_build = _string_build else: MANAGER.register_transform(scoped_nodes.Module, ssl_transform)
def test_inspect_build0(self): """test astng tree build from a living object""" builtin_astng = MANAGER.astng_from_module_name('__builtin__') fclass = builtin_astng['file'] self.assert_('name' in fclass) self.assert_('mode' in fclass) self.assert_('read' in fclass) self.assert_(fclass.newstyle) self.assert_(fclass.pytype(), '__builtin__.type') self.assertIsInstance(fclass['read'], nodes.Function) # check builtin function has args.args == None dclass = builtin_astng['dict'] self.assertEquals(dclass['has_key'].args.args, None) # just check type and object are there builtin_astng.getattr('type') builtin_astng.getattr('object') # check open file alias builtin_astng.getattr('open') # check 'help' is there (defined dynamically by site.py) builtin_astng.getattr('help') # check property has __init__ pclass = builtin_astng['property'] self.assert_('__init__' in pclass) self.assertIsInstance(builtin_astng['None'], nodes.Const) self.assertIsInstance(builtin_astng['True'], nodes.Const) self.assertIsInstance(builtin_astng['False'], nodes.Const) self.assertIsInstance(builtin_astng['Exception'], nodes.From) self.assertIsInstance(builtin_astng['NotImplementedError'], nodes.From)
def add_ormobject(self, orm_type, orm_name): if orm_type in dt.done: return dt.done.add(orm_type) module_name = orm_type.__module__ pymodule = namedAny(module_name) assert pymodule module = MANAGER.astng_from_module(pymodule) assert module class_node = module[orm_name] t = '' t += 'class %s:\n' % (orm_name, ) t += ' q = None\n' t += ' _connection = None\n' orm_ti = dt.orm_classes.get(orm_name) for name in sorted(orm_ti.get_column_names()): t += ' %s = None\n' % (name, ) for name, class_name in sorted(orm_ti.get_foreign_columns()): self.add_ormobject(dt.orm_classes[class_name].orm_type, class_name) t += ' %s = None\n' % (class_name, ) t += ' %s = %s()\n' % (name, class_name) for name, class_name in sorted(orm_ti.get_single_joins()): self.add_ormobject(dt.orm_classes[class_name].orm_type, class_name) t += ' %s = %s()\n' % (name, class_name) t += '\n' nodes = self.builder.string_build(t) for key, value in nodes[orm_name].items(): class_node.locals[key] = [value]
def test_module_path(self): mod = m.astng_from_module_name('import_package_subpackage_module') package = mod.igetattr('package').next() self.failUnlessEqual(package.name, 'package') subpackage = package.igetattr('subpackage').next() self.failUnlessEqual(subpackage.name, 'package.subpackage') module = subpackage.igetattr('module').next() self.failUnlessEqual(module.name, 'package.subpackage.module')
def infer_empty_node(self, context=None): if not self.has_underlying_object(): yield YES else: try: for infered in MANAGER.infer_astng_from_something(self.object, context=context): yield infered except ASTNGError: yield YES
def test_functional_relation_extraction(self): """functional test of relations extraction; different classes possibly in different modules""" # XXX should be catching pyreverse environnement problem but doesn't # pyreverse doesn't extracts the relations but this test ok project = MANAGER.project_from_files(['data'], astng_wrapper) handler = DiadefsHandler(Config()) diadefs = handler.get_diadefs(project, Linker(project, tag=True)) cd = diadefs[1] relations = _process_relations(cd.relationships) self.assertEquals(relations, self._should_rels)
def test_functional_relation_extraction(self): """functional test of relations extraction; different classes possibly in different modules""" # XXX should be catching pyreverse environnement problem but doesn't # pyreverse doesn't extracts the relations but this test ok project = MANAGER.project_from_files(["data"], astng_wrapper) handler = DiadefsHandler(Config()) diadefs = handler.get_diadefs(project, Linker(project, tag=True)) cd = diadefs[1] relations = _process_relations(cd.relationships) self.assertEquals(relations, self._should_rels)
def get_astng(self, filepath, modname): """return a astng representation for a module""" try: return MANAGER.astng_from_file(filepath, modname, source=True) except SyntaxError as ex: self.add_message('E0001', line=ex.lineno, args=ex.msg) except ASTNGBuildingException as ex: self.add_message('F0010', args=ex) except Exception as ex: import traceback traceback.print_exc() self.add_message('F0002', args=(ex.__class__, ex))
def test_known_values2(self): project = MANAGER.project_from_files(['data.clientmodule_test'], astng_wrapper) dd = DefaultDiadefGenerator(Linker(project), HANDLER).visit(project) self.assertEqual(len(dd), 1) keys = [d.TYPE for d in dd] self.assertEqual(keys, ['class']) cd = dd[0] self.assertEqual(cd.title, 'classes No Name') classes = _process_classes(cd.objects) self.assertEqual(classes, [(True, 'Ancestor'), (True, 'DoNothing'), (True, 'Specialization')])
def test_known_values2(self): project = MANAGER.project_from_files(['data.clientmodule_test'], astng_wrapper) dd = DefaultDiadefGenerator(Linker(project), HANDLER).visit(project) self.assertEqual(len(dd), 1) keys = [d.TYPE for d in dd] self.assertEqual(keys, ['class']) cd = dd[0] self.assertEqual(cd.title, 'classes No Name') classes = _process_classes(cd.objects) self.assertEqual(classes, [(True, 'Ancestor'), (True, 'DoNothing'), (True, 'Specialization')] )
def builtin_lookup(name): """lookup a name into the builtin module return the list of matching statements and the astng for the builtin module """ builtinastng = MANAGER.astng_from_module(__builtin__) if name == '__dict__': return builtinastng, () try: stmts = builtinastng.locals[name] except KeyError: stmts = () return builtinastng, stmts
def builtin_lookup(name): """lookup a name into the builtin module return the list of matching statements and the astng for the builtin module """ # TODO : once there is no more monkey patching, make a BUILTINASTNG const builtinastng = MANAGER.astng_from_module(__builtin__) if name == '__dict__': return builtinastng, () try: stmts = builtinastng.locals[name] except KeyError: stmts = () return builtinastng, stmts
def test_inspect_build_type_object(self): builtin_astng = MANAGER.astng_from_module_name('__builtin__') infered = list(builtin_astng.igetattr('object')) self.assertEquals(len(infered), 1) infered = infered[0] self.assertEquals(infered.name, 'object') as_string(infered) infered = list(builtin_astng.igetattr('type')) self.assertEquals(len(infered), 1) infered = infered[0] self.assertEquals(infered.name, 'type') as_string(infered)
def test_known_values2(self): project = MANAGER.project_from_files(["data.clientmodule_test"], astng_wrapper) dd = DefaultDiadefGenerator(Linker(project), HANDLER).visit(project) self.assertEquals(len(dd), 1) keys = [d.TYPE for d in dd] self.assertEquals(keys, ["class"]) cd = dd[0] self.assertEquals(cd.title, "classes No Name") classes = _process_classes(cd.objects) self.assertEquals( classes, [ {"node": True, "name": "Ancestor"}, {"node": True, "name": "DoNothing"}, {"node": True, "name": "Specialization"}, ], )
def test_pylint_config_attr(self): try: from pylint import lint except ImportError: self.skipTest('pylint not available') mod = MANAGER.astng_from_module_name('pylint.lint') pylinter = mod['PyLinter'] expect = ['OptionsManagerMixIn', 'object', 'MessagesHandlerMixIn', 'ReportsHandlerMixIn', 'BaseRawChecker', 'BaseChecker', 'OptionsProviderMixIn', 'ASTWalker'] self.assertListEqual([c.name for c in pylinter.ancestors()], expect) self.assertTrue(list(Instance(pylinter).getattr('config'))) infered = list(Instance(pylinter).igetattr('config')) self.assertEqual(len(infered), 1) self.assertEqual(infered[0].root().name, 'optparse') self.assertEqual(infered[0].name, 'Values')
def test_pylint_config_attr(self): try: from pylint import lint except ImportError: self.skipTest('pylint not available') mod = MANAGER.astng_from_module_name('pylint.lint') pylinter = mod['PyLinter'] expect = ['OptionsManagerMixIn', 'object', 'MessagesHandlerMixIn', 'ReportsHandlerMixIn', 'BaseRawChecker', 'BaseChecker', 'OptionsProviderMixIn', 'ASTWalker'] self.assertListEqual([c.name for c in pylinter.ancestors()], expect) self.assert_(list(Instance(pylinter).getattr('config'))) infered = list(Instance(pylinter).igetattr('config')) self.assertEqual(len(infered), 1) self.assertEqual(infered[0].root().name, 'optparse') self.assertEqual(infered[0].name, 'Values')
def add_ormobject(self, orm_type, orm_name): if orm_type in dt.done: return dt.done.add(orm_type) module_name = orm_type.__module__ pymodule = namedAny(module_name) assert pymodule module = MANAGER.astng_from_module(pymodule) assert module class_node = module[orm_name] t = '' t += 'class %s:\n' % (orm_name, ) t += ' q = None\n' t += ' _connection = None\n' orm_ti = dt.orm_classes.get(orm_name) if orm_ti is not None: for name in sorted(orm_ti.get_column_names()): t += ' %s = None\n' % (name, ) for name, class_name in sorted(orm_ti.get_foreign_columns()): self.add_ormobject(dt.orm_classes[class_name].orm_type, class_name) t += ' %s = None\n' % (class_name, ) t += ' %s = %s()\n' % (name, class_name) for name, class_name in sorted(orm_ti.get_single_joins()): self.add_ormobject( dt.orm_classes[class_name].orm_type, class_name) t += ' %s = %s()\n' % (name, class_name) t += '\n' nodes = self.builder.string_build(t) for key, value in nodes[orm_name].items(): class_node.locals[key] = [value]
def test_inspect_build1(self): time_astng = MANAGER.astng_from_module_name('time') self.assert_(time_astng) self.assertEquals(time_astng['time'].args.defaults, [])
def register(*_args, **_kwargs): MANAGER.register_transformer(nose_tools_transform)
def setUp(self): self.project = MANAGER.project_from_files(['data2'], astng_wrapper) self.linker = inspector.Linker(self.project) self.linker.visit(self.project)
import sys from logilab import astng from logilab.astng import MANAGER from logilab.astng.inspector import Linker from pylint.pyreverse.diadefslib import * from utils import Config def astng_wrapper(func, modname): return func(modname) PROJECT = MANAGER.project_from_files(['data'], astng_wrapper) CONFIG = Config() HANDLER = DiadefsHandler(CONFIG) def _process_classes(classes): """extract class names of a list""" result = [] for classe in classes: result.append({ 'node': isinstance(classe.node, astng.Class), 'name': classe.title }) result.sort() return result
def get_astng(self, filepath, modname): """return a astng representation for a module""" try: return MANAGER.astng_from_file(filepath, modname) except SyntaxError, ex: self.add_message('E0001', line=ex.lineno, args=ex.msg)
def setUp(self): MANAGER.set_cache_size(200) # reset cache
import sys from logilab import astng from logilab.astng import MANAGER from logilab.astng.inspector import Linker from pylint.pyreverse.diadefslib import * from utils import Config def astng_wrapper(func, modname): return func(modname) PROJECT = MANAGER.project_from_files(["data"], astng_wrapper) CONFIG = Config() HANDLER = DiadefsHandler(CONFIG) def _process_classes(classes): """extract class names of a list""" result = [] for classe in classes: result.append({"node": isinstance(classe.node, astng.Class), "name": classe.title}) result.sort() return result def _process_modules(modules):
except NameError: class GeneratorExit(Exception): pass from logilab.astng import MANAGER, nodes, raw_building from logilab.astng import ASTNGError, InferenceError, UnresolvableName, \ NoDefault, NotFoundError, ASTNGBuildingException from logilab.astng.bases import YES, Instance, InferenceContext, \ _infer_stmts, copy_context, path_wrapper, raise_if_nothing_infered from logilab.astng.protocols import _arguments_infer_argname _CONST_PROXY = { type(None): raw_building.build_class('NoneType'), bool: MANAGER.astng_from_class(bool), int: MANAGER.astng_from_class(int), long: MANAGER.astng_from_class(long), float: MANAGER.astng_from_class(float), complex: MANAGER.astng_from_class(complex), str: MANAGER.astng_from_class(str), unicode: MANAGER.astng_from_class(unicode), } _CONST_PROXY[type(None)].parent = _CONST_PROXY[bool].parent # TODO : find a nicer way to handle this situation; we should at least # be able to avoid calling MANAGER.astng_from_class(const.value.__class__) # each time (if we can not avoid the property). However __proxied introduced an # infinite recursion (see https://bugs.launchpad.net/pylint/+bug/456870) def _set_proxied(const):
def register(*_args, **_kwargs): MANAGER.register_transformer(atomiccmd_builder_transform)
def register(linter): """called when loaded by pylint --load-plugins, register our tranformation function here """ MANAGER.register_transformer(httpretty_transform)
def register(linter): MANAGER.register_transformer(web2py_transform)
def setUp(self): self.project = MANAGER.from_directory('data2') self.linker = inspector.Linker(self.project) self.linker.visit(self.project)
try: GeneratorExit # introduced in py2.5 except NameError: class GeneratorExit(Exception): pass from logilab.astng import MANAGER, _nodes as nodes, raw_building from logilab.astng import ASTNGError, InferenceError, UnresolvableName, \ NoDefault, NotFoundError, ASTNGBuildingException from logilab.astng.infutils import YES, Instance, InferenceContext, \ _infer_stmts, copy_context, path_wrapper, raise_if_nothing_infered from logilab.astng.protocols import _arguments_infer_argname _CONST_PROXY = { type(None): raw_building.build_class('NoneType'), bool: MANAGER.astng_from_class(bool), int: MANAGER.astng_from_class(int), long: MANAGER.astng_from_class(long), float: MANAGER.astng_from_class(float), complex: MANAGER.astng_from_class(complex), str: MANAGER.astng_from_class(str), unicode: MANAGER.astng_from_class(unicode), } _CONST_PROXY[type(None)].parent = _CONST_PROXY[bool].parent def _set_proxied(const): if not hasattr(const, '__proxied'): const.__proxied = _CONST_PROXY[const.value.__class__] return const.__proxied nodes.Const._proxied = property(_set_proxied)
def get_astng(self, filepath, modname): """return a astng representation for a module""" try: return MANAGER.astng_from_file(filepath, modname, source=True) except SyntaxError, ex: self.add_message('E0001', line=ex.lineno, args=ex.msg)
def register(linter): if PYLINT == 0: MANAGER.register_transformer(ssl_transform) else: MANAGER.register_transform(scoped_nodes.Module, ssl_transform)
def register(linter): #pylint: disable=W0613 """Pylint calls this hook to actually activate the plugin""" walker = PyLintASTWalker(linter) walker.add_checker(ImportRewriterVisitor()) MANAGER.register_transformer(walker.walk)
from nose import tools function_template = """ def {}(*args, **kwargs): pass """ def nose_transform(module): funcs = ''.join(function_template.format(func_name) for func_name in tools.__all__) fake = ASTNGBuilder(MANAGER).string_build(funcs) for func_name in tools.__all__: if func_name not in module.locals: module.locals[func_name] = fake[func_name] def transform(module): if module.name == 'nose.tools': nose_transform(module) from logilab.astng import MANAGER MANAGER.register_transformer(transform) def register(linter): pass
pass def terminate(self): pass def kill(self): pass ''') for func_name, func in fake.locals.items(): module.locals[func_name] = func MODULE_TRANSFORMS['hashlib'] = hashlib_transform MODULE_TRANSFORMS['collections'] = collections_transform MODULE_TRANSFORMS['pkg_resources'] = pkg_resources_transform MODULE_TRANSFORMS['urlparse'] = urlparse_transform MODULE_TRANSFORMS['subprocess'] = subprocess_transform def transform(module): try: tr = MODULE_TRANSFORMS[module.name] except KeyError: pass else: tr(module) from logilab.astng import MANAGER MANAGER.register_transformer(transform)