root/gaphor/tags/gaphor-0.3.0/utils/override.py

Revision 219, 2.3 kB (checked in by arjanmol, 5 years ago)

*** empty log message ***

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 # vim:sw=4:et
2 """This file contains code for loading up an override file.  The override file
3 provides implementations of functions where the code generator could not
4 do its job correctly.
5
6 This is a simple rip-off of the override script used in PyGTK.
7 """
8
9
10 import sys, string
11
12 class Overrides:
13
14     def __init__(self, filename=None):
15         self.overrides = {}
16         if filename:
17             self.read_overrides(filename)
18
19
20     def read_overrides(self, filename):
21         """Read a file and return a dictionary of overriden properties
22         and their implementation.
23
24         An override file ahs the form:
25         override <property>
26         <implementation>
27         %%
28         """
29         fp = open(filename, 'r')
30         # read all the components of the file ...
31         # bufs contains a list of (lines, startline) pairs.
32         bufs = []
33         startline = 1
34         lines = []
35         line = fp.readline()
36         linenum = 1
37         while line:
38             if line == '%%\n' or line == '%%':
39                 if lines:
40                     bufs.append((string.join(lines, ''), startline))
41                 startline = linenum + 1
42                 lines = []
43             else:
44                 lines.append(line)
45             line = fp.readline()
46             linenum = linenum + 1
47         if lines:
48             bufs.append((string.join(lines, ''), startline))
49
50         if not bufs:
51             return
52
53         # Parse the parts of the file
54         for buffer, startline in bufs:
55             pos = string.find(buffer, '\n')
56             if pos >= 0:
57                 line = buffer[:pos]
58                 rest = buffer[pos+1:]
59             else:
60                 line = buffer ; rest = ''
61             words = string.split(line)
62
63             if words[0] == 'override':
64                 func = words[1]
65                 self.overrides[func] = rest
66             elif words[0] == 'comment':
67                 pass # ignore comments
68             else:
69                 print "Unknown word: '%s', line %d" (words[0], startline)
70                 raise SystemExit
71
72     def has_override(self, key):
73         return bool(self.overrides.get(key))
74
75     def write_override(self, fp, key):
76         """Write override data for 'key' to a file refered to by 'fp'."""
77         data = self.overrides.get(key)
78         if not data:
79             return False
80
81         fp.write(data)
82         return True
83
Note: See TracBrowser for help on using the browser.