diff --git a/scripts/monboob b/scripts/monboob index 5cf92671..ebbecedb 100755 --- a/scripts/monboob +++ b/scripts/monboob @@ -35,6 +35,8 @@ from weboob.tools.misc import html2text class Monboob(ConsoleApplication): APPNAME = 'monboob' + VERSION = '1.0' + COPYRIGHT = 'Copyright(C) 2010 Romain Bignon' CONFIG = {'interval': 15, 'domain': 'weboob.example.org', 'recipient': 'weboob@example.org', diff --git a/weboob/frontends/boobank/boobank.py b/weboob/frontends/boobank/boobank.py index b07da453..6cb5f843 100644 --- a/weboob/frontends/boobank/boobank.py +++ b/weboob/frontends/boobank/boobank.py @@ -34,6 +34,8 @@ __all__ = ['Boobank'] class Boobank(ConsoleApplication): APPNAME = 'boobank' + VERSION = '1.0' + COPYRIGHT = 'Copyright(C) 2010 Romain Bignon' def main(self, argv): self.load_backends(ICapBank, names=self.enabled_backends) diff --git a/weboob/frontends/havesex/application.py b/weboob/frontends/havesex/application.py index 890722b4..3afc34d7 100644 --- a/weboob/frontends/havesex/application.py +++ b/weboob/frontends/havesex/application.py @@ -23,6 +23,8 @@ from weboob.capabilities.dating import ICapDating class HaveSex(PromptApplication): APPNAME = 'havesex' + VERSION = '1.0' + COPYRIGHT = 'Copyright(C) 2010 Romain Bignon' STORAGE_FILENAME = 'dating.storage' def main(self, argv): diff --git a/weboob/frontends/qvideoob/application.py b/weboob/frontends/qvideoob/application.py index 91c70a55..06988d93 100644 --- a/weboob/frontends/qvideoob/application.py +++ b/weboob/frontends/qvideoob/application.py @@ -25,6 +25,8 @@ from .main_window import MainWindow class QVideoob(QtApplication): APPNAME = 'qvideoob' + VERSION = '1.0' + COPYRIGHT = 'Copyright(C) 2010 Romain Bignon' CONFIG = {'settings': {'nsfw': True, 'sortby': 0, 'backend': '' diff --git a/weboob/frontends/travel/application.py b/weboob/frontends/travel/application.py index 7a1182a5..76e08246 100644 --- a/weboob/frontends/travel/application.py +++ b/weboob/frontends/travel/application.py @@ -23,6 +23,8 @@ from weboob.tools.application import ConsoleApplication class Travel(ConsoleApplication): APPNAME = 'travel' + VERSION = '1.0' + COPYRIGHT = 'Copyright(C) 2010 Romain Bignon' def main(self, argv): self.load_modules(ICapTravel) diff --git a/weboob/frontends/travel_ui/application.py b/weboob/frontends/travel_ui/application.py index 2438d1b6..8511aeee 100644 --- a/weboob/frontends/travel_ui/application.py +++ b/weboob/frontends/travel_ui/application.py @@ -156,6 +156,8 @@ class TransilienUI(): class Travel(BaseApplication): "Application Class" APPNAME = 'travel' + VERSION = '1.0' + COPYRIGHT = 'Copyright(C) 2010 Julien Hébert' def main(self, argv): "main fonction" diff --git a/weboob/frontends/videoob/application.py b/weboob/frontends/videoob/application.py index c1449526..c1ffa403 100644 --- a/weboob/frontends/videoob/application.py +++ b/weboob/frontends/videoob/application.py @@ -23,6 +23,8 @@ from weboob.tools.application import ConsoleApplication class Videoob(ConsoleApplication): APPNAME = 'videoob' + VERSION = '1.0' + COPYRIGHT = 'Copyright(C) 2010 Christophe Benz, Romain Bignon' CONFIG = {} def main(self, argv): diff --git a/weboob/frontends/weboobcfg/application.py b/weboob/frontends/weboobcfg/application.py index e2604906..e0de8a58 100644 --- a/weboob/frontends/weboobcfg/application.py +++ b/weboob/frontends/weboobcfg/application.py @@ -25,6 +25,8 @@ from weboob.tools.application import ConsoleApplication class WeboobCfg(ConsoleApplication): APPNAME = 'weboobcfg' + VERSION = '1.0' + COPYRIGHT = 'Copyright(C) 2010 Romain Bignon' def main(self, argv): return self.process_command(*argv[1:]) diff --git a/weboob/frontends/wetboobs/application.py b/weboob/frontends/wetboobs/application.py index a65fc8cc..ffc24e4a 100644 --- a/weboob/frontends/wetboobs/application.py +++ b/weboob/frontends/wetboobs/application.py @@ -23,6 +23,8 @@ from weboob.tools.application import ConsoleApplication class WetBoobs(ConsoleApplication): APPNAME = 'wetboobs' + VERSION = '1.0' + COPYRIGHT = 'Copyright(C) 2010 Romain Bignon' def main(self, argv): self.load_modules(ICapWeather) diff --git a/weboob/tools/application/base.py b/weboob/tools/application/base.py index 870325fd..6636734a 100644 --- a/weboob/tools/application/base.py +++ b/weboob/tools/application/base.py @@ -36,6 +36,12 @@ class BaseApplication(object): CONFIG = {} # Configuration directory CONFDIR = os.path.join(os.path.expanduser('~'), '.weboob') + # Synopsis + SYNOPSIS = 'Usage: %prog [options (-h for help)] ...' + # Version + VERSION = None + # Copyright + COPYRIGHT = None def __init__(self): self.weboob = self.create_weboob() @@ -119,7 +125,13 @@ class BaseApplication(object): @classmethod def run(klass): app = klass() - parser = OptionParser('Usage: %prog [options (-h for help)] URL...') + version = None + if app.VERSION: + if app.COPYRIGHT: + version = '%s v%s (%s)' % (app.APPNAME, app.VERSION, app.COPYRIGHT) + else: + version = '%s v%s' % (app.APPNAME, app.VERSION) + parser = OptionParser(app.SYNOPSIS, version=version) parser.add_option('-b', '--backends', help='what backend(s) to enable (comma separated)') parser.add_option('-d', '--debug', action='store_true', help='display debug messages') parser.add_option('-q', '--quiet', action='store_true', help='display only error messages') diff --git a/weboob/tools/application/console.py b/weboob/tools/application/console.py index 42b18a4f..a2b5cb06 100644 --- a/weboob/tools/application/console.py +++ b/weboob/tools/application/console.py @@ -87,12 +87,14 @@ class TextFormatter(object): return unicode(formatted).strip() -formatters = dict(text=TextFormatter, - table=TableFormatter, - ) +formatters = {'text': TextFormatter, + 'table': TableFormatter, + } class ConsoleApplication(BaseApplication): + SYNOPSIS = 'Usage: %prog [options (-h for help)] command [parameters...]' + def __init__(self): try: BaseApplication.__init__(self) @@ -149,40 +151,45 @@ class ConsoleApplication(BaseApplication): if len(matching_commands) == 0: sys.stderr.write("No such command: %s.\n" % command) - elif len(matching_commands) == 1: - func = getattr(self, matching_commands[0]) - - _args, varargs, varkw, defaults = getargspec(func) - nb_max_args = nb_min_args = len(_args) - 1 - if defaults: - nb_min_args -= len(defaults) - - if len(args) < nb_min_args or len(args) > nb_max_args and not varargs: - if varargs or defaults: - sys.stderr.write("Command '%s' takes at least %d arguments.\n" % (command, nb_min_args)) - else: - sys.stderr.write("Command '%s' takes %d arguments.\n" % (command, nb_min_args)) - return - command_result = func(*args) - if isinstance(command_result, dict): - if self.options.output_format is not None: - output_format = self.options.output_format - else: - if self.default_output_format is not None: - output_format = self.default_output_format - else: - output_format = 'table' - print formatters[output_format].format(command_result) - return 0 - elif isinstance(command_result, int): - return command_result - elif command_result is None: - return 0 - else: - raise Exception('Should never go here') - else: + return 1 + if len(matching_commands) != 1: sys.stderr.write("Ambiguious command %s: %s.\n" % (command, ', '.join( [s.replace('command_', '', 1) for s in matching_commands]))) + return 1 + + func = getattr(self, matching_commands[0]) + + _args, varargs, varkw, defaults = getargspec(func) + nb_max_args = nb_min_args = len(_args) - 1 + if defaults: + nb_min_args -= len(defaults) + + if len(args) < nb_min_args or len(args) > nb_max_args and not varargs: + if varargs or defaults: + sys.stderr.write("Command '%s' takes at least %d arguments.\n" % (command, nb_min_args)) + else: + sys.stderr.write("Command '%s' takes %d arguments.\n" % (command, nb_min_args)) + return 1 + + command_result = func(*args) + + # Process result + if isinstance(command_result, dict): + if self.options.output_format is not None: + output_format = self.options.output_format + else: + if self.default_output_format is not None: + output_format = self.default_output_format + else: + output_format = 'table' + print formatters[output_format].format(command_result) + return 0 + elif isinstance(command_result, int): + return command_result + elif command_result is None: + return 0 + else: + raise Exception('Should never go here') _command_help = [] def register_command(f, doc_string, register_to=_command_help):