~iferca/yape/trunk

« back to all changes in this revision

Viewing changes to editor/languages/fileinfo.py

  • Committer: Israel Fernández Cabrera
  • Date: 2008-10-03 21:12:17 UTC
  • Revision ID: iferca@gmail.com-20081003211217-uu1df2ucq3wd67nd
YaPe project moved to the new YaPe project structure. Old one will work fine but it was not suitable for the next development phases

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#-*- coding: utf-8 -*-
2
 
 
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>"
6
 
 
7
 
import re
8
 
 
9
 
 
10
 
class CSSFileInfo(object):
11
 
    def __init__(self):
12
 
        self._cssSelector = re.compile("^[\s]*(.+)[\s]*{")
13
 
 
14
 
    def update(self, content):
15
 
        self._selectors = []
16
 
        lines = content.split('\n')
17
 
        index=0
18
 
        for line in lines:
19
 
            match = self._cssSelector.match(line)
20
 
            if match:
21
 
                node = {}
22
 
                node['selector'] = match.group(1)
23
 
                node['line'] = index
24
 
                self._selectors.append(node)
25
 
            index += 1
26
 
 
27
 
    def getSelectors(self):
28
 
        return self._selectors
29
 
 
30
 
class PythonFileInfo(object):
31
 
    def __init__(self):
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]*)(.+)')
39
 
 
40
 
    def update(self, content):
41
 
        self._codeData = []
42
 
        self._importData = []
43
 
        if (content is None):
44
 
            return
45
 
        lines = content.split('\n')
46
 
        index = 0
47
 
        for line in lines:
48
 
            match = self._classOrMethod.match(line)
49
 
            if match != None:
50
 
                node = {}
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)
55
 
                node['line'] = index
56
 
                self._codeData.append(node)
57
 
            else:
58
 
                match = self._importRegExp1.match(line)
59
 
                if match != None:
60
 
                    node = {}
61
 
                    node['name'] = match.group(2)
62
 
                    node['line'] = index
63
 
                    self._importData.append(node)
64
 
                else:
65
 
                    match = self._importRegExp2.match(line)
66
 
                    if match != None:
67
 
                        node = {}
68
 
                        node['name'] = match.group(2)
69
 
                        node['line'] = index
70
 
                        self._importData.append(node)
71
 
                    else:
72
 
                        match = self._methodVariable.match(line)
73
 
                        if match != None:
74
 
                            node = {}
75
 
                            node['level'] = len(match.group(1))
76
 
                            node['name'] = match.group(2)
77
 
                            node['line'] = index
78
 
                            node['type'] = 'instance-var'
79
 
                            self._codeData.append(node)
80
 
                        else:
81
 
                            match = self._variable.match(line)
82
 
                            if match != None:
83
 
                                node = {}
84
 
                                node['level'] = len(match.group(1))
85
 
                                node['name'] = match.group(2)
86
 
                                node['line'] = index
87
 
                                node['type'] = 'var'
88
 
                                self._codeData.append(node)
89
 
                            else:
90
 
                                match = self._property.match(line)
91
 
                                if match != None:
92
 
                                    node = {}
93
 
                                    node['level'] = len(match.group(1))
94
 
                                    node['name'] = match.group(2)
95
 
                                    node['line'] = index
96
 
                                    node['type'] = 'property'
97
 
                                    self._codeData.append(node)
98
 
                                else:
99
 
                                    match = self._anythingElse.match(line)
100
 
                                    if match != None:
101
 
                                        pass
102
 
            index += 1
103
 
 
104
 
    def getImportData(self):
105
 
        return self._importData
106
 
 
107
 
    def getCodeData(self):
108
 
        for node in self._codeData:
109
 
            yield node
110
 
 
111
 
class VariableDescriptor(object):
112
 
    def __init__(self, level, name):
113
 
        self.level = level
114
 
        self.name = name
115
 
 
116
 
class FunctionDescriptor(object):
117
 
    def __init__(self, level, name, params):
118
 
        self.variables = []
119
 
        self.level = level
120
 
        self.name = name
121
 
        if params != None:
122
 
            self.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)
127
 
        else:
128
 
            self.params = None
129
 
 
130
 
    def addVariable(self, variable):
131
 
        self.variables.append(variable)
132
 
 
133
 
 
134
 
class ClassDescriptor(object):
135
 
    def __init__(self, level, name, base):
136
 
        self.methods = []
137
 
        self.variables = []
138
 
        self.level = level
139
 
        self.name = name
140
 
        if base != None:
141
 
            self.base = []
142
 
            for bName in base.split(","):
143
 
                self.base.append("".join([c for c in bName if c != " "]))
144
 
        else:
145
 
            self.base = None
146
 
 
147
 
    def addMethod(self, method):
148
 
        self.methods.append(method)
149
 
 
150
 
    def addVariable(self, variable):
151
 
        self.variables.append(variable)
152
 
 
153
 
 
154
 
class DesfaultCodeHandler(object):
155
 
    def __init__(self):
156
 
        self._content = []
157
 
        self._lastClass = None
158
 
        self._lastFunction = None
159
 
 
160
 
    def iteritems(self):
161
 
        return self._content
162
 
 
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
168
 
 
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)
173
 
        else:
174
 
            self._content.append(functionDescriptor)
175
 
        self._lastFunction = functionDescriptor
176
 
 
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)
185
 
 
186
 
 
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]*=.+)')
194
 
 
195
 
    #def _lineIsAString(self, line):
196
 
    #    if line.startswith("'''") and line.endswith("'''") or \
197
 
    #       line.startswith("'''") and line.endswith("'''")
198
 
 
199
 
    def analyze(self, code):
200
 
        self._ignoreLine = False
201
 
        lines = code.split('\n')
202
 
        index = 0
203
 
        previousLevel = 0
204
 
        for line in lines:
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
211
 
                continue
212
 
            if self._ignoreLine == True:
213
 
                print "ignorar la linea", line
214
 
            match = self._reClass.match(line)
215
 
            if match != None:
216
 
                self._onClassFound(match)
217
 
            else:
218
 
                match = self._reFunction.match(line)
219
 
                if match != None:
220
 
                    self._onFunctionFound(match)
221
 
                else:
222
 
                    match = self._memberVariable.match(line)
223
 
                    if match != None:
224
 
                        self._onFoundMemberVariable(match)
225
 
 
226
 
    def _onFoundMemberVariable(self, match):
227
 
        self._handler.memberVariableFound(level=match.group(1), name=match.group(3))
228
 
 
229
 
    def _onClassFound(self, match):
230
 
        self._handler.classFound(level=match.group(1), name=match.group(3), 
231
 
                                 base=match.group(5))
232
 
 
233
 
    def _onFunctionFound(self, match):
234
 
        self._handler.functionFound(level=match.group(1), name=match.group(3), 
235
 
                                    params=match.group(5))
236