3
__license__ = "GPL V.2 or (at your option) any later version"
4
__copyright__ = "2007, Israel Fernández Cabrera, Pável Varela Rodríguez"
5
__author__ = "Israel Fernández Cabrera <iferca@gmail.com>, Pável Varela Rodríguez <neonskull@gmail.com>"
10
class CSSFileInfo(object):
12
self._cssSelector = re.compile("^[\s]*(.+)[\s]*{")
14
def update(self, content):
16
lines = content.split('\n')
19
match = self._cssSelector.match(line)
22
node['selector'] = match.group(1)
24
self._selectors.append(node)
27
def getSelectors(self):
28
return self._selectors
30
class PythonFileInfo(object):
32
self._classOrMethod = re.compile('^([\s]*)(class|def)\s+(\w+)(\(.*\))?:')
33
self._importRegExp1 = re.compile('^([\s]*)(import .+)')
34
self._importRegExp2 = re.compile('^([\s]*)(from .+ import .+)')
35
self._methodVariable = re.compile('^([\s]*)(self\.\w+)([\s]*=)')
36
self._variable = re.compile('^([\s]*)(\w+)([\s]*=)')
37
self._property = re.compile('^([\s]*)(\w+)[\s]*=[\s]*property.+')
38
self._anythingElse = re.compile('^([\s]*)(.+)')
40
def update(self, content):
45
lines = content.split('\n')
48
match = self._classOrMethod.match(line)
51
node['level'] = len(match.group(1))
52
node['type'] = match.group(2)
53
node['name'] = match.group(3)
54
node['params'] = match.group(4)
56
self._codeData.append(node)
58
match = self._importRegExp1.match(line)
61
node['name'] = match.group(2)
63
self._importData.append(node)
65
match = self._importRegExp2.match(line)
68
node['name'] = match.group(2)
70
self._importData.append(node)
72
match = self._methodVariable.match(line)
75
node['level'] = len(match.group(1))
76
node['name'] = match.group(2)
78
node['type'] = 'instance-var'
79
self._codeData.append(node)
81
match = self._variable.match(line)
84
node['level'] = len(match.group(1))
85
node['name'] = match.group(2)
88
self._codeData.append(node)
90
match = self._property.match(line)
93
node['level'] = len(match.group(1))
94
node['name'] = match.group(2)
96
node['type'] = 'property'
97
self._codeData.append(node)
99
match = self._anythingElse.match(line)
104
def getImportData(self):
105
return self._importData
107
def getCodeData(self):
108
for node in self._codeData:
111
class VariableDescriptor(object):
112
def __init__(self, level, name):
116
class FunctionDescriptor(object):
117
def __init__(self, level, name, params):
123
for p in params.split(","):
124
varName = "".join([c for c in p if c != " "])
125
var = VariableDescriptor(level=None, name=varName)
126
self.params.append(var)
130
def addVariable(self, variable):
131
self.variables.append(variable)
134
class ClassDescriptor(object):
135
def __init__(self, level, name, base):
142
for bName in base.split(","):
143
self.base.append("".join([c for c in bName if c != " "]))
147
def addMethod(self, method):
148
self.methods.append(method)
150
def addVariable(self, variable):
151
self.variables.append(variable)
154
class DesfaultCodeHandler(object):
157
self._lastClass = None
158
self._lastFunction = None
163
def classFound(self, level, name, base):
164
classDescriptor = ClassDescriptor(level, name, base)
165
self._content.append(classDescriptor)
166
self._lastClass = classDescriptor
167
self._lastFunction = None
169
def functionFound(self, level, name, params):
170
functionDescriptor = FunctionDescriptor(level, name, params)
171
if self._lastClass != None and level > self._lastClass.level:
172
self._lastClass.addMethod(functionDescriptor)
174
self._content.append(functionDescriptor)
175
self._lastFunction = functionDescriptor
177
def memberVariableFound(self, level, name):
178
if self._lastClass == None: pass #FIXME: This indicates a syntax error
179
variable = VariableDescriptor(level, name)
180
self._lastClass.addVariable(variable)
181
if self._lastFunction != None and \
182
self._lastFunction.level > self._lastClass.level and \
183
level > self._lastFunction.level:
184
self._lastFunction.addVariable(variable)
187
class FileInfo1(object):
188
def __init__(self, handler):
189
self._handler = handler
190
self._reClass = re.compile('^([\s]*)(class)\s+(\w+)(\((.*)\))?:')
191
self._reFunction = re.compile('^([\s]*)(def)\s+(\w+)(\((.*)\))?:')
192
self._memberVariable = re.compile('^([\s]*)(self\.(\w+))([\s]*=.+)')
193
#self._variable = re.compile('^(\w+)([\s]*=.+)')
195
#def _lineIsAString(self, line):
196
# if line.startswith("'''") and line.endswith("'''") or \
197
# line.startswith("'''") and line.endswith("'''")
199
def analyze(self, code):
200
self._ignoreLine = False
201
lines = code.split('\n')
205
if line.startswith("'''"):
206
print "inicio de cadena", line
207
self._ignoreLine = True
208
elif line.endswith("'''"):
209
print "fin de cadena", line
210
self._ignoreLine = False
212
if self._ignoreLine == True:
213
print "ignorar la linea", line
214
match = self._reClass.match(line)
216
self._onClassFound(match)
218
match = self._reFunction.match(line)
220
self._onFunctionFound(match)
222
match = self._memberVariable.match(line)
224
self._onFoundMemberVariable(match)
226
def _onFoundMemberVariable(self, match):
227
self._handler.memberVariableFound(level=match.group(1), name=match.group(3))
229
def _onClassFound(self, match):
230
self._handler.classFound(level=match.group(1), name=match.group(3),
233
def _onFunctionFound(self, match):
234
self._handler.functionFound(level=match.group(1), name=match.group(3),
235
params=match.group(5))