Commit 6791c338 authored by Edouard Lefebvre du Prey's avatar Edouard Lefebvre du Prey Committed by ntome

Add AnyDBMConfig, an implementation of IConfig for anydbm

parent 82fc1196
# -*- coding: utf-8 -*-
import anydbm
import yaml
from .iconfig import ConfigError, IConfig
try:
from yaml import CLoader as Loader
except ImportError:
from yaml import Loader
from .yamlconfig import WeboobDumper
__all__ = ['AnyDBMConfig']
class AnyDBMConfig(IConfig):
def __init__(self, path):
self.path = path
def load(self, default={}):
self.storage = anydbm.open(self.path, 'c')
def save(self):
pass
def get(self, *args, **kwargs):
key = '.'.join(args)
try:
value = self.storage[key]
value = yaml.load(value, Loader=Loader)
except KeyError:
if 'default' in kwargs:
value = kwargs.get('default')
else:
raise ConfigError()
except TypeError:
raise ConfigError()
return value
def set(self, *args):
key = '.'.join(args[:-1])
value = args[-1]
try:
self.storage[key] = yaml.dump(value, None, Dumper=WeboobDumper, default_flow_style=False)
except KeyError:
raise ConfigError()
except TypeError:
raise ConfigError()
def delete(self, *args):
key = '.'.join(args)
try:
del self.storage[key]
except KeyError:
raise ConfigError()
except TypeError:
raise ConfigError()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment