~ubuntu-branches/ubuntu/precise/gst0.10-python/precise

« back to all changes in this revision

Viewing changes to codegen/override.py

  • Committer: Bazaar Package Importer
  • Author(s): Loic Minier
  • Date: 2006-06-25 19:37:45 UTC
  • Revision ID: james.westby@ubuntu.com-20060625193745-9yeg0wq56r24n57x
Tags: upstream-0.10.4
ImportĀ upstreamĀ versionĀ 0.10.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- Mode: Python; py-indent-offset: 4 -*-
 
2
 
 
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.
 
6
 
 
7
import fnmatch
 
8
import os
 
9
import re
 
10
import string
 
11
import sys
 
12
 
 
13
def class2cname(klass, method):
 
14
    c_name = ''
 
15
    for c in klass:
 
16
        if c.isupper():
 
17
            c_name += '_' + c.lower()
 
18
        else:
 
19
            c_name += c
 
20
    return c_name[1:] + '_'  + method
 
21
    
 
22
import_pat = re.compile(r'\s*import\s+(\S+)\.([^\s.]+)\s+as\s+(\S+)')
 
23
 
 
24
class Overrides:
 
25
    def __init__(self, filename=None, path=[]):
 
26
        self.modulename = None
 
27
        self.ignores = {}
 
28
        self.glob_ignores = []
 
29
        self.overrides = {}
 
30
        self.overridden = {}
 
31
        self.kwargs = {}
 
32
        self.noargs = {}
 
33
        self.startlines = {}
 
34
        self.override_attrs = {}
 
35
        self.override_slots = {}
 
36
        self.headers = ''
 
37
        self.init = ''
 
38
        self.imports = []
 
39
        self.defines = {}
 
40
        self.functions = {}
 
41
        self.path = path
 
42
        if filename:
 
43
            self.handle_file(filename)
 
44
 
 
45
    def handle_file(self, filename):
 
46
        oldpath = os.getcwd()
 
47
 
 
48
        fp = None
 
49
        for path in self.path:
 
50
            os.chdir(oldpath)
 
51
            os.chdir(os.path.abspath(path))
 
52
            try:
 
53
                fp = open(filename, 'r')
 
54
            except:
 
55
                continue
 
56
        if not fp:
 
57
            raise Exception, "Couldn't find file %s" % filename
 
58
        
 
59
        dirname = os.path.dirname(os.path.abspath(filename))
 
60
 
 
61
        if dirname != oldpath:
 
62
            os.chdir(dirname)
 
63
            
 
64
        # read all the components of the file ...
 
65
        bufs = []
 
66
        startline = 1
 
67
        lines = []
 
68
        line = fp.readline()
 
69
        linenum = 1
 
70
        while line:
 
71
            if line == '%%\n' or line == '%%':
 
72
                if lines:
 
73
                    bufs.append((string.join(lines, ''), startline))
 
74
                startline = linenum + 1
 
75
                lines = []
 
76
            else:
 
77
                lines.append(line)
 
78
            line = fp.readline()
 
79
            linenum = linenum + 1
 
80
        if lines:
 
81
            bufs.append((string.join(lines, ''), startline))
 
82
        if not bufs: return
 
83
 
 
84
        for buf, startline in bufs:
 
85
            self.__parse_override(buf, startline, filename)
 
86
 
 
87
        os.chdir(oldpath)
 
88
        
 
89
    def __parse_override(self, buffer, startline, filename):
 
90
        pos = string.find(buffer, '\n')
 
91
        if pos >= 0:
 
92
            line = buffer[:pos]
 
93
            rest = buffer[pos+1:]
 
94
        else:
 
95
            line = buffer ; rest = ''
 
96
        words = string.split(line)
 
97
        command = words[0]
 
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]"
 
114
            func = words[1]
 
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"
 
123
            attr = words[1]
 
124
            self.override_attrs[attr] = rest
 
125
            self.startlines[attr] = (startline + 1, filename)
 
126
        elif command == 'override-slot':
 
127
            "override-slot Class.slot"
 
128
            slot = words[1]
 
129
            self.override_slots[slot] = rest
 
130
            self.startlines[slot] = (startline + 1, filename)
 
131
        elif command == 'headers':
 
132
            "headers"
 
133
            self.headers = '%s\n#line %d "%s"\n%s' % \
 
134
                           (self.headers, startline + 1, filename, rest)
 
135
        elif command == 'init':
 
136
            "init"
 
137
            self.init = '%s\n#line %d "%s"\n%s' % \
 
138
                        (self.init, startline + 1, filename, rest)
 
139
        elif command == 'modulename':
 
140
            "modulename name"
 
141
            self.modulename = words[1]
 
142
        elif command == 'include':
 
143
            "include filename"
 
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)
 
152
                if match:
 
153
                    self.imports.append(match.groups())
 
154
        elif command == 'define':
 
155
            "define funcname [kwargs,noargs]"
 
156
            "define Class.method [kwargs,noargs]"
 
157
            func = words[1]
 
158
            klass = None
 
159
            if func.find('.') != -1:
 
160
                klass, func = func.split('.', 1)
 
161
 
 
162
                if not self.defines.has_key(klass):
 
163
                    self.defines[klass] = {}
 
164
                self.defines[klass][func] = rest
 
165
            else:
 
166
                self.functions[func] = rest
 
167
 
 
168
            if 'kwargs' in words[1:]:
 
169
                self.kwargs[func] = 1
 
170
            elif 'noargs' in words[1:]:
 
171
                self.noargs[func] = 1
 
172
 
 
173
            self.startlines[func] = (startline + 1, filename)
 
174
            
 
175
    def is_ignored(self, name):
 
176
        if self.ignores.has_key(name):
 
177
            return 1
 
178
        for glob in self.glob_ignores:
 
179
            if fnmatch.fnmatchcase(name, glob):
 
180
                return 1
 
181
        return 0
 
182
    
 
183
    def is_overriden(self, name):
 
184
        return self.overrides.has_key(name)
 
185
    
 
186
    def is_already_included(self, name):
 
187
        return self.overridden.has_key(name)
 
188
    
 
189
    def override(self, name):
 
190
        self.overridden[name] = 1
 
191
        return self.overrides[name]
 
192
 
 
193
    def define(self, klass, name):
 
194
        self.overridden[class2cname(klass, name)] = 1
 
195
        return self.defines[klass][name]
 
196
 
 
197
    def function(self, name):
 
198
        return self.functions[name]
 
199
        
 
200
    def getstartline(self, name):
 
201
        return self.startlines[name]
 
202
 
 
203
    def wants_kwargs(self, name):
 
204
        return self.kwargs.has_key(name)
 
205
    
 
206
    def wants_noargs(self, name):
 
207
        return self.noargs.has_key(name)
 
208
    
 
209
    def attr_is_overriden(self, attr):
 
210
        return self.override_attrs.has_key(attr)
 
211
    
 
212
    def attr_override(self, attr):
 
213
        return self.override_attrs[attr]
 
214
    
 
215
    def slot_is_overriden(self, slot):
 
216
        return self.override_slots.has_key(slot)
 
217
    
 
218
    def slot_override(self, slot):
 
219
        return self.override_slots[slot]
 
220
    
 
221
    def get_headers(self):
 
222
        return self.headers
 
223
    
 
224
    def get_init(self):
 
225
        return self.init
 
226
    
 
227
    def get_imports(self):
 
228
        return self.imports
 
229
 
 
230
    def get_defines_for(self, klass):
 
231
        return self.defines.get(klass, {})
 
232
    
 
233
    def get_functions(self):
 
234
        return self.functions