1
# -*- Mode: Python; py-indent-offset: 4 -*-
3
# this file contains code for loading up an override file. The override file
4
# provides implementations of functions where the code generator could not
5
# do its job correctly.
13
def class2cname(klass, method):
17
c_name += '_' + c.lower()
20
return c_name[1:] + '_' + method
22
import_pat = re.compile(r'\s*import\s+(\S+)\.([^\s.]+)\s+as\s+(\S+)')
25
def __init__(self, filename=None, path=[]):
26
self.modulename = None
28
self.glob_ignores = []
34
self.override_attrs = {}
35
self.override_slots = {}
43
self.handle_file(filename)
45
def handle_file(self, filename):
49
for path in self.path:
51
os.chdir(os.path.abspath(path))
53
fp = open(filename, 'r')
57
raise Exception, "Couldn't find file %s" % filename
59
dirname = os.path.dirname(os.path.abspath(filename))
61
if dirname != oldpath:
64
# read all the components of the file ...
71
if line == '%%\n' or line == '%%':
73
bufs.append((string.join(lines, ''), startline))
74
startline = linenum + 1
81
bufs.append((string.join(lines, ''), startline))
84
for buf, startline in bufs:
85
self.__parse_override(buf, startline, filename)
89
def __parse_override(self, buffer, startline, filename):
90
pos = string.find(buffer, '\n')
95
line = buffer ; rest = ''
96
words = string.split(line)
98
if (command == 'ignore' or
99
command == 'ignore-' + sys.platform):
100
"ignore/ignore-platform [functions..]"
101
for func in words[1:]:
102
self.ignores[func] = 1
103
for func in string.split(rest):
104
self.ignores[func] = 1
105
elif (command == 'ignore-glob' or
106
command == 'ignore-glob-' + sys.platform):
107
"ignore-glob/ignore-glob-platform [globs..]"
108
for func in words[1:]:
109
self.glob_ignores.append(func)
110
for func in string.split(rest):
111
self.glob_ignores.append(func)
112
elif command == 'override':
113
"override function/method [kwargs,noargs]"
115
if 'kwargs' in words[1:]:
116
self.kwargs[func] = 1
117
elif 'noargs' in words[1:]:
118
self.noargs[func] = 1
119
self.overrides[func] = rest
120
self.startlines[func] = (startline + 1, filename)
121
elif command == 'override-attr':
122
"override-slot Class.attr"
124
self.override_attrs[attr] = rest
125
self.startlines[attr] = (startline + 1, filename)
126
elif command == 'override-slot':
127
"override-slot Class.slot"
129
self.override_slots[slot] = rest
130
self.startlines[slot] = (startline + 1, filename)
131
elif command == 'headers':
133
self.headers = '%s\n#line %d "%s"\n%s' % \
134
(self.headers, startline + 1, filename, rest)
135
elif command == 'init':
137
self.init = '%s\n#line %d "%s"\n%s' % \
138
(self.init, startline + 1, filename, rest)
139
elif command == 'modulename':
141
self.modulename = words[1]
142
elif command == 'include':
144
for filename in words[1:]:
145
self.handle_file(filename)
146
for filename in string.split(rest):
147
self.handle_file(filename)
148
elif command == 'import':
149
"import module1 [\n module2, \n module3 ...]"
150
for line in string.split(buffer, '\n'):
151
match = import_pat.match(line)
153
self.imports.append(match.groups())
154
elif command == 'define':
155
"define funcname [kwargs,noargs]"
156
"define Class.method [kwargs,noargs]"
159
if func.find('.') != -1:
160
klass, func = func.split('.', 1)
162
if not self.defines.has_key(klass):
163
self.defines[klass] = {}
164
self.defines[klass][func] = rest
166
self.functions[func] = rest
168
if 'kwargs' in words[1:]:
169
self.kwargs[func] = 1
170
elif 'noargs' in words[1:]:
171
self.noargs[func] = 1
173
self.startlines[func] = (startline + 1, filename)
175
def is_ignored(self, name):
176
if self.ignores.has_key(name):
178
for glob in self.glob_ignores:
179
if fnmatch.fnmatchcase(name, glob):
183
def is_overriden(self, name):
184
return self.overrides.has_key(name)
186
def is_already_included(self, name):
187
return self.overridden.has_key(name)
189
def override(self, name):
190
self.overridden[name] = 1
191
return self.overrides[name]
193
def define(self, klass, name):
194
self.overridden[class2cname(klass, name)] = 1
195
return self.defines[klass][name]
197
def function(self, name):
198
return self.functions[name]
200
def getstartline(self, name):
201
return self.startlines[name]
203
def wants_kwargs(self, name):
204
return self.kwargs.has_key(name)
206
def wants_noargs(self, name):
207
return self.noargs.has_key(name)
209
def attr_is_overriden(self, attr):
210
return self.override_attrs.has_key(attr)
212
def attr_override(self, attr):
213
return self.override_attrs[attr]
215
def slot_is_overriden(self, slot):
216
return self.override_slots.has_key(slot)
218
def slot_override(self, slot):
219
return self.override_slots[slot]
221
def get_headers(self):
227
def get_imports(self):
230
def get_defines_for(self, klass):
231
return self.defines.get(klass, {})
233
def get_functions(self):
234
return self.functions