jp (base): added a self.disp method which manage verbosity and stderr + verbosity property

This commit is contained in:
Goffi 2015-11-15 23:25:58 +01:00
commit 4233150d7b

View file

@ -23,9 +23,8 @@ global pbar_available
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 @@ from sat_frontends.jp.constants import Const as C
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 @@ class Jp(object):
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 @@ class Jp(object):
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 @@ class Jp(object):
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 @@ class Jp(object):
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 @@ class Jp(object):
# 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 @@ class Jp(object):
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 @@ class Jp(object):
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 @@ class CommandBase(object):
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