# -*- coding: utf-8 -*-

# Copyright(C) 2010-2011 Romain Bignon
#
# This file is part of weboob.
#
# weboob is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# weboob 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with weboob. If not, see <http://www.gnu.org/licenses/>.


from copy import deepcopy

from .config.yamlconfig import YamlConfig


class IStorage:
    def load(self, what, name, default={}):
        raise NotImplementedError()

    def save(self, what, name):
        raise NotImplementedError()

    def set(self, what, name, *args):
        raise NotImplementedError()

    def delete(self, what, name, *args):
        raise NotImplementedError()

    def get(self, what, name, *args, **kwargs):
        raise NotImplementedError()


class StandardStorage(IStorage):
    def __init__(self, path):
        self.config = YamlConfig(path)
        self.config.load()

    def load(self, what, name, default={}):
        d = {}
        if not what in self.config.values:
            self.config.values[what] = {}
        else:
            d = self.config.values[what].get(name, {})

        self.config.values[what][name] = deepcopy(default)
        self.config.values[what][name].update(d)

    def save(self, what, name):
        self.config.save()

    def set(self, what, name, *args):
        self.config.set(what, name, *args)

    def delete(self, what, name, *args):
        self.config.delete(what, name, *args)

    def get(self, what, name, *args, **kwargs):
        return self.config.get(what, name, *args, **kwargs)
