BackendsConfig methods: edit_backend() and get_backend()

This commit is contained in:
Romain Bignon 2010-06-21 22:59:44 +02:00
commit 39f5deb7db

View file

@ -105,11 +105,12 @@ class BackendsConfig(object):
warning('Missing field "_type" for backend "%s"', name) warning('Missing field "_type" for backend "%s"', name)
continue continue
def add_backend(self, name, _type, params): def add_backend(self, name, _type, params, edit=False):
if not name: if not name:
raise ValueError(u'Please give a name to the backend.') raise ValueError(u'Please give a name to the backend.')
config = SafeConfigParser() config = SafeConfigParser()
config.read(self.confpath) config.read(self.confpath)
if not edit:
config.add_section(name) config.add_section(name)
config.set(name, '_type', _type) config.set(name, '_type', _type)
for key, value in params.iteritems(): for key, value in params.iteritems():
@ -117,6 +118,22 @@ class BackendsConfig(object):
with open(self.confpath, 'wb') as f: with open(self.confpath, 'wb') as f:
config.write(f) config.write(f)
def edit_backend(self, name, _type, params):
return self.add_backend(name, _type, params, True)
def get_backend(self, name):
config = SafeConfigParser()
config.read(self.confpath)
if not config.has_section(name):
raise KeyError(u'Backend "%s" not found' % name)
items = dict(config.items(name, raw=True))
try:
return items.pop('_type'), items
except KeyError:
warning('Missing field "_type" for backend "%s"', name)
raise KeyError(u'Backend "%s" not found' % name)
def remove_backend(self, name): def remove_backend(self, name):
config = SafeConfigParser() config = SafeConfigParser()
config.read(self.confpath) config.read(self.confpath)