2 Alphabetize is a script to alphabetize functions and signatures.
6 from __future__ import absolute_import
7 #Init has to be imported first because it has code to workaround the python bug where relative imports don't work if the module is imported as a main module.
10 from fabmetheus_utilities import archive
15 __author__ = 'Enrique Perez (perez_enrique@yahoo.com)'
16 __date__ = '$Date: 2008/21/04 $'
17 __license__ = 'GNU Affero General Public License http://www.gnu.org/licenses/agpl.html'
20 def addTogetherList(functionList, togetherLists):
21 'Add the togetherList to the togetherLists is the sorted is different.'
22 sortedList = functionList[:]
23 sortedList.sort(compareFunctionName)
25 for functionIndex in xrange(len(functionList)):
26 function = functionList[functionIndex]
27 sorted = sortedList[functionIndex]
28 if function != sorted:
29 together = (function, sorted)
30 if togetherList == None:
32 togetherLists.append(togetherList)
33 togetherList.append(together)
35 def compareFunctionName(first, second):
36 'Compare the function names.'
37 first = getConvertedName(first)
38 second = getConvertedName(second)
43 def getConvertedName(name):
44 'Get converted name with init at the beginning and main at the endCompare the function names.'
45 if name == 'def __init__':
46 return 'def !__init__'
47 if name == 'def main':
51 def getFunctionLists(fileName):
52 'Get the function lists in the file.'
53 fileText = archive.getFileText(fileName)
55 functionLists = [functionList]
56 lines = archive.getTextLines(fileText)
58 lineStripped = line.strip()
59 if lineStripped.startswith('def '):
60 bracketIndex = lineStripped.find('(')
62 lineStripped = lineStripped[: bracketIndex]
63 functionList.append(lineStripped)
64 elif line.startswith('class'):
66 functionLists.append(functionList)
69 def getFunctionsWithStringByFileName(fileName, searchString):
70 'Get the functions with the search string in the file.'
71 fileText = archive.getFileText(fileName)
73 lines = archive.getTextLines(fileText)
75 lineStripped = line.strip()
76 # if lineStripped.startswith('def ') and searchString in lineStripped and '=' in lineStripped:
77 if lineStripped.startswith('def ') and searchString in lineStripped:
78 if '(self, ' not in lineStripped or lineStripped.count(',') > 1:
79 functions.append(lineStripped[len('def ') :].strip())
83 def getFunctionsWithStringByFileNames(fileNames, searchString):
84 'Get the functions with the search string in the files.'
86 for fileName in fileNames:
87 functions += getFunctionsWithStringByFileName(fileName, searchString)
91 def getParameterSequence(functionName):
92 'Get the parameter sequence.'
93 parameterDictionary = {}
94 parameterSequence = []
95 parameterText = functionName[functionName.find('(') + 1 :].replace('xmlElement', 'elementNode')
96 snippet = Snippet(0, parameterText)
97 strippedParameters = []
98 for parameter in snippet.parameters:
99 strippedParameter = parameter.strip()
100 if strippedParameter != 'self':
101 strippedParameters.append(strippedParameter)
102 for parameterIndex, parameter in enumerate(strippedParameters):
103 parameterDictionary[parameter] = parameterIndex
104 sortedParameters = strippedParameters[:]
105 sortedParameters.sort()
106 for sortedParameter in sortedParameters:
107 parameterSequence.append(parameterDictionary[sortedParameter])
108 return parameterSequence
110 def getSnippetsByFileName(fileName, functionName):
111 'Get the function signature snippets by the file name.'
112 fileText = archive.getFileText(fileName)
114 functionStart = functionName[: functionName.find('(') + 1]
115 tokenEnd = getTokenEnd(0, fileText, functionStart)
116 while tokenEnd != -1:
117 snippet = Snippet(tokenEnd, fileText)
118 snippets.append(snippet)
119 tokenEnd = getTokenEnd(snippet.characterIndex, fileText, functionStart)
122 def getTogetherLists(fileName):
123 'Get the lists of the unsorted and sorted functions in the file.'
124 functionLists = getFunctionLists(fileName)
126 for functionList in functionLists:
127 addTogetherList(functionList, togetherLists)
130 def getTokenEnd(characterIndex, fileText, token):
131 'Get the token end index for the file text and token.'
132 tokenIndex = fileText.find(token, characterIndex)
135 return tokenIndex + len(token)
137 def printTogetherListsByFileNames(fileNames):
138 'Print the together lists of the file names, if the file name has a together list.'
139 for fileName in fileNames:
140 togetherLists = getTogetherLists(fileName)
141 if len(togetherLists) > 0:
142 for togetherList in togetherLists:
143 for together in togetherList:
144 function = together[0]
149 class EndCharacterMonad:
150 'A monad to return the parent monad when it encounters the end character.'
151 def __init__(self, endCharacter, parentMonad):
153 self.endCharacter = endCharacter
154 self.parentMonad = parentMonad
156 def getNextMonad(self, character):
157 'Get the next monad.'
158 self.getSnippet().input.write(character)
159 if character == self.endCharacter:
160 return self.parentMonad
163 def getSnippet(self):
165 return self.parentMonad.getSnippet()
168 class ParameterMonad:
169 'A monad to handle parameters.'
170 def __init__(self, snippet):
172 self.snippet = snippet
174 def addParameter(self):
175 'Add parameter to the snippet.'
176 parameterString = self.snippet.input.getvalue()
177 if len(parameterString) != 0:
178 self.snippet.input = cStringIO.StringIO()
179 self.snippet.parameters.append(parameterString)
181 def getNextMonad(self, character):
182 'Get the next monad.'
184 self.snippet.input.write(character)
185 return EndCharacterMonad('"', self)
187 self.snippet.input.write(character)
188 return EndCharacterMonad('"', self)
190 self.snippet.input.write(character)
191 return EndCharacterMonad(')', self)
198 self.snippet.input.write(character)
201 def getSnippet(self):
207 'A class to get the variables for a function.'
208 def __init__(self, characterIndex, fileText):
210 self.characterIndex = characterIndex
211 self.input = cStringIO.StringIO()
213 monad = ParameterMonad(self)
214 for characterIndex in xrange(self.characterIndex, len(fileText)):
215 character = fileText[characterIndex]
216 monad = monad.getNextMonad(character)
221 'Get the string representation of this Snippet.'
222 return '%s %s' % (self.characterIndex, self.parameters)
227 # printTogetherListsByFileNames(archive.getPythonFileNamesExceptInitRecursively('/home/enrique/Desktop/fabmetheus'))
228 functions = getFunctionsWithStringByFileNames(archive.getPythonFileNamesExceptInitRecursively('/home/enrique/Desktop/fabmetheus'), ', xmlElement')
231 if __name__ == "__main__":