annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
goffi@necton2
parents:
diff changeset
1 #! /usr/bin/python
goffi@necton2
parents:
diff changeset
2 # -*- coding: utf-8 -*-
goffi@necton2
parents:
diff changeset
3
609
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 601
diff changeset
4 # jp: a SAT command line tool
1396
069ad98b360d 2015 copyright dates update
Goffi <goffi@goffi.org>
parents: 1395
diff changeset
5 # Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014, 2015 Jérôme Poisson (goffi@goffi.org)
0
goffi@necton2
parents:
diff changeset
6
609
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 601
diff changeset
7 # This program is free software: you can redistribute it and/or modify
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 601
diff changeset
8 # it under the terms of the GNU Affero General Public License as published by
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 601
diff changeset
9 # the Free Software Foundation, either version 3 of the License, or
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 601
diff changeset
10 # (at your option) any later version.
0
goffi@necton2
parents:
diff changeset
11
609
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 601
diff changeset
12 # This program is distributed in the hope that it will be useful,
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 601
diff changeset
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 601
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 601
diff changeset
15 # GNU Affero General Public License for more details.
0
goffi@necton2
parents:
diff changeset
16
609
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 601
diff changeset
17 # You should have received a copy of the GNU Affero General Public License
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 601
diff changeset
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
0
goffi@necton2
parents:
diff changeset
19
771
bfabeedbf32e core: i18n refactoring:
Goffi <goffi@goffi.org>
parents: 721
diff changeset
20 from sat.core.i18n import _
402
f03688bdb858 jp: use with statement to open fifo
Goffi <goffi@goffi.org>
parents: 401
diff changeset
21
0
goffi@necton2
parents:
diff changeset
22 global pbar_available
goffi@necton2
parents:
diff changeset
23 pbar_available = True #checked before using ProgressBar
goffi@necton2
parents:
diff changeset
24
goffi@necton2
parents:
diff changeset
25 ### logging ###
1605
0aded9648c5c jp (base): added a self.disp method which manage verbosity and stderr + verbosity property
Goffi <goffi@goffi.org>
parents: 1604
diff changeset
26 import logging as log
0aded9648c5c jp (base): added a self.disp method which manage verbosity and stderr + verbosity property
Goffi <goffi@goffi.org>
parents: 1604
diff changeset
27 log.basicConfig(level=log.DEBUG,
0
goffi@necton2
parents:
diff changeset
28 format='%(message)s')
goffi@necton2
parents:
diff changeset
29 ###
goffi@necton2
parents:
diff changeset
30
goffi@necton2
parents:
diff changeset
31 import sys
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
32 import locale
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
33 import os.path
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
34 import argparse
964
e09f624d3aa9 jp: fixed missing import
Goffi <goffi@goffi.org>
parents: 898
diff changeset
35 from gi.repository import GLib, GObject
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
36 from glob import iglob
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
37 from importlib import import_module
1139
75025461141f move sat.tools.jid to sat_frontends.tools.jid
souliane <souliane@mailoo.org>
parents: 1033
diff changeset
38 from sat_frontends.tools.jid import JID
627
d207c2186519 core, bridge, jp, quick_frontend: SàT stop more gracefully if bridge can't be initialised:
Goffi <goffi@goffi.org>
parents: 613
diff changeset
39 from sat_frontends.bridge.DBus import DBusBridgeFrontend
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
40 from sat.core import exceptions
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
41 import sat_frontends.jp
970
2e052998c7eb jp: using C.APP_URL for application url
Goffi <goffi@goffi.org>
parents: 965
diff changeset
42 from sat_frontends.jp.constants import Const as C
0
goffi@necton2
parents:
diff changeset
43 try:
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
44 import progressbar
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
45 except ImportError:
1605
0aded9648c5c jp (base): added a self.disp method which manage verbosity and stderr + verbosity property
Goffi <goffi@goffi.org>
parents: 1604
diff changeset
46 log.info (_('ProgressBar not available, please download it at http://pypi.python.org/pypi/progressbar'))
0aded9648c5c jp (base): added a self.disp method which manage verbosity and stderr + verbosity property
Goffi <goffi@goffi.org>
parents: 1604
diff changeset
47 log.info (_('Progress bar deactivated\n--\n'))
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
48 progressbar=None
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
49
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
50 #consts
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
51 prog_name = u"jp"
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
52 description = """This software is a command line tool for XMPP.
970
2e052998c7eb jp: using C.APP_URL for application url
Goffi <goffi@goffi.org>
parents: 965
diff changeset
53 Get the latest version at """ + C.APP_URL
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
54
1396
069ad98b360d 2015 copyright dates update
Goffi <goffi@goffi.org>
parents: 1395
diff changeset
55 copyleft = u"""Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014, 2015 Jérôme Poisson (aka Goffi)
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
56 This program comes with ABSOLUTELY NO WARRANTY;
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
57 This is free software, and you are welcome to redistribute it under certain conditions.
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
58 """
0
goffi@necton2
parents:
diff changeset
59
goffi@necton2
parents:
diff changeset
60
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
61 def unicode_decoder(arg):
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
62 # Needed to have unicode strings from arguments
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
63 return arg.decode(locale.getpreferredencoding())
814
59c7bc51c323 jp: refactoring using ArgParse
Dal <kedals0@gmail.com>
parents: 811
diff changeset
64
0
goffi@necton2
parents:
diff changeset
65
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
66 class Jp(object):
814
59c7bc51c323 jp: refactoring using ArgParse
Dal <kedals0@gmail.com>
parents: 811
diff changeset
67 """
59c7bc51c323 jp: refactoring using ArgParse
Dal <kedals0@gmail.com>
parents: 811
diff changeset
68 This class can be use to establish a connection with the
59c7bc51c323 jp: refactoring using ArgParse
Dal <kedals0@gmail.com>
parents: 811
diff changeset
69 bridge. Moreover, it should manage a main loop.
59c7bc51c323 jp: refactoring using ArgParse
Dal <kedals0@gmail.com>
parents: 811
diff changeset
70
59c7bc51c323 jp: refactoring using ArgParse
Dal <kedals0@gmail.com>
parents: 811
diff changeset
71 To use it, you mainly have to redefine the method run to perform
59c7bc51c323 jp: refactoring using ArgParse
Dal <kedals0@gmail.com>
parents: 811
diff changeset
72 specify what kind of operation you want to perform.
59c7bc51c323 jp: refactoring using ArgParse
Dal <kedals0@gmail.com>
parents: 811
diff changeset
73
59c7bc51c323 jp: refactoring using ArgParse
Dal <kedals0@gmail.com>
parents: 811
diff changeset
74 """
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
75 def __init__(self):
165
8a2053de6f8c Frontends: management of unlaunched SàT Backend (information message and exit)
Goffi <goffi@goffi.org>
parents: 156
diff changeset
76 try:
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
77 self.bridge = DBusBridgeFrontend()
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
78 except exceptions.BridgeExceptionNoService:
165
8a2053de6f8c Frontends: management of unlaunched SàT Backend (information message and exit)
Goffi <goffi@goffi.org>
parents: 156
diff changeset
79 print(_(u"Can't connect to SàT backend, are you sure it's launched ?"))
627
d207c2186519 core, bridge, jp, quick_frontend: SàT stop more gracefully if bridge can't be initialised:
Goffi <goffi@goffi.org>
parents: 613
diff changeset
80 sys.exit(1)
864
241f6baa6687 frontends: fix typos, do not use logging in the xmlui tools:
souliane <souliane@mailoo.org>
parents: 823
diff changeset
81 except exceptions.BridgeInitError:
627
d207c2186519 core, bridge, jp, quick_frontend: SàT stop more gracefully if bridge can't be initialised:
Goffi <goffi@goffi.org>
parents: 613
diff changeset
82 print(_(u"Can't init bridge"))
165
8a2053de6f8c Frontends: management of unlaunched SàT Backend (information message and exit)
Goffi <goffi@goffi.org>
parents: 156
diff changeset
83 sys.exit(1)
814
59c7bc51c323 jp: refactoring using ArgParse
Dal <kedals0@gmail.com>
parents: 811
diff changeset
84
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
85 self.parser = argparse.ArgumentParser(formatter_class=argparse.RawDescriptionHelpFormatter,
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
86 description=description)
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
87
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
88 self._make_parents()
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
89 self.add_parser_options()
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
90 self.subparsers = self.parser.add_subparsers(title=_('Available commands'), dest='subparser_name')
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
91 self._auto_loop = False # when loop is used for internal reasons
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
92 self.need_loop = False # to set by commands when loop is needed
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
93 self._progress_id = None # TODO: manage several progress ids
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
94 self.quit_on_progress_end = True # set to False if you manage yourself exiting, or if you want the user to stop by himself
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
95
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
96 @property
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
97 def version(self):
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
98 return self.bridge.getVersion()
814
59c7bc51c323 jp: refactoring using ArgParse
Dal <kedals0@gmail.com>
parents: 811
diff changeset
99
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
100 @property
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
101 def progress_id(self):
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
102 return self._progress_id
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
103
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
104 @progress_id.setter
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
105 def progress_id(self, value):
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
106 self._progress_id = value
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
107
1604
9ac78437000d jp (base): added quitFromSignal method to quit from signal handler with errcode without traceback, and addOnQuitCallback to manage cleaning callbacks
Goffi <goffi@goffi.org>
parents: 1600
diff changeset
108
1605
0aded9648c5c jp (base): added a self.disp method which manage verbosity and stderr + verbosity property
Goffi <goffi@goffi.org>
parents: 1604
diff changeset
109 @property
0aded9648c5c jp (base): added a self.disp method which manage verbosity and stderr + verbosity property
Goffi <goffi@goffi.org>
parents: 1604
diff changeset
110 def verbosity(self):
0aded9648c5c jp (base): added a self.disp method which manage verbosity and stderr + verbosity property
Goffi <goffi@goffi.org>
parents: 1604
diff changeset
111 try:
0aded9648c5c jp (base): added a self.disp method which manage verbosity and stderr + verbosity property
Goffi <goffi@goffi.org>
parents: 1604
diff changeset
112 return self.args.verbose
0aded9648c5c jp (base): added a self.disp method which manage verbosity and stderr + verbosity property
Goffi <goffi@goffi.org>
parents: 1604
diff changeset
113 except AttributeError:
0aded9648c5c jp (base): added a self.disp method which manage verbosity and stderr + verbosity property
Goffi <goffi@goffi.org>
parents: 1604
diff changeset
114 return 0
0aded9648c5c jp (base): added a self.disp method which manage verbosity and stderr + verbosity property
Goffi <goffi@goffi.org>
parents: 1604
diff changeset
115
0aded9648c5c jp (base): added a self.disp method which manage verbosity and stderr + verbosity property
Goffi <goffi@goffi.org>
parents: 1604
diff changeset
116 def disp(self, msg, verbosity=0, error=False):
0aded9648c5c jp (base): added a self.disp method which manage verbosity and stderr + verbosity property
Goffi <goffi@goffi.org>
parents: 1604
diff changeset
117 """Print a message to user
0aded9648c5c jp (base): added a self.disp method which manage verbosity and stderr + verbosity property
Goffi <goffi@goffi.org>
parents: 1604
diff changeset
118
0aded9648c5c jp (base): added a self.disp method which manage verbosity and stderr + verbosity property
Goffi <goffi@goffi.org>
parents: 1604
diff changeset
119 @param msg(unicode): message to print
0aded9648c5c jp (base): added a self.disp method which manage verbosity and stderr + verbosity property
Goffi <goffi@goffi.org>
parents: 1604
diff changeset
120 @param verbosity(int): minimal verbosity to display the message
0aded9648c5c jp (base): added a self.disp method which manage verbosity and stderr + verbosity property
Goffi <goffi@goffi.org>
parents: 1604
diff changeset
121 @param error(bool): if True, print to stderr instead of stdout
0aded9648c5c jp (base): added a self.disp method which manage verbosity and stderr + verbosity property
Goffi <goffi@goffi.org>
parents: 1604
diff changeset
122 """
0aded9648c5c jp (base): added a self.disp method which manage verbosity and stderr + verbosity property
Goffi <goffi@goffi.org>
parents: 1604
diff changeset
123 if self.verbosity >= verbosity:
0aded9648c5c jp (base): added a self.disp method which manage verbosity and stderr + verbosity property
Goffi <goffi@goffi.org>
parents: 1604
diff changeset
124 if error:
0aded9648c5c jp (base): added a self.disp method which manage verbosity and stderr + verbosity property
Goffi <goffi@goffi.org>
parents: 1604
diff changeset
125 print >>sys.stderr,msg
0aded9648c5c jp (base): added a self.disp method which manage verbosity and stderr + verbosity property
Goffi <goffi@goffi.org>
parents: 1604
diff changeset
126 else:
0aded9648c5c jp (base): added a self.disp method which manage verbosity and stderr + verbosity property
Goffi <goffi@goffi.org>
parents: 1604
diff changeset
127 print msg
0aded9648c5c jp (base): added a self.disp method which manage verbosity and stderr + verbosity property
Goffi <goffi@goffi.org>
parents: 1604
diff changeset
128
1604
9ac78437000d jp (base): added quitFromSignal method to quit from signal handler with errcode without traceback, and addOnQuitCallback to manage cleaning callbacks
Goffi <goffi@goffi.org>
parents: 1600
diff changeset
129 def addOnQuitCallback(self, callback, *args, **kwargs):
9ac78437000d jp (base): added quitFromSignal method to quit from signal handler with errcode without traceback, and addOnQuitCallback to manage cleaning callbacks
Goffi <goffi@goffi.org>
parents: 1600
diff changeset
130 """Add a callback which will be called on quit command
9ac78437000d jp (base): added quitFromSignal method to quit from signal handler with errcode without traceback, and addOnQuitCallback to manage cleaning callbacks
Goffi <goffi@goffi.org>
parents: 1600
diff changeset
131
9ac78437000d jp (base): added quitFromSignal method to quit from signal handler with errcode without traceback, and addOnQuitCallback to manage cleaning callbacks
Goffi <goffi@goffi.org>
parents: 1600
diff changeset
132 @param callback(callback): method to call
9ac78437000d jp (base): added quitFromSignal method to quit from signal handler with errcode without traceback, and addOnQuitCallback to manage cleaning callbacks
Goffi <goffi@goffi.org>
parents: 1600
diff changeset
133 """
9ac78437000d jp (base): added quitFromSignal method to quit from signal handler with errcode without traceback, and addOnQuitCallback to manage cleaning callbacks
Goffi <goffi@goffi.org>
parents: 1600
diff changeset
134 try:
9ac78437000d jp (base): added quitFromSignal method to quit from signal handler with errcode without traceback, and addOnQuitCallback to manage cleaning callbacks
Goffi <goffi@goffi.org>
parents: 1600
diff changeset
135 callbacks_list = self._onQuitCallbacks
9ac78437000d jp (base): added quitFromSignal method to quit from signal handler with errcode without traceback, and addOnQuitCallback to manage cleaning callbacks
Goffi <goffi@goffi.org>
parents: 1600
diff changeset
136 except AttributeError:
9ac78437000d jp (base): added quitFromSignal method to quit from signal handler with errcode without traceback, and addOnQuitCallback to manage cleaning callbacks
Goffi <goffi@goffi.org>
parents: 1600
diff changeset
137 callbacks_list = self._onQuitCallbacks = []
9ac78437000d jp (base): added quitFromSignal method to quit from signal handler with errcode without traceback, and addOnQuitCallback to manage cleaning callbacks
Goffi <goffi@goffi.org>
parents: 1600
diff changeset
138 finally:
9ac78437000d jp (base): added quitFromSignal method to quit from signal handler with errcode without traceback, and addOnQuitCallback to manage cleaning callbacks
Goffi <goffi@goffi.org>
parents: 1600
diff changeset
139 callbacks_list.append((callback, args, kwargs))
9ac78437000d jp (base): added quitFromSignal method to quit from signal handler with errcode without traceback, and addOnQuitCallback to manage cleaning callbacks
Goffi <goffi@goffi.org>
parents: 1600
diff changeset
140
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
141 def _make_parents(self):
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
142 self.parents = {}
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
143
1594
313f2bb7841b jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents: 1588
diff changeset
144 # we have a special case here as the start-session option is present only if connection is not needed,
313f2bb7841b jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents: 1588
diff changeset
145 # so we create two similar parents, one with the option, the other one without it
313f2bb7841b jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents: 1588
diff changeset
146 for parent_name in ('profile', 'profile_session'):
313f2bb7841b jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents: 1588
diff changeset
147 parent = self.parents[parent_name] = argparse.ArgumentParser(add_help=False)
313f2bb7841b jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents: 1588
diff changeset
148 parent.add_argument("-p", "--profile", action="store", type=str, default='@DEFAULT@', help=_("Use PROFILE profile key (default: %(default)s)"))
313f2bb7841b jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents: 1588
diff changeset
149 parent.add_argument("--pwd", action="store", type=unicode, default='', metavar='PASSWORD', help=_("Password used to connect profile, if necessary"))
313f2bb7841b jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents: 1588
diff changeset
150
313f2bb7841b jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents: 1588
diff changeset
151 profile_parent, profile_session_parent = self.parents['profile'], self.parents['profile_session']
313f2bb7841b jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents: 1588
diff changeset
152
313f2bb7841b jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents: 1588
diff changeset
153 connect_short, connect_long, connect_action, connect_help = "-c", "--connect", "store_true", _(u"Connect the profile before doing anything else")
313f2bb7841b jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents: 1588
diff changeset
154 profile_parent.add_argument(connect_short, connect_long, action=connect_action, help=connect_help)
313f2bb7841b jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents: 1588
diff changeset
155
313f2bb7841b jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents: 1588
diff changeset
156 profile_session_connect_group = profile_session_parent.add_mutually_exclusive_group()
313f2bb7841b jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents: 1588
diff changeset
157 profile_session_connect_group.add_argument(connect_short, connect_long, action=connect_action, help=connect_help)
313f2bb7841b jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents: 1588
diff changeset
158 profile_session_connect_group.add_argument("--start-session", action="store_true", help=_("Start a profile session without connecting"))
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
159
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
160 progress_parent = self.parents['progress'] = argparse.ArgumentParser(add_help=False)
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
161 if progressbar:
823
300b4de701a6 jp: short option for progress is now -P instead of -g, so -g can be used for groups
Goffi <goffi@goffi.org>
parents: 817
diff changeset
162 progress_parent.add_argument("-P", "--progress", action="store_true", help=_("Show progress bar"))
0
goffi@necton2
parents:
diff changeset
163
1600
8d41cd4da2f6 jp: added a --verbose command
Goffi <goffi@goffi.org>
parents: 1594
diff changeset
164 verbose_parent = self.parents['verbose'] = argparse.ArgumentParser(add_help=False)
8d41cd4da2f6 jp: added a --verbose command
Goffi <goffi@goffi.org>
parents: 1594
diff changeset
165 verbose_parent.add_argument('--verbose', '-v', action='count', help=_(u"Add a verbosity level (can be used multiple times)"))
8d41cd4da2f6 jp: added a --verbose command
Goffi <goffi@goffi.org>
parents: 1594
diff changeset
166
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
167 def add_parser_options(self):
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
168 self.parser.add_argument('--version', action='version', version=("%(name)s %(version)s %(copyleft)s" % {'name': prog_name, 'version': self.version, 'copyleft': copyleft}))
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
169
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
170 def import_commands(self):
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
171 """ Automaticaly import commands to jp
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
172 looks from modules names cmd_*.py in jp path and import them
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
173
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
174 """
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
175 path = os.path.dirname(sat_frontends.jp.__file__)
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
176 modules = (os.path.splitext(module)[0] for module in map(os.path.basename, iglob(os.path.join(path, "cmd_*.py"))))
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
177 for module_name in modules:
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
178 module = import_module("sat_frontends.jp."+module_name)
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
179 try:
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
180 self.import_command_module(module)
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
181 except ImportError:
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
182 continue
0
goffi@necton2
parents:
diff changeset
183
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
184 def import_command_module(self, module):
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
185 """ Add commands from a module to jp
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
186 @param module: module containing commands
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
187
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
188 """
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
189 try:
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
190 for classname in module.__commands__:
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
191 cls = getattr(module, classname)
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
192 except AttributeError:
1605
0aded9648c5c jp (base): added a self.disp method which manage verbosity and stderr + verbosity property
Goffi <goffi@goffi.org>
parents: 1604
diff changeset
193 log.warning(_("Invalid module %s") % module)
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
194 raise ImportError
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
195 cls(self)
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
196
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
197 def run(self, args=None):
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
198 self.args = self.parser.parse_args(args)
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
199 self.args.func()
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
200 if self.need_loop or self._auto_loop:
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
201 self._start_loop()
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
202
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
203 def _start_loop(self):
898
9720d3d0a764 jp: updated main loop to gobject 3
Matteo Cypriani <mcy@lm7.fr>
parents: 864
diff changeset
204 self.loop = GLib.MainLoop()
814
59c7bc51c323 jp: refactoring using ArgParse
Dal <kedals0@gmail.com>
parents: 811
diff changeset
205 try:
59c7bc51c323 jp: refactoring using ArgParse
Dal <kedals0@gmail.com>
parents: 811
diff changeset
206 self.loop.run()
59c7bc51c323 jp: refactoring using ArgParse
Dal <kedals0@gmail.com>
parents: 811
diff changeset
207 except KeyboardInterrupt:
1605
0aded9648c5c jp (base): added a self.disp method which manage verbosity and stderr + verbosity property
Goffi <goffi@goffi.org>
parents: 1604
diff changeset
208 log.info(_("User interruption: good bye"))
0
goffi@necton2
parents:
diff changeset
209
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
210 def stop_loop(self):
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
211 try:
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
212 self.loop.quit()
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
213 except AttributeError:
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
214 pass
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
215
1604
9ac78437000d jp (base): added quitFromSignal method to quit from signal handler with errcode without traceback, and addOnQuitCallback to manage cleaning callbacks
Goffi <goffi@goffi.org>
parents: 1600
diff changeset
216 def quitFromSignal(self, errcode=0):
9ac78437000d jp (base): added quitFromSignal method to quit from signal handler with errcode without traceback, and addOnQuitCallback to manage cleaning callbacks
Goffi <goffi@goffi.org>
parents: 1600
diff changeset
217 """Same as self.quit, but from a signal handler
9ac78437000d jp (base): added quitFromSignal method to quit from signal handler with errcode without traceback, and addOnQuitCallback to manage cleaning callbacks
Goffi <goffi@goffi.org>
parents: 1600
diff changeset
218
9ac78437000d jp (base): added quitFromSignal method to quit from signal handler with errcode without traceback, and addOnQuitCallback to manage cleaning callbacks
Goffi <goffi@goffi.org>
parents: 1600
diff changeset
219 /!\: return must be used after calling this method !
9ac78437000d jp (base): added quitFromSignal method to quit from signal handler with errcode without traceback, and addOnQuitCallback to manage cleaning callbacks
Goffi <goffi@goffi.org>
parents: 1600
diff changeset
220 """
9ac78437000d jp (base): added quitFromSignal method to quit from signal handler with errcode without traceback, and addOnQuitCallback to manage cleaning callbacks
Goffi <goffi@goffi.org>
parents: 1600
diff changeset
221 assert self.need_loop
9ac78437000d jp (base): added quitFromSignal method to quit from signal handler with errcode without traceback, and addOnQuitCallback to manage cleaning callbacks
Goffi <goffi@goffi.org>
parents: 1600
diff changeset
222 # XXX: python-dbus will show a traceback if we exit in a signal handler with an error code
9ac78437000d jp (base): added quitFromSignal method to quit from signal handler with errcode without traceback, and addOnQuitCallback to manage cleaning callbacks
Goffi <goffi@goffi.org>
parents: 1600
diff changeset
223 # so we use this little timeout trick to avoid it
9ac78437000d jp (base): added quitFromSignal method to quit from signal handler with errcode without traceback, and addOnQuitCallback to manage cleaning callbacks
Goffi <goffi@goffi.org>
parents: 1600
diff changeset
224 GLib.timeout_add(0, self.quit, errcode)
9ac78437000d jp (base): added quitFromSignal method to quit from signal handler with errcode without traceback, and addOnQuitCallback to manage cleaning callbacks
Goffi <goffi@goffi.org>
parents: 1600
diff changeset
225
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
226 def quit(self, errcode=0):
1604
9ac78437000d jp (base): added quitFromSignal method to quit from signal handler with errcode without traceback, and addOnQuitCallback to manage cleaning callbacks
Goffi <goffi@goffi.org>
parents: 1600
diff changeset
227 # first the onQuitCallbacks
9ac78437000d jp (base): added quitFromSignal method to quit from signal handler with errcode without traceback, and addOnQuitCallback to manage cleaning callbacks
Goffi <goffi@goffi.org>
parents: 1600
diff changeset
228 try:
9ac78437000d jp (base): added quitFromSignal method to quit from signal handler with errcode without traceback, and addOnQuitCallback to manage cleaning callbacks
Goffi <goffi@goffi.org>
parents: 1600
diff changeset
229 callbacks_list = self._onQuitCallbacks
9ac78437000d jp (base): added quitFromSignal method to quit from signal handler with errcode without traceback, and addOnQuitCallback to manage cleaning callbacks
Goffi <goffi@goffi.org>
parents: 1600
diff changeset
230 except AttributeError:
9ac78437000d jp (base): added quitFromSignal method to quit from signal handler with errcode without traceback, and addOnQuitCallback to manage cleaning callbacks
Goffi <goffi@goffi.org>
parents: 1600
diff changeset
231 pass
9ac78437000d jp (base): added quitFromSignal method to quit from signal handler with errcode without traceback, and addOnQuitCallback to manage cleaning callbacks
Goffi <goffi@goffi.org>
parents: 1600
diff changeset
232 else:
9ac78437000d jp (base): added quitFromSignal method to quit from signal handler with errcode without traceback, and addOnQuitCallback to manage cleaning callbacks
Goffi <goffi@goffi.org>
parents: 1600
diff changeset
233 for callback, args, kwargs in callbacks_list:
9ac78437000d jp (base): added quitFromSignal method to quit from signal handler with errcode without traceback, and addOnQuitCallback to manage cleaning callbacks
Goffi <goffi@goffi.org>
parents: 1600
diff changeset
234 callback(*args, **kwargs)
9ac78437000d jp (base): added quitFromSignal method to quit from signal handler with errcode without traceback, and addOnQuitCallback to manage cleaning callbacks
Goffi <goffi@goffi.org>
parents: 1600
diff changeset
235
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
236 self.stop_loop()
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
237 if errcode:
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
238 sys.exit(errcode)
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
239
814
59c7bc51c323 jp: refactoring using ArgParse
Dal <kedals0@gmail.com>
parents: 811
diff changeset
240 def check_jids(self, jids):
59c7bc51c323 jp: refactoring using ArgParse
Dal <kedals0@gmail.com>
parents: 811
diff changeset
241 """Check jids validity, transform roster name to corresponding jids
110
cb904fa7de3c jp: profile management (new option: --profile)
Goffi <goffi@goffi.org>
parents: 70
diff changeset
242
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
243 @param profile: profile name
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
244 @param jids: list of jids
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
245 @return: List of jids
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
246
814
59c7bc51c323 jp: refactoring using ArgParse
Dal <kedals0@gmail.com>
parents: 811
diff changeset
247 """
401
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents: 393
diff changeset
248 names2jid = {}
493
b7c4bb2c0668 jp: - better expandJid: roster's jids' nodes are used after names to expand jid
Goffi <goffi@goffi.org>
parents: 480
diff changeset
249 nodes2jid = {}
393
393b35aa86d2 jp: added --connect option
Goffi <goffi@goffi.org>
parents: 391
diff changeset
250
814
59c7bc51c323 jp: refactoring using ArgParse
Dal <kedals0@gmail.com>
parents: 811
diff changeset
251 for contact in self.bridge.getContacts(self.profile):
1395
1ae9aa94c351 jp: fixed bad try/except hidding errors + fixed bad management of jids without node
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
252 jid_s, attr, groups = contact
1ae9aa94c351 jp: fixed bad try/except hidding errors + fixed bad management of jids without node
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
253 _jid = JID(jid_s)
1ae9aa94c351 jp: fixed bad try/except hidding errors + fixed bad management of jids without node
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
254 try:
1ae9aa94c351 jp: fixed bad try/except hidding errors + fixed bad management of jids without node
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
255 names2jid[attr["name"].lower()] = jid_s
1ae9aa94c351 jp: fixed bad try/except hidding errors + fixed bad management of jids without node
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
256 except KeyError:
1ae9aa94c351 jp: fixed bad try/except hidding errors + fixed bad management of jids without node
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
257 pass
1ae9aa94c351 jp: fixed bad try/except hidding errors + fixed bad management of jids without node
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
258
1ae9aa94c351 jp: fixed bad try/except hidding errors + fixed bad management of jids without node
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
259 if _jid.node:
1ae9aa94c351 jp: fixed bad try/except hidding errors + fixed bad management of jids without node
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
260 nodes2jid[_jid.node.lower()] = jid_s
401
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents: 393
diff changeset
261
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
262 def expand_jid(jid):
401
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents: 393
diff changeset
263 _jid = jid.lower()
493
b7c4bb2c0668 jp: - better expandJid: roster's jids' nodes are used after names to expand jid
Goffi <goffi@goffi.org>
parents: 480
diff changeset
264 if _jid in names2jid:
b7c4bb2c0668 jp: - better expandJid: roster's jids' nodes are used after names to expand jid
Goffi <goffi@goffi.org>
parents: 480
diff changeset
265 expanded = names2jid[_jid]
b7c4bb2c0668 jp: - better expandJid: roster's jids' nodes are used after names to expand jid
Goffi <goffi@goffi.org>
parents: 480
diff changeset
266 elif _jid in nodes2jid:
b7c4bb2c0668 jp: - better expandJid: roster's jids' nodes are used after names to expand jid
Goffi <goffi@goffi.org>
parents: 480
diff changeset
267 expanded = nodes2jid[_jid]
b7c4bb2c0668 jp: - better expandJid: roster's jids' nodes are used after names to expand jid
Goffi <goffi@goffi.org>
parents: 480
diff changeset
268 else:
b7c4bb2c0668 jp: - better expandJid: roster's jids' nodes are used after names to expand jid
Goffi <goffi@goffi.org>
parents: 480
diff changeset
269 expanded = jid
965
5b95ce21c2d3 jp: fixed jid unicode decoding
Goffi <goffi@goffi.org>
parents: 964
diff changeset
270 return expanded.decode('utf-8')
401
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents: 393
diff changeset
271
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents: 393
diff changeset
272 def check(jid):
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents: 393
diff changeset
273 if not jid.is_valid:
1605
0aded9648c5c jp (base): added a self.disp method which manage verbosity and stderr + verbosity property
Goffi <goffi@goffi.org>
parents: 1604
diff changeset
274 log.error (_("%s is not a valid JID !"), jid)
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
275 self.quit(1)
401
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents: 393
diff changeset
276
814
59c7bc51c323 jp: refactoring using ArgParse
Dal <kedals0@gmail.com>
parents: 811
diff changeset
277 dest_jids=[]
401
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents: 393
diff changeset
278 try:
814
59c7bc51c323 jp: refactoring using ArgParse
Dal <kedals0@gmail.com>
parents: 811
diff changeset
279 for i in range(len(jids)):
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
280 dest_jids.append(expand_jid(jids[i]))
814
59c7bc51c323 jp: refactoring using ArgParse
Dal <kedals0@gmail.com>
parents: 811
diff changeset
281 check(dest_jids[i])
401
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents: 393
diff changeset
282 except AttributeError:
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents: 393
diff changeset
283 pass
0
goffi@necton2
parents:
diff changeset
284
814
59c7bc51c323 jp: refactoring using ArgParse
Dal <kedals0@gmail.com>
parents: 811
diff changeset
285 return dest_jids
0
goffi@necton2
parents:
diff changeset
286
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
287 def connect_profile(self, callback):
1588
823a385235ef jp: fixed bad --connect option check
Goffi <goffi@goffi.org>
parents: 1544
diff changeset
288 """ Check if the profile is connected and do it if requested
1401
265ff2bd8d67 jp: fixed crash on commands using profile without "connect" option
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
289
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
290 @param callback: method to call when profile is connected
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
291 @exit: - 1 when profile is not connected and --connect is not set
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
292 - 1 when the profile doesn't exists
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
293 - 1 when there is a connection error
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
294 """
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
295 # FIXME: need better exit codes
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
296
971
8ca5c990ed92 jp: "disco" subcommand, moved into a new "info" subcommand + added "version" subcommand which get software version
Goffi <goffi@goffi.org>
parents: 970
diff changeset
297 def cant_connect(failure):
1605
0aded9648c5c jp (base): added a self.disp method which manage verbosity and stderr + verbosity property
Goffi <goffi@goffi.org>
parents: 1604
diff changeset
298 log.error(_(u"Can't connect profile: {reason}").format(reason=failure))
1594
313f2bb7841b jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents: 1588
diff changeset
299 self.quit(1)
313f2bb7841b jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents: 1588
diff changeset
300
313f2bb7841b jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents: 1588
diff changeset
301 def cant_start_session(failure):
1605
0aded9648c5c jp (base): added a self.disp method which manage verbosity and stderr + verbosity property
Goffi <goffi@goffi.org>
parents: 1604
diff changeset
302 log.error(_(u"Can't start {profile}'s session: {reason}").format(profile=self.profile, reason=failure))
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
303 self.quit(1)
401
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents: 393
diff changeset
304
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
305 self.profile = self.bridge.getProfileName(self.args.profile)
0
goffi@necton2
parents:
diff changeset
306
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
307 if not self.profile:
1605
0aded9648c5c jp (base): added a self.disp method which manage verbosity and stderr + verbosity property
Goffi <goffi@goffi.org>
parents: 1604
diff changeset
308 log.error(_("The profile [{profile}] doesn't exist").format(profile=self.args.profile))
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
309 self.quit(1)
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
310
1594
313f2bb7841b jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents: 1588
diff changeset
311 try:
313f2bb7841b jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents: 1588
diff changeset
312 start_session = self.args.start_session
313f2bb7841b jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents: 1588
diff changeset
313 except AttributeError:
313f2bb7841b jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents: 1588
diff changeset
314 pass
313f2bb7841b jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents: 1588
diff changeset
315 else:
313f2bb7841b jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents: 1588
diff changeset
316 if start_session:
313f2bb7841b jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents: 1588
diff changeset
317 self.bridge.profileStartSession(self.args.pwd, self.profile, lambda dummy: callback(), cant_start_session)
313f2bb7841b jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents: 1588
diff changeset
318 self._auto_loop = True
313f2bb7841b jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents: 1588
diff changeset
319 return
313f2bb7841b jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents: 1588
diff changeset
320 elif not self.bridge.profileIsSessionStarted(self.profile):
313f2bb7841b jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents: 1588
diff changeset
321 if not self.args.connect:
1605
0aded9648c5c jp (base): added a self.disp method which manage verbosity and stderr + verbosity property
Goffi <goffi@goffi.org>
parents: 1604
diff changeset
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))
1594
313f2bb7841b jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents: 1588
diff changeset
323 self.quit(1)
313f2bb7841b jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents: 1588
diff changeset
324 else:
313f2bb7841b jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents: 1588
diff changeset
325 callback()
313f2bb7841b jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents: 1588
diff changeset
326 return
313f2bb7841b jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents: 1588
diff changeset
327
313f2bb7841b jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents: 1588
diff changeset
328
1401
265ff2bd8d67 jp: fixed crash on commands using profile without "connect" option
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
329 if not hasattr(self.args, 'connect'):
1588
823a385235ef jp: fixed bad --connect option check
Goffi <goffi@goffi.org>
parents: 1544
diff changeset
330 # a profile can be present without connect option (e.g. on profile creation/deletion)
1401
265ff2bd8d67 jp: fixed crash on commands using profile without "connect" option
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
331 return
1588
823a385235ef jp: fixed bad --connect option check
Goffi <goffi@goffi.org>
parents: 1544
diff changeset
332 elif self.args.connect is True: # if connection is asked, we connect the profile
1544
3d5193b4c582 jp: separate password and connection of profile in --connect and --pwd arguments
Goffi <goffi@goffi.org>
parents: 1401
diff changeset
333 self.bridge.asyncConnect(self.profile, self.args.pwd, lambda dummy: callback(), cant_connect)
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
334 self._auto_loop = True
814
59c7bc51c323 jp: refactoring using ArgParse
Dal <kedals0@gmail.com>
parents: 811
diff changeset
335 return
1594
313f2bb7841b jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents: 1588
diff changeset
336 else:
313f2bb7841b jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents: 1588
diff changeset
337 if not self.bridge.isConnected(self.profile):
1605
0aded9648c5c jp (base): added a self.disp method which manage verbosity and stderr + verbosity property
Goffi <goffi@goffi.org>
parents: 1604
diff changeset
338 log.error(_(u"Profile [{profile}] is not connected, please connect it before using jp, or use --connect option").format(profile=self.profile))
1594
313f2bb7841b jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents: 1588
diff changeset
339 self.quit(1)
0
goffi@necton2
parents:
diff changeset
340
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
341 callback()
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
342
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
343 def get_full_jid(self, param_jid):
1290
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
344 """Return the full jid if possible (add main resource when find a bare jid)"""
401
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents: 393
diff changeset
345 _jid = JID(param_jid)
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents: 393
diff changeset
346 if not _jid.resource:
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents: 393
diff changeset
347 #if the resource is not given, we try to add the last known resource
1290
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1139
diff changeset
348 last_resource = self.bridge.getMainResource(param_jid, self.profile)
401
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents: 393
diff changeset
349 if last_resource:
688
f7878ad3c846 tools: renamed tools.jid.JID attribute "short" to "bare"
souliane <souliane@mailoo.org>
parents: 657
diff changeset
350 return "%s/%s" % (_jid.bare, last_resource)
401
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents: 393
diff changeset
351 return param_jid
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents: 393
diff changeset
352
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
353 def watch_progress(self):
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
354 self.pbar = None
898
9720d3d0a764 jp: updated main loop to gobject 3
Matteo Cypriani <mcy@lm7.fr>
parents: 864
diff changeset
355 GObject.timeout_add(10, self._progress_cb)
0
goffi@necton2
parents:
diff changeset
356
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
357 def _progress_cb(self):
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
358 if self.progress_id:
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
359 data = self.bridge.getProgress(self.progress_id, self.profile)
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
360 if data:
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
361 if not data['position']:
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
362 data['position'] = '0'
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
363 if not self.pbar:
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
364 #first answer, we must construct the bar
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
365 self.pbar = progressbar.ProgressBar(int(data['size']),
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
366 [_("Progress: "),progressbar.Percentage(),
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
367 " ",
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
368 progressbar.Bar(),
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
369 " ",
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
370 progressbar.FileTransferSpeed(),
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
371 " ",
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
372 progressbar.ETA()])
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
373 self.pbar.start()
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
374
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
375 self.pbar.update(int(data['position']))
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
376
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
377 elif self.pbar:
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
378 self.pbar.finish()
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
379 if self.quit_on_progress_end:
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
380 self.quit()
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
381 return False
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
382
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
383 return True
814
59c7bc51c323 jp: refactoring using ArgParse
Dal <kedals0@gmail.com>
parents: 811
diff changeset
384
59c7bc51c323 jp: refactoring using ArgParse
Dal <kedals0@gmail.com>
parents: 811
diff changeset
385
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
386 class CommandBase(object):
814
59c7bc51c323 jp: refactoring using ArgParse
Dal <kedals0@gmail.com>
parents: 811
diff changeset
387
1600
8d41cd4da2f6 jp: added a --verbose command
Goffi <goffi@goffi.org>
parents: 1594
diff changeset
388 def __init__(self, host, name, use_profile=True, use_progress=False, use_verbose=False, need_connect=None, help=None, **kwargs):
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
389 """ Initialise CommandBase
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
390 @param host: Jp instance
1594
313f2bb7841b jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents: 1588
diff changeset
391 @param name(unicode): name of the new command
313f2bb7841b jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents: 1588
diff changeset
392 @param use_profile(bool): if True, add profile selection/connection commands
313f2bb7841b jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents: 1588
diff changeset
393 @param use_progress(bool): if True, add progress bar activation commands
1600
8d41cd4da2f6 jp: added a --verbose command
Goffi <goffi@goffi.org>
parents: 1594
diff changeset
394 @param use_verbose(bool): if True, add verbosity command
1594
313f2bb7841b jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents: 1588
diff changeset
395 @param need_connect(bool, None): True if profile connection is needed
313f2bb7841b jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents: 1588
diff changeset
396 False else (profile session must still be started)
313f2bb7841b jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents: 1588
diff changeset
397 None to set auto value (i.e. True if use_profile is set)
313f2bb7841b jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents: 1588
diff changeset
398 Can't be set if use_profile is False
313f2bb7841b jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents: 1588
diff changeset
399 @param help(unicode): help message to display
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
400 @param **kwargs: args passed to ArgumentParser
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
401
814
59c7bc51c323 jp: refactoring using ArgParse
Dal <kedals0@gmail.com>
parents: 811
diff changeset
402 """
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
403 try: # If we have subcommands, host is a CommandBase and we need to use host.host
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
404 self.host = host.host
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
405 except AttributeError:
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
406 self.host = host
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
407
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
408 parents = kwargs.setdefault('parents', set())
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
409 if use_profile:
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
410 #self.host.parents['profile'] is an ArgumentParser with profile connection arguments
1594
313f2bb7841b jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents: 1588
diff changeset
411 if need_connect is None:
313f2bb7841b jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents: 1588
diff changeset
412 need_connect = True
313f2bb7841b jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents: 1588
diff changeset
413 parents.add(self.host.parents['profile' if need_connect else 'profile_session'])
313f2bb7841b jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents: 1588
diff changeset
414 else:
313f2bb7841b jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents: 1588
diff changeset
415 assert need_connect is None
313f2bb7841b jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents: 1588
diff changeset
416
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
417 if use_progress:
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
418 parents.add(self.host.parents['progress'])
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
419
1600
8d41cd4da2f6 jp: added a --verbose command
Goffi <goffi@goffi.org>
parents: 1594
diff changeset
420 if use_verbose:
8d41cd4da2f6 jp: added a --verbose command
Goffi <goffi@goffi.org>
parents: 1594
diff changeset
421 parents.add(self.host.parents['verbose'])
8d41cd4da2f6 jp: added a --verbose command
Goffi <goffi@goffi.org>
parents: 1594
diff changeset
422
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
423 self.parser = host.subparsers.add_parser(name, help=help, **kwargs)
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
424 if hasattr(self, "subcommands"):
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
425 self.subparsers = self.parser.add_subparsers()
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
426 else:
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
427 self.parser.set_defaults(func=self.run)
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
428 self.add_parser_options()
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
429
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
430 @property
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
431 def args(self):
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
432 return self.host.args
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
433
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
434 @property
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
435 def need_loop(self):
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
436 return self.host.need_loop
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
437
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
438 @need_loop.setter
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
439 def need_loop(self, value):
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
440 self.host.need_loop = value
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
441
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
442 @property
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
443 def profile(self):
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
444 return self.host.profile
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
445
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
446 @property
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
447 def progress_id(self):
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
448 return self.host.progress_id
814
59c7bc51c323 jp: refactoring using ArgParse
Dal <kedals0@gmail.com>
parents: 811
diff changeset
449
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
450 @progress_id.setter
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
451 def progress_id(self, value):
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
452 self.host.progress_id = value
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
453
1605
0aded9648c5c jp (base): added a self.disp method which manage verbosity and stderr + verbosity property
Goffi <goffi@goffi.org>
parents: 1604
diff changeset
454 def disp(self, msg, verbosity=0, error=False):
0aded9648c5c jp (base): added a self.disp method which manage verbosity and stderr + verbosity property
Goffi <goffi@goffi.org>
parents: 1604
diff changeset
455 return self.host.disp(msg, verbosity, error)
0aded9648c5c jp (base): added a self.disp method which manage verbosity and stderr + verbosity property
Goffi <goffi@goffi.org>
parents: 1604
diff changeset
456
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
457 def add_parser_options(self):
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
458 try:
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
459 subcommands = self.subcommands
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
460 except AttributeError:
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
461 # We don't have subcommands, the class need to implements add_parser_options
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
462 raise NotImplementedError
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
463
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
464 # now we add subcommands to ourself
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
465 for cls in subcommands:
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
466 cls(self)
814
59c7bc51c323 jp: refactoring using ArgParse
Dal <kedals0@gmail.com>
parents: 811
diff changeset
467
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
468 def run(self):
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
469 try:
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
470 if self.args.profile:
1395
1ae9aa94c351 jp: fixed bad try/except hidding errors + fixed bad management of jids without node
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
471 connect_profile = self.host.connect_profile
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
472 except AttributeError:
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
473 # the command doesn't need to connect profile
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
474 pass
1395
1ae9aa94c351 jp: fixed bad try/except hidding errors + fixed bad management of jids without node
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
475 else:
1ae9aa94c351 jp: fixed bad try/except hidding errors + fixed bad management of jids without node
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
476 connect_profile(self.connected)
1ae9aa94c351 jp: fixed bad try/except hidding errors + fixed bad management of jids without node
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
477
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
478 try:
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
479 if self.args.progress:
1395
1ae9aa94c351 jp: fixed bad try/except hidding errors + fixed bad management of jids without node
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
480 watch_progress = self.host.watch_progress
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
481 except AttributeError:
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
482 # the command doesn't use progress bar
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
483 pass
1395
1ae9aa94c351 jp: fixed bad try/except hidding errors + fixed bad management of jids without node
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
484 else:
1ae9aa94c351 jp: fixed bad try/except hidding errors + fixed bad management of jids without node
Goffi <goffi@goffi.org>
parents: 1290
diff changeset
485 watch_progress()
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
486
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
487 def connected(self):
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
488 if not self.need_loop:
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
489 self.host.stop_loop()
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
490
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
491
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
492 class CommandAnswering(CommandBase):
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
493 #FIXME: temp, will be refactored when progress_bar/confirmations will be refactored
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
494
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
495 def _ask_confirmation(self, confirm_id, confirm_type, data, profile):
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
496 """ Callback used for file transfer, accept files depending on parameters"""
538
2c4016921403 core, frontends, bridgen plugins: fixed methods which were unproperly managing multi-profiles
Goffi <goffi@goffi.org>
parents: 493
diff changeset
497 if profile != self.profile:
2c4016921403 core, frontends, bridgen plugins: fixed methods which were unproperly managing multi-profiles
Goffi <goffi@goffi.org>
parents: 493
diff changeset
498 debug("Ask confirmation ignored: not our profile")
2c4016921403 core, frontends, bridgen plugins: fixed methods which were unproperly managing multi-profiles
Goffi <goffi@goffi.org>
parents: 493
diff changeset
499 return
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
500 if confirm_type == self.confirm_type:
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
501 if self.dest_jids and not JID(data['from']).bare in [JID(_jid).bare for _jid in self.dest_jids()]:
0
goffi@necton2
parents:
diff changeset
502 return #file is not sent by a filtered jid
goffi@necton2
parents:
diff changeset
503 else:
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
504 self.ask(data, confirm_id)
0
goffi@necton2
parents:
diff changeset
505
814
59c7bc51c323 jp: refactoring using ArgParse
Dal <kedals0@gmail.com>
parents: 811
diff changeset
506 def ask(self):
59c7bc51c323 jp: refactoring using ArgParse
Dal <kedals0@gmail.com>
parents: 811
diff changeset
507 """
59c7bc51c323 jp: refactoring using ArgParse
Dal <kedals0@gmail.com>
parents: 811
diff changeset
508 The return value is used to answer to the bridge.
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
509 @return: bool or dict
814
59c7bc51c323 jp: refactoring using ArgParse
Dal <kedals0@gmail.com>
parents: 811
diff changeset
510 """
59c7bc51c323 jp: refactoring using ArgParse
Dal <kedals0@gmail.com>
parents: 811
diff changeset
511 raise NotImplementedError
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
512
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
513 def connected(self):
401
b2caa2615c4c jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents: 393
diff changeset
514 """Auto reply to confirmations requests"""
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
515 self.need_loop = True
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
516 super(CommandAnswering, self).connected()
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
517 # we watch confirmation signals
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
518 self.host.bridge.register("ask_confirmation", self._ask_confirmation)
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
519
542
3eeb6c865e4d frontends: incoming files transfer management:
Goffi <goffi@goffi.org>
parents: 538
diff changeset
520 #and we ask those we have missed
817
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
521 for confirm_id, confirm_type, data in self.host.bridge.getWaitingConf(self.profile):
c39117d00f35 jp: refactoring:
Goffi <goffi@goffi.org>
parents: 814
diff changeset
522 self._ask_confirmation(confirm_id, confirm_type, data, self.profile)