[Pydev-cvs] org.python.pydev/PySrc/ThirdParty/logilab/common/astng manager.py,1.4,1.5 utils.py,1.3,1
Brought to you by:
fabioz
From: Fabio Z. <fa...@us...> - 2005-02-16 16:46:27
|
Update of /cvsroot/pydev/org.python.pydev/PySrc/ThirdParty/logilab/common/astng In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv7532/PySrc/ThirdParty/logilab/common/astng Modified Files: manager.py utils.py raw_building.py __init__.py inspector.py builder.py astng.py Log Message: New pylint version Index: manager.py =================================================================== RCS file: /cvsroot/pydev/org.python.pydev/PySrc/ThirdParty/logilab/common/astng/manager.py,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** manager.py 21 Jan 2005 17:42:09 -0000 1.4 --- manager.py 16 Feb 2005 16:45:44 -0000 1.5 *************** *** 1,3 **** ! # Copyright (c) 2003 Sylvain Thenault (the...@ne...) # # This program is free software; you can redistribute it and/or modify it under --- 1,5 ---- ! # Copyright (c) 2003-2005 Sylvain Thenault (the...@gm...) ! # Copyright (c) 2003-2005 LOGILAB S.A. (Paris, FRANCE). ! # http://www.logilab.fr/ -- mailto:co...@lo... # # This program is free software; you can redistribute it and/or modify it under *************** *** 13,21 **** # this program; if not, write to the Free Software Foundation, Inc., # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. ! """astng loader, avoid multible astng build of a same module """ - __author__ = "Sylvain Thenault" __revision__ = "$Id$" import sys --- 15,31 ---- # this program; if not, write to the Free Software Foundation, Inc., # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. ! """astng manager: avoid multible astng build of a same module when ! possible by providing a class responsible to get astng representation ! from various source and using a cache of built modules) ! ! :version: $Revision$ ! :author: Sylvain Thenault ! :copyright: 2003-2005 LOGILAB S.A. (Paris, FRANCE) ! :copyright: 2003-2005 Sylvain Thenault ! :contact: http://www.logilab.fr/ -- mailto:pyt...@lo... """ __revision__ = "$Id$" + __doctype__ = "restructuredtext en" import sys *************** *** 24,32 **** from logilab.common.cache import Cache ! from logilab.common.astng import ASTNGBuildingException ! from logilab.common.modutils import modpath_from_file, file_from_modpath, \ ! get_modules, get_module_files, load_module_from_name from logilab.common.configuration import OptionsProviderMixIn ! def normalize_module_name(modname): --- 34,41 ---- from logilab.common.cache import Cache ! from logilab.common.modutils import NoSourceFile, is_python_source, \ ! file_from_modpath, get_module_files, load_module_from_name, get_source_file from logilab.common.configuration import OptionsProviderMixIn ! from logilab.common.astng import ASTNGBuildingException def normalize_module_name(modname): *************** *** 38,47 **** return modname - def normalize_file_name(filename): - """normalize a file name (i.e .pyc, .pyo, .pyd -> .py and normalize the - path) - """ - return abspath(filename.replace('.pyc', '.py').replace('.pyo', '.py').replace('.pyd', '.py')) - def astng_wrapper(func, modname): """wrapper to give to ASTNGManager.project_from_files""" --- 47,50 ---- *************** *** 58,64 **** ! class AbstractASTNGManager(OptionsProviderMixIn): ! """abstract class for astng manager, responsible to build astng from files ! and / or modules. Use the Borg pattern. --- 61,67 ---- ! class ASTNGManager(OptionsProviderMixIn): ! """the astng manager, responsible to build astng from files ! or modules. Use the Borg pattern. *************** *** 72,76 **** ."}), ("project", ! {'default': "No Name", 'type' : 'string', 'metavar' : '<project name>', 'help' : 'set the project name.'}), --- 75,79 ---- ."}), ("project", ! {'default': "No Name", 'type' : 'string', 'short': 'p', 'metavar' : '<project name>', 'help' : 'set the project name.'}), *************** *** 88,113 **** self._cache = Cache(cache_size) - # astng manager iface - - def astng_from_module_name(self, modname): - """given a module name, return the astng object""" - raise NotImplementedError - - def astng_from_module(self, module, modname=None): - """given an imported module, return the astng object""" - raise NotImplementedError - - def astng_from_class(self, klass, module=None): - """get astng for the given class""" - raise NotImplementedError - - def astng_from_file(self, filepath, modname=None): - """given a module name, return the astng object""" - raise NotImplementedError - - - class FileBasedASTNGManager(AbstractASTNGManager): - """control the creation of astng trees from files or modules""" - def from_directory(self, directory, modname=None): """given a module name, return the astng object""" --- 91,94 ---- *************** *** 118,154 **** def astng_from_file(self, filepath, modname=None, fallback=True): """given a module name, return the astng object""" - norm_file = normalize_file_name(filepath) try: ! return self._cache[norm_file] except KeyError: ! try: ! from logilab.common.astng.builder import ASTNGBuilder ! astng = ASTNGBuilder(self).file_build(norm_file, modname) ! except SyntaxError: ! raise ! except Exception, ex: ! if fallback and modname: ! return self.astng_from_module_name(modname) ! if __debug__: ! import traceback ! traceback.print_exc() ! msg = 'Unable to load module %s (%s)' % (modname, ex) ! raise ASTNGBuildingException(msg) ! self._cache[norm_file] = astng return astng - from_file = astng_from_file ! def astng_from_module_name(self, modname): """given a module name, return the astng object""" try: ! filepath = file_from_modpath(modname.split('.')) ! except ImportError: ! try: ! return self.astng_from_module(load_module_from_name(modname), modname) ! except ImportError, ex: ! msg = 'Unable to load module %s (%s)' % (modname, ex) ! raise ASTNGBuildingException(msg) ! if not filepath.endswith('.py'): ! return self.astng_from_module(load_module_from_name(modname), modname) return self.astng_from_file(filepath, modname, fallback=False) --- 99,141 ---- def astng_from_file(self, filepath, modname=None, fallback=True): """given a module name, return the astng object""" try: ! filepath = get_source_file(filepath) ! source = True ! except NoSourceFile: ! source = False ! try: ! return self._cache[filepath] except KeyError: ! if source: ! try: ! from logilab.common.astng.builder import ASTNGBuilder ! astng = ASTNGBuilder(self).file_build(filepath, modname) ! except SyntaxError: ! raise ! except Exception, ex: ! #if __debug__: ! # import traceback ! # traceback.print_exc() ! msg = 'Unable to load module %s (%s)' % (modname, ex) ! raise ASTNGBuildingException(msg) ! elif fallback and modname: ! return self.astng_from_module_name(modname) ! self._cache[filepath] = astng return astng ! from_file = astng_from_file # backward compat ! ! def astng_from_module_name(self, modname, context_file=None): """given a module name, return the astng object""" try: ! filepath = file_from_modpath(modname.split('.'), ! context_file=context_file) ! except ImportError, ex: ! msg = 'Unable to load module %s (%s)' % (modname, ex) ! raise ASTNGBuildingException(msg) ! if filepath is None or not is_python_source(filepath): ! # FIXME: context ! return self.astng_from_module(load_module_from_name(modname), ! modname) return self.astng_from_file(filepath, modname, fallback=False) *************** *** 158,162 **** # some builtin modules don't have __file__ attribute filepath = module.__file__ ! if not (filepath.endswith('.so') or filepath.endswith('.dll') or filepath.endswith('.pyd')): return self.astng_from_file(filepath, modname or module.__name__) except AttributeError: --- 145,149 ---- # some builtin modules don't have __file__ attribute filepath = module.__file__ ! if is_python_source(filepath): return self.astng_from_file(filepath, modname or module.__name__) except AttributeError: *************** *** 193,197 **** black_list = black_list or self.config.black_list project = Project(project_name) - modnames = [] for something in files: if not exists(something): --- 180,183 ---- *************** *** 210,214 **** if astng.package and not '__init__' in something: # recurse on others packages / modules if this is a package ! for fpath in get_module_files(dirname(astng.file), black_list): astng = func_wrapper(self.astng_from_file, fpath) if astng is None or astng.name == base_name: --- 196,201 ---- if astng.package and not '__init__' in something: # recurse on others packages / modules if this is a package ! for fpath in get_module_files(dirname(astng.file), ! black_list): astng = func_wrapper(self.astng_from_file, fpath) if astng is None or astng.name == base_name: *************** *** 219,319 **** sys.path.pop(0) - ## class ModuleBasedASTNGManager(AbstractASTNGManager): - ## """build astng from files or modules, but mainly files""" - - - ## def astng_from_class(self, klass, modname=None): - ## """get astng for the given class""" - ## if modname is None: - ## try: - ## modname = klass.__module__ - ## except AttributeError: - ## raise ASTNGBuildingException( - ## 'Unable to get module for class %s' % klass) - ## return self.astng_from_module_name(modname).resolve(klass.__name__) - - ## def astng_from_module(self, module, modname=None): - ## """given an imported module, return the astng object""" - ## try: - ## return self._cache[modname or module.__name__] - ## except: - ## if hasattr(module, '__file__'): - ## # this is required to make works relative imports in - ## # analyzed code - ## sys.path.insert(0, dirname(module.__file__)) - ## try: - ## astng = self._builder.build_from_module(module, modname) - ## # update caches - ## self._cache[astng.name] = astng - ## return astng - ## finally: - ## # clean path - ## if hasattr(module, '__file__'): - ## sys.path.pop(0) - - ## def astng_from_module_name(self, modname): - ## """given a module name, return the astng object""" - ## norm_modname = normalize_module_name(modname) - ## try: - ## return self._cache[norm_modname] - ## except KeyError: - ## try: - ## module = load_module_from_name(norm_modname) - ## except Exception, ex: - ## msg = 'Unable to load module %s (%s)' % (modname, ex) - ## raise ASTNGBuildingException(msg) - ## # return the astng representation - ## return self.astng_from_module(module, norm_modname) - - ## def astng_from_file(self, filepath, modname=None): - ## """given a module name, return the astng object""" - ## norm_modname = normalize_module_name(modname) - ## try: - ## return self._cache[norm_modname] - ## except KeyError: - ## try: - ## astng = self._builder.file_build(filepath, modname) - ## except Exception, ex: - ## msg = 'Unable to load module %s (%s)' % (modname, ex) - ## raise ASTNGBuildingException(msg) - ## self._cache[norm_modname] = astng - ## return astng - - ## def project_from_files(self, files, func_wrapper=astng_wrapper, - ## project_name=None, black_list=None): - ## """return a Project from a list of files or modules - ## """ - ## # insert current working directory to the python path to have a correct - ## # behaviour - ## sys.path.insert(0, os.getcwd()) - ## try: - ## # build the project representation - ## project_name = project_name or self.config.project - ## black_list = black_list or self.config.black_list - ## project = Project(project_name) - ## modnames = [] - ## for modname in files: - ## if modname[-3:] == '.py' or modname.find(os.sep) > -1: - ## modname = '.'.join(modpath_from_file(modname)) - ## astng = func_wrapper(self.astng_from_module_name, modname) - ## if astng is None: - ## continue - ## project.path = project.path or astng.file - ## project.add_module(astng) - ## # recurse in package except if __init__ was explicitly given - ## if not modname.endswith('.__init__') and astng.package: - ## # recurse on others packages / modules if this is a package - ## for submod in get_modules(modname, dirname(astng.file), - ## black_list): - ## astng = func_wrapper(self.astng_from_module_name, submod) - ## if astng is None: - ## continue - ## project.add_module(astng) - ## return project - ## finally: - ## sys.path.pop(0) - - - ASTNGManager = FileBasedASTNGManager --- 206,209 ---- *************** *** 334,337 **** --- 224,230 ---- def fullname(self): + """return the full name of the package (i.e. prefix by the full name + of the parent package if any + """ if self.parent is None: return self.name *************** *** 339,358 **** def get_subobject(self, name): if self.__subobjects is None: self.__subobjects = dict.fromkeys(self.keys()) obj = self.__subobjects[name] if obj is None: ! abspath = join(self.path, name) ! if isdir(abspath): ! obj = Package(abspath, name, self.manager) obj.parent = self else: - abspath += '.py' modname = '%s.%s' % (self.fullname(), name) ! obj = self.manager.astng_from_file(abspath, modname) self.__subobjects[name] = obj return obj def get_module(self, modname): path = modname.split('.') if path[0] != self.name: --- 232,255 ---- def get_subobject(self, name): + """method used to get sub-objects lazily : sub package or module are + only build once they are requested + """ if self.__subobjects is None: self.__subobjects = dict.fromkeys(self.keys()) obj = self.__subobjects[name] if obj is None: ! objpath = join(self.path, name) ! if isdir(objpath): ! obj = Package(objpath, name, self.manager) obj.parent = self else: modname = '%s.%s' % (self.fullname(), name) ! obj = self.manager.astng_from_file(objpath + '.py', modname) self.__subobjects[name] = obj return obj def get_module(self, modname): + """return the Module or Package object with the given name if any + """ path = modname.split('.') if path[0] != self.name: Index: builder.py =================================================================== RCS file: /cvsroot/pydev/org.python.pydev/PySrc/ThirdParty/logilab/common/astng/builder.py,v retrieving revision 1.6 retrieving revision 1.7 diff -C2 -d -r1.6 -r1.7 *** builder.py 31 Jan 2005 17:23:10 -0000 1.6 --- builder.py 16 Feb 2005 16:45:44 -0000 1.7 *************** *** 84,88 **** """ try: ! data = norm_read(path, '\n') except IOError, ex: msg = 'Unable to load file %r (%s)' % (path, ex) --- 84,88 ---- """ try: ! data = norm_read(path) except IOError, ex: msg = 'Unable to load file %r (%s)' % (path, ex) *************** *** 209,214 **** node.parent.set_local(name, node) except: ! import traceback ! traceback.print_exc() print >> sys.stderr, \ 'Unable to get imported names for %r line %s"' % ( --- 209,214 ---- node.parent.set_local(name, node) except: ! #import traceback ! #traceback.print_exc() print >> sys.stderr, \ 'Unable to get imported names for %r line %s"' % ( Index: astng.py =================================================================== RCS file: /cvsroot/pydev/org.python.pydev/PySrc/ThirdParty/logilab/common/astng/astng.py,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** astng.py 21 Jan 2005 17:42:09 -0000 1.5 --- astng.py 16 Feb 2005 16:45:44 -0000 1.6 *************** *** 222,226 **** try: yield node.resolve_dotted(name) ! except (ResolveError, NotFoundError): continue --- 222,226 ---- try: yield node.resolve_dotted(name) ! except (ResolveError, NotFoundError, AssertionError): continue *************** *** 235,241 **** FIXME: refactor !! """ assert ID_RGX.match(name), '%r is not a valid identifier' % name frame = node.get_frame() - #print 'resolving', name, 'from', frame.__class__.__name__, frame.name, frame.locals.keys() try: stmt = frame.locals[name] --- 235,241 ---- FIXME: refactor !! """ + #print 'resolve', node.__class__, getattr(node, 'name', '?'), name assert ID_RGX.match(name), '%r is not a valid identifier' % name frame = node.get_frame() try: stmt = frame.locals[name] *************** *** 292,295 **** --- 292,297 ---- raise ResolveError(name) except Exception, ex: + #import traceback + #traceback.print_exc() raise ResolveError(name) # check that get_module_object seems to have worked correctly... *************** *** 303,307 **** raise ResolveError(name) try: ! module = ASTNGManager().astng_from_module_name(modname) except (ASTNGBuildingException, SyntaxError): # FIXME --- 305,309 ---- raise ResolveError(name) try: ! module = ASTNGManager().astng_from_module_name(modname, context_file) except (ASTNGBuildingException, SyntaxError): # FIXME *************** *** 511,514 **** --- 513,519 ---- if astng.instance_attrs.has_key(attr): return astng + # anode = get_ancestor_for_class_attribute(node, attr) + # if not isinstance(anode, Function): + # return anode raise NotFoundError(attr) Index: raw_building.py =================================================================== RCS file: /cvsroot/pydev/org.python.pydev/PySrc/ThirdParty/logilab/common/astng/raw_building.py,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** raw_building.py 21 Jan 2005 17:46:21 -0000 1.1 --- raw_building.py 16 Feb 2005 16:45:44 -0000 1.2 *************** *** 1,4 **** ! # Copyright (c) 2003-2004 Sylvain Thenault (the...@ne...) ! # Copyright (c) 2003-2004 Logilab # # This program is free software; you can redistribute it and/or modify it under --- 1,4 ---- ! # Copyright (c) 2003-2005 Sylvain Thenault (the...@gm...) ! # Copyright (c) 2003-2005 Logilab (co...@lo...) # # This program is free software; you can redistribute it and/or modify it under *************** *** 20,23 **** --- 20,24 ---- __revision__ = "$Id$" + import sys from logilab.common.astng import astng *************** *** 32,47 **** node.globals = node.locals = {} return node ! def build_function(name, args=None, defaults=None, flag=0, doc=None): ! """create and initialize a astng Function node""" ! args, defaults = args or [], defaults or [] ! func = astng.Function(name, args, defaults, flag, doc, astng.Stmt([])) ! func.code.parent = func ! func.locals = {} ! func.object = None ! if args: ! register_arguments(func, args) ! return func def build_class(name, basenames=None, doc=None): --- 33,66 ---- node.globals = node.locals = {} return node + + + if sys.version_info >= (2, 4): ! # introduction of decorators has changed the Function initializer arguments + def build_function(name, args=None, defaults=None, flag=0, doc=None): + """create and initialize a astng Function node""" + args, defaults = args or [], defaults or [] + # first argument is now a list of decorators + func = astng.Function([], name, args, defaults, flag, doc, astng.Stmt([])) + func.code.parent = func + func.locals = {} + func.object = None + if args: + register_arguments(func, args) + return func + else: + + def build_function(name, args=None, defaults=None, flag=0, doc=None): + """create and initialize a astng Function node""" + args, defaults = args or [], defaults or [] + func = astng.Function(name, args, defaults, flag, doc, astng.Stmt([])) + func.code.parent = func + func.locals = {} + func.object = None + if args: + register_arguments(func, args) + return func + def build_class(name, basenames=None, doc=None): |