comparison frontends/src/jp/base.py @ 1863:b2ddd7f5dcdf

jp (base): refactored need_loop so it is set only when the command is run. It can now be set in __init__ methods of commands classes
author Goffi <goffi@goffi.org>
date Mon, 29 Feb 2016 16:52:51 +0100
parents 6d9c87bdc452
children 9d9d61d53684
comparison
equal deleted inserted replaced
1862:6d9c87bdc452 1863:b2ddd7f5dcdf
74 74
75 """ 75 """
76 def __init__(self): 76 def __init__(self):
77 """ 77 """
78 78
79 @attribute need_loop(bool): to set by commands when loop is needed
80 @attribute quit_on_progress_end (bool): set to False if you manage yourself exiting, 79 @attribute quit_on_progress_end (bool): set to False if you manage yourself exiting,
81 or if you want the user to stop by himself 80 or if you want the user to stop by himself
82 @attribute progress_success(callable): method to call when progress just started 81 @attribute progress_success(callable): method to call when progress just started
83 by default display a message 82 by default display a message
84 @attribute progress_success(callable): method to call when progress is successfully finished 83 @attribute progress_success(callable): method to call when progress is successfully finished
100 99
101 self._make_parents() 100 self._make_parents()
102 self.add_parser_options() 101 self.add_parser_options()
103 self.subparsers = self.parser.add_subparsers(title=_('Available commands'), dest='subparser_name') 102 self.subparsers = self.parser.add_subparsers(title=_('Available commands'), dest='subparser_name')
104 self._auto_loop = False # when loop is used for internal reasons 103 self._auto_loop = False # when loop is used for internal reasons
105 self.need_loop = False # to set by commands when loop is needed 104 self._need_loop = False
106 105
107 # progress attributes 106 # progress attributes
108 self._progress_id = None # TODO: manage several progress ids 107 self._progress_id = None # TODO: manage several progress ids
109 self.quit_on_progress_end = True 108 self.quit_on_progress_end = True
110 109
240 cls(self) 239 cls(self)
241 240
242 def run(self, args=None): 241 def run(self, args=None):
243 self.args = self.parser.parse_args(args) 242 self.args = self.parser.parse_args(args)
244 self.args.func() 243 self.args.func()
245 if self.need_loop or self._auto_loop: 244 if self._need_loop or self._auto_loop:
246 self._start_loop() 245 self._start_loop()
247 246
248 def _start_loop(self): 247 def _start_loop(self):
249 self.loop = GLib.MainLoop() 248 self.loop = GLib.MainLoop()
250 try: 249 try:
261 def quitFromSignal(self, errcode=0): 260 def quitFromSignal(self, errcode=0):
262 """Same as self.quit, but from a signal handler 261 """Same as self.quit, but from a signal handler
263 262
264 /!\: return must be used after calling this method ! 263 /!\: return must be used after calling this method !
265 """ 264 """
266 assert self.need_loop 265 assert self._need_loop
267 # XXX: python-dbus will show a traceback if we exit in a signal handler with an error code 266 # XXX: python-dbus will show a traceback if we exit in a signal handler with an error code
268 # so we use this little timeout trick to avoid it 267 # so we use this little timeout trick to avoid it
269 GLib.timeout_add(0, self.quit, errcode) 268 GLib.timeout_add(0, self.quit, errcode)
270 269
271 def quit(self, errcode=0): 270 def quit(self, errcode=0):
410 False else (profile session must still be started) 409 False else (profile session must still be started)
411 None to set auto value (i.e. True if use_profile is set) 410 None to set auto value (i.e. True if use_profile is set)
412 Can't be set if use_profile is False 411 Can't be set if use_profile is False
413 @param help(unicode): help message to display 412 @param help(unicode): help message to display
414 @param **kwargs: args passed to ArgumentParser 413 @param **kwargs: args passed to ArgumentParser
415 414 @attribute need_loop(bool): to set by commands when loop is needed
416 """ 415
416 """
417 self.need_loop = False # to be set by commands when loop is needed
417 try: # If we have subcommands, host is a CommandBase and we need to use host.host 418 try: # If we have subcommands, host is a CommandBase and we need to use host.host
418 self.host = host.host 419 self.host = host.host
419 except AttributeError: 420 except AttributeError:
420 self.host = host 421 self.host = host
421 422
445 self.add_parser_options() 446 self.add_parser_options()
446 447
447 @property 448 @property
448 def args(self): 449 def args(self):
449 return self.host.args 450 return self.host.args
450
451 @property
452 def need_loop(self):
453 return self.host.need_loop
454
455 @need_loop.setter
456 def need_loop(self, value):
457 self.host.need_loop = value
458 451
459 @property 452 @property
460 def profile(self): 453 def profile(self):
461 return self.host.profile 454 return self.host.profile
462 455
586 """this method is called when a command is actually run 579 """this method is called when a command is actually run
587 580
588 It set stuff like progression callbacks and profile connection 581 It set stuff like progression callbacks and profile connection
589 You should not overide this method: you should call self.start instead 582 You should not overide this method: you should call self.start instead
590 """ 583 """
584 # host._need_loop is set here from our current value and not before
585 # as the need_loop decision must be taken only by then running command
586 self.host._need_loop = self.need_loop
587
591 try: 588 try:
592 show_progress = self.args.progress 589 show_progress = self.args.progress
593 except AttributeError: 590 except AttributeError:
594 # the command doesn't use progress bar 591 # the command doesn't use progress bar
595 pass 592 pass