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