chiark / gitweb /
379d2ef532fe78ef9c4c081de86bcf86c5c44c61
[cura.git] / Cura / fabmetheus_utilities / fabmetheus_tools / alphabetize.py
1 """
2 Alphabetize is a script to alphabetize functions and signatures.
3
4 """
5
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.
8 import __init__
9
10 from fabmetheus_utilities import archive
11 import cStringIO
12 import os
13
14
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'
18
19
20 def addTogetherList(functionList, togetherLists):
21         'Add the togetherList to the togetherLists is the sorted is different.'
22         sortedList = functionList[:]
23         sortedList.sort(compareFunctionName)
24         togetherList = None
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:
31                                 togetherList = []
32                                 togetherLists.append(togetherList)
33                         togetherList.append(together)
34
35 def compareFunctionName(first, second):
36         'Compare the function names.'
37         first = getConvertedName(first)
38         second = getConvertedName(second)
39         if first < second:
40                 return -1
41         return first < second
42
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':
48                 return 'def |main'
49         return name.lower()
50
51 def getFunctionLists(fileName):
52         'Get the function lists in the file.'
53         fileText = archive.getFileText(fileName)
54         functionList = []
55         functionLists = [functionList]
56         lines = archive.getTextLines(fileText)
57         for line in lines:
58                 lineStripped = line.strip()
59                 if lineStripped.startswith('def '):
60                         bracketIndex = lineStripped.find('(')
61                         if bracketIndex > -1:
62                                 lineStripped = lineStripped[: bracketIndex]
63                         functionList.append(lineStripped)
64                 elif line.startswith('class'):
65                         functionList = []
66                         functionLists.append(functionList)
67         return functionLists
68
69 def getFunctionsWithStringByFileName(fileName, searchString):
70         'Get the functions with the search string in the file.'
71         fileText = archive.getFileText(fileName)
72         functions = []
73         lines = archive.getTextLines(fileText)
74         for line in lines:
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())
80         functions.sort()
81         return functions
82
83 def getFunctionsWithStringByFileNames(fileNames, searchString):
84         'Get the functions with the search string in the files.'
85         functions = []
86         for fileName in fileNames:
87                 functions += getFunctionsWithStringByFileName(fileName, searchString)
88         functions.sort()
89         return functions
90
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
109
110 def getSnippetsByFileName(fileName, functionName):
111         'Get the function signature snippets by the file name.'
112         fileText = archive.getFileText(fileName)
113         snippets = []
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)
120         return snippets
121
122 def getTogetherLists(fileName):
123         'Get the lists of the unsorted and sorted functions in the file.'
124         functionLists = getFunctionLists(fileName)
125         togetherLists = []
126         for functionList in functionLists:
127                 addTogetherList(functionList, togetherLists)
128         return togetherLists
129
130 def getTokenEnd(characterIndex, fileText, token):
131         'Get the token end index for the file text and token.'
132         tokenIndex = fileText.find(token, characterIndex)
133         if tokenIndex == -1:
134                 return -1
135         return tokenIndex + len(token)
136
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]
145                                         sorted = together[1]
146                         return
147
148
149 class EndCharacterMonad:
150         'A monad to return the parent monad when it encounters the end character.'
151         def __init__(self, endCharacter, parentMonad):
152                 'Initialize.'
153                 self.endCharacter = endCharacter
154                 self.parentMonad = parentMonad
155
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
161                 return self
162
163         def getSnippet(self):
164                 'Get the snippet.'
165                 return self.parentMonad.getSnippet()
166
167
168 class ParameterMonad:
169         'A monad to handle parameters.'
170         def __init__(self, snippet):
171                 'Initialize.'
172                 self.snippet = snippet
173
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)
180
181         def getNextMonad(self, character):
182                 'Get the next monad.'
183                 if character == '"':
184                         self.snippet.input.write(character)
185                         return EndCharacterMonad('"', self)
186                 if character == '"':
187                         self.snippet.input.write(character)
188                         return EndCharacterMonad('"', self)
189                 if character == '(':
190                         self.snippet.input.write(character)
191                         return EndCharacterMonad(')', self)
192                 if character == ')':
193                         self.addParameter()
194                         return None
195                 if character == ',':
196                         self.addParameter()
197                         return self
198                 self.snippet.input.write(character)
199                 return self
200
201         def getSnippet(self):
202                 'Get the snippet.'
203                 return self.snippet
204
205
206 class Snippet:
207         'A class to get the variables for a function.'
208         def __init__(self, characterIndex, fileText):
209                 'Initialize.'
210                 self.characterIndex = characterIndex
211                 self.input = cStringIO.StringIO()
212                 self.parameters = []
213                 monad = ParameterMonad(self)
214                 for characterIndex in xrange(self.characterIndex, len(fileText)):
215                         character = fileText[characterIndex]
216                         monad = monad.getNextMonad(character)
217                         if monad == None:
218                                 return
219
220         def __repr__(self):
221                 'Get the string representation of this Snippet.'
222                 return '%s %s' % (self.characterIndex, self.parameters)
223
224
225 def main():
226         'Run main function.'
227 #       printTogetherListsByFileNames(archive.getPythonFileNamesExceptInitRecursively('/home/enrique/Desktop/fabmetheus'))
228         functions = getFunctionsWithStringByFileNames(archive.getPythonFileNamesExceptInitRecursively('/home/enrique/Desktop/fabmetheus'), ', xmlElement')
229         print(functions)
230
231 if __name__ == "__main__":
232         main()