Пример #1
0
        def __init__(self):
            self.application = None
            self.splash = None
            self.mainWindow = None
            self.skin = None
            self.screenWidth = 0
            self.screenHeight = 0
            self.version = "unknown"
            self.project = CodimensionProject()

            self.pluginManager = CDMPluginManager()

            self.briefModinfoCache = BriefModuleInfoCache()
            self.runParamsCache = RunParametersCache()
            if os.path.isfile(settingsDir + "runparamscache"):
                self.runParamsCache.deserialize(settingsDir + "runparamscache")

            self.pylintAvailable = self.__checkPylint()
            self.graphvizAvailable = self.__checkGraphviz()

            self.pylintVersion = None
            if self.pylintAvailable:
                self.pylintVersion = self.__getPylintVersion()
                if self.pylintVersion is None:
                    self.pylintAvailable = False
            return
Пример #2
0
        def __init__( self ):
            self.application = None
            self.splash = None
            self.mainWindow = None
            self.skin = None
            self.screenWidth = 0
            self.screenHeight = 0
            self.version = "unknown"
            self.project = CodimensionProject()

            self.pluginManager = CDMPluginManager()

            self.briefModinfoCache = BriefModuleInfoCache()
            self.runParamsCache = RunParametersCache()
            if os.path.isfile( settingsDir + "runparamscache" ):
                self.runParamsCache.deserialize( settingsDir +
                                                 "runparamscache" )

            self.pylintAvailable = self.__checkPylint()
            self.graphvizAvailable = self.__checkGraphviz()

            self.pylintVersion = None
            if self.pylintAvailable:
                self.pylintVersion = self.__getPylintVersion()
                if self.pylintVersion is None:
                    self.pylintAvailable = False
            return
Пример #3
0
    class Singleton:
        """ Provides singleton facility """

        def __init__( self ):
            self.application = None
            self.splash = None
            self.mainWindow = None
            self.skin = None
            self.screenWidth = 0
            self.screenHeight = 0
            self.version = "unknown"
            self.project = CodimensionProject()

            self.pluginManager = CDMPluginManager()

            self.briefModinfoCache = BriefModuleInfoCache()
            self.runParamsCache = RunParametersCache()
            if os.path.isfile( settingsDir + "runparamscache" ):
                self.runParamsCache.deserialize( settingsDir +
                                                 "runparamscache" )

            self.pylintAvailable = self.__checkPylint()
            self.graphvizAvailable = self.__checkGraphviz()

            self.pylintVersion = None
            if self.pylintAvailable:
                self.pylintVersion = self.__getPylintVersion()
                if self.pylintVersion is None:
                    self.pylintAvailable = False
            return

        def getRunParameters( self, fileName ):
            " Provides the run parameters "
            if self.project.isLoaded():
                if self.project.isProjectFile( fileName ):
                    key = relpath( fileName,
                                   os.path.dirname( self.project.fileName ) )
                else:
                    key = fileName
                return self.project.runParamsCache.get( key )

            # No project loaded
            return self.runParamsCache.get( fileName )

        def addRunParams( self, fileName, params ):
            " Registers new latest run parameters "
            if self.project.isLoaded():
                if self.project.isProjectFile( fileName ):
                    key = relpath( fileName,
                                   os.path.dirname( self.project.fileName ) )
                else:
                    key = fileName
                self.project.runParamsCache.add( key, params )
                self.project.serializeRunParameters()
                return

            # No project loaded
            self.runParamsCache.add( fileName, params )
            self.runParamsCache.serialize( settingsDir + "runparamscache" )
            return

        def getProfileOutputPath( self ):
            " Provides the path to the profile output file "
            if self.project.isLoaded():
                return self.project.userProjectDir + "profile.out"

            # No project loaded
            return settingsDir + "profile.out"

        def getRopeProject( self, fileName = "" ):
            " Provides existed or creates a new rope project "
            if self.project.isLoaded():
                return self.project.getRopeProject()

            # There is no current project so create a temporary one.
            # Two cases: the buffer has been saved
            #            not saved buffer
            if os.path.isabs( fileName ):
                dirName = os.path.dirname( fileName )
            else:
                # Unsaved buffer, make an assumption that
                # it is in home directory
                dirName = str( QDir.homePath() )

            prefs = copy.deepcopy( ropePreferences )

            # Exclude nested dirs as it could take too long
            # Get only dir names and do not get those dirs
            # where __init__.py[3] are present
            subdirsToExclude = getSubdirs( dirName, True, True )

            if "ignored_resources" in prefs:
                prefs[ "ignored_resources" ] += subdirsToExclude
            else:
                prefs[ "ignored_resources" ] = subdirsToExclude

            project = RopeProject( dirName, None, None, **prefs )
            project.validate( project.root )
            return project

        def validateRopeProject( self, fileName = "" ):
            " Validates the existed rope project if so "
            if not self.project.isLoaded():
                return

            # Currently rope supports validating of directories only
            # There is a hope that it will support validating a single file
            # one day. So the fileName argument is ignored by now and the whole
            # project is invalidated.
            if fileName != "":
                from fileutils import ( detectFileType, PythonFileType,
                                        Python3FileType )
                fileType = detectFileType( fileName )
                if fileType not in [ PythonFileType, Python3FileType ]:
                    return
            self.project.validateRopeProject( fileName )
            return

        def getProjectImportDirs( self ):
            """ Provides a list of the project import dirs if so.
                Note: the paths do not have '/' at the end due to
                os.path.normpath """
            if not self.project.isLoaded():
                return []

            basePath = self.project.getProjectDir()
            result = list( self.project.importDirs )
            index = len( result ) - 1
            while index >= 0:
                path = result[ index ]
                if not os.path.isabs( path ):
                    result[ index ] = os.path.normpath( basePath + path )
                index -= 1
            return result

        def isProjectScriptValid( self ):
            " True if the project script valid "
            scriptName = self.project.getProjectScript()
            if not os.path.exists( scriptName ):
                return False
            scriptName = os.path.realpath( scriptName )
            if not os.path.isfile( scriptName ):
                return False

            from fileutils import ( detectFileType, PythonFileType,
                                    Python3FileType )
            if detectFileType( scriptName ) in [ PythonFileType,
                                                 Python3FileType ]:
                return True
            return False

        def getFileLineDocstring( self, fName, line ):
            " Provides a docstring if so for the given file and line "
            from fileutils import ( detectFileType, PythonFileType,
                                    Python3FileType )
            if detectFileType( fName ) not in [ PythonFileType,
                                                Python3FileType ]:
                return ""

            infoCache = self.briefModinfoCache

            def checkFuncObject( obj, line ):
                " Checks docstring for a function or a class "
                if obj.line == line or obj.keywordLine == line:
                    if obj.docstring is None:
                        return True, ""
                    return True, obj.docstring.text
                for item in obj.classes + obj.functions:
                    found, docstring = checkFuncObject( item, line )
                    if found:
                        return True, docstring
                return False, ""

            try:
                info = infoCache.get( fName )
                for item in info.classes + info.functions:
                    found, docstring = checkFuncObject( item, line )
                    if found:
                        return docstring
            except:
                pass
            return ""

        def getModInfo( self, path ):
            " Provides a module info for the given file "
            from fileutils import ( detectFileType, PythonFileType,
                                    Python3FileType )
            if detectFileType( path ) not in [ PythonFileType,
                                               Python3FileType ]:
                raise Exception( "Trying to parse non-python file: " + path )
            return self.briefModinfoCache.get( path )

        @staticmethod
        def __checkGraphviz():
            " Checks if the graphviz available "
            if 'win' in sys.platform.lower():
                return os.system( 'which dot > /NUL 2>&1' ) == 0
            return os.system( 'which dot > /dev/null 2>&1' ) == 0

        @staticmethod
        def __checkPylint():
            " Checks if pylint is available "
            if 'win' in sys.platform.lower():
                return os.system( 'which pylint > /NUL 2>&1' ) == 0
            return os.system( 'which pylint > /dev/null 2>&1' ) == 0

        @staticmethod
        def __getPylintVersion():
            " Provides the pylint version "
            output = check_output( "pylint --version; exit 0",
                                   stderr = STDOUT, shell = True )
            for line in output.splitlines():
                line = line.strip()
                if line.startswith( "pylint " ):
                    version = line.replace( "pylint", "" ).replace( ",", "" )
                    try:
                        return StrictVersion( version.strip() )
                    except:
                        return None
            return None
Пример #4
0
    class Singleton:
        """ Provides singleton facility """
        def __init__(self):
            self.application = None
            self.splash = None
            self.mainWindow = None
            self.skin = None
            self.screenWidth = 0
            self.screenHeight = 0
            self.version = "unknown"
            self.project = CodimensionProject()

            self.pluginManager = CDMPluginManager()

            self.briefModinfoCache = BriefModuleInfoCache()
            self.runParamsCache = RunParametersCache()
            if os.path.isfile(settingsDir + "runparamscache"):
                self.runParamsCache.deserialize(settingsDir + "runparamscache")

            self.pylintAvailable = self.__checkPylint()
            self.graphvizAvailable = self.__checkGraphviz()

            self.pylintVersion = None
            if self.pylintAvailable:
                self.pylintVersion = self.__getPylintVersion()
                if self.pylintVersion is None:
                    self.pylintAvailable = False
            return

        def getRunParameters(self, fileName):
            " Provides the run parameters "
            if self.project.isLoaded():
                if self.project.isProjectFile(fileName):
                    key = relpath(fileName,
                                  os.path.dirname(self.project.fileName))
                else:
                    key = fileName
                return self.project.runParamsCache.get(key)

            # No project loaded
            return self.runParamsCache.get(fileName)

        def addRunParams(self, fileName, params):
            " Registers new latest run parameters "
            if self.project.isLoaded():
                if self.project.isProjectFile(fileName):
                    key = relpath(fileName,
                                  os.path.dirname(self.project.fileName))
                else:
                    key = fileName
                self.project.runParamsCache.add(key, params)
                self.project.serializeRunParameters()
                return

            # No project loaded
            self.runParamsCache.add(fileName, params)
            self.runParamsCache.serialize(settingsDir + "runparamscache")
            return

        def getProfileOutputPath(self):
            " Provides the path to the profile output file "
            if self.project.isLoaded():
                return self.project.userProjectDir + "profile.out"

            # No project loaded
            return settingsDir + "profile.out"

        def getRopeProject(self, fileName=""):
            " Provides existed or creates a new rope project "
            if self.project.isLoaded():
                return self.project.getRopeProject()

            # There is no current project so create a temporary one.
            # Two cases: the buffer has been saved
            #            not saved buffer
            if os.path.isabs(fileName):
                dirName = os.path.dirname(fileName)
            else:
                # Unsaved buffer, make an assumption that
                # it is in home directory
                dirName = str(QDir.homePath())

            prefs = copy.deepcopy(ropePreferences)

            # Exclude nested dirs as it could take too long
            # Get only dir names and do not get those dirs
            # where __init__.py[3] are present
            subdirsToExclude = getSubdirs(dirName, True, True)

            if "ignored_resources" in prefs:
                prefs["ignored_resources"] += subdirsToExclude
            else:
                prefs["ignored_resources"] = subdirsToExclude

            project = RopeProject(dirName, None, None, **prefs)
            project.validate(project.root)
            return project

        def validateRopeProject(self, fileName=""):
            " Validates the existed rope project if so "
            if not self.project.isLoaded():
                return

            # Currently rope supports validating of directories only
            # There is a hope that it will support validating a single file
            # one day. So the fileName argument is ignored by now and the whole
            # project is invalidated.
            if fileName != "":
                from fileutils import (detectFileType, PythonFileType,
                                       Python3FileType)
                fileType = detectFileType(fileName)
                if fileType not in [PythonFileType, Python3FileType]:
                    return
            self.project.validateRopeProject(fileName)
            return

        def getProjectImportDirs(self):
            """ Provides a list of the project import dirs if so.
                Note: the paths do not have '/' at the end due to
                os.path.normpath """
            if not self.project.isLoaded():
                return []

            basePath = self.project.getProjectDir()
            result = list(self.project.importDirs)
            index = len(result) - 1
            while index >= 0:
                path = result[index]
                if not os.path.isabs(path):
                    result[index] = os.path.normpath(basePath + path)
                index -= 1
            return result

        def isProjectScriptValid(self):
            " True if the project script valid "
            scriptName = self.project.getProjectScript()
            if not os.path.exists(scriptName):
                return False
            scriptName = os.path.realpath(scriptName)
            if not os.path.isfile(scriptName):
                return False

            from fileutils import (detectFileType, PythonFileType,
                                   Python3FileType)
            if detectFileType(scriptName) in [PythonFileType, Python3FileType]:
                return True
            return False

        def getFileLineDocstring(self, fName, line):
            " Provides a docstring if so for the given file and line "
            from fileutils import (detectFileType, PythonFileType,
                                   Python3FileType)
            if detectFileType(fName) not in [PythonFileType, Python3FileType]:
                return ""

            infoCache = self.briefModinfoCache

            def checkFuncObject(obj, line):
                " Checks docstring for a function or a class "
                if obj.line == line or obj.keywordLine == line:
                    if obj.docstring is None:
                        return True, ""
                    return True, obj.docstring.text
                for item in obj.classes + obj.functions:
                    found, docstring = checkFuncObject(item, line)
                    if found:
                        return True, docstring
                return False, ""

            try:
                info = infoCache.get(fName)
                for item in info.classes + info.functions:
                    found, docstring = checkFuncObject(item, line)
                    if found:
                        return docstring
            except:
                pass
            return ""

        def getModInfo(self, path):
            " Provides a module info for the given file "
            from fileutils import (detectFileType, PythonFileType,
                                   Python3FileType)
            if detectFileType(path) not in [PythonFileType, Python3FileType]:
                raise Exception("Trying to parse non-python file: " + path)
            return self.briefModinfoCache.get(path)

        @staticmethod
        def __checkGraphviz():
            " Checks if the graphviz available "
            if 'win' in sys.platform.lower():
                return os.system('which dot > /NUL 2>&1') == 0
            return os.system('which dot > /dev/null 2>&1') == 0

        @staticmethod
        def __checkPylint():
            " Checks if pylint is available "
            if 'win' in sys.platform.lower():
                return os.system('which pylint > /NUL 2>&1') == 0
            return os.system('which pylint > /dev/null 2>&1') == 0

        @staticmethod
        def __getPylintVersion():
            " Provides the pylint version "
            output = check_output("pylint --version; exit 0",
                                  stderr=STDOUT,
                                  shell=True)
            for line in output.splitlines():
                line = line.strip()
                if line.startswith("pylint "):
                    version = line.replace("pylint", "").replace(",", "")
                    try:
                        return StrictVersion(version.strip())
                    except:
                        return None
            return None