change '_backend' setting to '_module' (closes #789)

This commit is contained in:
Romain Bignon 2013-07-27 13:47:04 +02:00
commit fdcc4ec4b9

View file

@ -63,19 +63,25 @@ class BackendsConfig(object):
def iter_backends(self): def iter_backends(self):
config = RawConfigParser() config = RawConfigParser()
config.read(self.confpath) config.read(self.confpath)
for instance_name in config.sections(): changed = False
params = dict(config.items(instance_name)) for backend_name in config.sections():
params = dict(config.items(backend_name))
try: try:
backend_name = params.pop('_backend') module_name = params.pop('_module')
except KeyError: except KeyError:
try: try:
backend_name = params.pop('_type') module_name = params.pop('_backend')
warning(u'Please replace _type with _backend in your config file "%s", for backend "%s"' % ( config.set(backend_name, '_module', module_name)
self.confpath, backend_name)) config.remove_option(backend_name, '_backend')
changed = True
except KeyError: except KeyError:
warning('Missing field "_backend" for configured backend "%s"', instance_name) warning('Missing field "_module" for configured backend "%s"', backend_name)
continue continue
yield instance_name, backend_name, params yield backend_name, module_name, params
if changed:
with open(self.confpath, 'wb') as f:
config.write(f)
def backend_exists(self, name): def backend_exists(self, name):
""" """
@ -85,50 +91,50 @@ class BackendsConfig(object):
config.read(self.confpath) config.read(self.confpath)
return name in config.sections() return name in config.sections()
def add_backend(self, instance_name, backend_name, params, edit=False): def add_backend(self, backend_name, module_name, params, edit=False):
if not instance_name: if not backend_name:
raise ValueError(u'Please give a name to the configured backend.') raise ValueError(u'Please give a name to the configured backend.')
config = RawConfigParser() config = RawConfigParser()
config.read(self.confpath) config.read(self.confpath)
if not edit: if not edit:
try: try:
config.add_section(instance_name) config.add_section(backend_name)
except DuplicateSectionError: except DuplicateSectionError:
raise BackendAlreadyExists(instance_name) raise BackendAlreadyExists(backend_name)
config.set(instance_name, '_backend', backend_name) config.set(backend_name, '_module', module_name)
for key, value in params.iteritems(): for key, value in params.iteritems():
if isinstance(value, unicode): if isinstance(value, unicode):
value = value.encode('utf-8') value = value.encode('utf-8')
config.set(instance_name, key, value) config.set(backend_name, key, value)
with open(self.confpath, 'wb') as f: with open(self.confpath, 'wb') as f:
config.write(f) config.write(f)
def edit_backend(self, instance_name, backend_name, params): def edit_backend(self, backend_name, module_name, params):
return self.add_backend(instance_name, backend_name, params, True) return self.add_backend(backend_name, module_name, params, True)
def get_backend(self, instance_name): def get_backend(self, backend_name):
config = RawConfigParser() config = RawConfigParser()
config.read(self.confpath) config.read(self.confpath)
if not config.has_section(instance_name): if not config.has_section(backend_name):
raise KeyError(u'Configured backend "%s" not found' % instance_name) raise KeyError(u'Configured backend "%s" not found' % backend_name)
items = dict(config.items(instance_name)) items = dict(config.items(backend_name))
try: try:
backend_name = items.pop('_backend') module_name = items.pop('_module')
except KeyError: except KeyError:
try: try:
backend_name = items.pop('_type') module_name = items.pop('_backend')
warning(u'Please replace _type with _backend in your config file "%s"' % self.confpath) self.edit_backend(backend_name, module_name, items)
except KeyError: except KeyError:
warning('Missing field "_backend" for configured backend "%s"', instance_name) warning('Missing field "_module" for configured backend "%s"', backend_name)
raise KeyError(u'Configured backend "%s" not found' % instance_name) raise KeyError(u'Configured backend "%s" not found' % backend_name)
return backend_name, items return module_name, items
def remove_backend(self, instance_name): def remove_backend(self, backend_name):
config = RawConfigParser() config = RawConfigParser()
config.read(self.confpath) config.read(self.confpath)
if not config.remove_section(instance_name): if not config.remove_section(backend_name):
return False return False
with open(self.confpath, 'w') as f: with open(self.confpath, 'w') as f:
config.write(f) config.write(f)