From fad7d2d56ce2f05d11f8f65e48ebe972f0763f65 Mon Sep 17 00:00:00 2001 From: Christophe Benz Date: Fri, 21 May 2010 19:18:46 +0200 Subject: [PATCH] add ability to add application specific options --- weboob/frontends/videoob/application.py | 5 ++--- weboob/tools/application/base.py | 22 ++++++++++++++-------- weboob/tools/application/console.py | 24 +++++++++++++++++------- 3 files changed, 33 insertions(+), 18 deletions(-) diff --git a/weboob/frontends/videoob/application.py b/weboob/frontends/videoob/application.py index 1d2d17a9..27d4a8e1 100644 --- a/weboob/frontends/videoob/application.py +++ b/weboob/frontends/videoob/application.py @@ -31,9 +31,8 @@ class Videoob(ConsoleApplication): COPYRIGHT = 'Copyright(C) 2010 Christophe Benz, Romain Bignon' CONFIG = {} - def __init__(self): - ConsoleApplication.__init__(self) - self._parser.add_option('--nsfw', action='store_true', help='enable non-suitable for work videos') + def add_application_options(self, group): + group.add_option('--nsfw', action='store_true', help='enable non-suitable for work videos') def main(self, argv): self.load_modules(ICapVideoProvider) diff --git a/weboob/tools/application/base.py b/weboob/tools/application/base.py index c0c7602c..f6ba9f10 100644 --- a/weboob/tools/application/base.py +++ b/weboob/tools/application/base.py @@ -44,16 +44,13 @@ class BaseApplication(object): # Copyright COPYRIGHT = None - def __init__(self): + def __init__(self, option_parser=None): self.weboob = self.create_weboob() self.config = None - version = None - if self.VERSION: - if self.COPYRIGHT: - version = '%s v%s (%s)' % (self.APPNAME, self.VERSION, self.COPYRIGHT) - else: - version = '%s v%s' % (self.APPNAME, self.VERSION) - self._parser = OptionParser(self.SYNOPSIS, version=version) + if option_parser is None: + self._parser = OptionParser(self.SYNOPSIS, version=self._get_optparse_version()) + else: + self._parser = option_parser self._parser.add_option('-b', '--backends', help='what backend(s) to enable (comma separated)') logging_options = OptionGroup(self._parser, 'Logging Options') logging_options.add_option('-d', '--debug', action='store_true', help='display debug messages') @@ -131,6 +128,15 @@ class BaseApplication(object): """ return set() + def _get_optparse_version(self): + version = None + if self.VERSION: + if self.COPYRIGHT: + version = '%s v%s (%s)' % (self.APPNAME, self.VERSION, self.COPYRIGHT) + else: + version = '%s v%s' % (self.APPNAME, self.VERSION) + return version + def _handle_app_options(self): """ Overload this method in subclasses if you want to handle options defined in subclass constructor. diff --git a/weboob/tools/application/console.py b/weboob/tools/application/console.py index eab06c73..9c40dffd 100644 --- a/weboob/tools/application/console.py +++ b/weboob/tools/application/console.py @@ -19,6 +19,7 @@ from functools import partial import getpass from inspect import getargspec import logging +from optparse import OptionGroup, OptionParser import re import sys @@ -26,8 +27,8 @@ import weboob from weboob.modules import BackendsConfig from .base import BaseApplication -from .formatters import formatters_classes -from .results import Results, WhereCondition, WhereConditionException +from .formatters import formatters +from .results import Results, ResultsCondition, ResultsConditionException __all__ = ['ConsoleApplication'] @@ -37,8 +38,13 @@ class ConsoleApplication(BaseApplication): SYNOPSIS = 'Usage: %prog [options (-h for help)] command [parameters...]' def __init__(self): + option_parser = OptionParser(self.SYNOPSIS, version=self._get_optparse_version()) + app_options = OptionGroup(option_parser, '%s Options' % self.APPNAME.capitalize()) + self.add_application_options(app_options) + option_parser.add_option_group(app_options) + try: - BaseApplication.__init__(self) + BaseApplication.__init__(self, option_parser=option_parser) except BackendsConfig.WrongPermissions, e: logging.error(u'Error: %s' % e) sys.exit(1) @@ -52,11 +58,15 @@ class ConsoleApplication(BaseApplication): command = '%s %s' % (name, arguments) self._parser.description += ' %-30s %s\n' % (command, doc_string) - self._parser.add_option('-f', '--formatter', default='simple', choices=formatters_classes.keys(), - help='select output formatter (%s)' % u','.join(formatters_classes.keys())) - self._parser.add_option('-s', '--select', help='select result item key(s) to display (comma-separated)') - self._parser.add_option('-w', '--where', help='filter results to display with boolean condition') + results_options = OptionGroup(self._parser, 'Results Options') + results_options.add_option('-c', '--condition', help='filter result items to display given a boolean condition') + results_options.add_option('-f', '--formatter', default='multiline', choices=formatters.keys(), + help='select output formatter (%s)' % u','.join(formatters.keys())) + results_options.add_option('-s', '--select', help='select result item key(s) to display (comma-separated)') + self._parser.add_option_group(results_options) + def add_application_options(self, group): + pass def _handle_app_options(self): self._formatter = formatters_classes[self.options.formatter]