当前位置: 首页>>代码示例>>Python>>正文


Python FileSystem.replaceFile方法代码示例

本文整理汇总了Python中FileSystem.FileSystem.replaceFile方法的典型用法代码示例。如果您正苦于以下问题:Python FileSystem.replaceFile方法的具体用法?Python FileSystem.replaceFile怎么用?Python FileSystem.replaceFile使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在FileSystem.FileSystem的用法示例。


在下文中一共展示了FileSystem.replaceFile方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: UserSettings

# 需要导入模块: from FileSystem import FileSystem [as 别名]
# 或者: from FileSystem.FileSystem import replaceFile [as 别名]
class UserSettings():
    def __init__(self, fileName=None, fileSystem=None):
        if fileSystem is not None:
            self.fileSystem = FileSystem()
        self._settingsVariables = dict()
        if fileName is not None:
            self.setFile(fileName)

    def set(self, variable, value):
        self._settingsVariables[variable] = value
        settingsNewContent = json.dumps(self._settingsVariables)

        return self.fileSystem.replaceFile(self.fileName, settingsNewContent)
    
    def get(self, variableName):
        return self._settingsVariables[variableName]

    def deleteAll(self):
        self.fileSystem.remove(self.fileName)

    def setFile(self, fileName):
        self.fileName = fileName
        userSettingsExist = self.fileSystem.isfile(fileName)
        if userSettingsExist != True:
            settingsContent = "{\n}"
            created = self.fileSystem.createFile(fileName, settingsContent)
        else:
            settingsContent = self.fileSystem.getFileContent(self.fileName)
        self._settingsVariables = json.loads(settingsContent)
开发者ID:anconaesselmann,项目名称:LiveUnit,代码行数:31,代码来源:UserSettings.py

示例2: LiveUnitTesting

# 需要导入模块: from FileSystem import FileSystem [as 别名]
# 或者: from FileSystem.FileSystem import replaceFile [as 别名]
class LiveUnitTesting():
    def __init__(self, commandFolders):
        if not isinstance(commandFolders, dict):
            raise Exception("CommandFolders is a dictionary")
        self._commandFolders = commandFolders
        self._lastModTempFile = None
        self.fileSystem = FileSystem()

    def updateTempFiles(self, currentView):
        self._setActiveFile(currentView)
        if self._activeFile.getExtension() in self._commandFolders:
            lastModTempFile = os.stat(self._activeFile.getTestFileName()).st_mtime
            if lastModTempFile != self._lastModTempFile:
                currentTestFileContent = self.fileSystem.getFileContent(self._activeFile.getTestFileName())
                self._saveToTempTestFile(currentTestFileContent)
                self._lastModTempFile = lastModTempFile

            if self._activeFile.getKind() == self._activeFile.KIND_IS_CLASS:
                curentViewContent = SublimeFunctions.getViewContent(currentView)
                self._saveToTempClassFile(curentViewContent)

    def getCommand(self):
        extension = self._activeFile.getExtension()
        if extension == "php":
            result = os.path.join(os.path.normpath(self._commandFolders["php"]), "phpunit")
        elif extension == "py":
            result = os.path.join(os.path.normpath(self._commandFolders["py"]), "python")
        elif extension == "js":
            result = UnitTestFunctions.getCommand(extension)
        else:
            result = None
        return result

    def getArgument(self):
        return self._getTempTestFileDir()

    def getActiveFile(self):
        return self._activeFile

    def _setActiveFile(self, currentView):
        md = MirroredDirectory()
        md.fileSystem = self.fileSystem
        md.set(currentView.file_name())
        self._activeFile = md

    def _getTempFileDir(self):
        folderName, completeFileName = os.path.split(self._activeFile.getFileName())
        fileName, extension = os.path.splitext(completeFileName)
        return os.path.join(folderName, "____liveUnitTesting_" + fileName + extension)

    def _getTempTestFileDir(self):
        folderName, completeFileName = os.path.split(self._activeFile.getTestFileName())
        fileName, extension = os.path.splitext(completeFileName)
        return os.path.join(folderName, "____liveUnitTesting_" + fileName + extension)

    def _saveToTempClassFile(self, curentViewContent):
        extension = self._activeFile.getExtension()
        self.fileSystem.replaceFile(self._getTempFileDir(), curentViewContent)

    def _saveToTempTestFile(self, testFileContent):
        self.fileSystem.replaceFile(self._getTempTestFileDir(), testFileContent)

        extension = self._activeFile.getExtension()
        if extension == "php":
            self._replacePhpTestFileLoadingStatements()
        elif extension == "py":
            self._replacePyTestFileLoadingStatements()
        else:
            pass

    def _replacePhpTestFileLoadingStatements(self):
        injected = False
        for line in fileinput.input(self._getTempTestFileDir(), inplace=True):
            sys.stdout.write(line)
            if not injected:
                if "require_once" in line:
                    sys.stdout.write("\trequire_once \"" + self._getTempFileDir() + "\";")
                    injected = True

    def _replacePyTestFileLoadingStatements(self):
        folderName, completeFileName = os.path.split(self._activeFile.getFileName())
        fileName, extension = os.path.splitext(completeFileName)
        newModule = "____liveUnitTesting_" + fileName
        
        replacementMade = False
        for line in fileinput.input(self._getTempTestFileDir(), inplace=True):
            if replacementMade == False and "import "in line:
                if fileName in line:
                    pos = line.find(fileName)
                    partialString = line[:pos]
                    regexString = "\\S+$"

                    match = re.search(regexString, partialString)
                    if match:
                        parentPackages = match.group()
                    else:
                        parentPackages = ""
                    if len(parentPackages) > 0 and parentPackages[-1] == ".":
                        newModule = parentPackages + newModule
                        line = "from " + newModule + " import *\n"
#.........这里部分代码省略.........
开发者ID:anconaesselmann,项目名称:LiveUnit,代码行数:103,代码来源:LiveUnitTesting.py


注:本文中的FileSystem.FileSystem.replaceFile方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。