Example #1
0
def get_module_source(modname):
    """Try to find the source code for a module.

    Can return ('file', 'filename') in which case the source is in the given
    file, or ('string', 'source') which which case the source is the string.
    """
    if modname not in sys.modules:
        try:
            __import__(modname)
        except Exception as err:
            raise PycodeError('error importing %r' % modname, err)
    mod = sys.modules[modname]
    if hasattr(mod, '__loader__'):
        try:
            source = mod.__loader__.get_source(modname)
        except Exception as err:
            raise PycodeError('error getting source for %r' % modname, err)
        return 'string', source
    filename = getattr(mod, '__file__', None)
    if filename is None:
        raise PycodeError('no source found for module %r' % modname)
    filename = path.normpath(path.abspath(filename))
    lfilename = filename.lower()
    if lfilename.endswith('.pyo') or lfilename.endswith('.pyc'):
        filename = filename[:-1]
        if not path.isfile(filename) and path.isfile(filename + 'w'):
            filename += 'w'
    elif not (lfilename.endswith('.py') or lfilename.endswith('.pyw')):
        raise PycodeError('source is not a .py file: %r' % filename)
    if not path.isfile(filename):
        raise PycodeError('source file is not present: %r' % filename)
    return 'file', filename
Example #2
0
    def get_module_source(modname: str) -> Tuple[Optional[str], Optional[str]]:
        """Try to find the source code for a module.

        Returns ('filename', 'source'). One of it can be None if
        no filename or source found
        """
        try:
            mod = import_module(modname)
        except Exception as err:
            raise PycodeError('error importing %r' % modname, err) from err
        loader = getattr(mod, '__loader__', None)
        filename = getattr(mod, '__file__', None)
        if loader and getattr(loader, 'get_source', None):
            # prefer Native loader, as it respects #coding directive
            try:
                source = loader.get_source(modname)
                if source:
                    # no exception and not None - it must be module source
                    return filename, source
            except ImportError:
                pass  # Try other "source-mining" methods
        if filename is None and loader and getattr(loader, 'get_filename',
                                                   None):
            # have loader, but no filename
            try:
                filename = loader.get_filename(modname)
            except ImportError as err:
                raise PycodeError('error getting filename for %r' % modname,
                                  err) from err
        if filename is None:
            # all methods for getting filename failed, so raise...
            raise PycodeError('no source found for module %r' % modname)
        filename = path.normpath(path.abspath(filename))
        if filename.lower().endswith(('.pyo', '.pyc')):
            filename = filename[:-1]
            if not path.isfile(filename) and path.isfile(filename + 'w'):
                filename += 'w'
        elif not filename.lower().endswith(('.py', '.pyw')):
            raise PycodeError('source is not a .py file: %r' % filename)
        elif ('.egg' + path.sep) in filename:
            pat = '(?<=\\.egg)' + re.escape(path.sep)
            eggpath, _ = re.split(pat, filename, 1)
            if path.isfile(eggpath):
                return filename, None

        if not path.isfile(filename):
            raise PycodeError('source file is not present: %r' % filename)
        return filename, None
Example #3
0
    def analyze(self) -> None:
        """Analyze the source code."""
        if self._analyzed:
            return None

        try:
            parser = Parser(self.code, self._encoding)
            parser.parse()

            self.attr_docs = OrderedDict()
            for (scope, comment) in parser.comments.items():
                if comment:
                    self.attr_docs[scope] = comment.splitlines() + ['']
                else:
                    self.attr_docs[scope] = ['']

            self.annotations = parser.annotations
            self.finals = parser.finals
            self.overloads = parser.overloads
            self.tags = parser.definitions
            self.tagorder = parser.deforders
            self._analyzed = True
        except Exception as exc:
            raise PycodeError('parsing %r failed: %r' %
                              (self.srcname, exc)) from exc
Example #4
0
 def for_file(cls, filename, modname):
     if ('file', filename) in cls.cache:
         return cls.cache['file', filename]
     try:
         fileobj = open(filename, 'r')
     except Exception, err:
         raise PycodeError('error opening %r' % filename, err)
Example #5
0
 def for_egg(cls, filename, modname):
     # type: (unicode, unicode) -> ModuleAnalyzer
     eggpath, relpath = re.split('(?<=\\.egg)/', filename)
     try:
         with ZipFile(eggpath) as egg:
             code = egg.read(relpath).decode('utf-8')
             return cls.for_string(code, modname, filename)
     except Exception as exc:
         raise PycodeError('error opening %r' % filename, exc)
Example #6
0
 def for_egg(cls, filename: str, modname: str) -> "ModuleAnalyzer":
     SEP = re.escape(path.sep)
     eggpath, relpath = re.split('(?<=\\.egg)' + SEP, filename)
     try:
         with ZipFile(eggpath) as egg:
             code = egg.read(relpath).decode()
             return cls.for_string(code, modname, filename)
     except Exception as exc:
         raise PycodeError('error opening %r' % filename, exc) from exc
Example #7
0
 def tokenize(self):
     """Generate tokens from the source."""
     if self.tokens is not None:
         return
     try:
         self.tokens = list(tokenize.generate_tokens(self.source.readline))
     except tokenize.TokenError as err:
         raise PycodeError('tokenizing failed', err)
     self.source.close()
Example #8
0
 def parse(self):
     """Parse the generated source tokens."""
     if self.parsetree is not None:
         return
     self.tokenize()
     try:
         self.parsetree = pydriver.parse_tokens(self.tokens)
     except parse.ParseError as err:
         raise PycodeError('parsing failed', err)
 def for_file(cls, filename, modname):
     if ('file', filename) in cls.cache:
         return cls.cache['file', filename]
     try:
         with open(filename, 'rb') as f:
             obj = cls(f, modname, filename)
             cls.cache['file', filename] = obj
     except Exception as err:
         raise PycodeError('error opening %r' % filename, err)
     return obj
Example #10
0
 def for_file(cls, filename, modname):
     # type: (unicode, unicode) -> ModuleAnalyzer
     if ('file', filename) in cls.cache:
         return cls.cache['file', filename]
     try:
         with open(filename, 'rb') as f:
             obj = cls(f, modname, filename)  # type: ignore
             cls.cache['file', filename] = obj
     except Exception as err:
         raise PycodeError('error opening %r' % filename, err)
     return obj
def get_module_source(modname):
    """Try to find the source code for a module.

    Can return ('file', 'filename') in which case the source is in the given
    file, or ('string', 'source') which which case the source is the string.
    """
    if modname not in sys.modules:
        try:
            __import__(modname)
        except Exception, err:
            raise PycodeError('error importing %r' % modname, err)
Example #12
0
 def for_file(cls, filename: str, modname: str) -> "ModuleAnalyzer":
     if ('file', filename) in cls.cache:
         return cls.cache['file', filename]
     try:
         with tokenize.open(filename) as f:
             obj = cls(f, modname, filename)
             cls.cache['file', filename] = obj
     except Exception as err:
         if '.egg' + path.sep in filename:
             obj = cls.cache['file', filename] = cls.for_egg(filename, modname)
         else:
             raise PycodeError('error opening %r' % filename, err) from err
     return obj
Example #13
0
 def for_file(cls, filename, modname):
     # type: (str, str) -> ModuleAnalyzer
     if ('file', filename) in cls.cache:
         return cls.cache['file', filename]
     try:
         with open(filename, 'rb') as f:
             obj = cls(f, modname, filename)
             cls.cache['file', filename] = obj
     except Exception as err:
         if '.egg/' in filename:
             obj = cls.cache['file',
                             filename] = cls.for_egg(filename, modname)
         else:
             raise PycodeError('error opening %r' % filename, err)
     return obj
Example #14
0
    def parse(self) -> None:
        """Parse the source code."""
        try:
            parser = Parser(self.code, self.encoding)
            parser.parse()

            self.attr_docs = {}
            for (scope, comment) in parser.comments.items():
                if comment:
                    self.attr_docs[scope] = comment.splitlines() + ['']
                else:
                    self.attr_docs[scope] = ['']

            self.tags = parser.definitions
            self.tagorder = parser.deforders
        except Exception as exc:
            raise PycodeError('parsing %r failed: %r' % (self.srcname, exc))
def get_module_source(modname: str) -> Tuple[str, str]:
    """Try to find the source code for a module.

    Can return ('file', 'filename') in which case the source is in the given
    file, or ('string', 'source') which which case the source is the string.
    """
    warnings.warn('get_module_source() is deprecated.',
                  RemovedInSphinx40Warning,
                  stacklevel=2)
    try:
        mod = import_module(modname)
    except Exception as err:
        raise PycodeError('error importing %r' % modname, err) from err
    filename = getattr(mod, '__file__', None)
    loader = getattr(mod, '__loader__', None)
    if loader and getattr(loader, 'get_filename', None):
        try:
            filename = loader.get_filename(modname)
        except Exception as err:
            raise PycodeError('error getting filename for %r' % filename,
                              err) from err
    if filename is None and loader:
        try:
            filename = loader.get_source(modname)
            if filename:
                return 'string', filename
        except Exception as err:
            raise PycodeError('error getting source for %r' % modname,
                              err) from err
    if filename is None:
        raise PycodeError('no source found for module %r' % modname)
    filename = path.normpath(path.abspath(filename))
    lfilename = filename.lower()
    if lfilename.endswith('.pyo') or lfilename.endswith('.pyc'):
        filename = filename[:-1]
        if not path.isfile(filename) and path.isfile(filename + 'w'):
            filename += 'w'
    elif not (lfilename.endswith('.py') or lfilename.endswith('.pyw')):
        raise PycodeError('source is not a .py file: %r' % filename)
    elif ('.egg' + os.path.sep) in filename:
        pat = '(?<=\\.egg)' + re.escape(os.path.sep)
        eggpath, _ = re.split(pat, filename, 1)
        if path.isfile(eggpath):
            return 'file', filename

    if not path.isfile(filename):
        raise PycodeError('source file is not present: %r' % filename)
    return 'file', filename
Example #16
0
def get_module_source(modname):
    # type: (str) -> Tuple[unicode, unicode]
    """Try to find the source code for a module.

    Can return ('file', 'filename') in which case the source is in the given
    file, or ('string', 'source') which which case the source is the string.
    """
    if modname not in sys.modules:
        try:
            __import__(modname)
        except Exception as err:
            raise PycodeError('error importing %r' % modname, err)
    mod = sys.modules[modname]
    filename = getattr(mod, '__file__', None)
    loader = getattr(mod, '__loader__', None)
    if loader and getattr(loader, 'get_filename', None):
        try:
            filename = loader.get_filename(modname)
        except Exception as err:
            raise PycodeError('error getting filename for %r' % filename, err)
    if filename is None and loader:
        try:
            filename = loader.get_source(modname)
            if filename:
                return 'string', filename
        except Exception as err:
            raise PycodeError('error getting source for %r' % modname, err)
    if filename is None:
        raise PycodeError('no source found for module %r' % modname)
    filename = path.normpath(path.abspath(filename))
    lfilename = filename.lower()
    if lfilename.endswith('.pyo') or lfilename.endswith('.pyc'):
        filename = filename[:-1]
        if not path.isfile(filename) and path.isfile(filename + 'w'):
            filename += 'w'
    elif not (lfilename.endswith('.py') or lfilename.endswith('.pyw')):
        raise PycodeError('source is not a .py file: %r' % filename)
    elif ('.egg' + os.path.sep) in filename:
        pat = '(?<=\\.egg)' + re.escape(os.path.sep)
        eggpath, _ = re.split(pat, filename, 1)
        if path.isfile(eggpath):
            return 'file', filename

    if not path.isfile(filename):
        raise PycodeError('source file is not present: %r' % filename)
    return 'file', filename
    """Try to find the source code for a module.

    Can return ('file', 'filename') in which case the source is in the given
    file, or ('string', 'source') which which case the source is the string.
    """
    if modname not in sys.modules:
        try:
            __import__(modname)
        except Exception, err:
            raise PycodeError('error importing %r' % modname, err)
    mod = sys.modules[modname]
    if hasattr(mod, '__loader__'):
        try:
            source = mod.__loader__.get_source(modname)
        except Exception, err:
            raise PycodeError('error getting source for %r' % modname, err)
        return 'string', source
    filename = getattr(mod, '__file__', None)
    if filename is None:
        raise PycodeError('no source found for module %r' % modname)
    filename = path.normpath(path.abspath(filename))
    lfilename = filename.lower()
    if lfilename.endswith('.pyo') or lfilename.endswith('.pyc'):
        filename = filename[:-1]
    elif not (lfilename.endswith('.py') or lfilename.endswith('.pyw')):
        raise PycodeError('source is not a .py file: %r' % filename)
    if not path.isfile(filename):
        raise PycodeError('source file is not present: %r' % filename)
    return 'file', filename