From 3d0a8b94ce0a2b6c9650a66c5ebe275212533b97 Mon Sep 17 00:00:00 2001 From: Six Date: Sat, 10 Apr 2010 23:14:40 -0400 Subject: added encoding options --- dodai/config/files.py | 73 +++++++++++++++++++++++++-------------------------- 1 file changed, 36 insertions(+), 37 deletions(-) diff --git a/dodai/config/files.py b/dodai/config/files.py index 57e964c..2401b1e 100644 --- a/dodai/config/files.py +++ b/dodai/config/files.py @@ -17,7 +17,7 @@ import os import ConfigParser -from dodai.tools.himo import Himo +from dodai.tools.himo import String2Himo from dodai.config.sections import ConfigSections class ConfigFiles(object): @@ -25,51 +25,50 @@ class ConfigFiles(object): def __init__(self): self._default_parser = ConfigParser.ConfigParser() - self._string_object = Himo + self._string_object = String2Himo() self._files = {} - self._paths = [] - def set_default_parser(self, parser): - self.check_config_parser(parser) - self._default_parser = parser + def set_default_parser(self, parser): + self.check_config_parser(parser) + self._default_parser = parser - def set_string_object(self, obj): - self._string_object = obj + def set_string_object(self, obj): + self._string_object = obj - def add_file(self, path, parser=None): + def add_file(self, path, encoding=None, parser=None): + if parser: + self.check_config_parser(parser) + if os.path.exists(path): if parser: - self.check_config_parser(parser) - if os.path.exists(path): - if parser: - self._files[path] = parser - else: - self._paths.append(path) + self._files[path] = [parser, encoding] else: - raise FileDoesNotExist(path) + self._files[path] = [self._default_parser, encoding] + else: + raise FileDoesNotExist(path) - def is_valid_config_parser(self, obj): - out = True - if not hasattr(obj, 'read'): - out = False - return out + def is_valid_config_parser(self, obj): + out = True + if not hasattr(obj, 'read'): + out = False + return out - def check_config_parser(self, obj): - if self.is_valid_config_parser(obj): - return True - else: - raise InvalidConfigParser("Please make sure your object "\ - "has the following methods: "\ - "sections, options, read and get.") + def check_config_parser(self, obj): + if self.is_valid_config_parser(obj): + return True + else: + raise InvalidConfigParser("Please make sure your object "\ + "has the following methods: "\ + "sections, options, read and get.") - def load(self, sections_object=None): - paths = [] - sections = sections_object or ConfigSections(self._string_object) - self._default_parser.read(self._paths) - sections(self._default_parser) - for path, parser in self._files: - parser.read(path) - sections(parser) - return sections + def load(self, sections_object=None): + paths = [] + sections = sections_object or ConfigSections(self._string_object) + for path, data in self._files.items(): + parser = data[0] + encoding = data[1] + parser.read(path) + sections(parser, encoding) + return sections class InvalidConfigParser(Exception): -- cgit v1.2.3