| 1 |
|
|---|
| 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 |
This is a simple rip-off of the override script used in PyGTK. |
|---|
| 8 |
""" |
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
import sys, string |
|---|
| 12 |
|
|---|
| 13 |
class Overrides: |
|---|
| 14 |
|
|---|
| 15 |
def __init__(self, filename=None): |
|---|
| 16 |
self.overrides = {} |
|---|
| 17 |
if filename: |
|---|
| 18 |
self.read_overrides(filename) |
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
def read_overrides(self, filename): |
|---|
| 22 |
"""Read a file and return a dictionary of overriden properties |
|---|
| 23 |
and their implementation. |
|---|
| 24 |
|
|---|
| 25 |
An override file ahs the form: |
|---|
| 26 |
override <property> |
|---|
| 27 |
<implementation> |
|---|
| 28 |
%% |
|---|
| 29 |
""" |
|---|
| 30 |
fp = open(filename, 'r') |
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 |
bufs = [] |
|---|
| 34 |
startline = 1 |
|---|
| 35 |
lines = [] |
|---|
| 36 |
line = fp.readline() |
|---|
| 37 |
linenum = 1 |
|---|
| 38 |
while line: |
|---|
| 39 |
if line == '%%\n' or line == '%%': |
|---|
| 40 |
if lines: |
|---|
| 41 |
bufs.append((string.join(lines, ''), startline)) |
|---|
| 42 |
startline = linenum + 1 |
|---|
| 43 |
lines = [] |
|---|
| 44 |
else: |
|---|
| 45 |
lines.append(line) |
|---|
| 46 |
line = fp.readline() |
|---|
| 47 |
linenum = linenum + 1 |
|---|
| 48 |
if lines: |
|---|
| 49 |
bufs.append((string.join(lines, ''), startline)) |
|---|
| 50 |
|
|---|
| 51 |
if not bufs: |
|---|
| 52 |
return |
|---|
| 53 |
|
|---|
| 54 |
|
|---|
| 55 |
for buffer, startline in bufs: |
|---|
| 56 |
pos = string.find(buffer, '\n') |
|---|
| 57 |
if pos >= 0: |
|---|
| 58 |
line = buffer[:pos] |
|---|
| 59 |
rest = buffer[pos+1:] |
|---|
| 60 |
else: |
|---|
| 61 |
line = buffer ; rest = '' |
|---|
| 62 |
words = string.split(line) |
|---|
| 63 |
|
|---|
| 64 |
if words[0] == 'override': |
|---|
| 65 |
func = words[1] |
|---|
| 66 |
self.overrides[func] = rest |
|---|
| 67 |
elif words[0] == 'comment': |
|---|
| 68 |
pass |
|---|
| 69 |
else: |
|---|
| 70 |
print "Unknown word: '%s', line %d" (words[0], startline) |
|---|
| 71 |
raise SystemExit |
|---|
| 72 |
|
|---|
| 73 |
def has_override(self, key): |
|---|
| 74 |
return bool(self.overrides.get(key)) |
|---|
| 75 |
|
|---|
| 76 |
def write_override(self, fp, key): |
|---|
| 77 |
"""Write override data for 'key' to a file refered to by 'fp'.""" |
|---|
| 78 |
data = self.overrides.get(key) |
|---|
| 79 |
if not data: |
|---|
| 80 |
return False |
|---|
| 81 |
|
|---|
| 82 |
fp.write(data) |
|---|
| 83 |
return True |
|---|
| 84 |
|
|---|