Example #1
0
def collectImportResolutions(content, fileName):
    """Provides classified import information and errors if so"""
    depClasses = {
        'system': [],
        'project': [],
        'other': [],
        'unresolved': [],
        'totalCount': 0,
        'errors': []
    }
    try:
        for resolution in getImportResolutions(fileName,
                                               getImportsList(content)):
            if resolution.isResolved():
                if resolution.builtIn:
                    depClasses['system'].append(resolution)
                elif __isLocalOrProject(fileName, resolution.path):
                    depClasses['project'].append(resolution)
                elif __isSystem(resolution.path):
                    depClasses['system'].append(resolution)
                else:
                    depClasses['unresolved'].append(resolution)
            else:
                depClasses['unresolved'].append(resolution)

            depClasses['totalCount'] += 1
    except Exception as exc:
        depClasses['errors'].append(str(exc))

    return depClasses
Example #2
0
 def onOpenImport(self):
     """Triggered when Ctrl+I is received"""
     if isPythonMime(self.__editor.mime):
         # Take all the file imports and resolve them
         fileImports = getImportsList(self.__editor.text)
         if not fileImports:
             GlobalData().mainWindow.showStatusBarMessage(
                 "There are no imports")
         else:
             self.__onImportList(self.__fileName, fileImports)
    def onOpenImport( self ):
        " Triggered when Ctrl+I is received "
        if self.__fileType not in [ PythonFileType, Python3FileType ]:
            return True

        # Python file, we may continue
        importLine, lineNo = isImportLine( self.__viewer )
        basePath = os.path.dirname( self.__fileName )

        if importLine:
            lineImports, importWhat = getImportsInLine( self.__viewer.text(),
                                                        lineNo + 1 )
            currentWord = str( self.__viewer.getCurrentWord( "." ) )
            if currentWord in lineImports:
                # The cursor is on some import
                path = resolveImport( basePath, currentWord )
                if path != '':
                    GlobalData().mainWindow.openFile( path, -1 )
                    return True
                GlobalData().mainWindow.showStatusBarMessage(
                        "The import '" + currentWord + "' is not resolved.", 0 )
                return True
            # We are not on a certain import.
            # Check if it is a line with exactly one import
            if len( lineImports ) == 1:
                path = resolveImport( basePath, lineImports[ 0 ] )
                if path == '':
                    GlobalData().mainWindow.showStatusBarMessage(
                        "The import '" + lineImports[ 0 ] +
                        "' is not resolved.", 0 )
                    return True
                # The import is resolved. Check where we are.
                if currentWord in importWhat:
                    # We are on a certain imported name in a resolved import
                    # So, jump to the definition line
                    line = getImportedNameDefinitionLine( path, currentWord )
                    GlobalData().mainWindow.openFile( path, line )
                    return True
                GlobalData().mainWindow.openFile( path, -1 )
                return True

            # Take all the imports in the line and resolve them.
            self.__onImportList( basePath, lineImports )
            return True

        # Here: the cursor is not on the import line. Take all the file imports
        # and resolve them
        fileImports = getImportsList( self.__viewer.text() )
        if not fileImports:
            GlobalData().mainWindow.showStatusBarMessage(
                                            "There are no imports.", 0 )
            return True
        if len( fileImports ) == 1:
            path = resolveImport( basePath, fileImports[ 0 ] )
            if path == '':
                GlobalData().mainWindow.showStatusBarMessage(
                    "The import '" + fileImports[ 0 ] + "' is not resolved.", 0 )
                return True
            GlobalData().mainWindow.openFile( path, -1 )
            return True

        self.__onImportList( basePath, fileImports )
        return True
Example #4
0
    def onOpenImport(self):
        " Triggered when Ctrl+I is received "
        if self.__fileType not in [PythonFileType, Python3FileType]:
            return True

        # Python file, we may continue
        importLine, lineNo = isImportLine(self.__viewer)
        basePath = os.path.dirname(self.__fileName)

        if importLine:
            lineImports, importWhat = getImportsInLine(self.__viewer.text(),
                                                       lineNo + 1)
            currentWord = str(self.__viewer.getCurrentWord("."))
            if currentWord in lineImports:
                # The cursor is on some import
                path = resolveImport(basePath, currentWord)
                if path != '':
                    GlobalData().mainWindow.openFile(path, -1)
                    return True
                GlobalData().mainWindow.showStatusBarMessage(
                    "The import '" + currentWord + "' is not resolved.", 0)
                return True
            # We are not on a certain import.
            # Check if it is a line with exactly one import
            if len(lineImports) == 1:
                path = resolveImport(basePath, lineImports[0])
                if path == '':
                    GlobalData().mainWindow.showStatusBarMessage(
                        "The import '" + lineImports[0] + "' is not resolved.",
                        0)
                    return True
                # The import is resolved. Check where we are.
                if currentWord in importWhat:
                    # We are on a certain imported name in a resolved import
                    # So, jump to the definition line
                    line = getImportedNameDefinitionLine(path, currentWord)
                    GlobalData().mainWindow.openFile(path, line)
                    return True
                GlobalData().mainWindow.openFile(path, -1)
                return True

            # Take all the imports in the line and resolve them.
            self.__onImportList(basePath, lineImports)
            return True

        # Here: the cursor is not on the import line. Take all the file imports
        # and resolve them
        fileImports = getImportsList(self.__viewer.text())
        if not fileImports:
            GlobalData().mainWindow.showStatusBarMessage(
                "There are no imports.", 0)
            return True
        if len(fileImports) == 1:
            path = resolveImport(basePath, fileImports[0])
            if path == '':
                GlobalData().mainWindow.showStatusBarMessage(
                    "The import '" + fileImports[0] + "' is not resolved.", 0)
                return True
            GlobalData().mainWindow.openFile(path, -1)
            return True

        self.__onImportList(basePath, fileImports)
        return True