Mercurial > hg > config
annotate python/simpleini.py @ 432:a9890ec0eab6
oops
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Thu, 08 Aug 2013 01:07:40 -0700 |
parents | c499f5a598cf |
children |
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', |
115
c499f5a598cf
make variable interpolation sorta work; the nonce thing probably isnt the best idea
Jeff Hammel <jhammel@mozilla.com>
parents:
114
diff
changeset
|
6 comments=';#', separators=('=', ':'), |
c499f5a598cf
make variable interpolation sorta work; the nonce thing probably isnt the best idea
Jeff Hammel <jhammel@mozilla.com>
parents:
114
diff
changeset
|
7 interpolate=True, strict=True): |
113
44534594f402
add variable interpolation
Jeff Hammel <jhammel@mozilla.com>
parents:
112
diff
changeset
|
8 """ |
44534594f402
add variable interpolation
Jeff Hammel <jhammel@mozilla.com>
parents:
112
diff
changeset
|
9 read an .ini file and return a list of [(section, values)] |
44534594f402
add variable interpolation
Jeff Hammel <jhammel@mozilla.com>
parents:
112
diff
changeset
|
10 - fp : file pointer or name to read |
44534594f402
add variable interpolation
Jeff Hammel <jhammel@mozilla.com>
parents:
112
diff
changeset
|
11 - variables : default set of variables |
44534594f402
add variable interpolation
Jeff Hammel <jhammel@mozilla.com>
parents:
112
diff
changeset
|
12 - default : name of the section for the default section |
44534594f402
add variable interpolation
Jeff Hammel <jhammel@mozilla.com>
parents:
112
diff
changeset
|
13 - comments : characters that if they start a line denote a comment |
44534594f402
add variable interpolation
Jeff Hammel <jhammel@mozilla.com>
parents:
112
diff
changeset
|
14 - separators : strings that denote key, value separation in order |
44534594f402
add variable interpolation
Jeff Hammel <jhammel@mozilla.com>
parents:
112
diff
changeset
|
15 - strict : whether to be strict about parsing |
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 |
44534594f402
add variable interpolation
Jeff Hammel <jhammel@mozilla.com>
parents:
112
diff
changeset
|
18 if variables is None: |
44534594f402
add variable interpolation
Jeff Hammel <jhammel@mozilla.com>
parents:
112
diff
changeset
|
19 variables = {} |
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) |
115
c499f5a598cf
make variable interpolation sorta work; the nonce thing probably isnt the best idea
Jeff Hammel <jhammel@mozilla.com>
parents:
114
diff
changeset
|
98 |
c499f5a598cf
make variable interpolation sorta work; the nonce thing probably isnt the best idea
Jeff Hammel <jhammel@mozilla.com>
parents:
114
diff
changeset
|
99 # string intepolation |
c499f5a598cf
make variable interpolation sorta work; the nonce thing probably isnt the best idea
Jeff Hammel <jhammel@mozilla.com>
parents:
114
diff
changeset
|
100 if interpolate: |
c499f5a598cf
make variable interpolation sorta work; the nonce thing probably isnt the best idea
Jeff Hammel <jhammel@mozilla.com>
parents:
114
diff
changeset
|
101 nonce = '__s__' |
c499f5a598cf
make variable interpolation sorta work; the nonce thing probably isnt the best idea
Jeff Hammel <jhammel@mozilla.com>
parents:
114
diff
changeset
|
102 assert nonce not in global_dict |
c499f5a598cf
make variable interpolation sorta work; the nonce thing probably isnt the best idea
Jeff Hammel <jhammel@mozilla.com>
parents:
114
diff
changeset
|
103 global_dict[nonce] = '%s' |
c499f5a598cf
make variable interpolation sorta work; the nonce thing probably isnt the best idea
Jeff Hammel <jhammel@mozilla.com>
parents:
114
diff
changeset
|
104 for key, value in variables.items(): |
c499f5a598cf
make variable interpolation sorta work; the nonce thing probably isnt the best idea
Jeff Hammel <jhammel@mozilla.com>
parents:
114
diff
changeset
|
105 try: |
c499f5a598cf
make variable interpolation sorta work; the nonce thing probably isnt the best idea
Jeff Hammel <jhammel@mozilla.com>
parents:
114
diff
changeset
|
106 value = value.replace('%s', '%(__s__)s') % global_dict |
c499f5a598cf
make variable interpolation sorta work; the nonce thing probably isnt the best idea
Jeff Hammel <jhammel@mozilla.com>
parents:
114
diff
changeset
|
107 variables[key] = value |
c499f5a598cf
make variable interpolation sorta work; the nonce thing probably isnt the best idea
Jeff Hammel <jhammel@mozilla.com>
parents:
114
diff
changeset
|
108 except: |
c499f5a598cf
make variable interpolation sorta work; the nonce thing probably isnt the best idea
Jeff Hammel <jhammel@mozilla.com>
parents:
114
diff
changeset
|
109 if strict: |
c499f5a598cf
make variable interpolation sorta work; the nonce thing probably isnt the best idea
Jeff Hammel <jhammel@mozilla.com>
parents:
114
diff
changeset
|
110 del global_dict[nonce] |
c499f5a598cf
make variable interpolation sorta work; the nonce thing probably isnt the best idea
Jeff Hammel <jhammel@mozilla.com>
parents:
114
diff
changeset
|
111 raise Exception("could not intepolate variable %s: %s" % (key, value)) |
c499f5a598cf
make variable interpolation sorta work; the nonce thing probably isnt the best idea
Jeff Hammel <jhammel@mozilla.com>
parents:
114
diff
changeset
|
112 pass |
c499f5a598cf
make variable interpolation sorta work; the nonce thing probably isnt the best idea
Jeff Hammel <jhammel@mozilla.com>
parents:
114
diff
changeset
|
113 |
c499f5a598cf
make variable interpolation sorta work; the nonce thing probably isnt the best idea
Jeff Hammel <jhammel@mozilla.com>
parents:
114
diff
changeset
|
114 del global_dict[nonce] |
113
44534594f402
add variable interpolation
Jeff Hammel <jhammel@mozilla.com>
parents:
112
diff
changeset
|
115 return variables |
44534594f402
add variable interpolation
Jeff Hammel <jhammel@mozilla.com>
parents:
112
diff
changeset
|
116 |
44534594f402
add variable interpolation
Jeff Hammel <jhammel@mozilla.com>
parents:
112
diff
changeset
|
117 sections = [(i, interpret_variables(variables, j)) for i, j in sections] |
112 | 118 return sections |
119 | |
120 if __name__ == '__main__': | |
121 import sys | |
122 for i in sys.argv[1:]: | |
115
c499f5a598cf
make variable interpolation sorta work; the nonce thing probably isnt the best idea
Jeff Hammel <jhammel@mozilla.com>
parents:
114
diff
changeset
|
123 path = os.path.abspath(i) |
c499f5a598cf
make variable interpolation sorta work; the nonce thing probably isnt the best idea
Jeff Hammel <jhammel@mozilla.com>
parents:
114
diff
changeset
|
124 print read(i, strict=False, variables=dict(here=os.path.dirname(path))) |