From adaa65d753234fbdbc13acf08f7aa53cae4db356 Mon Sep 17 00:00:00 2001 From: Six Date: Sat, 10 Apr 2010 17:42:40 -0400 Subject: Added new config file handler --- dodai/config/files.py | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 dodai/config/files.py diff --git a/dodai/config/files.py b/dodai/config/files.py new file mode 100644 index 0000000..57e964c --- /dev/null +++ b/dodai/config/files.py @@ -0,0 +1,79 @@ +# Copyright (C) 2010 Leonard Thomas +# +# This file is part of Dodai. +# +# Dodai is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Dodai is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Dodai. If not, see . + +import os +import ConfigParser +from dodai.tools.himo import Himo +from dodai.config.sections import ConfigSections + +class ConfigFiles(object): + + def __init__(self): + + self._default_parser = ConfigParser.ConfigParser() + self._string_object = Himo + self._files = {} + self._paths = [] + + 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 add_file(self, path, parser=None): + if parser: + self.check_config_parser(parser) + if os.path.exists(path): + if parser: + self._files[path] = parser + else: + self._paths.append(path) + else: + raise FileDoesNotExist(path) + + 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 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 + + +class InvalidConfigParser(Exception): + pass + +class FileDoesNotExist(Exception): + pass -- cgit v1.2.3