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
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
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
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
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
51
55
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
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
75
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
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
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
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
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
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
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
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
168
177
181
185
189
191 'Get the joined file path.'
192 if subName == '':
193 return path
194 return os.path.join(path, subName)
195
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
221
226
228 'Get the profiles directory path, which is the settings directory joined with profiles.'
229 return getJoinedPath(getSettingsPath('profiles'), subName)
230
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
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
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
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
275
279
283
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
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
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
316 'Get the file name of the version date.getFabmetheusUtilitiesPath(subName='')'
317 return getFabmetheusUtilitiesPath('version.txt')
318
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
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
346
351
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