git-svn-id: https://s3tools.svn.sourceforge.net/svnroot/s3tools/s3py/trunk@52 830e0280-6d2a-0410-9c65-932aecc39d9d
Michal Ludvig authored on 2007/01/18 11:17:171 | 1 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,48 @@ |
0 |
+import logging |
|
1 |
+from logging import debug, info, warning, error |
|
2 |
+import re |
|
3 |
+ |
|
4 |
+class ConfigParser: |
|
5 |
+ def __init__(self, file, sections = []): |
|
6 |
+ self.cfg = {} |
|
7 |
+ self.parse_file(file, sections) |
|
8 |
+ |
|
9 |
+ def parse_file(self, file, sections = []): |
|
10 |
+ info("ConfigParser: Reading file '%s'" % file) |
|
11 |
+ if type(sections) != type([]): |
|
12 |
+ sections = [sections] |
|
13 |
+ in_our_section = True |
|
14 |
+ f = open(file, "r") |
|
15 |
+ r_comment = re.compile("^\s*#.*") |
|
16 |
+ r_empty = re.compile("^\s*$") |
|
17 |
+ r_section = re.compile("^\[([^\]]+)\]") |
|
18 |
+ r_data = re.compile("^\s*(?P<key>\w+)\s*=\s*(?P<value>.*)") |
|
19 |
+ r_quotes = re.compile("^\"(.*)\"\s*$") |
|
20 |
+ for line in f: |
|
21 |
+ if r_comment.match(line) or r_empty.match(line): |
|
22 |
+ continue |
|
23 |
+ is_section = r_section.match(line) |
|
24 |
+ if is_section: |
|
25 |
+ section = is_section.groups()[0] |
|
26 |
+ in_our_section = (section in sections) or (len(sections) == 0) |
|
27 |
+ continue |
|
28 |
+ is_data = r_data.match(line) |
|
29 |
+ if is_data and in_our_section: |
|
30 |
+ data = is_data.groupdict() |
|
31 |
+ if r_quotes.match(data["value"]): |
|
32 |
+ data["value"] = data["value"][1:-1] |
|
33 |
+ debug("ConfigParser: %s->%s" % (data["key"], data["value"])) |
|
34 |
+ self.__setitem__(data["key"], data["value"]) |
|
35 |
+ continue |
|
36 |
+ warning("Ignoring invalid line in '%s': %s" % (file, line)) |
|
37 |
+ |
|
38 |
+ def __getitem__(self, name): |
|
39 |
+ return self.cfg[name] |
|
40 |
+ |
|
41 |
+ def __setitem__(self, name, value): |
|
42 |
+ self.cfg[name] = value |
|
43 |
+ |
|
44 |
+ def get(self, name, default = None): |
|
45 |
+ if self.cfg.has_key(name): |
|
46 |
+ return self.cfg[name] |
|
47 |
+ return default |
0 | 48 |
deleted file mode 100644 |
... | ... |
@@ -1,48 +0,0 @@ |
1 |
-import logging |
|
2 |
-from logging import debug, info, warning, error |
|
3 |
-import re |
|
4 |
- |
|
5 |
-class ConfigParser: |
|
6 |
- def __init__(self, file, sections = []): |
|
7 |
- self.cfg = {} |
|
8 |
- self.parse_file(file, sections) |
|
9 |
- |
|
10 |
- def parse_file(self, file, sections = []): |
|
11 |
- info("ConfigParser: Reading file '%s'" % file) |
|
12 |
- if type(sections) != type([]): |
|
13 |
- sections = [sections] |
|
14 |
- in_our_section = True |
|
15 |
- f = open(file, "r") |
|
16 |
- r_comment = re.compile("^\s*#.*") |
|
17 |
- r_empty = re.compile("^\s*$") |
|
18 |
- r_section = re.compile("^\[([^\]]+)\]") |
|
19 |
- r_data = re.compile("^\s*(?P<key>\w+)\s*=\s*(?P<value>.*)") |
|
20 |
- r_quotes = re.compile("^\"(.*)\"\s*$") |
|
21 |
- for line in f: |
|
22 |
- if r_comment.match(line) or r_empty.match(line): |
|
23 |
- continue |
|
24 |
- is_section = r_section.match(line) |
|
25 |
- if is_section: |
|
26 |
- section = is_section.groups()[0] |
|
27 |
- in_our_section = (section in sections) or (len(sections) == 0) |
|
28 |
- continue |
|
29 |
- is_data = r_data.match(line) |
|
30 |
- if is_data and in_our_section: |
|
31 |
- data = is_data.groupdict() |
|
32 |
- if r_quotes.match(data["value"]): |
|
33 |
- data["value"] = data["value"][1:-1] |
|
34 |
- debug("ConfigParser: %s->%s" % (data["key"], data["value"])) |
|
35 |
- self.__setitem__(data["key"], data["value"]) |
|
36 |
- continue |
|
37 |
- warning("Ignoring invalid line in '%s': %s" % (file, line)) |
|
38 |
- |
|
39 |
- def __getitem__(self, name): |
|
40 |
- return self.cfg[name] |
|
41 |
- |
|
42 |
- def __setitem__(self, name, value): |
|
43 |
- self.cfg[name] = value |
|
44 |
- |
|
45 |
- def get(self, name, default = None): |
|
46 |
- if self.cfg.has_key(name): |
|
47 |
- return self.cfg[name] |
|
48 |
- return default |