Mercurial > hg > config
annotate python/simpleini.py @ 113:44534594f402
add variable interpolation
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Thu, 02 Dec 2010 13:04:54 -0800 |
parents | e85298a35998 |
children | 9b193312ceba |
rev | line source |
---|---|
112 | 1 #!/usr/bin/env python |
2 | |
3 import os | |
4 | |
113
44534594f402
add variable interpolation
Jeff Hammel <jhammel@mozilla.com>
parents:
112
diff
changeset
|
5 def read(fp, variables=None, default='DEFAULT', |
44534594f402
add variable interpolation
Jeff Hammel <jhammel@mozilla.com>
parents:
112
diff
changeset
|
6 comments=';#', separators=('=', ':'), strict=True): |
44534594f402
add variable interpolation
Jeff Hammel <jhammel@mozilla.com>
parents:
112
diff
changeset
|
7 """ |
44534594f402
add variable interpolation
Jeff Hammel <jhammel@mozilla.com>
parents:
112
diff
changeset
|
8 read an .ini file and return a list of [(section, values)] |
44534594f402
add variable interpolation
Jeff Hammel <jhammel@mozilla.com>
parents:
112
diff
changeset
|
9 - fp : file pointer or name to read |
44534594f402
add variable interpolation
Jeff Hammel <jhammel@mozilla.com>
parents:
112
diff
changeset
|
10 - variables : default set of variables |
44534594f402
add variable interpolation
Jeff Hammel <jhammel@mozilla.com>
parents:
112
diff
changeset
|
11 - default : name of the section for the default section |
44534594f402
add variable interpolation
Jeff Hammel <jhammel@mozilla.com>
parents:
112
diff
changeset
|
12 - comments : characters that if they start a line denote a comment |
44534594f402
add variable interpolation
Jeff Hammel <jhammel@mozilla.com>
parents:
112
diff
changeset
|
13 - separators : strings that denote key, value separation in order |
44534594f402
add variable interpolation
Jeff Hammel <jhammel@mozilla.com>
parents:
112
diff
changeset
|
14 - strict : whether to be strict about parsing |
44534594f402
add variable interpolation
Jeff Hammel <jhammel@mozilla.com>
parents:
112
diff
changeset
|
15 """ |
44534594f402
add variable interpolation
Jeff Hammel <jhammel@mozilla.com>
parents:
112
diff
changeset
|
16 |
44534594f402
add variable interpolation
Jeff Hammel <jhammel@mozilla.com>
parents:
112
diff
changeset
|
17 if variables is None: |
44534594f402
add variable interpolation
Jeff Hammel <jhammel@mozilla.com>
parents:
112
diff
changeset
|
18 variables = {} |
44534594f402
add variable interpolation
Jeff Hammel <jhammel@mozilla.com>
parents:
112
diff
changeset
|
19 variables = variables.copy() # no reason to overwrite the originals |
112 | 20 |
21 if isinstance(fp, basestring): | |
22 fp = file(fp) | |
23 | |
24 sections = [] | |
25 key = value = None | |
113
44534594f402
add variable interpolation
Jeff Hammel <jhammel@mozilla.com>
parents:
112
diff
changeset
|
26 section_names = set([]) |
44534594f402
add variable interpolation
Jeff Hammel <jhammel@mozilla.com>
parents:
112
diff
changeset
|
27 |
44534594f402
add variable interpolation
Jeff Hammel <jhammel@mozilla.com>
parents:
112
diff
changeset
|
28 # read the lines |
112 | 29 for line in fp.readlines(): |
30 | |
31 stripped = line.strip() | |
32 | |
33 # ignore blank lines | |
34 if not stripped: | |
113
44534594f402
add variable interpolation
Jeff Hammel <jhammel@mozilla.com>
parents:
112
diff
changeset
|
35 # XXX should probably reset key and value to avoid continuation lines |
44534594f402
add variable interpolation
Jeff Hammel <jhammel@mozilla.com>
parents:
112
diff
changeset
|
36 key = value = None |
112 | 37 continue |
38 | |
39 # ignore comment lines | |
40 if stripped[0] in comments: | |
41 continue | |
42 | |
43 # check for a new section | |
44 if len(stripped) > 2 and stripped[0] == '[' and stripped[-1] == ']': | |
45 section = stripped[1:-1].strip() | |
46 key = value = None | |
113
44534594f402
add variable interpolation
Jeff Hammel <jhammel@mozilla.com>
parents:
112
diff
changeset
|
47 |
44534594f402
add variable interpolation
Jeff Hammel <jhammel@mozilla.com>
parents:
112
diff
changeset
|
48 # deal with DEFAULT section |
44534594f402
add variable interpolation
Jeff Hammel <jhammel@mozilla.com>
parents:
112
diff
changeset
|
49 if section.lower() == default.lower(): |
44534594f402
add variable interpolation
Jeff Hammel <jhammel@mozilla.com>
parents:
112
diff
changeset
|
50 if strict: |
44534594f402
add variable interpolation
Jeff Hammel <jhammel@mozilla.com>
parents:
112
diff
changeset
|
51 assert default not in section_names |
44534594f402
add variable interpolation
Jeff Hammel <jhammel@mozilla.com>
parents:
112
diff
changeset
|
52 section_names.add(default) |
44534594f402
add variable interpolation
Jeff Hammel <jhammel@mozilla.com>
parents:
112
diff
changeset
|
53 current_section = variables |
44534594f402
add variable interpolation
Jeff Hammel <jhammel@mozilla.com>
parents:
112
diff
changeset
|
54 continue |
44534594f402
add variable interpolation
Jeff Hammel <jhammel@mozilla.com>
parents:
112
diff
changeset
|
55 |
44534594f402
add variable interpolation
Jeff Hammel <jhammel@mozilla.com>
parents:
112
diff
changeset
|
56 if strict: |
44534594f402
add variable interpolation
Jeff Hammel <jhammel@mozilla.com>
parents:
112
diff
changeset
|
57 # make sure this section doesn't already exist |
44534594f402
add variable interpolation
Jeff Hammel <jhammel@mozilla.com>
parents:
112
diff
changeset
|
58 assert section not in section_names |
44534594f402
add variable interpolation
Jeff Hammel <jhammel@mozilla.com>
parents:
112
diff
changeset
|
59 |
44534594f402
add variable interpolation
Jeff Hammel <jhammel@mozilla.com>
parents:
112
diff
changeset
|
60 section_names.add(section) |
44534594f402
add variable interpolation
Jeff Hammel <jhammel@mozilla.com>
parents:
112
diff
changeset
|
61 |
44534594f402
add variable interpolation
Jeff Hammel <jhammel@mozilla.com>
parents:
112
diff
changeset
|
62 current_section = {} |
44534594f402
add variable interpolation
Jeff Hammel <jhammel@mozilla.com>
parents:
112
diff
changeset
|
63 sections.append((section, current_section)) |
112 | 64 continue |
65 | |
66 # if there aren't any sections yet, something bad happen | |
113
44534594f402
add variable interpolation
Jeff Hammel <jhammel@mozilla.com>
parents:
112
diff
changeset
|
67 if not section_names: |
112 | 68 raise Exception('No sections yet :(') |
69 | |
70 # (key, value) pair | |
71 for separator in separators: | |
72 if separator in stripped: | |
73 key, value = stripped.split(separator, 1) | |
74 key = key.strip() | |
75 value = value.strip() | |
113
44534594f402
add variable interpolation
Jeff Hammel <jhammel@mozilla.com>
parents:
112
diff
changeset
|
76 |
44534594f402
add variable interpolation
Jeff Hammel <jhammel@mozilla.com>
parents:
112
diff
changeset
|
77 if strict: |
44534594f402
add variable interpolation
Jeff Hammel <jhammel@mozilla.com>
parents:
112
diff
changeset
|
78 # make sure this key isn't already in the section or empty |
44534594f402
add variable interpolation
Jeff Hammel <jhammel@mozilla.com>
parents:
112
diff
changeset
|
79 assert key |
44534594f402
add variable interpolation
Jeff Hammel <jhammel@mozilla.com>
parents:
112
diff
changeset
|
80 if current_section is not variables: |
44534594f402
add variable interpolation
Jeff Hammel <jhammel@mozilla.com>
parents:
112
diff
changeset
|
81 assert key not in current_section |
44534594f402
add variable interpolation
Jeff Hammel <jhammel@mozilla.com>
parents:
112
diff
changeset
|
82 |
44534594f402
add variable interpolation
Jeff Hammel <jhammel@mozilla.com>
parents:
112
diff
changeset
|
83 current_section[key] = value |
112 | 84 break |
85 else: | |
86 # continuation line ? | |
87 if line[0].isspace() and key: | |
88 value = '%s%s%s' % (value, os.linesep, stripped) | |
113
44534594f402
add variable interpolation
Jeff Hammel <jhammel@mozilla.com>
parents:
112
diff
changeset
|
89 current_section[key] = value |
112 | 90 else: |
91 # something bad happen! | |
92 raise Exception("Not sure what you're trying to do") | |
93 | |
113
44534594f402
add variable interpolation
Jeff Hammel <jhammel@mozilla.com>
parents:
112
diff
changeset
|
94 # interpret the variables |
44534594f402
add variable interpolation
Jeff Hammel <jhammel@mozilla.com>
parents:
112
diff
changeset
|
95 def interpret_variables(global_dict, local_dict): |
44534594f402
add variable interpolation
Jeff Hammel <jhammel@mozilla.com>
parents:
112
diff
changeset
|
96 variables = global_dict.copy() |
44534594f402
add variable interpolation
Jeff Hammel <jhammel@mozilla.com>
parents:
112
diff
changeset
|
97 variables.update(local_dict) |
44534594f402
add variable interpolation
Jeff Hammel <jhammel@mozilla.com>
parents:
112
diff
changeset
|
98 # TODO: string intepolation |
44534594f402
add variable interpolation
Jeff Hammel <jhammel@mozilla.com>
parents:
112
diff
changeset
|
99 return variables |
44534594f402
add variable interpolation
Jeff Hammel <jhammel@mozilla.com>
parents:
112
diff
changeset
|
100 |
44534594f402
add variable interpolation
Jeff Hammel <jhammel@mozilla.com>
parents:
112
diff
changeset
|
101 sections = [(i, interpret_variables(variables, j)) for i, j in sections] |
112 | 102 return sections |
103 | |
104 if __name__ == '__main__': | |
105 import sys | |
106 for i in sys.argv[1:]: | |
107 print read(i) |