Mercurial > libervia-backend
annotate sat_frontends/jp/base.py @ 3043:3df611adb598
jp: handle dbus bridge with asyncio:
D-Bus bridge is now working again, using the new AsyncIO version. To make it work, the
GLib loop is run in a separated thread.
Loops have been moved to the `loops` module.
author | Goffi <goffi@goffi.org> |
---|---|
date | Tue, 01 Oct 2019 22:49:10 +0200 |
parents | fee60f17ebac |
children | d9f328374473 |
rev | line source |
---|---|
3043
3df611adb598
jp: handle dbus bridge with asyncio:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
1 #!/usr/bin/env python3 |
0 | 2 |
609
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
601
diff
changeset
|
3 # jp: a SAT command line tool |
2771 | 4 # Copyright (C) 2009-2019 Jérôme Poisson (goffi@goffi.org) |
0 | 5 |
609
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
601
diff
changeset
|
6 # 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
|
7 # 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
|
8 # 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
|
9 # (at your option) any later version. |
0 | 10 |
609
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
601
diff
changeset
|
11 # 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
|
12 # 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
|
13 # 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
|
14 # GNU Affero General Public License for more details. |
0 | 15 |
609
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
601
diff
changeset
|
16 # 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
|
17 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
0 | 18 |
3040 | 19 import asyncio |
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 ### 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
|
23 import logging as log |
3040 | 24 log.basicConfig(level=log.WARNING, |
25 format='[%(name)s] %(message)s') | |
0 | 26 ### |
27 | |
28 import sys | |
3040 | 29 import os |
817 | 30 import os.path |
31 import argparse | |
3040 | 32 import inspect |
33 from pathlib import Path | |
817 | 34 from glob import iglob |
35 from importlib import import_module | |
1139
75025461141f
move sat.tools.jid to sat_frontends.tools.jid
souliane <souliane@mailoo.org>
parents:
1033
diff
changeset
|
36 from sat_frontends.tools.jid import JID |
2098
e0066920a661
primitivus, jp: dynamic bridge + fixed D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
2086
diff
changeset
|
37 from sat.tools import config |
e0066920a661
primitivus, jp: dynamic bridge + fixed D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
2086
diff
changeset
|
38 from sat.tools.common import dynamic_import |
2532 | 39 from sat.tools.common import uri |
2704
ab37d1c7c38c
jp (base): new date_decoder to specify dates in arguments
Goffi <goffi@goffi.org>
parents:
2692
diff
changeset
|
40 from sat.tools.common import date_utils |
817 | 41 from sat.core import exceptions |
42 import sat_frontends.jp | |
3043
3df611adb598
jp: handle dbus bridge with asyncio:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
43 from sat_frontends.jp.loops import QuitException, getJPLoop |
970
2e052998c7eb
jp: using C.APP_URL for application url
Goffi <goffi@goffi.org>
parents:
965
diff
changeset
|
44 from sat_frontends.jp.constants import Const as C |
2532 | 45 from sat_frontends.tools import misc |
1840
9eabf7fadfdd
frontends (jp): onActionNew displays xmlui message if no meta_type is defined and xmlui is defined
souliane <souliane@mailoo.org>
parents:
1832
diff
changeset
|
46 import xml.etree.ElementTree as ET # FIXME: used temporarily to manage XMLUI |
1950 | 47 from collections import OrderedDict |
1872
df1ca137b0cb
jp (blog/edit): editor arguments can now be specified on sat.conf, and default on are applied for known editors:
Goffi <goffi@goffi.org>
parents:
1869
diff
changeset
|
48 |
2098
e0066920a661
primitivus, jp: dynamic bridge + fixed D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
2086
diff
changeset
|
49 ## bridge handling |
e0066920a661
primitivus, jp: dynamic bridge + fixed D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
2086
diff
changeset
|
50 # we get bridge name from conf and initialise the right class accordingly |
e0066920a661
primitivus, jp: dynamic bridge + fixed D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
2086
diff
changeset
|
51 main_config = config.parseMainConf() |
e0066920a661
primitivus, jp: dynamic bridge + fixed D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
2086
diff
changeset
|
52 bridge_name = config.getConfig(main_config, '', 'bridge', 'dbus') |
3043
3df611adb598
jp: handle dbus bridge with asyncio:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
53 JPLoop = getJPLoop(bridge_name) |
2098
e0066920a661
primitivus, jp: dynamic bridge + fixed D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
2086
diff
changeset
|
54 |
e0066920a661
primitivus, jp: dynamic bridge + fixed D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
2086
diff
changeset
|
55 |
0 | 56 try: |
817 | 57 import progressbar |
58 except ImportError: | |
3040 | 59 msg = (_('ProgressBar not available, please download it at ' |
60 'http://pypi.python.org/pypi/progressbar\n' | |
61 'Progress bar deactivated\n--\n')) | |
62 print(msg, file=sys.stderr) | |
817 | 63 progressbar=None |
64 | |
65 #consts | |
3028 | 66 PROG_NAME = "jp" |
1606
de785fcf9a7b
jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents:
1605
diff
changeset
|
67 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
|
68 Get the latest version at """ + C.APP_URL |
817 | 69 |
3028 | 70 COPYLEFT = """Copyright (C) 2009-2019 Jérôme Poisson, Adrien Cossa |
817 | 71 This program comes with ABSOLUTELY NO WARRANTY; |
72 This is free software, and you are welcome to redistribute it under certain conditions. | |
73 """ | |
0 | 74 |
3040 | 75 PROGRESS_DELAY = 0.1 # the progression will be checked every PROGRESS_DELAY s |
1606
de785fcf9a7b
jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents:
1605
diff
changeset
|
76 |
0 | 77 |
2704
ab37d1c7c38c
jp (base): new date_decoder to specify dates in arguments
Goffi <goffi@goffi.org>
parents:
2692
diff
changeset
|
78 def date_decoder(arg): |
ab37d1c7c38c
jp (base): new date_decoder to specify dates in arguments
Goffi <goffi@goffi.org>
parents:
2692
diff
changeset
|
79 return date_utils.date_parse_ext(arg, default_tz=date_utils.TZ_LOCAL) |
ab37d1c7c38c
jp (base): new date_decoder to specify dates in arguments
Goffi <goffi@goffi.org>
parents:
2692
diff
changeset
|
80 |
ab37d1c7c38c
jp (base): new date_decoder to specify dates in arguments
Goffi <goffi@goffi.org>
parents:
2692
diff
changeset
|
81 |
817 | 82 class Jp(object): |
814 | 83 """ |
84 This class can be use to establish a connection with the | |
85 bridge. Moreover, it should manage a main loop. | |
86 | |
87 To use it, you mainly have to redefine the method run to perform | |
88 specify what kind of operation you want to perform. | |
89 | |
90 """ | |
817 | 91 def __init__(self): |
1606
de785fcf9a7b
jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents:
1605
diff
changeset
|
92 """ |
de785fcf9a7b
jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents:
1605
diff
changeset
|
93 |
3040 | 94 @attribute quit_on_progress_end (bool): set to False if you manage yourself |
95 exiting, or if you want the user to stop by himself | |
1606
de785fcf9a7b
jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents:
1605
diff
changeset
|
96 @attribute progress_success(callable): method to call when progress just started |
de785fcf9a7b
jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents:
1605
diff
changeset
|
97 by default display a message |
3040 | 98 @attribute progress_success(callable): method to call when progress is |
99 successfully finished by default display a message | |
1606
de785fcf9a7b
jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents:
1605
diff
changeset
|
100 @attribute progress_failure(callable): method to call when progress failed |
de785fcf9a7b
jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents:
1605
diff
changeset
|
101 by default display a message |
de785fcf9a7b
jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents:
1605
diff
changeset
|
102 """ |
2098
e0066920a661
primitivus, jp: dynamic bridge + fixed D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
2086
diff
changeset
|
103 bridge_module = dynamic_import.bridge(bridge_name, 'sat_frontends.bridge') |
e0066920a661
primitivus, jp: dynamic bridge + fixed D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
2086
diff
changeset
|
104 if bridge_module is None: |
3028 | 105 log.error("Can't import {} bridge".format(bridge_name)) |
165
8a2053de6f8c
Frontends: management of unlaunched SàT Backend (information message and exit)
Goffi <goffi@goffi.org>
parents:
156
diff
changeset
|
106 sys.exit(1) |
814 | 107 |
3040 | 108 self.bridge = bridge_module.AIOBridge() |
109 self._onQuitCallbacks = [] | |
2098
e0066920a661
primitivus, jp: dynamic bridge + fixed D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
2086
diff
changeset
|
110 |
3040 | 111 def _bridgeConnected(self): |
112 self.parser = argparse.ArgumentParser( | |
113 formatter_class=argparse.RawDescriptionHelpFormatter, description=DESCRIPTION) | |
817 | 114 self._make_parents() |
115 self.add_parser_options() | |
3040 | 116 self.subparsers = self.parser.add_subparsers( |
117 title=_('Available commands'), dest='command', required=True) | |
1606
de785fcf9a7b
jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents:
1605
diff
changeset
|
118 |
de785fcf9a7b
jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents:
1605
diff
changeset
|
119 # progress attributes |
817 | 120 self._progress_id = None # TODO: manage several progress ids |
1606
de785fcf9a7b
jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents:
1605
diff
changeset
|
121 self.quit_on_progress_end = True |
817 | 122 |
1950 | 123 # outputs |
124 self._outputs = {} | |
125 for type_ in C.OUTPUT_TYPES: | |
126 self._outputs[type_] = OrderedDict() | |
2189
a25a256688e2
jp (base): output can now specify a default when registering
Goffi <goffi@goffi.org>
parents:
2188
diff
changeset
|
127 self.default_output = {} |
1950 | 128 |
2705
0bb811aaf11d
jp (base): new own_jid host attribute:
Goffi <goffi@goffi.org>
parents:
2704
diff
changeset
|
129 self.own_jid = None # must be filled at runtime if needed |
0bb811aaf11d
jp (base): new own_jid host attribute:
Goffi <goffi@goffi.org>
parents:
2704
diff
changeset
|
130 |
817 | 131 @property |
132 def progress_id(self): | |
133 return self._progress_id | |
134 | |
3040 | 135 async def set_progress_id(self, progress_id): |
136 # because we use async, we need an explicit setter | |
137 self._progress_id = progress_id | |
138 await self.replayCache('progress_ids_cache') | |
817 | 139 |
1606
de785fcf9a7b
jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents:
1605
diff
changeset
|
140 @property |
de785fcf9a7b
jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents:
1605
diff
changeset
|
141 def watch_progress(self): |
de785fcf9a7b
jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents:
1605
diff
changeset
|
142 try: |
de785fcf9a7b
jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents:
1605
diff
changeset
|
143 self.pbar |
de785fcf9a7b
jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents:
1605
diff
changeset
|
144 except AttributeError: |
de785fcf9a7b
jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents:
1605
diff
changeset
|
145 return False |
de785fcf9a7b
jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents:
1605
diff
changeset
|
146 else: |
de785fcf9a7b
jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents:
1605
diff
changeset
|
147 return True |
de785fcf9a7b
jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents:
1605
diff
changeset
|
148 |
de785fcf9a7b
jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents:
1605
diff
changeset
|
149 @watch_progress.setter |
de785fcf9a7b
jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents:
1605
diff
changeset
|
150 def watch_progress(self, watch_progress): |
de785fcf9a7b
jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents:
1605
diff
changeset
|
151 if watch_progress: |
de785fcf9a7b
jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents:
1605
diff
changeset
|
152 self.pbar = None |
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
|
153 |
1605
0aded9648c5c
jp (base): added a self.disp method which manage verbosity and stderr + verbosity property
Goffi <goffi@goffi.org>
parents:
1604
diff
changeset
|
154 @property |
0aded9648c5c
jp (base): added a self.disp method which manage verbosity and stderr + verbosity property
Goffi <goffi@goffi.org>
parents:
1604
diff
changeset
|
155 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
|
156 try: |
0aded9648c5c
jp (base): added a self.disp method which manage verbosity and stderr + verbosity property
Goffi <goffi@goffi.org>
parents:
1604
diff
changeset
|
157 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
|
158 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
|
159 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
|
160 |
3040 | 161 async def replayCache(self, cache_attribute): |
1642
7ec7ce9cdc4c
jp (base): progressStarted signals are cached until progress_id is known, this avoid missing the progression if we have the signal before the id.
Goffi <goffi@goffi.org>
parents:
1641
diff
changeset
|
162 """Replay cached signals |
7ec7ce9cdc4c
jp (base): progressStarted signals are cached until progress_id is known, this avoid missing the progression if we have the signal before the id.
Goffi <goffi@goffi.org>
parents:
1641
diff
changeset
|
163 |
7ec7ce9cdc4c
jp (base): progressStarted signals are cached until progress_id is known, this avoid missing the progression if we have the signal before the id.
Goffi <goffi@goffi.org>
parents:
1641
diff
changeset
|
164 @param cache_attribute(str): name of the attribute containing the cache |
7ec7ce9cdc4c
jp (base): progressStarted signals are cached until progress_id is known, this avoid missing the progression if we have the signal before the id.
Goffi <goffi@goffi.org>
parents:
1641
diff
changeset
|
165 if the attribute doesn't exist, there is no cache and the call is ignored |
3040 | 166 else the cache must be a list of tuples containing the replay callback as |
167 first item, then the arguments to use | |
1642
7ec7ce9cdc4c
jp (base): progressStarted signals are cached until progress_id is known, this avoid missing the progression if we have the signal before the id.
Goffi <goffi@goffi.org>
parents:
1641
diff
changeset
|
168 """ |
7ec7ce9cdc4c
jp (base): progressStarted signals are cached until progress_id is known, this avoid missing the progression if we have the signal before the id.
Goffi <goffi@goffi.org>
parents:
1641
diff
changeset
|
169 try: |
7ec7ce9cdc4c
jp (base): progressStarted signals are cached until progress_id is known, this avoid missing the progression if we have the signal before the id.
Goffi <goffi@goffi.org>
parents:
1641
diff
changeset
|
170 cache = getattr(self, cache_attribute) |
7ec7ce9cdc4c
jp (base): progressStarted signals are cached until progress_id is known, this avoid missing the progression if we have the signal before the id.
Goffi <goffi@goffi.org>
parents:
1641
diff
changeset
|
171 except AttributeError: |
7ec7ce9cdc4c
jp (base): progressStarted signals are cached until progress_id is known, this avoid missing the progression if we have the signal before the id.
Goffi <goffi@goffi.org>
parents:
1641
diff
changeset
|
172 pass |
7ec7ce9cdc4c
jp (base): progressStarted signals are cached until progress_id is known, this avoid missing the progression if we have the signal before the id.
Goffi <goffi@goffi.org>
parents:
1641
diff
changeset
|
173 else: |
7ec7ce9cdc4c
jp (base): progressStarted signals are cached until progress_id is known, this avoid missing the progression if we have the signal before the id.
Goffi <goffi@goffi.org>
parents:
1641
diff
changeset
|
174 for cache_data in cache: |
3040 | 175 await cache_data[0](*cache_data[1:]) |
1642
7ec7ce9cdc4c
jp (base): progressStarted signals are cached until progress_id is known, this avoid missing the progression if we have the signal before the id.
Goffi <goffi@goffi.org>
parents:
1641
diff
changeset
|
176 |
2277
637886ac35f6
jp (base): added no_lf in disp() to avoid printing a line feed
Goffi <goffi@goffi.org>
parents:
2237
diff
changeset
|
177 def disp(self, msg, verbosity=0, error=False, no_lf=False): |
1605
0aded9648c5c
jp (base): added a self.disp method which manage verbosity and stderr + verbosity property
Goffi <goffi@goffi.org>
parents:
1604
diff
changeset
|
178 """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
|
179 |
0aded9648c5c
jp (base): added a self.disp method which manage verbosity and stderr + verbosity property
Goffi <goffi@goffi.org>
parents:
1604
diff
changeset
|
180 @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
|
181 @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
|
182 @param error(bool): if True, print to stderr instead of stdout |
2277
637886ac35f6
jp (base): added no_lf in disp() to avoid printing a line feed
Goffi <goffi@goffi.org>
parents:
2237
diff
changeset
|
183 @param no_lf(bool): if True, do not emit line feed at the end of line |
1605
0aded9648c5c
jp (base): added a self.disp method which manage verbosity and stderr + verbosity property
Goffi <goffi@goffi.org>
parents:
1604
diff
changeset
|
184 """ |
0aded9648c5c
jp (base): added a self.disp method which manage verbosity and stderr + verbosity property
Goffi <goffi@goffi.org>
parents:
1604
diff
changeset
|
185 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
|
186 if error: |
2277
637886ac35f6
jp (base): added no_lf in disp() to avoid printing a line feed
Goffi <goffi@goffi.org>
parents:
2237
diff
changeset
|
187 if no_lf: |
3028 | 188 print(msg, end=' ', file=sys.stderr) |
2277
637886ac35f6
jp (base): added no_lf in disp() to avoid printing a line feed
Goffi <goffi@goffi.org>
parents:
2237
diff
changeset
|
189 else: |
3028 | 190 print(msg, file=sys.stderr) |
1605
0aded9648c5c
jp (base): added a self.disp method which manage verbosity and stderr + verbosity property
Goffi <goffi@goffi.org>
parents:
1604
diff
changeset
|
191 else: |
2277
637886ac35f6
jp (base): added no_lf in disp() to avoid printing a line feed
Goffi <goffi@goffi.org>
parents:
2237
diff
changeset
|
192 if no_lf: |
3028 | 193 print(msg, end=' ') |
2277
637886ac35f6
jp (base): added no_lf in disp() to avoid printing a line feed
Goffi <goffi@goffi.org>
parents:
2237
diff
changeset
|
194 else: |
3028 | 195 print(msg) |
1605
0aded9648c5c
jp (base): added a self.disp method which manage verbosity and stderr + verbosity property
Goffi <goffi@goffi.org>
parents:
1604
diff
changeset
|
196 |
3040 | 197 async def output(self, type_, name, extra_outputs, data): |
2155 | 198 if name in extra_outputs: |
3040 | 199 method = extra_outputs[name] |
2155 | 200 else: |
3040 | 201 method = self._outputs[type_][name]['callback'] |
202 | |
203 ret = method(data) | |
204 if inspect.isawaitable(ret): | |
205 await ret | |
1950 | 206 |
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
|
207 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
|
208 """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
|
209 |
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
|
210 @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
|
211 """ |
3040 | 212 self._onQuitCallbacks.append((callback, args, kwargs)) |
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
|
213 |
1950 | 214 def getOutputChoices(self, output_type): |
215 """Return valid output filters for output_type | |
216 | |
217 @param output_type: True for default, | |
218 else can be any registered type | |
219 """ | |
3028 | 220 return list(self._outputs[output_type].keys()) |
1950 | 221 |
817 | 222 def _make_parents(self): |
223 self.parents = {} | |
224 | |
3040 | 225 # we have a special case here as the start-session option is present only if |
226 # connection is not needed, so we create two similar parents, one with the | |
227 # option, the other one without it | |
1594
313f2bb7841b
jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents:
1588
diff
changeset
|
228 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
|
229 parent = self.parents[parent_name] = argparse.ArgumentParser(add_help=False) |
3040 | 230 parent.add_argument( |
231 "-p", "--profile", action="store", type=str, default='@DEFAULT@', | |
232 help=_("Use PROFILE profile key (default: %(default)s)")) | |
233 parent.add_argument( | |
234 "--pwd", action="store", default='', metavar='PASSWORD', | |
235 help=_("Password used to connect profile, if necessary")) | |
1594
313f2bb7841b
jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents:
1588
diff
changeset
|
236 |
3040 | 237 profile_parent, profile_session_parent = (self.parents['profile'], |
238 self.parents['profile_session']) | |
1594
313f2bb7841b
jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents:
1588
diff
changeset
|
239 |
3040 | 240 connect_short, connect_long, connect_action, connect_help = ( |
241 "-c", "--connect", "store_true", | |
242 _("Connect the profile before doing anything else") | |
243 ) | |
244 profile_parent.add_argument( | |
245 connect_short, connect_long, action=connect_action, help=connect_help) | |
1594
313f2bb7841b
jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents:
1588
diff
changeset
|
246 |
313f2bb7841b
jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents:
1588
diff
changeset
|
247 profile_session_connect_group = profile_session_parent.add_mutually_exclusive_group() |
3040 | 248 profile_session_connect_group.add_argument( |
249 connect_short, connect_long, action=connect_action, help=connect_help) | |
250 profile_session_connect_group.add_argument( | |
251 "--start-session", action="store_true", | |
252 help=_("Start a profile session without connecting")) | |
817 | 253 |
3040 | 254 progress_parent = self.parents['progress'] = argparse.ArgumentParser( |
255 add_help=False) | |
817 | 256 if progressbar: |
3040 | 257 progress_parent.add_argument( |
258 "-P", "--progress", action="store_true", help=_("Show progress bar")) | |
0 | 259 |
1600 | 260 verbose_parent = self.parents['verbose'] = argparse.ArgumentParser(add_help=False) |
3040 | 261 verbose_parent.add_argument( |
262 '--verbose', '-v', action='count', default=0, | |
263 help=_("Add a verbosity level (can be used multiple times)")) | |
1600 | 264 |
2532 | 265 draft_parent = self.parents['draft'] = argparse.ArgumentParser(add_help=False) |
266 draft_group = draft_parent.add_argument_group(_('draft handling')) | |
3040 | 267 draft_group.add_argument( |
268 "-D", "--current", action="store_true", help=_("load current draft")) | |
269 draft_group.add_argument( | |
270 "-F", "--draft-path", type=Path, help=_("path to a draft file to retrieve")) | |
2532 | 271 |
272 | |
2547
2d69a0afe039
jp: new argument pubsub_default can be used in Commands to replace default value in help string
Goffi <goffi@goffi.org>
parents:
2539
diff
changeset
|
273 def make_pubsub_group(self, flags, defaults): |
2532 | 274 """generate pubsub options according to flags |
275 | |
276 @param flags(iterable[unicode]): see [CommandBase.__init__] | |
2547
2d69a0afe039
jp: new argument pubsub_default can be used in Commands to replace default value in help string
Goffi <goffi@goffi.org>
parents:
2539
diff
changeset
|
277 @param defaults(dict[unicode, unicode]): help text for default value |
2d69a0afe039
jp: new argument pubsub_default can be used in Commands to replace default value in help string
Goffi <goffi@goffi.org>
parents:
2539
diff
changeset
|
278 key can be "service" or "node" |
2d69a0afe039
jp: new argument pubsub_default can be used in Commands to replace default value in help string
Goffi <goffi@goffi.org>
parents:
2539
diff
changeset
|
279 value will be set in " (DEFAULT: {value})", or can be None to remove DEFAULT |
2532 | 280 @return (ArgumentParser): parser to add |
281 """ | |
282 flags = misc.FlagsHandler(flags) | |
283 parent = argparse.ArgumentParser(add_help=False) | |
284 pubsub_group = parent.add_argument_group('pubsub') | |
3028 | 285 pubsub_group.add_argument("-u", "--pubsub-url", |
286 help=_("Pubsub URL (xmpp or http)")) | |
2532 | 287 |
3028 | 288 service_help = _("JID of the PubSub service") |
2532 | 289 if not flags.service: |
3028 | 290 default = defaults.pop('service', _('PEP service')) |
2547
2d69a0afe039
jp: new argument pubsub_default can be used in Commands to replace default value in help string
Goffi <goffi@goffi.org>
parents:
2539
diff
changeset
|
291 if default is not None: |
3028 | 292 service_help += _(" (DEFAULT: {default})".format(default=default)) |
293 pubsub_group.add_argument("-s", "--service", default='', | |
2532 | 294 help=service_help) |
295 | |
3028 | 296 node_help = _("node to request") |
2532 | 297 if not flags.node: |
3028 | 298 default = defaults.pop('node', _('standard node')) |
2547
2d69a0afe039
jp: new argument pubsub_default can be used in Commands to replace default value in help string
Goffi <goffi@goffi.org>
parents:
2539
diff
changeset
|
299 if default is not None: |
3028 | 300 node_help += _(" (DEFAULT: {default})".format(default=default)) |
301 pubsub_group.add_argument("-n", "--node", default='', help=node_help) | |
2532 | 302 |
303 if flags.single_item: | |
3028 | 304 item_help = ("item to retrieve") |
2552
38e1e29c48e9
jp (base): added C.ITEM pubsub flag when --item is required
Goffi <goffi@goffi.org>
parents:
2550
diff
changeset
|
305 if not flags.item: |
3028 | 306 default = defaults.pop('item', _('last item')) |
2552
38e1e29c48e9
jp (base): added C.ITEM pubsub flag when --item is required
Goffi <goffi@goffi.org>
parents:
2550
diff
changeset
|
307 if default is not None: |
3028 | 308 item_help += _(" (DEFAULT: {default})".format(default=default)) |
309 pubsub_group.add_argument("-i", "--item", default='', | |
2910
b2f323237fce
jp, plugin merge-requests: used u'' as default for item id in pubsub arguments + fixed some required arguments:
Goffi <goffi@goffi.org>
parents:
2775
diff
changeset
|
310 help=item_help) |
3040 | 311 pubsub_group.add_argument( |
312 "-L", "--last-item", action='store_true', help=_('retrieve last item')) | |
2532 | 313 elif flags.multi_items: |
2764
92af49cde255
jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents:
2761
diff
changeset
|
314 # mutiple items, this activate several features: max-items, RSM, MAM |
92af49cde255
jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents:
2761
diff
changeset
|
315 # and Orbder-by |
3040 | 316 pubsub_group.add_argument( |
317 "-i", "--item", action='append', dest='items', default=[], | |
318 help=_("items to retrieve (DEFAULT: all)")) | |
2539 | 319 if not flags.no_max: |
2775
2dfd5b1d39df
jp (base): fixed default values for --max-items and --max when using pubsub
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
320 max_group = pubsub_group.add_mutually_exclusive_group() |
2dfd5b1d39df
jp (base): fixed default values for --max-items and --max when using pubsub
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
321 # XXX: defaut value for --max-items or --max is set in parse_pubsub_args |
2dfd5b1d39df
jp (base): fixed default values for --max-items and --max when using pubsub
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
322 max_group.add_argument( |
2dfd5b1d39df
jp (base): fixed default values for --max-items and --max when using pubsub
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
323 "-M", "--max-items", dest="max", type=int, |
3028 | 324 help=_("maximum number of items to get ({no_limit} to get all items)" |
2764
92af49cde255
jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents:
2761
diff
changeset
|
325 .format(no_limit=C.NO_LIMIT))) |
2775
2dfd5b1d39df
jp (base): fixed default values for --max-items and --max when using pubsub
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
326 # FIXME: it could be possible to no duplicate max (between pubsub |
2dfd5b1d39df
jp (base): fixed default values for --max-items and --max when using pubsub
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
327 # max-items and RSM max)should not be duplicated, RSM could be |
2dfd5b1d39df
jp (base): fixed default values for --max-items and --max when using pubsub
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
328 # used when available and pubsub max otherwise |
2dfd5b1d39df
jp (base): fixed default values for --max-items and --max when using pubsub
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
329 max_group.add_argument( |
2dfd5b1d39df
jp (base): fixed default values for --max-items and --max when using pubsub
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
330 "-m", "--max", dest="rsm_max", type=int, |
3028 | 331 help=_("maximum number of items to get per page (DEFAULT: 10)")) |
2764
92af49cde255
jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents:
2761
diff
changeset
|
332 |
92af49cde255
jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents:
2761
diff
changeset
|
333 # RSM |
92af49cde255
jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents:
2761
diff
changeset
|
334 |
92af49cde255
jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents:
2761
diff
changeset
|
335 rsm_page_group = pubsub_group.add_mutually_exclusive_group() |
92af49cde255
jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents:
2761
diff
changeset
|
336 rsm_page_group.add_argument( |
3028 | 337 "-a", "--after", dest="rsm_after", |
338 help=_("find page after this item"), metavar='ITEM_ID') | |
2764
92af49cde255
jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents:
2761
diff
changeset
|
339 rsm_page_group.add_argument( |
3028 | 340 "-b", "--before", dest="rsm_before", |
341 help=_("find page before this item"), metavar='ITEM_ID') | |
2764
92af49cde255
jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents:
2761
diff
changeset
|
342 rsm_page_group.add_argument( |
92af49cde255
jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents:
2761
diff
changeset
|
343 "--index", dest="rsm_index", type=int, |
3028 | 344 help=_("index of the page to retrieve")) |
2764
92af49cde255
jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents:
2761
diff
changeset
|
345 |
92af49cde255
jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents:
2761
diff
changeset
|
346 |
92af49cde255
jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents:
2761
diff
changeset
|
347 # MAM |
92af49cde255
jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents:
2761
diff
changeset
|
348 |
92af49cde255
jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents:
2761
diff
changeset
|
349 pubsub_group.add_argument( |
3028 | 350 "-f", "--filter", dest='mam_filters', nargs=2, |
351 action='append', default=[], help=_("MAM filters to use"), | |
352 metavar=("FILTER_NAME", "VALUE") | |
2764
92af49cde255
jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents:
2761
diff
changeset
|
353 ) |
92af49cde255
jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents:
2761
diff
changeset
|
354 |
92af49cde255
jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents:
2761
diff
changeset
|
355 # Order-By |
92af49cde255
jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents:
2761
diff
changeset
|
356 |
92af49cde255
jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents:
2761
diff
changeset
|
357 # TODO: order-by should be a list to handle several levels of ordering |
92af49cde255
jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents:
2761
diff
changeset
|
358 # but this is not yet done in SàT (and not really useful with |
92af49cde255
jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents:
2761
diff
changeset
|
359 # current specifications, as only "creation" and "modification" are |
92af49cde255
jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents:
2761
diff
changeset
|
360 # available) |
92af49cde255
jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents:
2761
diff
changeset
|
361 pubsub_group.add_argument( |
92af49cde255
jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents:
2761
diff
changeset
|
362 "-o", "--order-by", choices=[C.ORDER_BY_CREATION, |
92af49cde255
jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents:
2761
diff
changeset
|
363 C.ORDER_BY_MODIFICATION], |
3028 | 364 help=_("how items should be ordered")) |
2532 | 365 |
2615
b4ecbcc2fd08
tools (misc): do not remove flags while using them anymore, instead added "all_used" and "unused" properties in FlagsHandler
Goffi <goffi@goffi.org>
parents:
2614
diff
changeset
|
366 if not flags.all_used: |
3040 | 367 raise exceptions.InternalError('unknown flags: {flags}'.format( |
368 flags=', '.join(flags.unused))) | |
2547
2d69a0afe039
jp: new argument pubsub_default can be used in Commands to replace default value in help string
Goffi <goffi@goffi.org>
parents:
2539
diff
changeset
|
369 if defaults: |
3040 | 370 raise exceptions.InternalError(f'unused defaults: {defaults}') |
2532 | 371 |
372 return parent | |
2235
4db836386641
jp: added use_pubsub and use_pubsub_node_req arguments to CommandBase
Goffi <goffi@goffi.org>
parents:
2213
diff
changeset
|
373 |
817 | 374 def add_parser_options(self): |
3040 | 375 self.parser.add_argument( |
376 '--version', | |
377 action='version', | |
378 version=("{name} {version} {copyleft}".format( | |
379 name = PROG_NAME, | |
380 version = self.version, | |
381 copyleft = COPYLEFT)) | |
382 ) | |
817 | 383 |
2189
a25a256688e2
jp (base): output can now specify a default when registering
Goffi <goffi@goffi.org>
parents:
2188
diff
changeset
|
384 def register_output(self, type_, name, callback, description="", default=False): |
1950 | 385 if type_ not in C.OUTPUT_TYPES: |
3028 | 386 log.error("Invalid output type {}".format(type_)) |
1950 | 387 return |
388 self._outputs[type_][name] = {'callback': callback, | |
389 'description': description | |
390 } | |
2189
a25a256688e2
jp (base): output can now specify a default when registering
Goffi <goffi@goffi.org>
parents:
2188
diff
changeset
|
391 if default: |
a25a256688e2
jp (base): output can now specify a default when registering
Goffi <goffi@goffi.org>
parents:
2188
diff
changeset
|
392 if type_ in self.default_output: |
3040 | 393 self.disp( |
394 _(f'there is already a default output for {type_}, ignoring new one') | |
395 ) | |
2189
a25a256688e2
jp (base): output can now specify a default when registering
Goffi <goffi@goffi.org>
parents:
2188
diff
changeset
|
396 else: |
a25a256688e2
jp (base): output can now specify a default when registering
Goffi <goffi@goffi.org>
parents:
2188
diff
changeset
|
397 self.default_output[type_] = name |
a25a256688e2
jp (base): output can now specify a default when registering
Goffi <goffi@goffi.org>
parents:
2188
diff
changeset
|
398 |
1950 | 399 |
2187
4ec72927a222
jp (outputs): moved output options parsing and checking to base methods
Goffi <goffi@goffi.org>
parents:
2178
diff
changeset
|
400 def parse_output_options(self): |
4ec72927a222
jp (outputs): moved output options parsing and checking to base methods
Goffi <goffi@goffi.org>
parents:
2178
diff
changeset
|
401 options = self.command.args.output_opts |
4ec72927a222
jp (outputs): moved output options parsing and checking to base methods
Goffi <goffi@goffi.org>
parents:
2178
diff
changeset
|
402 options_dict = {} |
4ec72927a222
jp (outputs): moved output options parsing and checking to base methods
Goffi <goffi@goffi.org>
parents:
2178
diff
changeset
|
403 for option in options: |
4ec72927a222
jp (outputs): moved output options parsing and checking to base methods
Goffi <goffi@goffi.org>
parents:
2178
diff
changeset
|
404 try: |
3028 | 405 key, value = option.split('=', 1) |
2187
4ec72927a222
jp (outputs): moved output options parsing and checking to base methods
Goffi <goffi@goffi.org>
parents:
2178
diff
changeset
|
406 except ValueError: |
4ec72927a222
jp (outputs): moved output options parsing and checking to base methods
Goffi <goffi@goffi.org>
parents:
2178
diff
changeset
|
407 key, value = option, None |
4ec72927a222
jp (outputs): moved output options parsing and checking to base methods
Goffi <goffi@goffi.org>
parents:
2178
diff
changeset
|
408 options_dict[key.strip()] = value.strip() if value is not None else None |
4ec72927a222
jp (outputs): moved output options parsing and checking to base methods
Goffi <goffi@goffi.org>
parents:
2178
diff
changeset
|
409 return options_dict |
4ec72927a222
jp (outputs): moved output options parsing and checking to base methods
Goffi <goffi@goffi.org>
parents:
2178
diff
changeset
|
410 |
4ec72927a222
jp (outputs): moved output options parsing and checking to base methods
Goffi <goffi@goffi.org>
parents:
2178
diff
changeset
|
411 def check_output_options(self, accepted_set, options): |
4ec72927a222
jp (outputs): moved output options parsing and checking to base methods
Goffi <goffi@goffi.org>
parents:
2178
diff
changeset
|
412 if not accepted_set.issuperset(options): |
3040 | 413 self.disp( |
414 _("The following output options are invalid: {invalid_options}").format( | |
3028 | 415 invalid_options = ', '.join(set(options).difference(accepted_set))), |
2187
4ec72927a222
jp (outputs): moved output options parsing and checking to base methods
Goffi <goffi@goffi.org>
parents:
2178
diff
changeset
|
416 error=True) |
4ec72927a222
jp (outputs): moved output options parsing and checking to base methods
Goffi <goffi@goffi.org>
parents:
2178
diff
changeset
|
417 self.quit(C.EXIT_BAD_ARG) |
4ec72927a222
jp (outputs): moved output options parsing and checking to base methods
Goffi <goffi@goffi.org>
parents:
2178
diff
changeset
|
418 |
1950 | 419 def import_plugins(self): |
420 """Automaticaly import commands and outputs in jp | |
421 | |
817 | 422 looks from modules names cmd_*.py in jp path and import them |
423 """ | |
424 path = os.path.dirname(sat_frontends.jp.__file__) | |
1950 | 425 # XXX: outputs must be imported before commands as they are used for arguments |
3040 | 426 for type_, pattern in ((C.PLUGIN_OUTPUT, 'output_*.py'), |
427 (C.PLUGIN_CMD, 'cmd_*.py')): | |
428 modules = ( | |
429 os.path.splitext(module)[0] | |
430 for module in map(os.path.basename, iglob(os.path.join(path, pattern)))) | |
1950 | 431 for module_name in modules: |
2162
c9a67eb5bf72
jp (base): improved module import:
Goffi <goffi@goffi.org>
parents:
2161
diff
changeset
|
432 module_path = "sat_frontends.jp." + module_name |
1950 | 433 try: |
2162
c9a67eb5bf72
jp (base): improved module import:
Goffi <goffi@goffi.org>
parents:
2161
diff
changeset
|
434 module = import_module(module_path) |
1950 | 435 self.import_plugin_module(module, type_) |
2178
b9bfc45cea22
jp (base): added generic errback to CommandBase + show error message on plugin ImportError
Goffi <goffi@goffi.org>
parents:
2168
diff
changeset
|
436 except ImportError as e: |
3040 | 437 self.disp( |
438 _(f"Can't import {module_path} plugin, ignoring it: {e}"), | |
439 error=True) | |
2162
c9a67eb5bf72
jp (base): improved module import:
Goffi <goffi@goffi.org>
parents:
2161
diff
changeset
|
440 except exceptions.CancelError: |
1950 | 441 continue |
2162
c9a67eb5bf72
jp (base): improved module import:
Goffi <goffi@goffi.org>
parents:
2161
diff
changeset
|
442 except exceptions.MissingModule as e: |
3028 | 443 self.disp(_("Missing module for plugin {name}: {missing}".format( |
2162
c9a67eb5bf72
jp (base): improved module import:
Goffi <goffi@goffi.org>
parents:
2161
diff
changeset
|
444 name = module_path, |
2188
052d560d0dce
jp (base): replaced log.warning by stderr prints
Goffi <goffi@goffi.org>
parents:
2187
diff
changeset
|
445 missing = e)), error=True) |
2162
c9a67eb5bf72
jp (base): improved module import:
Goffi <goffi@goffi.org>
parents:
2161
diff
changeset
|
446 |
0 | 447 |
1950 | 448 def import_plugin_module(self, module, type_): |
449 """add commands or outpus from a module to jp | |
817 | 450 |
1950 | 451 @param module: module containing commands or outputs |
452 @param type_(str): one of C_PLUGIN_* | |
817 | 453 """ |
454 try: | |
1950 | 455 class_names = getattr(module, '__{}__'.format(type_)) |
817 | 456 except AttributeError: |
3040 | 457 log.disp(_(f"Invalid plugin module [{type_}] {module}"), error=True) |
817 | 458 raise ImportError |
1950 | 459 else: |
460 for class_name in class_names: | |
461 cls = getattr(module, class_name) | |
462 cls(self) | |
817 | 463 |
2532 | 464 def get_xmpp_uri_from_http(self, http_url): |
465 """parse HTML page at http(s) URL, and looks for xmpp: uri""" | |
466 if http_url.startswith('https'): | |
3028 | 467 scheme = 'https' |
2532 | 468 elif http_url.startswith('http'): |
3028 | 469 scheme = 'http' |
2532 | 470 else: |
3028 | 471 raise exceptions.InternalError('An HTTP scheme is expected in this method') |
3040 | 472 self.disp(f"{scheme.upper()} URL found, trying to find associated xmpp: URI", 1) |
2532 | 473 # HTTP URL, we try to find xmpp: links |
474 try: | |
475 from lxml import etree | |
476 except ImportError: | |
3040 | 477 self.disp( |
478 "lxml module must be installed to use http(s) scheme, please install it " | |
479 "with \"pip install lxml\"", | |
480 error=True) | |
2672 | 481 self.quit(1) |
3028 | 482 import urllib.request, urllib.error, urllib.parse |
2532 | 483 parser = etree.HTMLParser() |
484 try: | |
3028 | 485 root = etree.parse(urllib.request.urlopen(http_url), parser) |
2532 | 486 except etree.XMLSyntaxError as e: |
3028 | 487 self.disp(_("Can't parse HTML page : {msg}").format(msg=e)) |
2532 | 488 links = [] |
489 else: | |
490 links = root.xpath("//link[@rel='alternate' and starts-with(@href, 'xmpp:')]") | |
491 if not links: | |
3040 | 492 self.disp( |
493 _('Could not find alternate "xmpp:" URI, can\'t find associated XMPP ' | |
494 'PubSub node/item'), | |
495 error=True) | |
2672 | 496 self.quit(1) |
2532 | 497 xmpp_uri = links[0].get('href') |
498 return xmpp_uri | |
499 | |
500 def parse_pubsub_args(self): | |
501 if self.args.pubsub_url is not None: | |
502 url = self.args.pubsub_url | |
503 | |
504 if url.startswith('http'): | |
505 # http(s) URL, we try to retrieve xmpp one from there | |
506 url = self.get_xmpp_uri_from_http(url) | |
507 | |
508 try: | |
509 uri_data = uri.parseXMPPUri(url) | |
510 except ValueError: | |
3028 | 511 self.parser.error(_('invalid XMPP URL: {url}').format(url=url)) |
2532 | 512 else: |
3028 | 513 if uri_data['type'] == 'pubsub': |
2532 | 514 # URL is alright, we only set data not already set by other options |
515 if not self.args.service: | |
3028 | 516 self.args.service = uri_data['path'] |
2532 | 517 if not self.args.node: |
3028 | 518 self.args.node = uri_data['node'] |
519 uri_item = uri_data.get('item') | |
2532 | 520 if uri_item: |
2556
340128e0b354
jp (base): fixed URI's item with use_pubsub
Goffi <goffi@goffi.org>
parents:
2552
diff
changeset
|
521 # there is an item in URI |
340128e0b354
jp (base): fixed URI's item with use_pubsub
Goffi <goffi@goffi.org>
parents:
2552
diff
changeset
|
522 # we use it only if item is not already set |
340128e0b354
jp (base): fixed URI's item with use_pubsub
Goffi <goffi@goffi.org>
parents:
2552
diff
changeset
|
523 # and item_last is not used either |
2532 | 524 try: |
2556
340128e0b354
jp (base): fixed URI's item with use_pubsub
Goffi <goffi@goffi.org>
parents:
2552
diff
changeset
|
525 item = self.args.item |
2532 | 526 except AttributeError: |
2609
75d2ac872965
jp (base): do not crash when item is specified (e.g. in URL) but not needed in command, but display a message on stderr.
Goffi <goffi@goffi.org>
parents:
2588
diff
changeset
|
527 try: |
75d2ac872965
jp (base): do not crash when item is specified (e.g. in URL) but not needed in command, but display a message on stderr.
Goffi <goffi@goffi.org>
parents:
2588
diff
changeset
|
528 items = self.args.items |
75d2ac872965
jp (base): do not crash when item is specified (e.g. in URL) but not needed in command, but display a message on stderr.
Goffi <goffi@goffi.org>
parents:
2588
diff
changeset
|
529 except AttributeError: |
3040 | 530 self.disp( |
531 _("item specified in URL but not needed in command, " | |
532 "ignoring it"), | |
533 error=True) | |
2609
75d2ac872965
jp (base): do not crash when item is specified (e.g. in URL) but not needed in command, but display a message on stderr.
Goffi <goffi@goffi.org>
parents:
2588
diff
changeset
|
534 else: |
75d2ac872965
jp (base): do not crash when item is specified (e.g. in URL) but not needed in command, but display a message on stderr.
Goffi <goffi@goffi.org>
parents:
2588
diff
changeset
|
535 if not items: |
75d2ac872965
jp (base): do not crash when item is specified (e.g. in URL) but not needed in command, but display a message on stderr.
Goffi <goffi@goffi.org>
parents:
2588
diff
changeset
|
536 self.args.items = [uri_item] |
2532 | 537 else: |
2556
340128e0b354
jp (base): fixed URI's item with use_pubsub
Goffi <goffi@goffi.org>
parents:
2552
diff
changeset
|
538 if not item: |
340128e0b354
jp (base): fixed URI's item with use_pubsub
Goffi <goffi@goffi.org>
parents:
2552
diff
changeset
|
539 try: |
340128e0b354
jp (base): fixed URI's item with use_pubsub
Goffi <goffi@goffi.org>
parents:
2552
diff
changeset
|
540 item_last = self.args.item_last |
340128e0b354
jp (base): fixed URI's item with use_pubsub
Goffi <goffi@goffi.org>
parents:
2552
diff
changeset
|
541 except AttributeError: |
340128e0b354
jp (base): fixed URI's item with use_pubsub
Goffi <goffi@goffi.org>
parents:
2552
diff
changeset
|
542 item_last = False |
340128e0b354
jp (base): fixed URI's item with use_pubsub
Goffi <goffi@goffi.org>
parents:
2552
diff
changeset
|
543 if not item_last: |
340128e0b354
jp (base): fixed URI's item with use_pubsub
Goffi <goffi@goffi.org>
parents:
2552
diff
changeset
|
544 self.args.item = uri_item |
2532 | 545 else: |
3040 | 546 self.parser.error(_(f'XMPP URL is not a pubsub one: {url}')) |
2532 | 547 flags = self.args._cmd._pubsub_flags |
548 # we check required arguments here instead of using add_arguments' required option | |
549 # because the required argument can be set in URL | |
550 if C.SERVICE in flags and not self.args.service: | |
3028 | 551 self.parser.error(_("argument -s/--service is required")) |
2532 | 552 if C.NODE in flags and not self.args.node: |
3028 | 553 self.parser.error(_("argument -n/--node is required")) |
2552
38e1e29c48e9
jp (base): added C.ITEM pubsub flag when --item is required
Goffi <goffi@goffi.org>
parents:
2550
diff
changeset
|
554 if C.ITEM in flags and not self.args.item: |
3028 | 555 self.parser.error(_("argument -i/--item is required")) |
2532 | 556 |
557 # FIXME: mutually groups can't be nested in a group and don't support title | |
558 # so we check conflict here. This may be fixed in Python 3, to be checked | |
559 try: | |
560 if self.args.item and self.args.item_last: | |
3040 | 561 self.parser.error( |
562 _("--item and --item-last can't be used at the same time")) | |
2532 | 563 except AttributeError: |
564 pass | |
565 | |
2775
2dfd5b1d39df
jp (base): fixed default values for --max-items and --max when using pubsub
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
566 try: |
2dfd5b1d39df
jp (base): fixed default values for --max-items and --max when using pubsub
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
567 max_items = self.args.max |
2dfd5b1d39df
jp (base): fixed default values for --max-items and --max when using pubsub
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
568 rsm_max = self.args.rsm_max |
2dfd5b1d39df
jp (base): fixed default values for --max-items and --max when using pubsub
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
569 except AttributeError: |
2dfd5b1d39df
jp (base): fixed default values for --max-items and --max when using pubsub
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
570 pass |
2dfd5b1d39df
jp (base): fixed default values for --max-items and --max when using pubsub
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
571 else: |
2dfd5b1d39df
jp (base): fixed default values for --max-items and --max when using pubsub
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
572 # we need to set a default value for max, but we need to know if we want |
2dfd5b1d39df
jp (base): fixed default values for --max-items and --max when using pubsub
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
573 # to use pubsub's max or RSM's max. The later is used if any RSM or MAM |
2dfd5b1d39df
jp (base): fixed default values for --max-items and --max when using pubsub
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
574 # argument is set |
2dfd5b1d39df
jp (base): fixed default values for --max-items and --max when using pubsub
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
575 if max_items is None and rsm_max is None: |
2dfd5b1d39df
jp (base): fixed default values for --max-items and --max when using pubsub
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
576 to_check = ('mam_filters', 'rsm_max', 'rsm_after', 'rsm_before', |
2dfd5b1d39df
jp (base): fixed default values for --max-items and --max when using pubsub
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
577 'rsm_index') |
2dfd5b1d39df
jp (base): fixed default values for --max-items and --max when using pubsub
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
578 if any((getattr(self.args, name) for name in to_check)): |
2dfd5b1d39df
jp (base): fixed default values for --max-items and --max when using pubsub
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
579 # we use RSM |
2dfd5b1d39df
jp (base): fixed default values for --max-items and --max when using pubsub
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
580 self.args.rsm_max = 10 |
2dfd5b1d39df
jp (base): fixed default values for --max-items and --max when using pubsub
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
581 else: |
2dfd5b1d39df
jp (base): fixed default values for --max-items and --max when using pubsub
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
582 # we use pubsub without RSM |
2dfd5b1d39df
jp (base): fixed default values for --max-items and --max when using pubsub
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
583 self.args.max = 10 |
2dfd5b1d39df
jp (base): fixed default values for --max-items and --max when using pubsub
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
584 if self.args.max is None: |
2dfd5b1d39df
jp (base): fixed default values for --max-items and --max when using pubsub
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
585 self.args.max = C.NO_LIMIT |
2dfd5b1d39df
jp (base): fixed default values for --max-items and --max when using pubsub
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
586 |
3040 | 587 async def main(self, args, namespace): |
1884
0fe69871b71f
jp: moved KeyboardInterrupt catch one level higher, so it is also catched if the loop is not started
Goffi <goffi@goffi.org>
parents:
1872
diff
changeset
|
588 try: |
3040 | 589 await self.bridge.bridgeConnect() |
590 except Exception as e: | |
591 if isinstance(e, exceptions.BridgeExceptionNoService): | |
592 print((_("Can't connect to SàT backend, are you sure it's launched ?"))) | |
593 elif isinstance(e, exceptions.BridgeInitError): | |
594 print((_("Can't init bridge"))) | |
595 else: | |
596 print((_(f"Error while initialising bridge: {e}"))) | |
597 self.quit(C.EXIT_BRIDGE_ERROR, raise_exc=False) | |
598 return | |
599 self.version = await self.bridge.getVersion() | |
600 self._bridgeConnected() | |
601 self.import_plugins() | |
602 try: | |
603 self.args = self.parser.parse_args(args, namespace=None) | |
604 if self.args._cmd._use_pubsub: | |
605 self.parse_pubsub_args() | |
606 await self.args._cmd.run() | |
607 except SystemExit as e: | |
608 self.quit(e.code, raise_exc=False) | |
609 return | |
610 except QuitException: | |
611 return | |
817 | 612 |
3040 | 613 def run(self, args=None, namespace=None): |
2098
e0066920a661
primitivus, jp: dynamic bridge + fixed D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
2086
diff
changeset
|
614 self.loop = JPLoop() |
3040 | 615 self.loop.run(self, args, namespace) |
0 | 616 |
3040 | 617 def _read_stdin(self, stdin_fut): |
618 """Callback called by ainput to read stdin""" | |
619 line = sys.stdin.readline() | |
620 if line: | |
621 stdin_fut.set_result(line.rstrip(os.linesep)) | |
622 else: | |
623 stdin_fut.set_exception(EOFError()) | |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
624 |
3040 | 625 async def ainput(self, msg=''): |
626 """Asynchronous version of buildin "input" function""" | |
627 self.disp(msg, no_lf=True) | |
628 sys.stdout.flush() | |
629 loop = asyncio.get_running_loop() | |
630 stdin_fut = loop.create_future() | |
631 loop.add_reader(sys.stdin, self._read_stdin, stdin_fut) | |
632 return await stdin_fut | |
633 | |
634 async def confirmOrQuit(self, message, cancel_message=_("action cancelled by user")): | |
2550
1d754bc14381
jp (base): new confirmOrQuit helper method to ask confirmation to user, and quit if he cancel
Goffi <goffi@goffi.org>
parents:
2547
diff
changeset
|
635 """Request user to confirm action, and quit if he doesn't""" |
1d754bc14381
jp (base): new confirmOrQuit helper method to ask confirmation to user, and quit if he cancel
Goffi <goffi@goffi.org>
parents:
2547
diff
changeset
|
636 |
3040 | 637 res = await self.ainput(f"{message} (y/N)? ") |
2550
1d754bc14381
jp (base): new confirmOrQuit helper method to ask confirmation to user, and quit if he cancel
Goffi <goffi@goffi.org>
parents:
2547
diff
changeset
|
638 if res not in ("y", "Y"): |
1d754bc14381
jp (base): new confirmOrQuit helper method to ask confirmation to user, and quit if he cancel
Goffi <goffi@goffi.org>
parents:
2547
diff
changeset
|
639 self.disp(cancel_message) |
1d754bc14381
jp (base): new confirmOrQuit helper method to ask confirmation to user, and quit if he cancel
Goffi <goffi@goffi.org>
parents:
2547
diff
changeset
|
640 self.quit(C.EXIT_USER_CANCELLED) |
1d754bc14381
jp (base): new confirmOrQuit helper method to ask confirmation to user, and quit if he cancel
Goffi <goffi@goffi.org>
parents:
2547
diff
changeset
|
641 |
3040 | 642 def quitFromSignal(self, exit_code=0): |
643 r"""Same as self.quit, but from a signal handler | |
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
|
644 |
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
|
645 /!\: 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
|
646 """ |
1918
01d56efd488b
jp: fixed traceback shown on a normal quit when a progression is finished
Goffi <goffi@goffi.org>
parents:
1884
diff
changeset
|
647 # XXX: python-dbus will show a traceback if we exit in a signal handler |
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
|
648 # so we use this little timeout trick to avoid it |
3040 | 649 self.loop.call_later(0, self.quit, exit_code) |
650 | |
651 def quit(self, exit_code=0, raise_exc=True): | |
652 """Terminate the execution with specified exit_code | |
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
|
653 |
3040 | 654 This will stop the loop. |
655 @param exit_code(int): code to return when quitting the program | |
656 @param raise_exp(boolean): if True raise a QuitException to stop code execution | |
657 The default value should be used most of time. | |
658 """ | |
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
|
659 # 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
|
660 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
|
661 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
|
662 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
|
663 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
|
664 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
|
665 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
|
666 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
|
667 |
3040 | 668 self.loop.quit(exit_code) |
669 if raise_exc: | |
670 raise QuitException | |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
671 |
3040 | 672 async def check_jids(self, jids): |
814 | 673 """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
|
674 |
817 | 675 @param profile: profile name |
676 @param jids: list of jids | |
677 @return: List of jids | |
678 | |
814 | 679 """ |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
680 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
|
681 nodes2jid = {} |
393 | 682 |
3040 | 683 for contact in await 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
|
684 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
|
685 _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
|
686 try: |
1ae9aa94c351
jp: fixed bad try/except hidding errors + fixed bad management of jids without node
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
687 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
|
688 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
|
689 pass |
1ae9aa94c351
jp: fixed bad try/except hidding errors + fixed bad management of jids without node
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
690 |
1ae9aa94c351
jp: fixed bad try/except hidding errors + fixed bad management of jids without node
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
691 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
|
692 nodes2jid[_jid.node.lower()] = jid_s |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
693 |
817 | 694 def expand_jid(jid): |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
695 _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
|
696 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
|
697 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
|
698 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
|
699 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
|
700 else: |
b7c4bb2c0668
jp: - better expandJid: roster's jids' nodes are used after names to expand jid
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
701 expanded = jid |
2588 | 702 return expanded |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
703 |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
704 def check(jid): |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
705 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
|
706 log.error (_("%s is not a valid JID !"), jid) |
817 | 707 self.quit(1) |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
708 |
814 | 709 dest_jids=[] |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
710 try: |
814 | 711 for i in range(len(jids)): |
817 | 712 dest_jids.append(expand_jid(jids[i])) |
814 | 713 check(dest_jids[i]) |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
714 except AttributeError: |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
715 pass |
0 | 716 |
814 | 717 return dest_jids |
0 | 718 |
3040 | 719 async def connect_profile(self): |
720 """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
|
721 |
817 | 722 @exit: - 1 when profile is not connected and --connect is not set |
723 - 1 when the profile doesn't exists | |
724 - 1 when there is a connection error | |
725 """ | |
726 # FIXME: need better exit codes | |
727 | |
3040 | 728 self.profile = await self.bridge.profileNameGet(self.args.profile) |
0 | 729 |
817 | 730 if not self.profile: |
3040 | 731 log.error(_(f"The profile [{self.args.profile}] doesn't exist")) |
732 self.quit(C.EXIT_ERROR) | |
817 | 733 |
1594
313f2bb7841b
jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents:
1588
diff
changeset
|
734 try: |
313f2bb7841b
jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents:
1588
diff
changeset
|
735 start_session = self.args.start_session |
313f2bb7841b
jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents:
1588
diff
changeset
|
736 except AttributeError: |
313f2bb7841b
jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents:
1588
diff
changeset
|
737 pass |
313f2bb7841b
jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents:
1588
diff
changeset
|
738 else: |
313f2bb7841b
jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents:
1588
diff
changeset
|
739 if start_session: |
3040 | 740 try: |
741 await self.bridge.profileStartSession(self.args.pwd, self.profile) | |
742 except Exception as e: | |
743 self.disp(_(f"Can't start {self.profile}'s session: {e}"), err=True) | |
744 self.quit(1) | |
1594
313f2bb7841b
jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents:
1588
diff
changeset
|
745 return |
3040 | 746 elif not await self.bridge.profileIsSessionStarted(self.profile): |
1594
313f2bb7841b
jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents:
1588
diff
changeset
|
747 if not self.args.connect: |
3040 | 748 self.disp(_( |
749 f"Session for [{self.profile}] is not started, please start it " | |
750 f"before using jp, or use either --start-session or --connect " | |
751 f"option"), error=True) | |
1594
313f2bb7841b
jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents:
1588
diff
changeset
|
752 self.quit(1) |
2692
7d4679ee7ded
jp (base): fixed connection when start_session arg exists but is False
Goffi <goffi@goffi.org>
parents:
2672
diff
changeset
|
753 elif not getattr(self.args, "connect", False): |
1594
313f2bb7841b
jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents:
1588
diff
changeset
|
754 return |
313f2bb7841b
jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents:
1588
diff
changeset
|
755 |
313f2bb7841b
jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents:
1588
diff
changeset
|
756 |
1401
265ff2bd8d67
jp: fixed crash on commands using profile without "connect" option
Goffi <goffi@goffi.org>
parents:
1396
diff
changeset
|
757 if not hasattr(self.args, 'connect'): |
3040 | 758 # a profile can be present without connect option (e.g. on profile |
759 # creation/deletion) | |
1401
265ff2bd8d67
jp: fixed crash on commands using profile without "connect" option
Goffi <goffi@goffi.org>
parents:
1396
diff
changeset
|
760 return |
1588
823a385235ef
jp: fixed bad --connect option check
Goffi <goffi@goffi.org>
parents:
1544
diff
changeset
|
761 elif self.args.connect is True: # if connection is asked, we connect the profile |
3040 | 762 try: |
763 await self.bridge.connect(self.profile, self.args.pwd, {}) | |
764 except Exception as e: | |
765 self.disp(_(f"Can't connect profile: {e}"), error=True) | |
766 self.quit(1) | |
814 | 767 return |
1594
313f2bb7841b
jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents:
1588
diff
changeset
|
768 else: |
3040 | 769 if not await self.bridge.isConnected(self.profile): |
770 log.error( | |
771 _(f"Profile [{self.profile}] is not connected, please connect it " | |
772 f"before using jp, or use --connect option")) | |
1594
313f2bb7841b
jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents:
1588
diff
changeset
|
773 self.quit(1) |
0 | 774 |
3040 | 775 async 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
|
776 """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
|
777 _jid = JID(param_jid) |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
778 if not _jid.resource: |
1606
de785fcf9a7b
jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents:
1605
diff
changeset
|
779 #if the resource is not given, we try to add the main resource |
3040 | 780 main_resource = await self.bridge.getMainResource(param_jid, self.profile) |
1606
de785fcf9a7b
jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents:
1605
diff
changeset
|
781 if main_resource: |
3040 | 782 return f"{_jid.bare}/{main_resource}" |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
783 return param_jid |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
784 |
814 | 785 |
817 | 786 class CommandBase(object): |
814 | 787 |
2155 | 788 def __init__(self, host, name, use_profile=True, use_output=False, extra_outputs=None, |
1950 | 789 need_connect=None, help=None, **kwargs): |
2098
e0066920a661
primitivus, jp: dynamic bridge + fixed D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
2086
diff
changeset
|
790 """Initialise CommandBase |
e0066920a661
primitivus, jp: dynamic bridge + fixed D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
2086
diff
changeset
|
791 |
817 | 792 @param host: Jp instance |
1594
313f2bb7841b
jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents:
1588
diff
changeset
|
793 @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
|
794 @param use_profile(bool): if True, add profile selection/connection commands |
2114
dc5d214f0a3b
jp (info/session): added a command to get data on current session
Goffi <goffi@goffi.org>
parents:
2098
diff
changeset
|
795 @param use_output(bool, unicode): if not False, add --output option |
2155 | 796 @param extra_outputs(dict): list of command specific outputs: |
797 key is output name ("default" to use as main output) | |
3040 | 798 value is a callable which will format the output (data will be used as only |
799 argument) | |
2155 | 800 if a key already exists with normal outputs, the extra one will be used |
1594
313f2bb7841b
jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents:
1588
diff
changeset
|
801 @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
|
802 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
|
803 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
|
804 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
|
805 @param help(unicode): help message to display |
817 | 806 @param **kwargs: args passed to ArgumentParser |
1950 | 807 use_* are handled directly, they can be: |
808 - use_progress(bool): if True, add progress bar activation option | |
809 progress* signals will be handled | |
810 - use_verbose(bool): if True, add verbosity option | |
2532 | 811 - use_pubsub(bool): if True, add pubsub options |
812 mandatory arguments are controlled by pubsub_req | |
813 - use_draft(bool): if True, add draft handling options | |
814 ** other arguments ** | |
3040 | 815 - pubsub_flags(iterable[unicode]): tuple of flags to set pubsub options, |
816 can be: | |
2532 | 817 C.SERVICE: service is required |
818 C.NODE: node is required | |
2614
a5b96950b81a
jp (event): fixed crash on missing item in get and inviteeGet by making it mandatory.
Goffi <goffi@goffi.org>
parents:
2609
diff
changeset
|
819 C.ITEM: item is required |
2532 | 820 C.SINGLE_ITEM: only one item is allowed |
814 | 821 """ |
817 | 822 try: # If we have subcommands, host is a CommandBase and we need to use host.host |
823 self.host = host.host | |
824 except AttributeError: | |
825 self.host = host | |
826 | |
1950 | 827 # --profile option |
817 | 828 parents = kwargs.setdefault('parents', set()) |
829 if use_profile: | |
3040 | 830 # self.host.parents['profile'] is an ArgumentParser with profile connection |
831 # arguments | |
1594
313f2bb7841b
jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents:
1588
diff
changeset
|
832 if need_connect is None: |
313f2bb7841b
jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents:
1588
diff
changeset
|
833 need_connect = True |
3040 | 834 parents.add( |
835 self.host.parents['profile' if need_connect else 'profile_session']) | |
1594
313f2bb7841b
jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents:
1588
diff
changeset
|
836 else: |
313f2bb7841b
jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents:
1588
diff
changeset
|
837 assert need_connect is None |
1860
afc7f6328394
jp (base): use need_connect attribue to detect if connection is needed instead of self.args.profile, as profile argument can be used by any command even if use_profile is False
Goffi <goffi@goffi.org>
parents:
1840
diff
changeset
|
838 self.need_connect = need_connect |
afc7f6328394
jp (base): use need_connect attribue to detect if connection is needed instead of self.args.profile, as profile argument can be used by any command even if use_profile is False
Goffi <goffi@goffi.org>
parents:
1840
diff
changeset
|
839 # from this point, self.need_connect is None if connection is not needed at all |
afc7f6328394
jp (base): use need_connect attribue to detect if connection is needed instead of self.args.profile, as profile argument can be used by any command even if use_profile is False
Goffi <goffi@goffi.org>
parents:
1840
diff
changeset
|
840 # False if session starting is needed, and True if full connection is needed |
1594
313f2bb7841b
jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents:
1588
diff
changeset
|
841 |
1950 | 842 # --output option |
843 if use_output: | |
2155 | 844 if extra_outputs is None: |
845 extra_outputs = {} | |
846 self.extra_outputs = extra_outputs | |
1950 | 847 if use_output == True: |
848 use_output = C.OUTPUT_TEXT | |
849 assert use_output in C.OUTPUT_TYPES | |
850 self._output_type = use_output | |
851 output_parent = argparse.ArgumentParser(add_help=False) | |
2155 | 852 choices = set(self.host.getOutputChoices(use_output)) |
853 choices.update(extra_outputs) | |
1950 | 854 if not choices: |
3040 | 855 raise exceptions.InternalError( |
856 "No choice found for {} output type".format(use_output)) | |
2189
a25a256688e2
jp (base): output can now specify a default when registering
Goffi <goffi@goffi.org>
parents:
2188
diff
changeset
|
857 try: |
a25a256688e2
jp (base): output can now specify a default when registering
Goffi <goffi@goffi.org>
parents:
2188
diff
changeset
|
858 default = self.host.default_output[use_output] |
a25a256688e2
jp (base): output can now specify a default when registering
Goffi <goffi@goffi.org>
parents:
2188
diff
changeset
|
859 except KeyError: |
3028 | 860 if 'default' in choices: |
861 default = 'default' | |
862 elif 'simple' in choices: | |
863 default = 'simple' | |
2189
a25a256688e2
jp (base): output can now specify a default when registering
Goffi <goffi@goffi.org>
parents:
2188
diff
changeset
|
864 else: |
a25a256688e2
jp (base): output can now specify a default when registering
Goffi <goffi@goffi.org>
parents:
2188
diff
changeset
|
865 default = list(choices)[0] |
3040 | 866 output_parent.add_argument( |
867 '--output', '-O', choices=sorted(choices), default=default, | |
868 help=_("select output format (default: {})".format(default))) | |
869 output_parent.add_argument( | |
870 '--output-option', '--oo', action="append", dest='output_opts', | |
871 default=[], help=_("output specific option")) | |
1950 | 872 parents.add(output_parent) |
2155 | 873 else: |
874 assert extra_outputs is None | |
817 | 875 |
2532 | 876 self._use_pubsub = kwargs.pop('use_pubsub', False) |
877 if self._use_pubsub: | |
2538
0a22dc80d671
jp (base): fixed use_pubsub when pubsub_flags is not needed
Goffi <goffi@goffi.org>
parents:
2532
diff
changeset
|
878 flags = kwargs.pop('pubsub_flags', []) |
2547
2d69a0afe039
jp: new argument pubsub_default can be used in Commands to replace default value in help string
Goffi <goffi@goffi.org>
parents:
2539
diff
changeset
|
879 defaults = kwargs.pop('pubsub_defaults', {}) |
2d69a0afe039
jp: new argument pubsub_default can be used in Commands to replace default value in help string
Goffi <goffi@goffi.org>
parents:
2539
diff
changeset
|
880 parents.add(self.host.make_pubsub_group(flags, defaults)) |
2532 | 881 self._pubsub_flags = flags |
2235
4db836386641
jp: added use_pubsub and use_pubsub_node_req arguments to CommandBase
Goffi <goffi@goffi.org>
parents:
2213
diff
changeset
|
882 |
1950 | 883 # other common options |
3028 | 884 use_opts = {k:v for k,v in kwargs.items() if k.startswith('use_')} |
885 for param, do_use in use_opts.items(): | |
1950 | 886 opt=param[4:] # if param is use_verbose, opt is verbose |
887 if opt not in self.host.parents: | |
3028 | 888 raise exceptions.InternalError("Unknown parent option {}".format(opt)) |
1950 | 889 del kwargs[param] |
890 if do_use: | |
891 parents.add(self.host.parents[opt]) | |
1600 | 892 |
817 | 893 self.parser = host.subparsers.add_parser(name, help=help, **kwargs) |
894 if hasattr(self, "subcommands"): | |
3040 | 895 self.subparsers = self.parser.add_subparsers(dest='subcommand', required=True) |
817 | 896 else: |
2532 | 897 self.parser.set_defaults(_cmd=self) |
817 | 898 self.add_parser_options() |
899 | |
900 @property | |
901 def args(self): | |
902 return self.host.args | |
903 | |
904 @property | |
905 def profile(self): | |
906 return self.host.profile | |
907 | |
908 @property | |
2490
b4bf282d6354
jp (base): added verbosity property to CommandBase
Goffi <goffi@goffi.org>
parents:
2486
diff
changeset
|
909 def verbosity(self): |
b4bf282d6354
jp (base): added verbosity property to CommandBase
Goffi <goffi@goffi.org>
parents:
2486
diff
changeset
|
910 return self.host.verbosity |
b4bf282d6354
jp (base): added verbosity property to CommandBase
Goffi <goffi@goffi.org>
parents:
2486
diff
changeset
|
911 |
b4bf282d6354
jp (base): added verbosity property to CommandBase
Goffi <goffi@goffi.org>
parents:
2486
diff
changeset
|
912 @property |
817 | 913 def progress_id(self): |
914 return self.host.progress_id | |
814 | 915 |
3040 | 916 async def set_progress_id(self, progress_id): |
917 return await self.host.set_progress_id(progress_id) | |
817 | 918 |
3040 | 919 async def progressStartedHandler(self, uid, metadata, profile): |
1627
5a641e7b858a
jp (base, file): use of new progress API. Progress callbacks are managed through CommandBase.onProgress* method instead of host attributes.
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
920 if profile != self.profile: |
5a641e7b858a
jp (base, file): use of new progress API. Progress callbacks are managed through CommandBase.onProgress* method instead of host attributes.
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
921 return |
1642
7ec7ce9cdc4c
jp (base): progressStarted signals are cached until progress_id is known, this avoid missing the progression if we have the signal before the id.
Goffi <goffi@goffi.org>
parents:
1641
diff
changeset
|
922 if self.progress_id is None: |
7ec7ce9cdc4c
jp (base): progressStarted signals are cached until progress_id is known, this avoid missing the progression if we have the signal before the id.
Goffi <goffi@goffi.org>
parents:
1641
diff
changeset
|
923 # the progress started message can be received before the id |
7ec7ce9cdc4c
jp (base): progressStarted signals are cached until progress_id is known, this avoid missing the progression if we have the signal before the id.
Goffi <goffi@goffi.org>
parents:
1641
diff
changeset
|
924 # so we keep progressStarted signals in cache to replay they |
7ec7ce9cdc4c
jp (base): progressStarted signals are cached until progress_id is known, this avoid missing the progression if we have the signal before the id.
Goffi <goffi@goffi.org>
parents:
1641
diff
changeset
|
925 # when the progress_id is received |
7ec7ce9cdc4c
jp (base): progressStarted signals are cached until progress_id is known, this avoid missing the progression if we have the signal before the id.
Goffi <goffi@goffi.org>
parents:
1641
diff
changeset
|
926 cache_data = (self.progressStartedHandler, uid, metadata, profile) |
7ec7ce9cdc4c
jp (base): progressStarted signals are cached until progress_id is known, this avoid missing the progression if we have the signal before the id.
Goffi <goffi@goffi.org>
parents:
1641
diff
changeset
|
927 try: |
3040 | 928 cache = self.host.progress_ids_cache |
1642
7ec7ce9cdc4c
jp (base): progressStarted signals are cached until progress_id is known, this avoid missing the progression if we have the signal before the id.
Goffi <goffi@goffi.org>
parents:
1641
diff
changeset
|
929 except AttributeError: |
3040 | 930 cache = self.host.progress_ids_cache = [] |
931 cache.append(cache_data) | |
1642
7ec7ce9cdc4c
jp (base): progressStarted signals are cached until progress_id is known, this avoid missing the progression if we have the signal before the id.
Goffi <goffi@goffi.org>
parents:
1641
diff
changeset
|
932 else: |
7ec7ce9cdc4c
jp (base): progressStarted signals are cached until progress_id is known, this avoid missing the progression if we have the signal before the id.
Goffi <goffi@goffi.org>
parents:
1641
diff
changeset
|
933 if self.host.watch_progress and uid == self.progress_id: |
3040 | 934 await self.onProgressStarted(metadata) |
935 while True: | |
936 await asyncio.sleep(PROGRESS_DELAY) | |
937 cont = await self.progressUpdate() | |
938 if not cont: | |
939 break | |
1627
5a641e7b858a
jp (base, file): use of new progress API. Progress callbacks are managed through CommandBase.onProgress* method instead of host attributes.
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
940 |
3040 | 941 async def progressFinishedHandler(self, uid, metadata, profile): |
1627
5a641e7b858a
jp (base, file): use of new progress API. Progress callbacks are managed through CommandBase.onProgress* method instead of host attributes.
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
942 if profile != self.profile: |
5a641e7b858a
jp (base, file): use of new progress API. Progress callbacks are managed through CommandBase.onProgress* method instead of host attributes.
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
943 return |
5a641e7b858a
jp (base, file): use of new progress API. Progress callbacks are managed through CommandBase.onProgress* method instead of host attributes.
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
944 if uid == self.progress_id: |
5a641e7b858a
jp (base, file): use of new progress API. Progress callbacks are managed through CommandBase.onProgress* method instead of host attributes.
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
945 try: |
5a641e7b858a
jp (base, file): use of new progress API. Progress callbacks are managed through CommandBase.onProgress* method instead of host attributes.
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
946 self.host.pbar.finish() |
5a641e7b858a
jp (base, file): use of new progress API. Progress callbacks are managed through CommandBase.onProgress* method instead of host attributes.
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
947 except AttributeError: |
5a641e7b858a
jp (base, file): use of new progress API. Progress callbacks are managed through CommandBase.onProgress* method instead of host attributes.
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
948 pass |
3040 | 949 await self.onProgressFinished(metadata) |
1627
5a641e7b858a
jp (base, file): use of new progress API. Progress callbacks are managed through CommandBase.onProgress* method instead of host attributes.
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
950 if self.host.quit_on_progress_end: |
1918
01d56efd488b
jp: fixed traceback shown on a normal quit when a progression is finished
Goffi <goffi@goffi.org>
parents:
1884
diff
changeset
|
951 self.host.quitFromSignal() |
1627
5a641e7b858a
jp (base, file): use of new progress API. Progress callbacks are managed through CommandBase.onProgress* method instead of host attributes.
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
952 |
3040 | 953 async def progressErrorHandler(self, uid, message, profile): |
1627
5a641e7b858a
jp (base, file): use of new progress API. Progress callbacks are managed through CommandBase.onProgress* method instead of host attributes.
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
954 if profile != self.profile: |
5a641e7b858a
jp (base, file): use of new progress API. Progress callbacks are managed through CommandBase.onProgress* method instead of host attributes.
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
955 return |
5a641e7b858a
jp (base, file): use of new progress API. Progress callbacks are managed through CommandBase.onProgress* method instead of host attributes.
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
956 if uid == self.progress_id: |
1641
44a14f83e64b
jp (base): fixed verbose option + there is a new line on ProgressErrorHandler only if the progress bar is actually displayed
Goffi <goffi@goffi.org>
parents:
1627
diff
changeset
|
957 if self.args.progress: |
44a14f83e64b
jp (base): fixed verbose option + there is a new line on ProgressErrorHandler only if the progress bar is actually displayed
Goffi <goffi@goffi.org>
parents:
1627
diff
changeset
|
958 self.disp('') # progress is not finished, so we skip a line |
1627
5a641e7b858a
jp (base, file): use of new progress API. Progress callbacks are managed through CommandBase.onProgress* method instead of host attributes.
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
959 if self.host.quit_on_progress_end: |
3040 | 960 await self.onProgressError(message) |
961 self.host.quitFromSignal(C.EXIT_ERROR) | |
1627
5a641e7b858a
jp (base, file): use of new progress API. Progress callbacks are managed through CommandBase.onProgress* method instead of host attributes.
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
962 |
3040 | 963 async def progressUpdate(self): |
964 """This method is continualy called to update the progress bar | |
965 | |
966 @return (bool): False to stop being called | |
967 """ | |
968 data = await self.host.bridge.progressGet(self.progress_id, self.profile) | |
1832
39545dc527a1
jp: an onProgressUpdate method is now called on each progress update, allowing to handle metadata
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
969 if data: |
39545dc527a1
jp: an onProgressUpdate method is now called on each progress update, allowing to handle metadata
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
970 try: |
39545dc527a1
jp: an onProgressUpdate method is now called on each progress update, allowing to handle metadata
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
971 size = data['size'] |
39545dc527a1
jp: an onProgressUpdate method is now called on each progress update, allowing to handle metadata
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
972 except KeyError: |
3040 | 973 self.disp(_("file size is not known, we can't show a progress bar"), 1, |
974 error=True) | |
1832
39545dc527a1
jp: an onProgressUpdate method is now called on each progress update, allowing to handle metadata
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
975 return False |
39545dc527a1
jp: an onProgressUpdate method is now called on each progress update, allowing to handle metadata
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
976 if self.host.pbar is None: |
39545dc527a1
jp: an onProgressUpdate method is now called on each progress update, allowing to handle metadata
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
977 #first answer, we must construct the bar |
3040 | 978 |
979 # if the instance has a pbar_template attribute, it is used has model, | |
980 # else default one is used | |
981 # template is a list of part, where part can be either a str to show directly | |
982 # or a list where first argument is a name of a progressbar widget, and others | |
983 # are used as widget arguments | |
984 try: | |
985 template = self.pbar_template | |
986 except AttributeError: | |
987 template = [ | |
988 _("Progress: "), ["Percentage"], " ", ["Bar"], " ", | |
989 ["FileTransferSpeed"], " ", ["ETA"] | |
990 ] | |
991 | |
992 widgets = [] | |
993 for part in template: | |
994 if isinstance(part, str): | |
995 widgets.append(part) | |
996 else: | |
997 widget = getattr(progressbar, part.pop(0)) | |
998 widgets.append(widget(*part)) | |
999 | |
1000 self.host.pbar = progressbar.ProgressBar(max_value=int(size), widgets=widgets) | |
1832
39545dc527a1
jp: an onProgressUpdate method is now called on each progress update, allowing to handle metadata
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
1001 self.host.pbar.start() |
39545dc527a1
jp: an onProgressUpdate method is now called on each progress update, allowing to handle metadata
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
1002 |
39545dc527a1
jp: an onProgressUpdate method is now called on each progress update, allowing to handle metadata
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
1003 self.host.pbar.update(int(data['position'])) |
39545dc527a1
jp: an onProgressUpdate method is now called on each progress update, allowing to handle metadata
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
1004 |
39545dc527a1
jp: an onProgressUpdate method is now called on each progress update, allowing to handle metadata
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
1005 elif self.host.pbar is not None: |
39545dc527a1
jp: an onProgressUpdate method is now called on each progress update, allowing to handle metadata
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
1006 return False |
39545dc527a1
jp: an onProgressUpdate method is now called on each progress update, allowing to handle metadata
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
1007 |
3040 | 1008 await self.onProgressUpdate(data) |
1832
39545dc527a1
jp: an onProgressUpdate method is now called on each progress update, allowing to handle metadata
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
1009 |
39545dc527a1
jp: an onProgressUpdate method is now called on each progress update, allowing to handle metadata
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
1010 return True |
39545dc527a1
jp: an onProgressUpdate method is now called on each progress update, allowing to handle metadata
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
1011 |
3040 | 1012 async def onProgressStarted(self, metadata): |
1832
39545dc527a1
jp: an onProgressUpdate method is now called on each progress update, allowing to handle metadata
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
1013 """Called when progress has just started |
39545dc527a1
jp: an onProgressUpdate method is now called on each progress update, allowing to handle metadata
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
1014 |
39545dc527a1
jp: an onProgressUpdate method is now called on each progress update, allowing to handle metadata
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
1015 can be overidden by a command |
39545dc527a1
jp: an onProgressUpdate method is now called on each progress update, allowing to handle metadata
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
1016 @param metadata(dict): metadata as sent by bridge.progressStarted |
39545dc527a1
jp: an onProgressUpdate method is now called on each progress update, allowing to handle metadata
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
1017 """ |
3028 | 1018 self.disp(_("Operation started"), 2) |
1627
5a641e7b858a
jp (base, file): use of new progress API. Progress callbacks are managed through CommandBase.onProgress* method instead of host attributes.
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1019 |
3040 | 1020 async def onProgressUpdate(self, metadata): |
1832
39545dc527a1
jp: an onProgressUpdate method is now called on each progress update, allowing to handle metadata
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
1021 """Method called on each progress updata |
39545dc527a1
jp: an onProgressUpdate method is now called on each progress update, allowing to handle metadata
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
1022 |
39545dc527a1
jp: an onProgressUpdate method is now called on each progress update, allowing to handle metadata
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
1023 can be overidden by a command to handle progress metadata |
39545dc527a1
jp: an onProgressUpdate method is now called on each progress update, allowing to handle metadata
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
1024 @para metadata(dict): metadata as returned by bridge.progressGet |
39545dc527a1
jp: an onProgressUpdate method is now called on each progress update, allowing to handle metadata
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
1025 """ |
39545dc527a1
jp: an onProgressUpdate method is now called on each progress update, allowing to handle metadata
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
1026 pass |
39545dc527a1
jp: an onProgressUpdate method is now called on each progress update, allowing to handle metadata
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
1027 |
3040 | 1028 async def onProgressFinished(self, metadata): |
1832
39545dc527a1
jp: an onProgressUpdate method is now called on each progress update, allowing to handle metadata
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
1029 """Called when progress has just finished |
39545dc527a1
jp: an onProgressUpdate method is now called on each progress update, allowing to handle metadata
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
1030 |
39545dc527a1
jp: an onProgressUpdate method is now called on each progress update, allowing to handle metadata
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
1031 can be overidden by a command |
39545dc527a1
jp: an onProgressUpdate method is now called on each progress update, allowing to handle metadata
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
1032 @param metadata(dict): metadata as sent by bridge.progressFinished |
39545dc527a1
jp: an onProgressUpdate method is now called on each progress update, allowing to handle metadata
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
1033 """ |
3028 | 1034 self.disp(_("Operation successfully finished"), 2) |
1627
5a641e7b858a
jp (base, file): use of new progress API. Progress callbacks are managed through CommandBase.onProgress* method instead of host attributes.
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1035 |
3040 | 1036 async def onProgressError(self, e): |
1832
39545dc527a1
jp: an onProgressUpdate method is now called on each progress update, allowing to handle metadata
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
1037 """Called when a progress failed |
39545dc527a1
jp: an onProgressUpdate method is now called on each progress update, allowing to handle metadata
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
1038 |
39545dc527a1
jp: an onProgressUpdate method is now called on each progress update, allowing to handle metadata
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
1039 @param error_msg(unicode): error message as sent by bridge.progressError |
39545dc527a1
jp: an onProgressUpdate method is now called on each progress update, allowing to handle metadata
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
1040 """ |
3040 | 1041 self.disp(_(f"Error while doing operation: {e}"), error=True) |
1627
5a641e7b858a
jp (base, file): use of new progress API. Progress callbacks are managed through CommandBase.onProgress* method instead of host attributes.
Goffi <goffi@goffi.org>
parents:
1623
diff
changeset
|
1042 |
2277
637886ac35f6
jp (base): added no_lf in disp() to avoid printing a line feed
Goffi <goffi@goffi.org>
parents:
2237
diff
changeset
|
1043 def disp(self, msg, verbosity=0, error=False, no_lf=False): |
637886ac35f6
jp (base): added no_lf in disp() to avoid printing a line feed
Goffi <goffi@goffi.org>
parents:
2237
diff
changeset
|
1044 return self.host.disp(msg, verbosity, error, no_lf) |
1605
0aded9648c5c
jp (base): added a self.disp method which manage verbosity and stderr + verbosity property
Goffi <goffi@goffi.org>
parents:
1604
diff
changeset
|
1045 |
1950 | 1046 def output(self, data): |
2193
33b82250eadd
jp (base): raise InternalError if output is used when use_output is not declared
Goffi <goffi@goffi.org>
parents:
2189
diff
changeset
|
1047 try: |
33b82250eadd
jp (base): raise InternalError if output is used when use_output is not declared
Goffi <goffi@goffi.org>
parents:
2189
diff
changeset
|
1048 output_type = self._output_type |
33b82250eadd
jp (base): raise InternalError if output is used when use_output is not declared
Goffi <goffi@goffi.org>
parents:
2189
diff
changeset
|
1049 except AttributeError: |
3040 | 1050 raise exceptions.InternalError( |
1051 _('trying to use output when use_output has not been set')) | |
2193
33b82250eadd
jp (base): raise InternalError if output is used when use_output is not declared
Goffi <goffi@goffi.org>
parents:
2189
diff
changeset
|
1052 return self.host.output(output_type, self.args.output, self.extra_outputs, data) |
1950 | 1053 |
2761
4b693ea24d5f
jp (base, pubsub, ticket): handle order-by:
Goffi <goffi@goffi.org>
parents:
2706
diff
changeset
|
1054 def getPubsubExtra(self, extra=None): |
4b693ea24d5f
jp (base, pubsub, ticket): handle order-by:
Goffi <goffi@goffi.org>
parents:
2706
diff
changeset
|
1055 """Helper method to compute extra data from pubsub arguments |
4b693ea24d5f
jp (base, pubsub, ticket): handle order-by:
Goffi <goffi@goffi.org>
parents:
2706
diff
changeset
|
1056 |
4b693ea24d5f
jp (base, pubsub, ticket): handle order-by:
Goffi <goffi@goffi.org>
parents:
2706
diff
changeset
|
1057 @param extra(None, dict): base extra dict, or None to generate a new one |
4b693ea24d5f
jp (base, pubsub, ticket): handle order-by:
Goffi <goffi@goffi.org>
parents:
2706
diff
changeset
|
1058 @return (dict): dict which can be used directly in the bridge for pubsub |
4b693ea24d5f
jp (base, pubsub, ticket): handle order-by:
Goffi <goffi@goffi.org>
parents:
2706
diff
changeset
|
1059 """ |
4b693ea24d5f
jp (base, pubsub, ticket): handle order-by:
Goffi <goffi@goffi.org>
parents:
2706
diff
changeset
|
1060 if extra is None: |
4b693ea24d5f
jp (base, pubsub, ticket): handle order-by:
Goffi <goffi@goffi.org>
parents:
2706
diff
changeset
|
1061 extra = {} |
4b693ea24d5f
jp (base, pubsub, ticket): handle order-by:
Goffi <goffi@goffi.org>
parents:
2706
diff
changeset
|
1062 else: |
3028 | 1063 intersection = {C.KEY_ORDER_BY}.intersection(list(extra.keys())) |
2764
92af49cde255
jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents:
2761
diff
changeset
|
1064 if intersection: |
92af49cde255
jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents:
2761
diff
changeset
|
1065 raise exceptions.ConflictError( |
3028 | 1066 "given extra dict has conflicting keys with pubsub keys " |
1067 "{intersection}".format(intersection=intersection)) | |
2764
92af49cde255
jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents:
2761
diff
changeset
|
1068 |
92af49cde255
jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents:
2761
diff
changeset
|
1069 # RSM |
92af49cde255
jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents:
2761
diff
changeset
|
1070 |
3028 | 1071 for attribute in ('max', 'after', 'before', 'index'): |
1072 key = 'rsm_' + attribute | |
2764
92af49cde255
jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents:
2761
diff
changeset
|
1073 if key in extra: |
92af49cde255
jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents:
2761
diff
changeset
|
1074 raise exceptions.ConflictError( |
3028 | 1075 "This key already exists in extra: u{key}".format(key=key)) |
2764
92af49cde255
jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents:
2761
diff
changeset
|
1076 value = getattr(self.args, key, None) |
92af49cde255
jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents:
2761
diff
changeset
|
1077 if value is not None: |
3028 | 1078 extra[key] = str(value) |
2764
92af49cde255
jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents:
2761
diff
changeset
|
1079 |
92af49cde255
jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents:
2761
diff
changeset
|
1080 # MAM |
92af49cde255
jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents:
2761
diff
changeset
|
1081 |
3028 | 1082 if hasattr(self.args, 'mam_filters'): |
2764
92af49cde255
jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents:
2761
diff
changeset
|
1083 for key, value in self.args.mam_filters: |
3028 | 1084 key = 'filter_' + key |
2764
92af49cde255
jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents:
2761
diff
changeset
|
1085 if key in extra: |
92af49cde255
jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents:
2761
diff
changeset
|
1086 raise exceptions.ConflictError( |
3028 | 1087 "This key already exists in extra: u{key}".format(key=key)) |
2764
92af49cde255
jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents:
2761
diff
changeset
|
1088 extra[key] = value |
92af49cde255
jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents:
2761
diff
changeset
|
1089 |
92af49cde255
jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents:
2761
diff
changeset
|
1090 # Order-By |
92af49cde255
jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents:
2761
diff
changeset
|
1091 |
2761
4b693ea24d5f
jp (base, pubsub, ticket): handle order-by:
Goffi <goffi@goffi.org>
parents:
2706
diff
changeset
|
1092 try: |
4b693ea24d5f
jp (base, pubsub, ticket): handle order-by:
Goffi <goffi@goffi.org>
parents:
2706
diff
changeset
|
1093 order_by = self.args.order_by |
4b693ea24d5f
jp (base, pubsub, ticket): handle order-by:
Goffi <goffi@goffi.org>
parents:
2706
diff
changeset
|
1094 except AttributeError: |
4b693ea24d5f
jp (base, pubsub, ticket): handle order-by:
Goffi <goffi@goffi.org>
parents:
2706
diff
changeset
|
1095 pass |
4b693ea24d5f
jp (base, pubsub, ticket): handle order-by:
Goffi <goffi@goffi.org>
parents:
2706
diff
changeset
|
1096 else: |
4b693ea24d5f
jp (base, pubsub, ticket): handle order-by:
Goffi <goffi@goffi.org>
parents:
2706
diff
changeset
|
1097 if order_by is not None: |
4b693ea24d5f
jp (base, pubsub, ticket): handle order-by:
Goffi <goffi@goffi.org>
parents:
2706
diff
changeset
|
1098 extra[C.KEY_ORDER_BY] = self.args.order_by |
4b693ea24d5f
jp (base, pubsub, ticket): handle order-by:
Goffi <goffi@goffi.org>
parents:
2706
diff
changeset
|
1099 return extra |
4b693ea24d5f
jp (base, pubsub, ticket): handle order-by:
Goffi <goffi@goffi.org>
parents:
2706
diff
changeset
|
1100 |
817 | 1101 def add_parser_options(self): |
1102 try: | |
1103 subcommands = self.subcommands | |
1104 except AttributeError: | |
1105 # We don't have subcommands, the class need to implements add_parser_options | |
1106 raise NotImplementedError | |
1107 | |
1108 # now we add subcommands to ourself | |
1109 for cls in subcommands: | |
1110 cls(self) | |
814 | 1111 |
3040 | 1112 async def run(self): |
1862
6d9c87bdc452
jp (base): added a CommandBase.start method which is called by run or connected, so subclasses can implement it (this is for simplicity sake, as it's not always clear if run or connected must be used)
Goffi <goffi@goffi.org>
parents:
1860
diff
changeset
|
1113 """this method is called when a command is actually run |
1395
1ae9aa94c351
jp: fixed bad try/except hidding errors + fixed bad management of jids without node
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
1114 |
1862
6d9c87bdc452
jp (base): added a CommandBase.start method which is called by run or connected, so subclasses can implement it (this is for simplicity sake, as it's not always clear if run or connected must be used)
Goffi <goffi@goffi.org>
parents:
1860
diff
changeset
|
1115 It set stuff like progression callbacks and profile connection |
6d9c87bdc452
jp (base): added a CommandBase.start method which is called by run or connected, so subclasses can implement it (this is for simplicity sake, as it's not always clear if run or connected must be used)
Goffi <goffi@goffi.org>
parents:
1860
diff
changeset
|
1116 You should not overide this method: you should call self.start instead |
6d9c87bdc452
jp (base): added a CommandBase.start method which is called by run or connected, so subclasses can implement it (this is for simplicity sake, as it's not always clear if run or connected must be used)
Goffi <goffi@goffi.org>
parents:
1860
diff
changeset
|
1117 """ |
2155 | 1118 # we keep a reference to run command, it may be useful e.g. for outputs |
1119 self.host.command = self | |
1863
b2ddd7f5dcdf
jp (base): refactored need_loop so it is set only when the command is run. It can now be set in __init__ methods of commands classes
Goffi <goffi@goffi.org>
parents:
1862
diff
changeset
|
1120 |
817 | 1121 try: |
1606
de785fcf9a7b
jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents:
1605
diff
changeset
|
1122 show_progress = self.args.progress |
817 | 1123 except AttributeError: |
1124 # the command doesn't use progress bar | |
1125 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
|
1126 else: |
1606
de785fcf9a7b
jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents:
1605
diff
changeset
|
1127 if show_progress: |
de785fcf9a7b
jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents:
1605
diff
changeset
|
1128 self.host.watch_progress = True |
3040 | 1129 # we need to register the following signal even if we don't display the |
1130 # progress bar | |
1131 self.host.bridge.register_signal( | |
1132 "progressStarted", self.progressStartedHandler) | |
1133 self.host.bridge.register_signal( | |
1134 "progressFinished", self.progressFinishedHandler) | |
1135 self.host.bridge.register_signal( | |
1136 "progressError", self.progressErrorHandler) | |
817 | 1137 |
1862
6d9c87bdc452
jp (base): added a CommandBase.start method which is called by run or connected, so subclasses can implement it (this is for simplicity sake, as it's not always clear if run or connected must be used)
Goffi <goffi@goffi.org>
parents:
1860
diff
changeset
|
1138 if self.need_connect is not None: |
3040 | 1139 await self.host.connect_profile() |
1140 await self.start() | |
1862
6d9c87bdc452
jp (base): added a CommandBase.start method which is called by run or connected, so subclasses can implement it (this is for simplicity sake, as it's not always clear if run or connected must be used)
Goffi <goffi@goffi.org>
parents:
1860
diff
changeset
|
1141 |
3040 | 1142 async def start(self): |
1143 """This is the starting point of the command, this method must be overriden | |
1862
6d9c87bdc452
jp (base): added a CommandBase.start method which is called by run or connected, so subclasses can implement it (this is for simplicity sake, as it's not always clear if run or connected must be used)
Goffi <goffi@goffi.org>
parents:
1860
diff
changeset
|
1144 |
6d9c87bdc452
jp (base): added a CommandBase.start method which is called by run or connected, so subclasses can implement it (this is for simplicity sake, as it's not always clear if run or connected must be used)
Goffi <goffi@goffi.org>
parents:
1860
diff
changeset
|
1145 at this point, profile are connected if needed |
6d9c87bdc452
jp (base): added a CommandBase.start method which is called by run or connected, so subclasses can implement it (this is for simplicity sake, as it's not always clear if run or connected must be used)
Goffi <goffi@goffi.org>
parents:
1860
diff
changeset
|
1146 """ |
3040 | 1147 raise NotImplementedError |
817 | 1148 |
1149 | |
1150 class CommandAnswering(CommandBase): | |
1606
de785fcf9a7b
jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents:
1605
diff
changeset
|
1151 """Specialised commands which answer to specific actions |
817 | 1152 |
1606
de785fcf9a7b
jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents:
1605
diff
changeset
|
1153 to manage action_types answer, |
de785fcf9a7b
jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents:
1605
diff
changeset
|
1154 """ |
2409
d2ff5ff3de77
jp (ad-hoc): new "list" and "run" commands:
Goffi <goffi@goffi.org>
parents:
2368
diff
changeset
|
1155 action_callbacks = {} # XXX: set managed action types in a dict here: |
1606
de785fcf9a7b
jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents:
1605
diff
changeset
|
1156 # key is the action_type, value is the callable |
de785fcf9a7b
jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents:
1605
diff
changeset
|
1157 # which will manage the answer. profile filtering is |
de785fcf9a7b
jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents:
1605
diff
changeset
|
1158 # already managed when callback is called |
de785fcf9a7b
jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents:
1605
diff
changeset
|
1159 |
2486
031aa3cc67ac
jp (base): fixed assertion error when using CommandAnswering
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1160 def __init__(self, *args, **kwargs): |
031aa3cc67ac
jp (base): fixed assertion error when using CommandAnswering
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1161 super(CommandAnswering, self).__init__(*args, **kwargs) |
031aa3cc67ac
jp (base): fixed assertion error when using CommandAnswering
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1162 |
3040 | 1163 async def onActionNew(self, action_data, action_id, security_limit, profile): |
538
2c4016921403
core, frontends, bridgen plugins: fixed methods which were unproperly managing multi-profiles
Goffi <goffi@goffi.org>
parents:
493
diff
changeset
|
1164 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
|
1165 return |
1606
de785fcf9a7b
jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents:
1605
diff
changeset
|
1166 try: |
de785fcf9a7b
jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents:
1605
diff
changeset
|
1167 action_type = action_data['meta_type'] |
de785fcf9a7b
jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents:
1605
diff
changeset
|
1168 except KeyError: |
1840
9eabf7fadfdd
frontends (jp): onActionNew displays xmlui message if no meta_type is defined and xmlui is defined
souliane <souliane@mailoo.org>
parents:
1832
diff
changeset
|
1169 try: |
9eabf7fadfdd
frontends (jp): onActionNew displays xmlui message if no meta_type is defined and xmlui is defined
souliane <souliane@mailoo.org>
parents:
1832
diff
changeset
|
1170 xml_ui = action_data["xmlui"] |
9eabf7fadfdd
frontends (jp): onActionNew displays xmlui message if no meta_type is defined and xmlui is defined
souliane <souliane@mailoo.org>
parents:
1832
diff
changeset
|
1171 except KeyError: |
9eabf7fadfdd
frontends (jp): onActionNew displays xmlui message if no meta_type is defined and xmlui is defined
souliane <souliane@mailoo.org>
parents:
1832
diff
changeset
|
1172 pass |
9eabf7fadfdd
frontends (jp): onActionNew displays xmlui message if no meta_type is defined and xmlui is defined
souliane <souliane@mailoo.org>
parents:
1832
diff
changeset
|
1173 else: |
9eabf7fadfdd
frontends (jp): onActionNew displays xmlui message if no meta_type is defined and xmlui is defined
souliane <souliane@mailoo.org>
parents:
1832
diff
changeset
|
1174 self.onXMLUI(xml_ui) |
1606
de785fcf9a7b
jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents:
1605
diff
changeset
|
1175 else: |
de785fcf9a7b
jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents:
1605
diff
changeset
|
1176 try: |
de785fcf9a7b
jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents:
1605
diff
changeset
|
1177 callback = self.action_callbacks[action_type] |
de785fcf9a7b
jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents:
1605
diff
changeset
|
1178 except KeyError: |
de785fcf9a7b
jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents:
1605
diff
changeset
|
1179 pass |
0 | 1180 else: |
3040 | 1181 await callback(action_data, action_id, security_limit, profile) |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
1182 |
1840
9eabf7fadfdd
frontends (jp): onActionNew displays xmlui message if no meta_type is defined and xmlui is defined
souliane <souliane@mailoo.org>
parents:
1832
diff
changeset
|
1183 def onXMLUI(self, xml_ui): |
9eabf7fadfdd
frontends (jp): onActionNew displays xmlui message if no meta_type is defined and xmlui is defined
souliane <souliane@mailoo.org>
parents:
1832
diff
changeset
|
1184 """Display a dialog received from the backend. |
9eabf7fadfdd
frontends (jp): onActionNew displays xmlui message if no meta_type is defined and xmlui is defined
souliane <souliane@mailoo.org>
parents:
1832
diff
changeset
|
1185 |
9eabf7fadfdd
frontends (jp): onActionNew displays xmlui message if no meta_type is defined and xmlui is defined
souliane <souliane@mailoo.org>
parents:
1832
diff
changeset
|
1186 @param xml_ui (unicode): dialog XML representation |
9eabf7fadfdd
frontends (jp): onActionNew displays xmlui message if no meta_type is defined and xmlui is defined
souliane <souliane@mailoo.org>
parents:
1832
diff
changeset
|
1187 """ |
9eabf7fadfdd
frontends (jp): onActionNew displays xmlui message if no meta_type is defined and xmlui is defined
souliane <souliane@mailoo.org>
parents:
1832
diff
changeset
|
1188 # FIXME: we temporarily use ElementTree, but a real XMLUI managing module |
9eabf7fadfdd
frontends (jp): onActionNew displays xmlui message if no meta_type is defined and xmlui is defined
souliane <souliane@mailoo.org>
parents:
1832
diff
changeset
|
1189 # should be available in the future |
9eabf7fadfdd
frontends (jp): onActionNew displays xmlui message if no meta_type is defined and xmlui is defined
souliane <souliane@mailoo.org>
parents:
1832
diff
changeset
|
1190 # TODO: XMLUI module |
9eabf7fadfdd
frontends (jp): onActionNew displays xmlui message if no meta_type is defined and xmlui is defined
souliane <souliane@mailoo.org>
parents:
1832
diff
changeset
|
1191 ui = ET.fromstring(xml_ui.encode('utf-8')) |
9eabf7fadfdd
frontends (jp): onActionNew displays xmlui message if no meta_type is defined and xmlui is defined
souliane <souliane@mailoo.org>
parents:
1832
diff
changeset
|
1192 dialog = ui.find("dialog") |
9eabf7fadfdd
frontends (jp): onActionNew displays xmlui message if no meta_type is defined and xmlui is defined
souliane <souliane@mailoo.org>
parents:
1832
diff
changeset
|
1193 if dialog is not None: |
9eabf7fadfdd
frontends (jp): onActionNew displays xmlui message if no meta_type is defined and xmlui is defined
souliane <souliane@mailoo.org>
parents:
1832
diff
changeset
|
1194 self.disp(dialog.findtext("message"), error=dialog.get("level") == "error") |
9eabf7fadfdd
frontends (jp): onActionNew displays xmlui message if no meta_type is defined and xmlui is defined
souliane <souliane@mailoo.org>
parents:
1832
diff
changeset
|
1195 |
3040 | 1196 async def start_answering(self): |
1197 """Auto reply to confirmation requests""" | |
2368
3865a772c360
jp (base): fixed CommandAnswering and progress bar
Goffi <goffi@goffi.org>
parents:
2313
diff
changeset
|
1198 self.host.bridge.register_signal("actionNew", self.onActionNew) |
3040 | 1199 actions = await self.host.bridge.actionsGet(self.profile) |
1623 | 1200 for action_data, action_id, security_limit in actions: |
3040 | 1201 await self.onActionNew(action_data, action_id, security_limit, self.profile) |