aboutsummaryrefslogtreecommitdiff
path: root/dodai/config/files.py
diff options
context:
space:
mode:
Diffstat (limited to 'dodai/config/files.py')
-rw-r--r--dodai/config/files.py114
1 files changed, 71 insertions, 43 deletions
diff --git a/dodai/config/files.py b/dodai/config/files.py
index 3ca9121..a27e4a2 100644
--- a/dodai/config/files.py
+++ b/dodai/config/files.py
@@ -17,62 +17,90 @@
17 17
18import os 18import os
19import ConfigParser 19import ConfigParser
20from dodai.tools.himo import String2Himo
21from dodai.config.sections import ConfigSections 20from dodai.config.sections import ConfigSections
21from dodai.exception import InvalidConfigParser
22from dodai.exception import FileDoesNotExist
23from dodai.exception import FileIsDirectory
22 24
23class ConfigFiles(object): 25class ConfigFiles(object):
24 26
25 def __init__(self): 27 REQUIRED_METHODS = ['read', 'sections', 'options', 'get']
26 28
27 self._default_parser = ConfigParser.ConfigParser() 29 def __init__(self, ordered_dict_object, section_object, string_object):
28 self._string_object = String2Himo()
29 self._files = {}
30 30
31 def set_default_parser(self, parser): 31 self._ordered_dict_object = ordered_dict_object
32 self.check_config_parser(parser) 32 self._section_object = section_object
33 self._default_parser = parser 33 self._string_object = string_object
34 self.parser_objects = []
35 self.files = self._ordered_dict_object()
36 self.files_read = []
34 37
35 def set_string_object(self, obj): 38 def register_parser_object(self, parser_object):
36 self._string_object = obj 39 """Registers a config file parser with this object. Raises
40 InvalidConfigParser error if the parser does not have the 'read',
41 'sections', 'options' and 'get' methods.
37 42
38 def add(self, path, encoding=None, parser=None): 43 """
39 if parser: 44 if self.check_parser_object(parser_object):
40 self.check_config_parser(parser) 45 self.parser_objects.append(parser_object)
41 if os.path.exists(path): 46
42 if parser: 47 def check_parser_object(self, parser_object):
43 self._files[path] = [parser, encoding] 48 """Checks the given parser object to insure it has all of the required
49 methods needed to parse files.
50
51 """
52 for name in self.REQUIRED_METHODS:
53 if not hasattr(parser_object, name):
54 raise InvalidConfigParser(self.REQUIRED_METHODS,
55 parser_object.__name__)
56 return True
57
58 def add(self, filename, encoding=None):
59 """Adds the given filename to this object to be parsed.
60
61 """
62 if os.path.exists(filename):
63 if os.path.isdir(filename):
64 raise FileIsDirectory(filename)
44 else: 65 else:
45 self._files[path] = [self._default_parser, encoding] 66 filename = os.path.realpath(filename)
67 self.files[filename] = encoding
46 else: 68 else:
47 raise FileDoesNotExist(path) 69 raise FileDoesNotExist(filename)
48 70
49 def is_valid_config_parser(self, obj): 71 def _read(self):
50 out = True 72 self.files_read = []
51 if not hasattr(obj, 'read'): 73 out = self._ordered_dict_object()
52 out = False 74 for filename, encoding in self.files.items():
75 error = False
76 for parser_obj in self.parser_objects:
77 try:
78 parser = parser_obj()
79 parser.read(filename)
80 except Exception, e:
81 error = True
82 else:
83 out[filename] = self._ordered_dict_object()
84 out[filename]['encoding'] = encoding
85 out[filename]['parser'] = parser
86 self.files_read.append(filename)
87 error = False
88 break
89 if error:
90 raise e
53 return out 91 return out
54 92
55 def check_config_parser(self, obj):
56 if self.is_valid_config_parser(obj):
57 return True
58 else:
59 raise InvalidConfigParser("Please make sure your object "\
60 "has the following methods: "\
61 "sections, options, read and get.")
62 93
63 def load(self, sections_object=None): 94 def load(self):
64 paths = [] 95 """Returns a ConfigSections object, which acts like a dictionary,
65 sections = sections_object or ConfigSections(self._string_object) 96 that contains all parsed data.
66 for path, data in self._files.items(): 97
67 parser = data[0] 98 """
68 encoding = data[1] 99 files = self._read()
69 parser.read(path) 100 sections = ConfigSections(self._ordered_dict_object,
101 self._section_object, self._string_object)
102 for filename, data in files.items():
103 encoding = data['encoding']
104 parser = data['parser']
70 sections(parser, encoding) 105 sections(parser, encoding)
71 return sections 106 return sections
72
73
74class InvalidConfigParser(Exception):
75 pass
76
77class FileDoesNotExist(Exception):
78 pass