Mercurial > libervia-backend
annotate frontends/src/jp/base.py @ 1588:823a385235ef
jp: fixed bad --connect option check
author | Goffi <goffi@goffi.org> |
---|---|
date | Sat, 14 Nov 2015 19:18:10 +0100 |
parents | 3d5193b4c582 |
children | 313f2bb7841b |
rev | line source |
---|---|
0 | 1 #! /usr/bin/python |
2 # -*- coding: utf-8 -*- | |
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 | 5 # Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014, 2015 Jérôme Poisson (goffi@goffi.org) |
0 | 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 | 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 | 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 | 19 |
771 | 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 | 22 global pbar_available |
23 pbar_available = True #checked before using ProgressBar | |
24 | |
25 ### logging ### | |
26 import logging | |
27 from logging import debug, info, error, warning | |
28 logging.basicConfig(level=logging.DEBUG, | |
29 format='%(message)s') | |
30 ### | |
31 | |
32 import sys | |
817 | 33 import locale |
34 import os.path | |
35 import argparse | |
964 | 36 from gi.repository import GLib, GObject |
817 | 37 from glob import iglob |
38 from importlib import import_module | |
1139
75025461141f
move sat.tools.jid to sat_frontends.tools.jid
souliane <souliane@mailoo.org>
parents:
1033
diff
changeset
|
39 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
|
40 from sat_frontends.bridge.DBus import DBusBridgeFrontend |
817 | 41 from sat.core import exceptions |
42 import sat_frontends.jp | |
970
2e052998c7eb
jp: using C.APP_URL for application url
Goffi <goffi@goffi.org>
parents:
965
diff
changeset
|
43 from sat_frontends.jp.constants import Const as C |
0 | 44 try: |
817 | 45 import progressbar |
46 except ImportError: | |
47 info (_('ProgressBar not available, please download it at http://pypi.python.org/pypi/progressbar')) | |
48 info (_('Progress bar deactivated\n--\n')) | |
49 progressbar=None | |
50 | |
51 #consts | |
52 prog_name = u"jp" | |
53 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
|
54 Get the latest version at """ + C.APP_URL |
817 | 55 |
1396 | 56 copyleft = u"""Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014, 2015 Jérôme Poisson (aka Goffi) |
817 | 57 This program comes with ABSOLUTELY NO WARRANTY; |
58 This is free software, and you are welcome to redistribute it under certain conditions. | |
59 """ | |
0 | 60 |
61 | |
817 | 62 def unicode_decoder(arg): |
63 # Needed to have unicode strings from arguments | |
64 return arg.decode(locale.getpreferredencoding()) | |
814 | 65 |
0 | 66 |
817 | 67 class Jp(object): |
814 | 68 """ |
69 This class can be use to establish a connection with the | |
70 bridge. Moreover, it should manage a main loop. | |
71 | |
72 To use it, you mainly have to redefine the method run to perform | |
73 specify what kind of operation you want to perform. | |
74 | |
75 """ | |
817 | 76 def __init__(self): |
165
8a2053de6f8c
Frontends: management of unlaunched SàT Backend (information message and exit)
Goffi <goffi@goffi.org>
parents:
156
diff
changeset
|
77 try: |
817 | 78 self.bridge = DBusBridgeFrontend() |
79 except exceptions.BridgeExceptionNoService: | |
165
8a2053de6f8c
Frontends: management of unlaunched SàT Backend (information message and exit)
Goffi <goffi@goffi.org>
parents:
156
diff
changeset
|
80 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
|
81 sys.exit(1) |
864
241f6baa6687
frontends: fix typos, do not use logging in the xmlui tools:
souliane <souliane@mailoo.org>
parents:
823
diff
changeset
|
82 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
|
83 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
|
84 sys.exit(1) |
814 | 85 |
817 | 86 self.parser = argparse.ArgumentParser(formatter_class=argparse.RawDescriptionHelpFormatter, |
87 description=description) | |
88 | |
89 self._make_parents() | |
90 self.add_parser_options() | |
91 self.subparsers = self.parser.add_subparsers(title=_('Available commands'), dest='subparser_name') | |
92 self._auto_loop = False # when loop is used for internal reasons | |
93 self.need_loop = False # to set by commands when loop is needed | |
94 self._progress_id = None # TODO: manage several progress ids | |
95 self.quit_on_progress_end = True # set to False if you manage yourself exiting, or if you want the user to stop by himself | |
96 | |
97 @property | |
98 def version(self): | |
99 return self.bridge.getVersion() | |
814 | 100 |
817 | 101 @property |
102 def progress_id(self): | |
103 return self._progress_id | |
104 | |
105 @progress_id.setter | |
106 def progress_id(self, value): | |
107 self._progress_id = value | |
108 | |
109 def _make_parents(self): | |
110 self.parents = {} | |
111 | |
112 profile_parent = self.parents['profile'] = argparse.ArgumentParser(add_help=False) | |
113 profile_parent.add_argument("-p", "--profile", action="store", type=str, default='@DEFAULT@', help=_("Use PROFILE profile key (default: %(default)s)")) | |
1544
3d5193b4c582
jp: separate password and connection of profile in --connect and --pwd arguments
Goffi <goffi@goffi.org>
parents:
1401
diff
changeset
|
114 profile_parent.add_argument("--pwd", action="store", type=unicode, default='', metavar='PASSWORD', help=_("Password used to connect profile, if necessary")) |
3d5193b4c582
jp: separate password and connection of profile in --connect and --pwd arguments
Goffi <goffi@goffi.org>
parents:
1401
diff
changeset
|
115 profile_parent.add_argument("-c", "--connect", action="store_true", help=_("Connect the profile before doing anything else")) |
817 | 116 |
117 progress_parent = self.parents['progress'] = argparse.ArgumentParser(add_help=False) | |
118 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
|
119 progress_parent.add_argument("-P", "--progress", action="store_true", help=_("Show progress bar")) |
0 | 120 |
817 | 121 def add_parser_options(self): |
122 self.parser.add_argument('--version', action='version', version=("%(name)s %(version)s %(copyleft)s" % {'name': prog_name, 'version': self.version, 'copyleft': copyleft})) | |
123 | |
124 def import_commands(self): | |
125 """ Automaticaly import commands to jp | |
126 looks from modules names cmd_*.py in jp path and import them | |
127 | |
128 """ | |
129 path = os.path.dirname(sat_frontends.jp.__file__) | |
130 modules = (os.path.splitext(module)[0] for module in map(os.path.basename, iglob(os.path.join(path, "cmd_*.py")))) | |
131 for module_name in modules: | |
132 module = import_module("sat_frontends.jp."+module_name) | |
133 try: | |
134 self.import_command_module(module) | |
135 except ImportError: | |
136 continue | |
0 | 137 |
817 | 138 def import_command_module(self, module): |
139 """ Add commands from a module to jp | |
140 @param module: module containing commands | |
141 | |
142 """ | |
143 try: | |
144 for classname in module.__commands__: | |
145 cls = getattr(module, classname) | |
146 except AttributeError: | |
147 warning(_("Invalid module %s") % module) | |
148 raise ImportError | |
149 cls(self) | |
150 | |
151 | |
152 def run(self, args=None): | |
153 self.args = self.parser.parse_args(args) | |
154 self.args.func() | |
155 if self.need_loop or self._auto_loop: | |
156 self._start_loop() | |
157 | |
158 def _start_loop(self): | |
898
9720d3d0a764
jp: updated main loop to gobject 3
Matteo Cypriani <mcy@lm7.fr>
parents:
864
diff
changeset
|
159 self.loop = GLib.MainLoop() |
814 | 160 try: |
161 self.loop.run() | |
162 except KeyboardInterrupt: | |
163 info(_("User interruption: good bye")) | |
0 | 164 |
817 | 165 def stop_loop(self): |
166 try: | |
167 self.loop.quit() | |
168 except AttributeError: | |
169 pass | |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
170 |
817 | 171 def quit(self, errcode=0): |
172 self.stop_loop() | |
173 if errcode: | |
174 sys.exit(errcode) | |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
175 |
814 | 176 def check_jids(self, jids): |
177 """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
|
178 |
817 | 179 @param profile: profile name |
180 @param jids: list of jids | |
181 @return: List of jids | |
182 | |
814 | 183 """ |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
184 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
|
185 nodes2jid = {} |
393 | 186 |
814 | 187 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
|
188 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
|
189 _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
|
190 try: |
1ae9aa94c351
jp: fixed bad try/except hidding errors + fixed bad management of jids without node
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
191 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
|
192 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
|
193 pass |
1ae9aa94c351
jp: fixed bad try/except hidding errors + fixed bad management of jids without node
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
194 |
1ae9aa94c351
jp: fixed bad try/except hidding errors + fixed bad management of jids without node
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
195 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
|
196 nodes2jid[_jid.node.lower()] = jid_s |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
197 |
817 | 198 def expand_jid(jid): |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
199 _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
|
200 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
|
201 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
|
202 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
|
203 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
|
204 else: |
b7c4bb2c0668
jp: - better expandJid: roster's jids' nodes are used after names to expand jid
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
205 expanded = jid |
965 | 206 return expanded.decode('utf-8') |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
207 |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
208 def check(jid): |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
209 if not jid.is_valid: |
814 | 210 error (_("%s is not a valid JID !"), jid) |
817 | 211 self.quit(1) |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
212 |
814 | 213 dest_jids=[] |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
214 try: |
814 | 215 for i in range(len(jids)): |
817 | 216 dest_jids.append(expand_jid(jids[i])) |
814 | 217 check(dest_jids[i]) |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
218 except AttributeError: |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
219 pass |
0 | 220 |
814 | 221 return dest_jids |
0 | 222 |
817 | 223 def connect_profile(self, callback): |
1588
823a385235ef
jp: fixed bad --connect option check
Goffi <goffi@goffi.org>
parents:
1544
diff
changeset
|
224 """ 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
|
225 |
817 | 226 @param callback: method to call when profile is connected |
227 @exit: - 1 when profile is not connected and --connect is not set | |
228 - 1 when the profile doesn't exists | |
229 - 1 when there is a connection error | |
230 """ | |
231 # FIXME: need better exit codes | |
232 | |
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
|
233 def cant_connect(failure): |
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
|
234 error(_(u"Can't connect profile [%s]") % failure) |
817 | 235 self.quit(1) |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
236 |
817 | 237 self.profile = self.bridge.getProfileName(self.args.profile) |
0 | 238 |
817 | 239 if not self.profile: |
240 error(_("The profile [%s] doesn't exist") % self.args.profile) | |
241 self.quit(1) | |
242 | |
1401
265ff2bd8d67
jp: fixed crash on commands using profile without "connect" option
Goffi <goffi@goffi.org>
parents:
1396
diff
changeset
|
243 if not hasattr(self.args, 'connect'): |
1588
823a385235ef
jp: fixed bad --connect option check
Goffi <goffi@goffi.org>
parents:
1544
diff
changeset
|
244 # 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
|
245 return |
1588
823a385235ef
jp: fixed bad --connect option check
Goffi <goffi@goffi.org>
parents:
1544
diff
changeset
|
246 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
|
247 self.bridge.asyncConnect(self.profile, self.args.pwd, lambda dummy: callback(), cant_connect) |
817 | 248 self._auto_loop = True |
814 | 249 return |
817 | 250 elif not self.bridge.isConnected(self.profile): |
251 error(_(u"Profile [%(profile)s] is not connected, please connect it before using jp, or use --connect option") % { "profile": self.profile }) | |
252 self.quit(1) | |
0 | 253 |
817 | 254 callback() |
255 | |
256 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
|
257 """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
|
258 _jid = JID(param_jid) |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
259 if not _jid.resource: |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
260 #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
|
261 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
|
262 if last_resource: |
688
f7878ad3c846
tools: renamed tools.jid.JID attribute "short" to "bare"
souliane <souliane@mailoo.org>
parents:
657
diff
changeset
|
263 return "%s/%s" % (_jid.bare, last_resource) |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
264 return param_jid |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
265 |
817 | 266 def watch_progress(self): |
267 self.pbar = None | |
898
9720d3d0a764
jp: updated main loop to gobject 3
Matteo Cypriani <mcy@lm7.fr>
parents:
864
diff
changeset
|
268 GObject.timeout_add(10, self._progress_cb) |
0 | 269 |
817 | 270 def _progress_cb(self): |
271 if self.progress_id: | |
272 data = self.bridge.getProgress(self.progress_id, self.profile) | |
273 if data: | |
274 if not data['position']: | |
275 data['position'] = '0' | |
276 if not self.pbar: | |
277 #first answer, we must construct the bar | |
278 self.pbar = progressbar.ProgressBar(int(data['size']), | |
279 [_("Progress: "),progressbar.Percentage(), | |
280 " ", | |
281 progressbar.Bar(), | |
282 " ", | |
283 progressbar.FileTransferSpeed(), | |
284 " ", | |
285 progressbar.ETA()]) | |
286 self.pbar.start() | |
287 | |
288 self.pbar.update(int(data['position'])) | |
289 | |
290 elif self.pbar: | |
291 self.pbar.finish() | |
292 if self.quit_on_progress_end: | |
293 self.quit() | |
294 return False | |
295 | |
296 return True | |
814 | 297 |
298 | |
817 | 299 class CommandBase(object): |
814 | 300 |
817 | 301 def __init__(self, host, name, use_profile=True, use_progress=False, help=None, **kwargs): |
302 """ Initialise CommandBase | |
303 @param host: Jp instance | |
304 @param name: name of the new command | |
305 @param use_profile: if True, add profile selection/connection commands | |
306 @param use_progress: if True, add progress bar activation commands | |
307 @param help: help message to display | |
308 @param **kwargs: args passed to ArgumentParser | |
309 | |
814 | 310 """ |
817 | 311 try: # If we have subcommands, host is a CommandBase and we need to use host.host |
312 self.host = host.host | |
313 except AttributeError: | |
314 self.host = host | |
315 | |
316 parents = kwargs.setdefault('parents', set()) | |
317 if use_profile: | |
318 #self.host.parents['profile'] is an ArgumentParser with profile connection arguments | |
319 parents.add(self.host.parents['profile']) | |
320 if use_progress: | |
321 parents.add(self.host.parents['progress']) | |
322 | |
323 self.parser = host.subparsers.add_parser(name, help=help, **kwargs) | |
324 if hasattr(self, "subcommands"): | |
325 self.subparsers = self.parser.add_subparsers() | |
326 else: | |
327 self.parser.set_defaults(func=self.run) | |
328 self.add_parser_options() | |
329 | |
330 @property | |
331 def args(self): | |
332 return self.host.args | |
333 | |
334 @property | |
335 def need_loop(self): | |
336 return self.host.need_loop | |
337 | |
338 @need_loop.setter | |
339 def need_loop(self, value): | |
340 self.host.need_loop = value | |
341 | |
342 @property | |
343 def profile(self): | |
344 return self.host.profile | |
345 | |
346 @property | |
347 def progress_id(self): | |
348 return self.host.progress_id | |
814 | 349 |
817 | 350 @progress_id.setter |
351 def progress_id(self, value): | |
352 self.host.progress_id = value | |
353 | |
354 def add_parser_options(self): | |
355 try: | |
356 subcommands = self.subcommands | |
357 except AttributeError: | |
358 # We don't have subcommands, the class need to implements add_parser_options | |
359 raise NotImplementedError | |
360 | |
361 # now we add subcommands to ourself | |
362 for cls in subcommands: | |
363 cls(self) | |
814 | 364 |
817 | 365 def run(self): |
366 try: | |
367 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
|
368 connect_profile = self.host.connect_profile |
817 | 369 except AttributeError: |
370 # the command doesn't need to connect profile | |
371 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
|
372 else: |
1ae9aa94c351
jp: fixed bad try/except hidding errors + fixed bad management of jids without node
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
373 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
|
374 |
817 | 375 try: |
376 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
|
377 watch_progress = self.host.watch_progress |
817 | 378 except AttributeError: |
379 # the command doesn't use progress bar | |
380 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
|
381 else: |
1ae9aa94c351
jp: fixed bad try/except hidding errors + fixed bad management of jids without node
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
382 watch_progress() |
817 | 383 |
384 def connected(self): | |
385 if not self.need_loop: | |
386 self.host.stop_loop() | |
387 | |
388 | |
389 class CommandAnswering(CommandBase): | |
390 #FIXME: temp, will be refactored when progress_bar/confirmations will be refactored | |
391 | |
392 def _ask_confirmation(self, confirm_id, confirm_type, data, profile): | |
393 """ 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
|
394 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
|
395 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
|
396 return |
817 | 397 if confirm_type == self.confirm_type: |
398 if self.dest_jids and not JID(data['from']).bare in [JID(_jid).bare for _jid in self.dest_jids()]: | |
0 | 399 return #file is not sent by a filtered jid |
400 else: | |
817 | 401 self.ask(data, confirm_id) |
0 | 402 |
814 | 403 def ask(self): |
404 """ | |
405 The return value is used to answer to the bridge. | |
817 | 406 @return: bool or dict |
814 | 407 """ |
408 raise NotImplementedError | |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
409 |
817 | 410 def connected(self): |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
411 """Auto reply to confirmations requests""" |
817 | 412 self.need_loop = True |
413 super(CommandAnswering, self).connected() | |
414 # we watch confirmation signals | |
415 self.host.bridge.register("ask_confirmation", self._ask_confirmation) | |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
416 |
542
3eeb6c865e4d
frontends: incoming files transfer management:
Goffi <goffi@goffi.org>
parents:
538
diff
changeset
|
417 #and we ask those we have missed |
817 | 418 for confirm_id, confirm_type, data in self.host.bridge.getWaitingConf(self.profile): |
419 self._ask_confirmation(confirm_id, confirm_type, data, self.profile) |