Skip to content
config.py 1.82 KiB
Newer Older
Seblu's avatar
Seblu committed
# coding: utf-8

# archversion - Archlinux Version Controller
# Copyright © 2012 Sébastien Luttringer
#
# This program 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 2
# of the License, or (at your option) any later version.
#
# This program 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 this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

'''Configuration Module'''

Seblu's avatar
Seblu committed
from archversion import XDG_DIRECTORY
Seblu's avatar
Seblu committed
from archversion.error import MissingConfigFile
from collections import OrderedDict
from configparser import RawConfigParser
Seblu's avatar
Seblu committed
from logging import debug
from os.path import join, exists
from xdg.BaseDirectory import save_config_path
Seblu's avatar
Seblu committed

class BaseConfigFile(OrderedDict):
    '''Base config file class'''

Seblu's avatar
Seblu committed
    def __init__(self, filename):
Seblu's avatar
Seblu committed
        '''Initialize config object'''
Seblu's avatar
Seblu committed
        assert(filename is not None)
Seblu's avatar
Seblu committed
        OrderedDict.__init__(self)
Seblu's avatar
Seblu committed
        path = save_config_path(XDG_DIRECTORY)
        self.path = join(path, filename)
        if not isinstance(self.path, str) or not exists(self.path):
Seblu's avatar
Seblu committed
            raise MissingConfigFile(self.path)
Seblu's avatar
Seblu committed
        self.load()

    def load(self):
        '''Load configuration'''
Seblu's avatar
Seblu committed
        debug("loading config file at: %s" % self.path)
Seblu's avatar
Seblu committed
        self._configparser = RawConfigParser()
        self._configparser.read(self.path)
        for name in self._configparser.sections():
            self[name] = OrderedDict(self._configparser.items(name))