Package fabmetheus_utilities :: Module archive
[hide private]
[frames] | no frames]

Source Code for Module fabmetheus_utilities.archive

  1  """ 
  2  Boolean geometry utilities. 
  3  """ 
  4   
  5  import os 
  6  import sys 
  7  import traceback 
  8   
  9  __author__ = 'Enrique Perez (perez_enrique@yahoo.com)' 
 10  __credits__ = 'Art of Illusion <http://www.artofillusion.org/>' 
 11  __date__ = '$Date: 2008/02/05 $' 
 12  __license__ = 'GNU Affero General Public License http://www.gnu.org/licenses/agpl.html' 
 13   
 14  globalTemporarySettingsPath = os.path.join(os.getcwd(), 'sfact_profiles') 
 15   
16 -def getLinesFromAlterationsFile(fileName):
17 lines = [] 18 absPath = os.path.join('alterations', fileName) 19 try: 20 f = open(absPath, 'r') 21 lines = f.read().replace('\r', '\n').replace('\n\n', '\n').split('\n') 22 f.close() 23 except IOError as e: 24 logger.warning("Unable to open file: %s", absPath) 25 return lines
26
27 -def addToNamePathDictionary(directoryPath, namePathDictionary):
28 'Add to the name path dictionary.' 29 pluginFileNames = getPluginFileNamesFromDirectoryPath(directoryPath) 30 for pluginFileName in pluginFileNames: 31 namePathDictionary[pluginFileName.replace('_', '')] = os.path.join(directoryPath, pluginFileName)
32
33 -def getAbsoluteFolderPath(filePath, folderName=''):
34 'Get the absolute folder path.' 35 absoluteFolderPath = os.path.dirname(os.path.abspath(filePath)) 36 if folderName == '': 37 return absoluteFolderPath 38 return os.path.join(absoluteFolderPath, folderName)
39
40 -def getAbsoluteFrozenFolderPath(filePath, folderName=''):
41 'Get the absolute frozen folder path.' 42 if hasattr(sys, 'frozen'): 43 if '.py' in filePath: 44 filePath = ''.join(filePath.rpartition('\\')[: 2]) 45 filePath = os.path.join(filePath, 'skeinforge_application') 46 return getAbsoluteFolderPath(filePath, folderName)
47
48 -def getDocumentationPath(subName=''):
49 'Get the documentation file path.' 50 return getJoinedPath(getFabmetheusPath('documentation'), subName)
51
52 -def getElementsPath(subName=''):
53 'Get the evaluate_elements directory path.' 54 return getJoinedPath(getGeometryUtilitiesPath('evaluate_elements'), subName)
55
56 -def getEndsWithList(word, wordEndings):
57 'Determine if the word ends with a list.' 58 for wordEnding in wordEndings: 59 if word.endswith(wordEnding): 60 return True 61 return False
62
63 -def getFabmetheusPath(subName=''):
64 'Get the fabmetheus directory path.' 65 fabmetheusFile = None 66 if hasattr(sys, 'frozen'): 67 fabmetheusFile = unicode(sys.executable, sys.getfilesystemencoding()) 68 else: 69 fabmetheusFile = os.path.dirname(os.path.abspath(__file__)) 70 return getJoinedPath(os.path.dirname(fabmetheusFile), subName)
71
72 -def getFabmetheusUtilitiesPath(subName=''):
73 'Get the fabmetheus utilities directory path.' 74 return getJoinedPath(getFabmetheusPath('fabmetheus_utilities'), subName)
75
76 -def getFileNamesByFilePaths(pluginFilePaths):
77 'Get the file names of the plugins by the file paths.' 78 fileNames = [] 79 for pluginFilePath in pluginFilePaths: 80 pluginBasename = os.path.basename(pluginFilePath) 81 pluginBasename = getUntilDot(pluginBasename) 82 fileNames.append(pluginBasename) 83 return fileNames
84
85 -def getFilePaths(fileInDirectory=''):
86 'Get the file paths in the directory of the file in directory.' 87 directoryName = os.getcwd() 88 if fileInDirectory != '': 89 directoryName = os.path.dirname(fileInDirectory) 90 return getFilePathsByDirectory(directoryName)
91
92 -def getFilePathsByDirectory(directoryName):
93 'Get the file paths in the directory of the file in directory.' 94 absoluteDirectoryPath = os.path.abspath(directoryName) 95 directory = os.listdir(directoryName) 96 filePaths = [] 97 for fileName in directory: 98 filePaths.append(os.path.join(absoluteDirectoryPath, fileName)) 99 return filePaths
100
101 -def getFilePathsRecursively(fileInDirectory=''):
102 'Get the file paths in the directory of the file in directory.' 103 filePaths = getFilePaths(fileInDirectory) 104 filePathsRecursively = filePaths[:] 105 for filePath in filePaths: 106 if os.path.isdir(filePath): 107 directory = os.listdir(filePath) 108 if len(directory) > 0: 109 filePathsRecursively += getFilePathsRecursively(os.path.join(filePath, directory[0])) 110 return filePathsRecursively
111
112 -def getFilePathWithUnderscoredBasename(fileName, suffix):
113 'Get the file path with all spaces in the basename replaced with underscores.' 114 suffixFileName = getUntilDot(fileName) + suffix 115 suffixDirectoryName = os.path.dirname(suffixFileName) 116 suffixReplacedBaseName = os.path.basename(suffixFileName).replace(' ', '_') 117 return os.path.join(suffixDirectoryName, suffixReplacedBaseName)
118
119 -def getFileText(fileName, printWarning=True, readMode='r'):
120 'Get the entire text of a file.' 121 try: 122 file = open(fileName, readMode) 123 fileText = file.read() 124 file.close() 125 return fileText 126 except IOError: 127 if printWarning: 128 print('The file ' + fileName + ' does not exist.') 129 return ''
130
131 -def getFileTextInFileDirectory(fileInDirectory, fileName, readMode='r'):
132 'Get the entire text of a file in the directory of the file in directory.' 133 absoluteFilePathInFileDirectory = os.path.join(os.path.dirname(fileInDirectory), fileName) 134 return getFileText(absoluteFilePathInFileDirectory, True, readMode)
135
136 -def getFilesWithFileTypesWithoutWords(fileTypes, words = [], fileInDirectory=''):
137 'Get files which have a given file type, but with do not contain a word in a list.' 138 filesWithFileTypes = [] 139 for filePath in getFilePaths(fileInDirectory): 140 for fileType in fileTypes: 141 if isFileWithFileTypeWithoutWords(fileType, filePath, words): 142 filesWithFileTypes.append(filePath) 143 filesWithFileTypes.sort() 144 return filesWithFileTypes
145
146 -def getFilesWithFileTypesWithoutWordsRecursively(fileTypes, words = [], fileInDirectory=''):
147 'Get files recursively which have a given file type, but with do not contain a word in a list.' 148 filesWithFileTypesRecursively = [] 149 for filePath in getFilePathsRecursively(fileInDirectory): 150 for fileType in fileTypes: 151 if isFileWithFileTypeWithoutWords(fileType, filePath, words): 152 filesWithFileTypesRecursively.append(filePath) 153 filesWithFileTypesRecursively.sort() 154 return filesWithFileTypesRecursively
155
156 -def getFilesWithFileTypeWithoutWords(fileType, words = [], fileInDirectory=''):
157 'Get files which have a given file type, but with do not contain a word in a list.' 158 filesWithFileType = [] 159 for filePath in getFilePaths(fileInDirectory): 160 if isFileWithFileTypeWithoutWords(fileType, filePath, words): 161 filesWithFileType.append(filePath) 162 filesWithFileType.sort() 163 return filesWithFileType
164
165 -def getFundamentalsPath(subName=''):
166 'Get the evaluate_fundamentals directory path.' 167 return getJoinedPath(getGeometryUtilitiesPath('evaluate_fundamentals'), subName)
168
169 -def getGeometryDictionary(folderName):
170 'Get to the geometry name path dictionary.' 171 geometryDictionary={} 172 geometryDirectory = getGeometryPath() 173 addToNamePathDictionary(os.path.join(geometryDirectory, folderName), geometryDictionary) 174 geometryPluginsDirectory = getFabmetheusUtilitiesPath('geometry_plugins') 175 addToNamePathDictionary(os.path.join(geometryPluginsDirectory, folderName), geometryDictionary) 176 return geometryDictionary
177
178 -def getGeometryPath(subName=''):
179 'Get the geometry directory path.' 180 return getJoinedPath(getFabmetheusUtilitiesPath('geometry'), subName)
181
182 -def getGeometryToolsPath(subName=''):
183 'Get the geometry tools directory path.' 184 return getJoinedPath(getGeometryPath('geometry_tools'), subName)
185
186 -def getGeometryUtilitiesPath(subName=''):
187 'Get the geometry_utilities directory path.' 188 return getJoinedPath(getGeometryPath('geometry_utilities'), subName)
189
190 -def getJoinedPath(path, subName=''):
191 'Get the joined file path.' 192 if subName == '': 193 return path 194 return os.path.join(path, subName)
195
196 -def getModuleWithDirectoryPath(directoryPath, fileName):
197 'Get the module from the fileName and folder name.' 198 if fileName == '': 199 print('The file name in getModule in archive was empty.') 200 return None 201 originalSystemPath = sys.path[:] 202 try: 203 sys.path.insert(0, directoryPath) 204 folderPluginsModule = __import__(fileName) 205 sys.path = originalSystemPath 206 return folderPluginsModule 207 except: 208 sys.path = originalSystemPath 209 print('') 210 print('Exception traceback in getModuleWithDirectoryPath in archive:') 211 traceback.print_exc(file=sys.stdout) 212 print('') 213 print('That error means; could not import a module with the fileName ' + fileName) 214 print('and an absolute directory name of ' + directoryPath) 215 print('') 216 return None
217
218 -def getModuleWithPath(path):
219 'Get the module from the path.' 220 return getModuleWithDirectoryPath(os.path.dirname(path), os.path.basename(path))
221
222 -def getPluginFileNamesFromDirectoryPath(directoryPath):
223 'Get the file names of the python plugins in the directory path.' 224 fileInDirectory = os.path.join(directoryPath, '__init__.py') 225 return getFileNamesByFilePaths(getPythonFileNamesExceptInit(fileInDirectory))
226
227 -def getProfilesPath(subName=''):
228 'Get the profiles directory path, which is the settings directory joined with profiles.' 229 return getJoinedPath(getSettingsPath('profiles'), subName)
230
231 -def getPythonDirectoryNames(directoryName):
232 'Get the python directories.' 233 pythonDirectoryNames = [] 234 directory = os.listdir(directoryName) 235 for fileName in directory: 236 subdirectoryName = os.path.join(directoryName, fileName) 237 if os.path.isdir(subdirectoryName): 238 if os.path.isfile(os.path.join(subdirectoryName, '__init__.py')): 239 pythonDirectoryNames.append(subdirectoryName) 240 return pythonDirectoryNames
241
242 -def getPythonDirectoryNamesRecursively(directoryName=''):
243 'Get the python directories recursively.' 244 recursivePythonDirectoryNames = [] 245 if directoryName == '': 246 directoryName = os.getcwd() 247 if os.path.isfile(os.path.join(directoryName, '__init__.py')): 248 recursivePythonDirectoryNames.append(directoryName) 249 pythonDirectoryNames = getPythonDirectoryNames(directoryName) 250 for pythonDirectoryName in pythonDirectoryNames: 251 recursivePythonDirectoryNames += getPythonDirectoryNamesRecursively(pythonDirectoryName) 252 else: 253 return [] 254 return recursivePythonDirectoryNames
255
256 -def getPythonFileNamesExceptInit(fileInDirectory=''):
257 'Get the python fileNames of the directory which the fileInDirectory is in, except for the __init__.py file.' 258 pythonFileNamesExceptInit = getFilesWithFileTypeWithoutWords('py', ['__init__.py'], fileInDirectory) 259 pythonFileNamesExceptInit.sort() 260 return pythonFileNamesExceptInit
261
262 -def getPythonFileNamesExceptInitRecursively(directoryName=''):
263 'Get the python fileNames of the directory recursively, except for the __init__.py files.' 264 pythonDirectoryNames = getPythonDirectoryNamesRecursively(directoryName) 265 pythonFileNamesExceptInitRecursively = [] 266 for pythonDirectoryName in pythonDirectoryNames: 267 pythonFileNamesExceptInitRecursively += getPythonFileNamesExceptInit(os.path.join(pythonDirectoryName, '__init__.py')) 268 pythonFileNamesExceptInitRecursively.sort() 269 return pythonFileNamesExceptInitRecursively
270
271 -def getSettingsPath(subName=''):
272 'Get the settings directory path, which is the home directory joined with .skeinforge.' 273 global globalTemporarySettingsPath 274 return getJoinedPath(globalTemporarySettingsPath, subName)
275
276 -def getSkeinforgePath(subName=''):
277 'Get the skeinforge directory path.' 278 return getJoinedPath(getFabmetheusPath('skeinforge_application'), subName)
279
280 -def getSkeinforgePluginsPath(subName=''):
281 'Get the skeinforge plugins directory path.' 282 return getJoinedPath(getSkeinforgePath('skeinforge_plugins'), subName)
283
284 -def getSummarizedFileName(fileName):
285 'Get the fileName basename if the file is in the current working directory, otherwise return the original full name.' 286 if os.getcwd() == os.path.dirname(fileName): 287 return os.path.basename(fileName) 288 return fileName
289
290 -def getTemplatesPath(subName=''):
291 'Get the templates directory path.' 292 return getJoinedPath(getFabmetheusUtilitiesPath('templates'), subName)
293
294 -def getTextIfEmpty(fileName, text):
295 'Get the text from a file if it the text is empty.' 296 if text != '': 297 return text 298 return getFileText(fileName)
299
300 -def getTextLines(text):
301 'Get the all the lines of text of a text.' 302 textLines = text.replace('\r', '\n').replace('\n\n', '\n').split('\n') 303 if len(textLines) == 1: 304 if textLines[0] == '': 305 return [] 306 return textLines
307
308 -def getUntilDot(text):
309 'Get the text until the last dot, if any.' 310 dotIndex = text.rfind('.') 311 if dotIndex < 0: 312 return text 313 return text[: dotIndex]
314
315 -def getVersionFileName():
316 'Get the file name of the version date.getFabmetheusUtilitiesPath(subName='')' 317 return getFabmetheusUtilitiesPath('version.txt')
318
319 -def isFileWithFileTypeWithoutWords(fileType, fileName, words):
320 'Determine if file has a given file type, but with does not contain a word in a list.' 321 fileName = os.path.basename(fileName) 322 fileTypeDot = '.' + fileType 323 if not fileName.endswith(fileTypeDot): 324 return False 325 for word in words: 326 if fileName.find(word) >= 0: 327 return False 328 return True
329
330 -def makeDirectory(directoryPath):
331 'Make a directory if it does not already exist.' 332 if os.path.isdir(directoryPath): 333 return 334 try: 335 print('The following directory was made:') 336 print(os.path.abspath(directoryPath)) 337 os.makedirs(directoryPath) 338 except OSError: 339 print('Skeinforge can not make the directory %s so give it read/write permission for that directory and the containing directory.' % directoryPath)
340
341 -def removeBackupFilesByType(fileType):
342 'Remove backup files by type.' 343 backupFilePaths = getFilesWithFileTypesWithoutWordsRecursively([fileType + '~']) 344 for backupFilePath in backupFilePaths: 345 os.remove(backupFilePath)
346
347 -def removeBackupFilesByTypes(fileTypes):
348 'Remove backup files by types.' 349 for fileType in fileTypes: 350 removeBackupFilesByType(fileType)
351
352 -def writeFileMessageEnd(end, fileName, fileText, message):
353 'Write to a fileName with a suffix and print a message.' 354 suffixFileName = getUntilDot(fileName) + end 355 writeFileText(suffixFileName, fileText) 356 print( message + getSummarizedFileName(suffixFileName) )
357
358 -def writeFileText(fileName, fileText, writeMode='w+'):
359 'Write a text to a file.' 360 try: 361 file = open(fileName, writeMode) 362 file.write(fileText) 363 file.close() 364 except IOError: 365 print('The file ' + fileName + ' can not be written to.')
366