annotate python/simpleini.py @ 924:fc88a12acf48 default tip

add lfs filter
author Jeff Hammel <k0scist@gmail.com>
date Wed, 04 Dec 2024 15:50:53 -0800
parents c499f5a598cf
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
112
e85298a35998 add a simpleini parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1 #!/usr/bin/env python
e85298a35998 add a simpleini parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
2
e85298a35998 add a simpleini parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
3 import os
e85298a35998 add a simpleini parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
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
e85298a35998 add a simpleini parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
20
e85298a35998 add a simpleini parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
21 if isinstance(fp, basestring):
e85298a35998 add a simpleini parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
22 fp = file(fp)
e85298a35998 add a simpleini parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
23
e85298a35998 add a simpleini parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
24 sections = []
e85298a35998 add a simpleini parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
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
e85298a35998 add a simpleini parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
29 for line in fp.readlines():
e85298a35998 add a simpleini parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
30
e85298a35998 add a simpleini parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
31 stripped = line.strip()
e85298a35998 add a simpleini parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
32
e85298a35998 add a simpleini parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
33 # ignore blank lines
e85298a35998 add a simpleini parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
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
e85298a35998 add a simpleini parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
37 continue
e85298a35998 add a simpleini parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
38
e85298a35998 add a simpleini parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
39 # ignore comment lines
e85298a35998 add a simpleini parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
40 if stripped[0] in comments:
e85298a35998 add a simpleini parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
41 continue
e85298a35998 add a simpleini parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
42
e85298a35998 add a simpleini parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
43 # check for a new section
e85298a35998 add a simpleini parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
44 if len(stripped) > 2 and stripped[0] == '[' and stripped[-1] == ']':
e85298a35998 add a simpleini parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
45 section = stripped[1:-1].strip()
e85298a35998 add a simpleini parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
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
e85298a35998 add a simpleini parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
64 continue
e85298a35998 add a simpleini parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
65
e85298a35998 add a simpleini parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
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
e85298a35998 add a simpleini parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
68 raise Exception('No sections yet :(')
e85298a35998 add a simpleini parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
69
e85298a35998 add a simpleini parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
70 # (key, value) pair
e85298a35998 add a simpleini parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
71 for separator in separators:
e85298a35998 add a simpleini parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
72 if separator in stripped:
e85298a35998 add a simpleini parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
73 key, value = stripped.split(separator, 1)
e85298a35998 add a simpleini parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
74 key = key.strip()
e85298a35998 add a simpleini parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
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
e85298a35998 add a simpleini parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
84 break
e85298a35998 add a simpleini parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
85 else:
e85298a35998 add a simpleini parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
86 # continuation line ?
e85298a35998 add a simpleini parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
87 if line[0].isspace() and key:
e85298a35998 add a simpleini parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
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
e85298a35998 add a simpleini parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
90 else:
e85298a35998 add a simpleini parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
91 # something bad happen!
e85298a35998 add a simpleini parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
92 raise Exception("Not sure what you're trying to do")
e85298a35998 add a simpleini parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
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
e85298a35998 add a simpleini parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
118 return sections
e85298a35998 add a simpleini parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
119
e85298a35998 add a simpleini parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
120 if __name__ == '__main__':
e85298a35998 add a simpleini parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
121 import sys
e85298a35998 add a simpleini parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
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)))