# HG changeset patch # User Goffi # Date 1447626358 -3600 # Node ID 0aded9648c5c17acdd9826cc965b8b6748958382 # Parent 9ac78437000dd2a4b873f1aea78f66f9a76bdc4a jp (base): added a self.disp method which manage verbosity and stderr + verbosity property diff -r 9ac78437000d -r 0aded9648c5c frontends/src/jp/base.py --- a/frontends/src/jp/base.py Sun Nov 15 23:16:54 2015 +0100 +++ b/frontends/src/jp/base.py Sun Nov 15 23:25:58 2015 +0100 @@ -23,9 +23,8 @@ pbar_available = True #checked before using ProgressBar ### logging ### -import logging -from logging import debug, info, error, warning -logging.basicConfig(level=logging.DEBUG, +import logging as log +log.basicConfig(level=log.DEBUG, format='%(message)s') ### @@ -44,8 +43,8 @@ try: import progressbar except ImportError: - info (_('ProgressBar not available, please download it at http://pypi.python.org/pypi/progressbar')) - info (_('Progress bar deactivated\n--\n')) + log.info (_('ProgressBar not available, please download it at http://pypi.python.org/pypi/progressbar')) + log.info (_('Progress bar deactivated\n--\n')) progressbar=None #consts @@ -107,6 +106,26 @@ self._progress_id = value + @property + def verbosity(self): + try: + return self.args.verbose + except AttributeError: + return 0 + + def disp(self, msg, verbosity=0, error=False): + """Print a message to user + + @param msg(unicode): message to print + @param verbosity(int): minimal verbosity to display the message + @param error(bool): if True, print to stderr instead of stdout + """ + if self.verbosity >= verbosity: + if error: + print >>sys.stderr,msg + else: + print msg + def addOnQuitCallback(self, callback, *args, **kwargs): """Add a callback which will be called on quit command @@ -171,11 +190,10 @@ for classname in module.__commands__: cls = getattr(module, classname) except AttributeError: - warning(_("Invalid module %s") % module) + log.warning(_("Invalid module %s") % module) raise ImportError cls(self) - def run(self, args=None): self.args = self.parser.parse_args(args) self.args.func() @@ -187,7 +205,7 @@ try: self.loop.run() except KeyboardInterrupt: - info(_("User interruption: good bye")) + log.info(_("User interruption: good bye")) def stop_loop(self): try: @@ -253,7 +271,7 @@ def check(jid): if not jid.is_valid: - error (_("%s is not a valid JID !"), jid) + log.error (_("%s is not a valid JID !"), jid) self.quit(1) dest_jids=[] @@ -277,17 +295,17 @@ # FIXME: need better exit codes def cant_connect(failure): - error(_(u"Can't connect profile: {reason}").format(reason=failure)) + log.error(_(u"Can't connect profile: {reason}").format(reason=failure)) self.quit(1) def cant_start_session(failure): - error(_(u"Can't start {profile}'s session: {reason}").format(profile=self.profile, reason=failure)) + log.error(_(u"Can't start {profile}'s session: {reason}").format(profile=self.profile, reason=failure)) self.quit(1) self.profile = self.bridge.getProfileName(self.args.profile) if not self.profile: - error(_("The profile [{profile}] doesn't exist").format(profile=self.args.profile)) + log.error(_("The profile [{profile}] doesn't exist").format(profile=self.args.profile)) self.quit(1) try: @@ -301,7 +319,7 @@ return elif not self.bridge.profileIsSessionStarted(self.profile): if not self.args.connect: - error(_(u"Session for [{profile}] is not started, please start it before using jp, or use either --start-session or --connect option").format(profile=self.profile)) + log.error(_(u"Session for [{profile}] is not started, please start it before using jp, or use either --start-session or --connect option").format(profile=self.profile)) self.quit(1) else: callback() @@ -317,7 +335,7 @@ return else: if not self.bridge.isConnected(self.profile): - error(_(u"Profile [{profile}] is not connected, please connect it before using jp, or use --connect option").format(profile=self.profile)) + log.error(_(u"Profile [{profile}] is not connected, please connect it before using jp, or use --connect option").format(profile=self.profile)) self.quit(1) callback() @@ -433,6 +451,9 @@ def progress_id(self, value): self.host.progress_id = value + def disp(self, msg, verbosity=0, error=False): + return self.host.disp(msg, verbosity, error) + def add_parser_options(self): try: subcommands = self.subcommands