Exemplo n.º 1
0
 def infer_astng_from_something(self, obj, context=None):
     """infer astng 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 ASTNGBuildingException(
             'Unable to get module for %s' % safe_repr(klass))
     except Exception as ex:
         raise ASTNGBuildingException(
             'Unexpected error while retrieving module for %s: %s'
             % (safe_repr(klass), ex))
     try:
         name = klass.__name__
     except AttributeError:
         raise ASTNGBuildingException(
             'Unable to get name for %s' % safe_repr(klass))
     except Exception as ex:
         raise ASTNGBuildingException(
             'Unexpected error while retrieving name for %s: %s'
             % (safe_repr(klass), ex))
     # take care, on living object __module__ is regularly wrong :(
     modastng = self.astng_from_module_name(modname)
     if klass is obj:
         for  infered in modastng.igetattr(name, context):
             yield infered
     else:
         for infered in modastng.igetattr(name, context):
             yield infered.instanciate_class()
Exemplo n.º 2
0
    def file_build(self, path, modname=None):
        """build astng 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 ASTNGBuildingException(msg)
        except SyntaxError as exc:  # py3k encoding specification error
            raise ASTNGBuildingException(exc)
        except LookupError as exc:  # unknown encoding
            raise ASTNGBuildingException(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 astng representation
        node = self.string_build(data, modname, path)
        node.file_encoding = encoding
        node.file_stream = stream
        return node
Exemplo n.º 3
0
 def infer_astng_from_something(self, obj, context=None):
     """infer astng 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 ASTNGBuildingException('Unable to get module for %s' %
                                      safe_repr(klass))
     except Exception, ex:
         raise ASTNGBuildingException(
             'Unexpected error while retrieving module for %s: %s' %
             (safe_repr(klass), ex))
Exemplo n.º 4
0
class ASTNGBuilder(InspectBuilder):
    """provide astng building methods"""
    rebuilder = TreeRebuilder()

    def __init__(self, manager=None):
        self._manager = manager or MANAGER

    def module_build(self, module, modname=None):
        """build an astng 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 astng 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 ASTNGBuildingException(msg)
        except SyntaxError, exc:  # py3k encoding specification error
            raise ASTNGBuildingException(exc)
Exemplo n.º 5
0
 def astng_from_file(self,
                     filepath,
                     modname=None,
                     fallback=True,
                     source=False):
     """given a module name, return the astng object"""
     try:
         filepath = get_source_file(filepath, include_no_ext=True)
         source = True
     except NoSourceFile:
         pass
     if modname is None:
         try:
             modname = '.'.join(modpath_from_file(filepath))
         except ImportError:
             modname = filepath
     if modname in self.astng_cache:
         return self.astng_cache[modname]
     if source:
         from logilab.astng.builder import ASTNGBuilder
         return ASTNGBuilder(self).file_build(filepath, modname)
     elif fallback and modname:
         return self.astng_from_module_name(modname)
     raise ASTNGBuildingException('unable to get astng for file %s' %
                                  filepath)
Exemplo n.º 6
0
 def astng_from_module_name(self, modname, context_file=None):
     """given a module name, return the astng object"""
     if modname in self.astng_cache:
         return self.astng_cache[modname]
     if modname == '__main__':
         from logilab.astng.builder import ASTNGBuilder
         return ASTNGBuilder(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 as ex:
                 msg = 'Unable to load module %s (%s)' % (modname, ex)
                 raise ASTNGBuildingException(msg)
             return self.astng_from_module(module, modname)
         return self.astng_from_file(filepath, modname, fallback=False)
     finally:
         os.chdir(old_cwd)
Exemplo n.º 7
0
 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 ASTNGBuildingException(msg)
Exemplo n.º 8
0
    def file_build(self, path, modname=None):
        """build astng 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 ASTNGBuildingException(msg)
Exemplo n.º 9
0
 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' % safe_repr(klass))
     modastng = self.astng_from_module_name(modname)
     return modastng.getattr(klass.__name__)[0]  # XXX
Exemplo n.º 10
0
def file_from_module_name(self, modname, contextfile):
    try:
        value = self._mod_file_cache[(modname, contextfile)]
    except KeyError:
        try:
            value = file_from_modname(modname, context_file=contextfile)
        except ImportError, ex:
            msg = 'Unable to load module %s (%s)' % (modname, ex)
            value = ASTNGBuildingException(msg)
        self._mod_file_cache[(modname, contextfile)] = value
Exemplo n.º 11
0
 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 as ex:
             msg = 'Unable to load module %s (%s)' % (modname, ex)
             value = ASTNGBuildingException(msg)
         self._mod_file_cache[(modname, contextfile)] = value
     if isinstance(value, ASTNGBuildingException):
         raise value
     return value
Exemplo n.º 12
0
        return node

    def file_build(self, path, modname=None):
        """build astng 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 ASTNGBuildingException(msg)
        except SyntaxError, exc:  # py3k encoding specification error
            raise ASTNGBuildingException(exc)
        except LookupError, exc:  # unknown encoding
            raise ASTNGBuildingException(exc)
        # get module name if necessary, *before modifying sys.path*
        if modname is None:
            try:
                modname = '.'.join(modpath_from_file(path))
            except ImportError:
                modname = splitext(basename(path))[0]
        # build astng representation
        try:
            sys.path.insert(0, dirname(path))  # XXX (syt) iirk
            node = self.string_build(data, modname, path)
        finally:
            sys.path.pop(0)
        node.file_encoding = encoding
        node.file_stream = stream
        return node
Exemplo n.º 13
0
            klass = obj.__class__
        else:
            klass = obj
        try:
            modname = klass.__module__
        except AttributeError:
            raise ASTNGBuildingException('Unable to get module for %s' %
                                         safe_repr(klass))
        except Exception, ex:
            raise ASTNGBuildingException(
                'Unexpected error while retrieving module for %s: %s' %
                (safe_repr(klass), ex))
        try:
            name = klass.__name__
        except AttributeError:
            raise ASTNGBuildingException('Unable to get name for %s' %
                                         safe_repr(klass))
        except Exception, ex:
            raise ASTNGBuildingException(
                'Unexpected error while retrieving name for %s: %s' %
                (safe_repr(klass), ex))
        # take care, on living object __module__ is regularly wrong :(
        modastng = self.astng_from_module_name(modname)
        if klass is obj:
            for infered in modastng.igetattr(name, context):
                yield infered
        else:
            for infered in modastng.igetattr(name, context):
                yield infered.instanciate_class()

    def project_from_files(self,
                           files,