def infer_ast_from_something(self, obj, context=None): """infer astroid for the given class""" if hasattr(obj, '__class__') and not isinstance(obj, type): klass = obj.__class__ else: klass = obj try: modname = klass.__module__ except AttributeError: raise AstroidBuildingException('Unable to get module for %s' % safe_repr(klass)) except Exception as ex: raise AstroidBuildingException( 'Unexpected error while retrieving module for %s: %s' % (safe_repr(klass), ex)) try: name = klass.__name__ except AttributeError: raise AstroidBuildingException('Unable to get name for %s' % safe_repr(klass)) except Exception as ex: raise AstroidBuildingException( 'Unexpected error while retrieving name for %s: %s' % (safe_repr(klass), ex)) # take care, on living object __module__ is regularly wrong :( modastroid = self.ast_from_module_name(modname) if klass is obj: for infered in modastroid.igetattr(name, context): yield infered else: for infered in modastroid.igetattr(name, context): yield infered.instanciate_class()
def _buildAstroidModuleFromComponentModuleName(modname): from Products.ERP5.ERP5Site import getSite from Acquisition import aq_base portal = getSite() component_tool = aq_base(portal.portal_components) component_obj = None component_id = modname[len('erp5.component.'):] if '_version' in modname: try: obj = getattr(component_tool, component_id.replace('_version', '', 1)) except AttributeError: raise AstroidBuildingException() if obj.getValidationState() in ('modified', 'validated'): component_obj = obj else: raise AstroidBuildingException() else: try: package, reference = component_id.split('.', 1) except ValueError: raise AstroidBuildingException() for version in portal.getVersionPriorityNameList(): try: obj = getattr(component_tool, '%s.%s.%s' % (package, version, reference)) except AttributeError: continue if obj.getValidationState() in ('modified', 'validated'): version_modname = 'erp5.component.%s.%s_version.%s' % ( package, version, reference) module = MANAGER.astroid_cache.get( version_modname, _buildAstroidModuleFromComponentModuleName( version_modname)) MANAGER.astroid_cache[modname] = module return module if component_obj is None: raise AstroidBuildingException() # module_build() could also be used but this requires importing # the ZODB Component and also monkey-patch it to support PEP-302 # for __file__ starting with '<' module = AstroidBuilder(MANAGER).string_build( component_obj.getTextContent(validated_only=True), modname) return module
def infer_ast_from_something(self, obj, context=None): """infer astroid for the given class""" if hasattr(obj, '__class__') and not isinstance(obj, type): klass = obj.__class__ else: klass = obj try: modname = klass.__module__ except AttributeError: raise AstroidBuildingException('Unable to get module for %s' % safe_repr(klass)) except Exception, ex: raise AstroidBuildingException( 'Unexpected error while retrieving module for %s: %s' % (safe_repr(klass), ex))
def ast_from_file(self, filepath, modname=None, fallback=True, source=False): """given a module name, return the astroid object""" try: filepath = modutils.get_source_file(filepath, include_no_ext=True) source = True except modutils.NoSourceFile: pass if modname is None: try: modname = '.'.join(modutils.modpath_from_file(filepath)) except ImportError: modname = filepath if modname in self.astroid_cache and self.astroid_cache[ modname].file == filepath: return self.astroid_cache[modname] if source: from astroid.builder import AstroidBuilder return AstroidBuilder(self).file_build(filepath, modname) elif fallback and modname: return self.ast_from_module_name(modname) raise AstroidBuildingException('unable to get astroid for file %s' % filepath)
class AstroidBuilder(InspectBuilder): """provide astroid building methods""" def __init__(self, manager=None): InspectBuilder.__init__(self) self._manager = manager or MANAGER def module_build(self, module, modname=None): """build an astroid from a living module instance """ node = None path = getattr(module, '__file__', None) if path is not None: path_, ext = splitext(module.__file__) if ext in ('.py', '.pyc', '.pyo') and exists(path_ + '.py'): node = self.file_build(path_ + '.py', modname) if node is None: # this is a built-in module # get a partial representation by introspection node = self.inspect_build(module, modname=modname, path=path) return node def file_build(self, path, modname=None): """build astroid from a source code file (i.e. from an ast) path is expected to be a python source file """ try: stream, encoding, data = open_source_file(path) except IOError, exc: msg = 'Unable to load file %r (%s)' % (path, exc) raise AstroidBuildingException(msg) except SyntaxError, exc: # py3k encoding specification error raise AstroidBuildingException(exc)
def ast_from_class(self, klass, modname=None): """get astroid for the given class""" if modname is None: try: modname = klass.__module__ except AttributeError: raise AstroidBuildingException( 'Unable to get module for class %s' % safe_repr(klass)) modastroid = self.ast_from_module_name(modname) return modastroid.getattr(klass.__name__)[0] # XXX
def file_build(self, path, modname=None): """build astroid from a source code file (i.e. from an ast) path is expected to be a python source file """ try: stream, encoding, data = open_source_file(path) except IOError, exc: msg = 'Unable to load file %r (%s)' % (path, exc) raise AstroidBuildingException(msg)
def open_source_file(filename): byte_stream = open(filename, 'bU') encoding = detect_encoding(byte_stream.readline)[0] stream = open(filename, 'U', encoding=encoding) try: data = stream.read() except UnicodeError, uex: # wrong encodingg # detect_encoding returns utf-8 if no encoding specified msg = 'Wrong (%s) or no encoding specified' % encoding raise AstroidBuildingException(msg)
def ast_from_module_name(self, modname, context_file=None): """given a module name, return the astroid object""" if modname in self.astroid_cache: return self.astroid_cache[modname] if modname == '__main__': return self._build_stub_module(modname) old_cwd = os.getcwd() if context_file: os.chdir(dirname(context_file)) try: filepath, mp_type = self.file_from_module_name( modname, context_file) if mp_type == modutils.PY_ZIPMODULE: module = self.zip_import_data(filepath) if module is not None: return module elif mp_type in (imp.C_BUILTIN, imp.C_EXTENSION): if mp_type == imp.C_EXTENSION and not self._can_load_extension( modname): return self._build_stub_module(modname) try: module = modutils.load_module_from_name(modname) except Exception as ex: msg = 'Unable to load module %s (%s)' % (modname, ex) raise AstroidBuildingException(msg) return self.ast_from_module(module, modname) elif mp_type == imp.PY_COMPILED: raise AstroidBuildingException( "Unable to load compiled module %s" % (modname, )) if filepath is None: raise AstroidBuildingException("Unable to load module %s" % (modname, )) return self.ast_from_file(filepath, modname, fallback=False) except AstroidBuildingException as e: for hook in self._failed_import_hooks: try: return hook(modname) except AstroidBuildingException: pass raise e finally: os.chdir(old_cwd)
def file_from_module_name(self, modname, contextfile): try: value = self._mod_file_cache[(modname, contextfile)] except KeyError: try: value = file_from_modpath(modname.split('.'), context_file=contextfile) except ImportError, ex: msg = 'Unable to load module %s (%s)' % (modname, ex) value = AstroidBuildingException(msg) self._mod_file_cache[(modname, contextfile)] = value
def open_source_file(filename): with open(filename, 'rb') as byte_stream: encoding = detect_encoding(byte_stream.readline)[0] stream = open(filename, 'r', newline=None, encoding=encoding) try: data = stream.read() except UnicodeError: # wrong encodingg # detect_encoding returns utf-8 if no encoding specified msg = 'Wrong (%s) or no encoding specified' % encoding raise AstroidBuildingException(msg) return stream, encoding, data
def file_from_module_name(self, modname, contextfile): try: value = self._mod_file_cache[(modname, contextfile)] except KeyError: try: value = modutils.file_info_from_modpath( modname.split('.'), context_file=contextfile) except ImportError as ex: msg = 'Unable to load module %s (%s)' % (modname, ex) value = AstroidBuildingException(msg) self._mod_file_cache[(modname, contextfile)] = value if isinstance(value, AstroidBuildingException): raise value return value
def file_build(self, path, modname=None): """build astroid from a source code file (i.e. from an ast) path is expected to be a python source file """ try: stream, encoding, data = open_source_file(path) except IOError as exc: msg = 'Unable to load file %r (%s)' % (path, exc) raise AstroidBuildingException(msg) except SyntaxError as exc: # py3k encoding specification error raise AstroidBuildingException(exc) except LookupError as exc: # unknown encoding raise AstroidBuildingException(exc) with stream: # get module name if necessary if modname is None: try: modname = '.'.join(modpath_from_file(path)) except ImportError: modname = splitext(basename(path))[0] # build astroid representation module = self._data_build(data, modname, path) return self._post_build(module, encoding)
def fail_hook_erp5_component(modname): if not modname.startswith('erp5.'): raise AstroidBuildingException() if (modname in ('erp5.portal_type', 'erp5.component', 'erp5.component.module', 'erp5.component.extension', 'erp5.component.document', 'erp5.component.tool', 'erp5.component.interface', 'erp5.component.mixin', 'erp5.component.test') or (modname.startswith('erp5.component.') and modname.endswith('_version'))): module = AstroidBuilder(MANAGER).string_build('', modname) if modname.startswith('erp5.component'): module.package = True else: module = _buildAstroidModuleFromComponentModuleName(modname) return module
def _data_build(self, data, modname, path): """build tree node from data and add some informations""" # this method could be wrapped with a pickle/cache function try: node = parse(data + '\n') except TypeError as exc: raise AstroidBuildingException(exc) if path is not None: node_file = abspath(path) else: node_file = '<?>' if modname.endswith('.__init__'): modname = modname[:-9] package = True else: package = path and path.find('__init__.py') > -1 or False rebuilder = TreeRebuilder(self._manager) module = rebuilder.visit_module(node, modname, node_file, package) module._from_nodes = rebuilder._from_nodes module._delayed_assattr = rebuilder._delayed_assattr return module
def string_build(self, data, modname='', path=None): """ build astroid from source code string and return rebuilded astroid Monkey patched to check encoding properly, as `file_build()` does: # `data` can only be an str (not unicode) module = self._data_build(data, modname, path) # But here it tries to `encode()` which will fails if there is any # non-ASCII character... module.file_bytes = data.encode('utf-8') return self._post_build(module, 'utf-8') """ if isinstance(data, unicode): # When called internally by pylint/astroid and if the source code imports # `unicode_literals`, the source code may end up being an unicode object # (example: `infer_named_tuple()`) data = data.encode('utf-8') encoding = _guess_encoding(data) if encoding is None: # Encoding not defined in the source file, assuming utf-8... encoding = 'utf-8' try: # BytesIO() does not handle unicode: # TypeError: 'unicode' does not have the buffer interface if isinstance(data, unicode): data = data.encode(encoding) else: # Just to avoid error later on... data.decode(encoding) except Exception as exc: from zLOG import LOG, WARNING LOG( "Products.ERP5Type.patches.pylint", WARNING, "%s: Considered as not importable: Wrong encoding? (%r)" % (modname, exc)) raise AstroidBuildingException(exc) module = self._data_build(data, modname, path) module.file_bytes = data return self._post_build(module, encoding)
def ast_from_module_name(self, modname, context_file=None): """given a module name, return the astroid object""" if modname in self.astroid_cache: return self.astroid_cache[modname] if modname == '__main__': from astroid.builder import AstroidBuilder return AstroidBuilder(self).string_build('', modname) old_cwd = os.getcwd() if context_file: os.chdir(dirname(context_file)) try: filepath = self.file_from_module_name(modname, context_file) if filepath is not None and not is_python_source(filepath): module = self.zip_import_data(filepath) if module is not None: return module if filepath is None or not is_python_source(filepath): try: module = load_module_from_name(modname) except Exception, ex: msg = 'Unable to load module %s (%s)' % (modname, ex) raise AstroidBuildingException(msg) return self.ast_from_module(module, modname) return self.ast_from_file(filepath, modname, fallback=False)
def hook(modname): if modname == 'foo.bar': return unittest else: raise AstroidBuildingException()
return node def file_build(self, path, modname=None): """build astroid from a source code file (i.e. from an ast) path is expected to be a python source file """ try: stream, encoding, data = open_source_file(path) except IOError, exc: msg = 'Unable to load file %r (%s)' % (path, exc) raise AstroidBuildingException(msg) except SyntaxError, exc: # py3k encoding specification error raise AstroidBuildingException(exc) except LookupError, exc: # unknown encoding raise AstroidBuildingException(exc) # get module name if necessary if modname is None: try: modname = '.'.join(modpath_from_file(path)) except ImportError: modname = splitext(basename(path))[0] # build astroid representation node = self.string_build(data, modname, path) node.file_encoding = encoding return node def string_build(self, data, modname='', path=None): """build astroid from source code string and return rebuilded astroid""" module = self._data_build(data, modname, path) self._manager.astroid_cache[module.name] = module
klass = obj.__class__ else: klass = obj try: modname = klass.__module__ except AttributeError: raise AstroidBuildingException('Unable to get module for %s' % safe_repr(klass)) except Exception, ex: raise AstroidBuildingException( 'Unexpected error while retrieving module for %s: %s' % (safe_repr(klass), ex)) try: name = klass.__name__ except AttributeError: raise AstroidBuildingException('Unable to get name for %s' % safe_repr(klass)) except Exception, ex: raise AstroidBuildingException( 'Unexpected error while retrieving name for %s: %s' % (safe_repr(klass), ex)) # take care, on living object __module__ is regularly wrong :( modastroid = self.ast_from_module_name(modname) if klass is obj: for infered in modastroid.igetattr(name, context): yield infered else: for infered in modastroid.igetattr(name, context): yield infered.instanciate_class() def project_from_files(self, files,