comparison frontends/src/jp/base.py @ 1605:0aded9648c5c

jp (base): added a self.disp method which manage verbosity and stderr + verbosity property
author Goffi <goffi@goffi.org>
date Sun, 15 Nov 2015 23:25:58 +0100
parents 9ac78437000d
children de785fcf9a7b
comparison
equal deleted inserted replaced
1604:9ac78437000d 1605:0aded9648c5c
21 21
22 global pbar_available 22 global pbar_available
23 pbar_available = True #checked before using ProgressBar 23 pbar_available = True #checked before using ProgressBar
24 24
25 ### logging ### 25 ### logging ###
26 import logging 26 import logging as log
27 from logging import debug, info, error, warning 27 log.basicConfig(level=log.DEBUG,
28 logging.basicConfig(level=logging.DEBUG,
29 format='%(message)s') 28 format='%(message)s')
30 ### 29 ###
31 30
32 import sys 31 import sys
33 import locale 32 import locale
42 import sat_frontends.jp 41 import sat_frontends.jp
43 from sat_frontends.jp.constants import Const as C 42 from sat_frontends.jp.constants import Const as C
44 try: 43 try:
45 import progressbar 44 import progressbar
46 except ImportError: 45 except ImportError:
47 info (_('ProgressBar not available, please download it at http://pypi.python.org/pypi/progressbar')) 46 log.info (_('ProgressBar not available, please download it at http://pypi.python.org/pypi/progressbar'))
48 info (_('Progress bar deactivated\n--\n')) 47 log.info (_('Progress bar deactivated\n--\n'))
49 progressbar=None 48 progressbar=None
50 49
51 #consts 50 #consts
52 prog_name = u"jp" 51 prog_name = u"jp"
53 description = """This software is a command line tool for XMPP. 52 description = """This software is a command line tool for XMPP.
104 103
105 @progress_id.setter 104 @progress_id.setter
106 def progress_id(self, value): 105 def progress_id(self, value):
107 self._progress_id = value 106 self._progress_id = value
108 107
108
109 @property
110 def verbosity(self):
111 try:
112 return self.args.verbose
113 except AttributeError:
114 return 0
115
116 def disp(self, msg, verbosity=0, error=False):
117 """Print a message to user
118
119 @param msg(unicode): message to print
120 @param verbosity(int): minimal verbosity to display the message
121 @param error(bool): if True, print to stderr instead of stdout
122 """
123 if self.verbosity >= verbosity:
124 if error:
125 print >>sys.stderr,msg
126 else:
127 print msg
109 128
110 def addOnQuitCallback(self, callback, *args, **kwargs): 129 def addOnQuitCallback(self, callback, *args, **kwargs):
111 """Add a callback which will be called on quit command 130 """Add a callback which will be called on quit command
112 131
113 @param callback(callback): method to call 132 @param callback(callback): method to call
169 """ 188 """
170 try: 189 try:
171 for classname in module.__commands__: 190 for classname in module.__commands__:
172 cls = getattr(module, classname) 191 cls = getattr(module, classname)
173 except AttributeError: 192 except AttributeError:
174 warning(_("Invalid module %s") % module) 193 log.warning(_("Invalid module %s") % module)
175 raise ImportError 194 raise ImportError
176 cls(self) 195 cls(self)
177
178 196
179 def run(self, args=None): 197 def run(self, args=None):
180 self.args = self.parser.parse_args(args) 198 self.args = self.parser.parse_args(args)
181 self.args.func() 199 self.args.func()
182 if self.need_loop or self._auto_loop: 200 if self.need_loop or self._auto_loop:
185 def _start_loop(self): 203 def _start_loop(self):
186 self.loop = GLib.MainLoop() 204 self.loop = GLib.MainLoop()
187 try: 205 try:
188 self.loop.run() 206 self.loop.run()
189 except KeyboardInterrupt: 207 except KeyboardInterrupt:
190 info(_("User interruption: good bye")) 208 log.info(_("User interruption: good bye"))
191 209
192 def stop_loop(self): 210 def stop_loop(self):
193 try: 211 try:
194 self.loop.quit() 212 self.loop.quit()
195 except AttributeError: 213 except AttributeError:
251 expanded = jid 269 expanded = jid
252 return expanded.decode('utf-8') 270 return expanded.decode('utf-8')
253 271
254 def check(jid): 272 def check(jid):
255 if not jid.is_valid: 273 if not jid.is_valid:
256 error (_("%s is not a valid JID !"), jid) 274 log.error (_("%s is not a valid JID !"), jid)
257 self.quit(1) 275 self.quit(1)
258 276
259 dest_jids=[] 277 dest_jids=[]
260 try: 278 try:
261 for i in range(len(jids)): 279 for i in range(len(jids)):
275 - 1 when there is a connection error 293 - 1 when there is a connection error
276 """ 294 """
277 # FIXME: need better exit codes 295 # FIXME: need better exit codes
278 296
279 def cant_connect(failure): 297 def cant_connect(failure):
280 error(_(u"Can't connect profile: {reason}").format(reason=failure)) 298 log.error(_(u"Can't connect profile: {reason}").format(reason=failure))
281 self.quit(1) 299 self.quit(1)
282 300
283 def cant_start_session(failure): 301 def cant_start_session(failure):
284 error(_(u"Can't start {profile}'s session: {reason}").format(profile=self.profile, reason=failure)) 302 log.error(_(u"Can't start {profile}'s session: {reason}").format(profile=self.profile, reason=failure))
285 self.quit(1) 303 self.quit(1)
286 304
287 self.profile = self.bridge.getProfileName(self.args.profile) 305 self.profile = self.bridge.getProfileName(self.args.profile)
288 306
289 if not self.profile: 307 if not self.profile:
290 error(_("The profile [{profile}] doesn't exist").format(profile=self.args.profile)) 308 log.error(_("The profile [{profile}] doesn't exist").format(profile=self.args.profile))
291 self.quit(1) 309 self.quit(1)
292 310
293 try: 311 try:
294 start_session = self.args.start_session 312 start_session = self.args.start_session
295 except AttributeError: 313 except AttributeError:
299 self.bridge.profileStartSession(self.args.pwd, self.profile, lambda dummy: callback(), cant_start_session) 317 self.bridge.profileStartSession(self.args.pwd, self.profile, lambda dummy: callback(), cant_start_session)
300 self._auto_loop = True 318 self._auto_loop = True
301 return 319 return
302 elif not self.bridge.profileIsSessionStarted(self.profile): 320 elif not self.bridge.profileIsSessionStarted(self.profile):
303 if not self.args.connect: 321 if not self.args.connect:
304 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)) 322 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))
305 self.quit(1) 323 self.quit(1)
306 else: 324 else:
307 callback() 325 callback()
308 return 326 return
309 327
315 self.bridge.asyncConnect(self.profile, self.args.pwd, lambda dummy: callback(), cant_connect) 333 self.bridge.asyncConnect(self.profile, self.args.pwd, lambda dummy: callback(), cant_connect)
316 self._auto_loop = True 334 self._auto_loop = True
317 return 335 return
318 else: 336 else:
319 if not self.bridge.isConnected(self.profile): 337 if not self.bridge.isConnected(self.profile):
320 error(_(u"Profile [{profile}] is not connected, please connect it before using jp, or use --connect option").format(profile=self.profile)) 338 log.error(_(u"Profile [{profile}] is not connected, please connect it before using jp, or use --connect option").format(profile=self.profile))
321 self.quit(1) 339 self.quit(1)
322 340
323 callback() 341 callback()
324 342
325 def get_full_jid(self, param_jid): 343 def get_full_jid(self, param_jid):
431 449
432 @progress_id.setter 450 @progress_id.setter
433 def progress_id(self, value): 451 def progress_id(self, value):
434 self.host.progress_id = value 452 self.host.progress_id = value
435 453
454 def disp(self, msg, verbosity=0, error=False):
455 return self.host.disp(msg, verbosity, error)
456
436 def add_parser_options(self): 457 def add_parser_options(self):
437 try: 458 try:
438 subcommands = self.subcommands 459 subcommands = self.subcommands
439 except AttributeError: 460 except AttributeError:
440 # We don't have subcommands, the class need to implements add_parser_options 461 # We don't have subcommands, the class need to implements add_parser_options