add weboobcfg edit command

This commit is contained in:
Christophe Benz 2010-05-19 11:54:30 +02:00
commit d3cb59b86a

View file

@ -1,26 +1,24 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
""" # Copyright(C) 2010 Romain Bignon, Christophe Benz
Copyright(C) 2010 Romain Bignon #
# This program is free software; you can redistribute it and/or modify
This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by
it under the terms of the GNU General Public License as published by # the Free Software Foundation, version 3 of the License.
the Free Software Foundation, version 3 of the License. #
# This program is distributed in the hope that it will be useful,
This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of
but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details.
GNU General Public License for more details. #
# You should have received a copy of the GNU General Public License
You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software
along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
"""
import ConfigParser import ConfigParser
import logging import logging
import os import os
import subprocess
import weboob import weboob
from weboob.tools.application import ConsoleApplication from weboob.tools.application import ConsoleApplication
@ -97,7 +95,7 @@ class WeboobCfg(ConsoleApplication):
print "'-----------------' " print "'-----------------' "
@ConsoleApplication.command('Add a backend') @ConsoleApplication.command('Add a configured backend')
def command_add(self, name, *options): def command_add(self, name, *options):
self.weboob.modules_loader.load() self.weboob.modules_loader.load()
if name not in [module_name for module_name, module in self.weboob.modules_loader.modules.iteritems()]: if name not in [module_name for module_name, module in self.weboob.modules_loader.modules.iteritems()]:
@ -105,24 +103,28 @@ class WeboobCfg(ConsoleApplication):
return 1 return 1
params = {} params = {}
# set default configuration with empty values # set backend params from command-line arguments
module = self.weboob.modules_loader.get_or_load_module(name) for option in options:
for key, field in module.get_config().iteritems():
params[key] = u''
for param in options:
try: try:
key, value = param.split('=', 1) key, value = option.split('=', 1)
except ValueError: except ValueError:
logging.error("Parameters have to be in form 'key=value'") logging.error(u'Parameters have to be formatted "key=value"')
return 1 return 1
params[key] = value params[key] = value
# ask for params non-specified on command-line arguments
module = self.weboob.modules_loader.get_or_load_module(name)
for key, value in module.get_config().iteritems():
if key not in params:
masked = key == 'password'
params[key] = self.ask(key, default=u'', masked=masked)
try: try:
self.weboob.backends_config.add_backend(name, name, params) self.weboob.backends_config.add_backend(name, name, params)
print u'Backend "%s" successfully added to file %s. Please check configuration parameters values.' % (name, self.weboob.backends_config.confpath) print u'Backend "%s" successfully added to file "%s".\n'\
except ConfigParser.DuplicateSectionError, e: 'Please check configuration parameters values with "weboobcfg edit".' % (
print u'Backend "%s" is already configured in file %s' % (name, self.weboob.backends_config.confpath) name, self.weboob.backends_config.confpath)
except ConfigParser.DuplicateSectionError:
print u'Backend "%s" is already configured in file "%s"' % (name, self.weboob.backends_config.confpath)
response = raw_input(u'Add new instance of "%s" backend ? [yN] ' % name) response = raw_input(u'Add new instance of "%s" backend ? [yN] ' % name)
if response.lower() == 'y': if response.lower() == 'y':
while True: while True:
@ -131,8 +133,9 @@ class WeboobCfg(ConsoleApplication):
continue continue
try: try:
self.weboob.backends_config.add_backend(new_name, name, params) self.weboob.backends_config.add_backend(new_name, name, params)
print u'Backend "%s" successfully added under instance name "%s" to file %s. Please check configuration parameters values.' % ( print u'Backend "%s" successfully added to file "%s".\n'\
name, new_name, self.weboob.backends_config.confpath) 'Please check configuration parameters values with "weboobcfg edit".' % (
name, self.weboob.backends_config.confpath)
break break
except ConfigParser.DuplicateSectionError: except ConfigParser.DuplicateSectionError:
print u'Instance "%s" is already configured for backend "%s".' % (new_name, name) print u'Instance "%s" is already configured for backend "%s".' % (new_name, name)
@ -151,3 +154,7 @@ class WeboobCfg(ConsoleApplication):
except ConfigParser.NoSectionError: except ConfigParser.NoSectionError:
logging.error("Backend '%s' does not exist" % instance_name) logging.error("Backend '%s' does not exist" % instance_name)
return 1 return 1
@ConsoleApplication.command('Edit configuration file')
def command_edit(self):
subprocess.call([os.environ.get('EDITOR', 'vi'), self.weboob.backends_config.confpath])