if backend name is already taken, suggest the first available one with the same base

This commit is contained in:
Romain Bignon 2015-04-04 12:48:06 +02:00
commit b110f10c9e

View file

@ -355,20 +355,23 @@ class ConsoleApplication(Application):
if asked_config: if asked_config:
print('-------------------------%s' % ('-' * len(module.name))) print('-------------------------%s' % ('-' * len(module.name)))
i = 2
while not edit and self.weboob.backends_config.backend_exists(backend_name): while not edit and self.weboob.backends_config.backend_exists(backend_name):
print('Backend instance "%s" already exists in "%s"' % (backend_name, self.weboob.backends_config.confpath), file=self.stderr) if not self.ask('Backend "%s" already exists. Add a new one for module %s?' % (backend_name, module.name), default=False):
if not self.ask('Add new backend for module "%s"?' % module.name, default=False):
return 1 return 1
# TODO : Probably smarter to increment max existing backend, instead of fixed suffix backend_name = backend_name.rstrip('0123456789')
backend_name = self.ask('Please give new instance name', default='%s2' % module_name, regexp=r'^[\w\-_]+$') while self.weboob.backends_config.backend_exists('%s%s' % (backend_name, i)):
i += 1
backend_name = self.ask('Please give new instance name', default='%s%s' % (backend_name, i), regexp=r'^[\w\-_]+$')
try: try:
config = config.load(self.weboob, module.name, backend_name, params, nofail=True) config = config.load(self.weboob, module.name, backend_name, params, nofail=True)
for key, value in params.iteritems(): for key, value in params.iteritems():
if key.startswith('_'): if key not in config:
continue continue
config[key].set(value) config[key].set(value)
config.save(edit=edit) config.save(edit=edit)
print('Backend "%s" successfully %s.' % (backend_name, 'edited' if edit else 'added')) print('Backend "%s" successfully %s.' % (backend_name, 'edited' if edit else 'added'))
return backend_name return backend_name