annotate python/simpleini.py @ 112:e85298a35998

add a simpleini parser
author Jeff Hammel <jhammel@mozilla.com>
date Thu, 02 Dec 2010 09:53:29 -0800
parents
children 44534594f402
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
e85298a35998 add a simpleini parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
5 def read(fp, comments=';#', separators=('=', ':')):
e85298a35998 add a simpleini parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
6
e85298a35998 add a simpleini parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
7 if isinstance(fp, basestring):
e85298a35998 add a simpleini parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
8 fp = file(fp)
e85298a35998 add a simpleini parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
9
e85298a35998 add a simpleini parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
10 sections = []
e85298a35998 add a simpleini parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
11 key = value = None
e85298a35998 add a simpleini parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
12
e85298a35998 add a simpleini parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
13 for line in fp.readlines():
e85298a35998 add a simpleini parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
14
e85298a35998 add a simpleini parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
15 stripped = line.strip()
e85298a35998 add a simpleini parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
16
e85298a35998 add a simpleini parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
17 # ignore blank lines
e85298a35998 add a simpleini parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
18 if not stripped:
e85298a35998 add a simpleini parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
19 continue
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 # ignore comment lines
e85298a35998 add a simpleini parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
22 if stripped[0] in comments:
e85298a35998 add a simpleini parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
23 continue
e85298a35998 add a simpleini parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
24
e85298a35998 add a simpleini parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
25 # check for a new section
e85298a35998 add a simpleini parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
26 if len(stripped) > 2 and stripped[0] == '[' and stripped[-1] == ']':
e85298a35998 add a simpleini parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
27 section = stripped[1:-1].strip()
e85298a35998 add a simpleini parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
28 sections.append((section, {}))
e85298a35998 add a simpleini parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
29 key = value = None
e85298a35998 add a simpleini parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
30 # TODO: should probably make sure this section doesn't already exist
e85298a35998 add a simpleini parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
31 continue
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 # if there aren't any sections yet, something bad happen
e85298a35998 add a simpleini parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
34 if not sections:
e85298a35998 add a simpleini parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
35 raise Exception('No sections yet :(')
e85298a35998 add a simpleini parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
36
e85298a35998 add a simpleini parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
37 # (key, value) pair
e85298a35998 add a simpleini parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
38 for separator in separators:
e85298a35998 add a simpleini parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
39 if separator in stripped:
e85298a35998 add a simpleini parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
40 key, value = stripped.split(separator, 1)
e85298a35998 add a simpleini parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
41 key = key.strip()
e85298a35998 add a simpleini parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
42 value = value.strip()
e85298a35998 add a simpleini parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
43 sections[-1][1][key] = value
e85298a35998 add a simpleini parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
44 # TODO: should probably make sure this key isn't already in the section
e85298a35998 add a simpleini parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
45 break
e85298a35998 add a simpleini parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
46 else:
e85298a35998 add a simpleini parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
47 # continuation line ?
e85298a35998 add a simpleini parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
48 if line[0].isspace() and key:
e85298a35998 add a simpleini parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
49 value = '%s%s%s' % (value, os.linesep, stripped)
e85298a35998 add a simpleini parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
50 sections[-1][1][key] = value
e85298a35998 add a simpleini parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
51 else:
e85298a35998 add a simpleini parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
52 # something bad happen!
e85298a35998 add a simpleini parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
53 raise Exception("Not sure what you're trying to do")
e85298a35998 add a simpleini parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
54
e85298a35998 add a simpleini parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
55 return sections
e85298a35998 add a simpleini parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
56
e85298a35998 add a simpleini parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
57 if __name__ == '__main__':
e85298a35998 add a simpleini parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
58 import sys
e85298a35998 add a simpleini parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
59 for i in sys.argv[1:]:
e85298a35998 add a simpleini parser
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
60 print read(i)