Mercurial > libervia-backend
annotate sat_frontends/jp/base.py @ 3481:7892585b7e17
core (setup), jp, primitivus: update console scripts + classifiers:
- console scripts have been updated following the renaming with:
- "libervia-cli" and "libervia-tui" for "jp" and "primitivus"
- "li" alias as a shortcut for libervia-cli
- "jp" and "primitivus" are kept as alias
- updated classifiers to reflect Python 3.9 support
author | Goffi <goffi@goffi.org> |
---|---|
date | Sat, 20 Mar 2021 20:42:07 +0100 |
parents | be6d91572633 |
children | c9703067c700 |
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 |
3479 | 4 # Copyright (C) 2009-2021 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 |
3046 | 33 import tty |
34 import termios | |
3040 | 35 from pathlib import Path |
817 | 36 from glob import iglob |
37 from importlib import import_module | |
1139
75025461141f
move sat.tools.jid to sat_frontends.tools.jid
souliane <souliane@mailoo.org>
parents:
1033
diff
changeset
|
38 from sat_frontends.tools.jid import JID |
2098
e0066920a661
primitivus, jp: dynamic bridge + fixed D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
2086
diff
changeset
|
39 from sat.tools import config |
e0066920a661
primitivus, jp: dynamic bridge + fixed D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
2086
diff
changeset
|
40 from sat.tools.common import dynamic_import |
2532 | 41 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
|
42 from sat.tools.common import date_utils |
3046 | 43 from sat.tools.common import utils |
44 from sat.tools.common.ansi import ANSI as A | |
817 | 45 from sat.core import exceptions |
46 import sat_frontends.jp | |
3043
3df611adb598
jp: handle dbus bridge with asyncio:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
47 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
|
48 from sat_frontends.jp.constants import Const as C |
3049
9839ce068140
jp: password is now prompted if needed:
Goffi <goffi@goffi.org>
parents:
3046
diff
changeset
|
49 from sat_frontends.bridge.bridge_frontend import BridgeException |
2532 | 50 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
|
51 import xml.etree.ElementTree as ET # FIXME: used temporarily to manage XMLUI |
1950 | 52 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
|
53 |
2098
e0066920a661
primitivus, jp: dynamic bridge + fixed D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
2086
diff
changeset
|
54 ## bridge handling |
e0066920a661
primitivus, jp: dynamic bridge + fixed D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
2086
diff
changeset
|
55 # 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
|
56 main_config = config.parseMainConf() |
e0066920a661
primitivus, jp: dynamic bridge + fixed D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
2086
diff
changeset
|
57 bridge_name = config.getConfig(main_config, '', 'bridge', 'dbus') |
3043
3df611adb598
jp: handle dbus bridge with asyncio:
Goffi <goffi@goffi.org>
parents:
3040
diff
changeset
|
58 JPLoop = getJPLoop(bridge_name) |
2098
e0066920a661
primitivus, jp: dynamic bridge + fixed D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
2086
diff
changeset
|
59 |
e0066920a661
primitivus, jp: dynamic bridge + fixed D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
2086
diff
changeset
|
60 |
0 | 61 try: |
817 | 62 import progressbar |
63 except ImportError: | |
3040 | 64 msg = (_('ProgressBar not available, please download it at ' |
65 'http://pypi.python.org/pypi/progressbar\n' | |
66 'Progress bar deactivated\n--\n')) | |
67 print(msg, file=sys.stderr) | |
817 | 68 progressbar=None |
69 | |
70 #consts | |
3028 | 71 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
|
72 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
|
73 Get the latest version at """ + C.APP_URL |
817 | 74 |
3479 | 75 COPYLEFT = """Copyright (C) 2009-2021 Jérôme Poisson, Adrien Cossa |
817 | 76 This program comes with ABSOLUTELY NO WARRANTY; |
77 This is free software, and you are welcome to redistribute it under certain conditions. | |
78 """ | |
0 | 79 |
3040 | 80 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
|
81 |
0 | 82 |
2704
ab37d1c7c38c
jp (base): new date_decoder to specify dates in arguments
Goffi <goffi@goffi.org>
parents:
2692
diff
changeset
|
83 def date_decoder(arg): |
ab37d1c7c38c
jp (base): new date_decoder to specify dates in arguments
Goffi <goffi@goffi.org>
parents:
2692
diff
changeset
|
84 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
|
85 |
ab37d1c7c38c
jp (base): new date_decoder to specify dates in arguments
Goffi <goffi@goffi.org>
parents:
2692
diff
changeset
|
86 |
3481
7892585b7e17
core (setup), jp, primitivus: update console scripts + classifiers:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
87 class LiberviaCli: |
814 | 88 """ |
89 This class can be use to establish a connection with the | |
90 bridge. Moreover, it should manage a main loop. | |
91 | |
92 To use it, you mainly have to redefine the method run to perform | |
93 specify what kind of operation you want to perform. | |
94 | |
95 """ | |
817 | 96 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
|
97 """ |
de785fcf9a7b
jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents:
1605
diff
changeset
|
98 |
3040 | 99 @attribute quit_on_progress_end (bool): set to False if you manage yourself |
100 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
|
101 @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
|
102 by default display a message |
3040 | 103 @attribute progress_success(callable): method to call when progress is |
104 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
|
105 @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
|
106 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
|
107 """ |
3046 | 108 self.sat_conf = main_config |
109 self.set_color_theme() | |
2098
e0066920a661
primitivus, jp: dynamic bridge + fixed D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
2086
diff
changeset
|
110 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
|
111 if bridge_module is None: |
3028 | 112 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
|
113 sys.exit(1) |
814 | 114 |
3040 | 115 self.bridge = bridge_module.AIOBridge() |
116 self._onQuitCallbacks = [] | |
2098
e0066920a661
primitivus, jp: dynamic bridge + fixed D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
2086
diff
changeset
|
117 |
3046 | 118 def get_config(self, name, section='jp', default=None): |
119 """Retrieve a setting value from sat.conf""" | |
120 return config.getConfig(self.sat_conf, section, name, default=default) | |
121 | |
122 def guess_background(self): | |
3378
67e306cae157
jp: don't guess background when stdout is not a tty
Goffi <goffi@goffi.org>
parents:
3136
diff
changeset
|
123 if not sys.stdout.isatty(): |
67e306cae157
jp: don't guess background when stdout is not a tty
Goffi <goffi@goffi.org>
parents:
3136
diff
changeset
|
124 return 'dark' |
3046 | 125 stdin_fd = sys.stdin.fileno() |
126 old_settings = termios.tcgetattr(stdin_fd) | |
127 try: | |
128 tty.setraw(sys.stdin.fileno()) | |
129 # we request background color | |
130 sys.stdout.write("\033]11;?\a") | |
131 sys.stdout.flush() | |
132 expected = "\033]11;rgb:" | |
133 for c in expected: | |
134 ch = sys.stdin.read(1) | |
135 if ch != c: | |
3051
ee2654308a57
jp: default to "dark" if background is not set and can't be detected
Goffi <goffi@goffi.org>
parents:
3049
diff
changeset
|
136 # background id is not supported, we default to "dark" |
3046 | 137 # TODO: log something? |
3051
ee2654308a57
jp: default to "dark" if background is not set and can't be detected
Goffi <goffi@goffi.org>
parents:
3049
diff
changeset
|
138 return 'dark' |
3046 | 139 red, green, blue = [int(c, 16)/65535 for c in sys.stdin.read(14).split('/')] |
140 # '\a' is the last character | |
141 sys.stdin.read(1) | |
142 finally: | |
143 termios.tcsetattr(stdin_fd, termios.TCSADRAIN, old_settings) | |
144 | |
145 lum = utils.per_luminance(red, green, blue) | |
146 if lum <= 0.5: | |
147 return 'dark' | |
148 else: | |
149 return 'light' | |
150 | |
151 def set_color_theme(self): | |
152 background = self.get_config('background', default='auto') | |
153 if background == 'auto': | |
154 background = self.guess_background() | |
155 if background not in ('dark', 'light'): | |
156 raise exceptions.ConfigError(_( | |
157 f'Invalid value set for "background" ({background!r}), please check ' | |
158 f'your settings in sat.conf')) | |
159 if background == 'light': | |
160 C.A_HEADER = A.FG_MAGENTA | |
161 C.A_SUBHEADER = A.BOLD + A.FG_RED | |
162 C.A_LEVEL_COLORS = (C.A_HEADER, A.BOLD + A.FG_BLUE, A.FG_MAGENTA, A.FG_CYAN) | |
163 C.A_SUCCESS = A.FG_GREEN | |
164 C.A_FAILURE = A.BOLD + A.FG_RED | |
165 C.A_WARNING = A.FG_RED | |
166 C.A_PROMPT_PATH = A.FG_BLUE | |
167 C.A_PROMPT_SUF = A.BOLD | |
168 C.A_DIRECTORY = A.BOLD + A.FG_MAGENTA | |
169 C.A_FILE = A.FG_BLACK | |
170 | |
3040 | 171 def _bridgeConnected(self): |
172 self.parser = argparse.ArgumentParser( | |
173 formatter_class=argparse.RawDescriptionHelpFormatter, description=DESCRIPTION) | |
817 | 174 self._make_parents() |
175 self.add_parser_options() | |
3040 | 176 self.subparsers = self.parser.add_subparsers( |
177 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
|
178 |
de785fcf9a7b
jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents:
1605
diff
changeset
|
179 # progress attributes |
817 | 180 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
|
181 self.quit_on_progress_end = True |
817 | 182 |
1950 | 183 # outputs |
184 self._outputs = {} | |
185 for type_ in C.OUTPUT_TYPES: | |
186 self._outputs[type_] = OrderedDict() | |
2189
a25a256688e2
jp (base): output can now specify a default when registering
Goffi <goffi@goffi.org>
parents:
2188
diff
changeset
|
187 self.default_output = {} |
1950 | 188 |
2705
0bb811aaf11d
jp (base): new own_jid host attribute:
Goffi <goffi@goffi.org>
parents:
2704
diff
changeset
|
189 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
|
190 |
817 | 191 @property |
192 def progress_id(self): | |
193 return self._progress_id | |
194 | |
3040 | 195 async def set_progress_id(self, progress_id): |
196 # because we use async, we need an explicit setter | |
197 self._progress_id = progress_id | |
198 await self.replayCache('progress_ids_cache') | |
817 | 199 |
1606
de785fcf9a7b
jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents:
1605
diff
changeset
|
200 @property |
de785fcf9a7b
jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents:
1605
diff
changeset
|
201 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
|
202 try: |
de785fcf9a7b
jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents:
1605
diff
changeset
|
203 self.pbar |
de785fcf9a7b
jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents:
1605
diff
changeset
|
204 except AttributeError: |
de785fcf9a7b
jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents:
1605
diff
changeset
|
205 return False |
de785fcf9a7b
jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents:
1605
diff
changeset
|
206 else: |
de785fcf9a7b
jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents:
1605
diff
changeset
|
207 return True |
de785fcf9a7b
jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents:
1605
diff
changeset
|
208 |
de785fcf9a7b
jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents:
1605
diff
changeset
|
209 @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
|
210 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
|
211 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
|
212 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
|
213 |
1605
0aded9648c5c
jp (base): added a self.disp method which manage verbosity and stderr + verbosity property
Goffi <goffi@goffi.org>
parents:
1604
diff
changeset
|
214 @property |
0aded9648c5c
jp (base): added a self.disp method which manage verbosity and stderr + verbosity property
Goffi <goffi@goffi.org>
parents:
1604
diff
changeset
|
215 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
|
216 try: |
0aded9648c5c
jp (base): added a self.disp method which manage verbosity and stderr + verbosity property
Goffi <goffi@goffi.org>
parents:
1604
diff
changeset
|
217 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
|
218 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
|
219 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
|
220 |
3040 | 221 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
|
222 """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
|
223 |
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
|
224 @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
|
225 if the attribute doesn't exist, there is no cache and the call is ignored |
3040 | 226 else the cache must be a list of tuples containing the replay callback as |
227 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
|
228 """ |
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
|
229 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
|
230 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
|
231 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
|
232 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
|
233 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
|
234 for cache_data in cache: |
3040 | 235 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
|
236 |
3407
2f0be2b7de68
jp: replace `no_lf` argument by `end` in `disp` (same as in `print`)
Goffi <goffi@goffi.org>
parents:
3378
diff
changeset
|
237 def disp(self, msg, verbosity=0, error=False, end='\n'): |
1605
0aded9648c5c
jp (base): added a self.disp method which manage verbosity and stderr + verbosity property
Goffi <goffi@goffi.org>
parents:
1604
diff
changeset
|
238 """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
|
239 |
0aded9648c5c
jp (base): added a self.disp method which manage verbosity and stderr + verbosity property
Goffi <goffi@goffi.org>
parents:
1604
diff
changeset
|
240 @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
|
241 @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
|
242 @param error(bool): if True, print to stderr instead of stdout |
3407
2f0be2b7de68
jp: replace `no_lf` argument by `end` in `disp` (same as in `print`)
Goffi <goffi@goffi.org>
parents:
3378
diff
changeset
|
243 @param end(str): string appended after the last value, default a newline |
1605
0aded9648c5c
jp (base): added a self.disp method which manage verbosity and stderr + verbosity property
Goffi <goffi@goffi.org>
parents:
1604
diff
changeset
|
244 """ |
0aded9648c5c
jp (base): added a self.disp method which manage verbosity and stderr + verbosity property
Goffi <goffi@goffi.org>
parents:
1604
diff
changeset
|
245 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
|
246 if error: |
3407
2f0be2b7de68
jp: replace `no_lf` argument by `end` in `disp` (same as in `print`)
Goffi <goffi@goffi.org>
parents:
3378
diff
changeset
|
247 print(msg, end=end, 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
|
248 else: |
3407
2f0be2b7de68
jp: replace `no_lf` argument by `end` in `disp` (same as in `print`)
Goffi <goffi@goffi.org>
parents:
3378
diff
changeset
|
249 print(msg, end=end) |
1605
0aded9648c5c
jp (base): added a self.disp method which manage verbosity and stderr + verbosity property
Goffi <goffi@goffi.org>
parents:
1604
diff
changeset
|
250 |
3040 | 251 async def output(self, type_, name, extra_outputs, data): |
2155 | 252 if name in extra_outputs: |
3040 | 253 method = extra_outputs[name] |
2155 | 254 else: |
3040 | 255 method = self._outputs[type_][name]['callback'] |
256 | |
257 ret = method(data) | |
258 if inspect.isawaitable(ret): | |
259 await ret | |
1950 | 260 |
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
|
261 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
|
262 """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
|
263 |
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
|
264 @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
|
265 """ |
3040 | 266 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
|
267 |
1950 | 268 def getOutputChoices(self, output_type): |
269 """Return valid output filters for output_type | |
270 | |
271 @param output_type: True for default, | |
272 else can be any registered type | |
273 """ | |
3028 | 274 return list(self._outputs[output_type].keys()) |
1950 | 275 |
817 | 276 def _make_parents(self): |
277 self.parents = {} | |
278 | |
3040 | 279 # we have a special case here as the start-session option is present only if |
280 # connection is not needed, so we create two similar parents, one with the | |
281 # 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
|
282 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
|
283 parent = self.parents[parent_name] = argparse.ArgumentParser(add_help=False) |
3040 | 284 parent.add_argument( |
285 "-p", "--profile", action="store", type=str, default='@DEFAULT@', | |
286 help=_("Use PROFILE profile key (default: %(default)s)")) | |
287 parent.add_argument( | |
3049
9839ce068140
jp: password is now prompted if needed:
Goffi <goffi@goffi.org>
parents:
3046
diff
changeset
|
288 "--pwd", action="store", metavar='PASSWORD', |
3040 | 289 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
|
290 |
3040 | 291 profile_parent, profile_session_parent = (self.parents['profile'], |
292 self.parents['profile_session']) | |
1594
313f2bb7841b
jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents:
1588
diff
changeset
|
293 |
3040 | 294 connect_short, connect_long, connect_action, connect_help = ( |
295 "-c", "--connect", "store_true", | |
296 _("Connect the profile before doing anything else") | |
297 ) | |
298 profile_parent.add_argument( | |
299 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
|
300 |
313f2bb7841b
jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents:
1588
diff
changeset
|
301 profile_session_connect_group = profile_session_parent.add_mutually_exclusive_group() |
3040 | 302 profile_session_connect_group.add_argument( |
303 connect_short, connect_long, action=connect_action, help=connect_help) | |
304 profile_session_connect_group.add_argument( | |
305 "--start-session", action="store_true", | |
306 help=_("Start a profile session without connecting")) | |
817 | 307 |
3040 | 308 progress_parent = self.parents['progress'] = argparse.ArgumentParser( |
309 add_help=False) | |
817 | 310 if progressbar: |
3040 | 311 progress_parent.add_argument( |
312 "-P", "--progress", action="store_true", help=_("Show progress bar")) | |
0 | 313 |
1600 | 314 verbose_parent = self.parents['verbose'] = argparse.ArgumentParser(add_help=False) |
3040 | 315 verbose_parent.add_argument( |
316 '--verbose', '-v', action='count', default=0, | |
317 help=_("Add a verbosity level (can be used multiple times)")) | |
1600 | 318 |
3412
7b4ae3dbc041
jp (base, pubsub/set): new `--quiet` argument:
Goffi <goffi@goffi.org>
parents:
3409
diff
changeset
|
319 quiet_parent = self.parents['quiet'] = argparse.ArgumentParser(add_help=False) |
7b4ae3dbc041
jp (base, pubsub/set): new `--quiet` argument:
Goffi <goffi@goffi.org>
parents:
3409
diff
changeset
|
320 quiet_parent.add_argument( |
7b4ae3dbc041
jp (base, pubsub/set): new `--quiet` argument:
Goffi <goffi@goffi.org>
parents:
3409
diff
changeset
|
321 '--quiet', '-q', action='store_true', |
7b4ae3dbc041
jp (base, pubsub/set): new `--quiet` argument:
Goffi <goffi@goffi.org>
parents:
3409
diff
changeset
|
322 help=_("be quiet (only output machine readable data)")) |
7b4ae3dbc041
jp (base, pubsub/set): new `--quiet` argument:
Goffi <goffi@goffi.org>
parents:
3409
diff
changeset
|
323 |
2532 | 324 draft_parent = self.parents['draft'] = argparse.ArgumentParser(add_help=False) |
325 draft_group = draft_parent.add_argument_group(_('draft handling')) | |
3040 | 326 draft_group.add_argument( |
327 "-D", "--current", action="store_true", help=_("load current draft")) | |
328 draft_group.add_argument( | |
329 "-F", "--draft-path", type=Path, help=_("path to a draft file to retrieve")) | |
2532 | 330 |
331 | |
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
|
332 def make_pubsub_group(self, flags, defaults): |
2532 | 333 """generate pubsub options according to flags |
334 | |
335 @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
|
336 @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
|
337 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
|
338 value will be set in " (DEFAULT: {value})", or can be None to remove DEFAULT |
2532 | 339 @return (ArgumentParser): parser to add |
340 """ | |
341 flags = misc.FlagsHandler(flags) | |
342 parent = argparse.ArgumentParser(add_help=False) | |
343 pubsub_group = parent.add_argument_group('pubsub') | |
3028 | 344 pubsub_group.add_argument("-u", "--pubsub-url", |
345 help=_("Pubsub URL (xmpp or http)")) | |
2532 | 346 |
3028 | 347 service_help = _("JID of the PubSub service") |
2532 | 348 if not flags.service: |
3028 | 349 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
|
350 if default is not None: |
3028 | 351 service_help += _(" (DEFAULT: {default})".format(default=default)) |
352 pubsub_group.add_argument("-s", "--service", default='', | |
2532 | 353 help=service_help) |
354 | |
3028 | 355 node_help = _("node to request") |
2532 | 356 if not flags.node: |
3028 | 357 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
|
358 if default is not None: |
3028 | 359 node_help += _(" (DEFAULT: {default})".format(default=default)) |
360 pubsub_group.add_argument("-n", "--node", default='', help=node_help) | |
2532 | 361 |
362 if flags.single_item: | |
3028 | 363 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
|
364 if not flags.item: |
3028 | 365 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
|
366 if default is not None: |
3028 | 367 item_help += _(" (DEFAULT: {default})".format(default=default)) |
368 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
|
369 help=item_help) |
3040 | 370 pubsub_group.add_argument( |
371 "-L", "--last-item", action='store_true', help=_('retrieve last item')) | |
2532 | 372 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
|
373 # 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
|
374 # and Orbder-by |
3040 | 375 pubsub_group.add_argument( |
376 "-i", "--item", action='append', dest='items', default=[], | |
377 help=_("items to retrieve (DEFAULT: all)")) | |
2539 | 378 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
|
379 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
|
380 # 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
|
381 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
|
382 "-M", "--max-items", dest="max", type=int, |
3028 | 383 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
|
384 .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
|
385 # 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
|
386 # 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
|
387 # 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
|
388 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
|
389 "-m", "--max", dest="rsm_max", type=int, |
3028 | 390 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
|
391 |
92af49cde255
jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents:
2761
diff
changeset
|
392 # RSM |
92af49cde255
jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents:
2761
diff
changeset
|
393 |
92af49cde255
jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents:
2761
diff
changeset
|
394 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
|
395 rsm_page_group.add_argument( |
3028 | 396 "-a", "--after", dest="rsm_after", |
397 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
|
398 rsm_page_group.add_argument( |
3028 | 399 "-b", "--before", dest="rsm_before", |
400 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
|
401 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
|
402 "--index", dest="rsm_index", type=int, |
3028 | 403 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
|
404 |
92af49cde255
jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents:
2761
diff
changeset
|
405 |
92af49cde255
jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents:
2761
diff
changeset
|
406 # MAM |
92af49cde255
jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents:
2761
diff
changeset
|
407 |
92af49cde255
jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents:
2761
diff
changeset
|
408 pubsub_group.add_argument( |
3028 | 409 "-f", "--filter", dest='mam_filters', nargs=2, |
410 action='append', default=[], help=_("MAM filters to use"), | |
411 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
|
412 ) |
92af49cde255
jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents:
2761
diff
changeset
|
413 |
92af49cde255
jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents:
2761
diff
changeset
|
414 # Order-By |
92af49cde255
jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents:
2761
diff
changeset
|
415 |
92af49cde255
jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents:
2761
diff
changeset
|
416 # 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
|
417 # 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
|
418 # 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
|
419 # available) |
92af49cde255
jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents:
2761
diff
changeset
|
420 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
|
421 "-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
|
422 C.ORDER_BY_MODIFICATION], |
3028 | 423 help=_("how items should be ordered")) |
2532 | 424 |
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
|
425 if not flags.all_used: |
3040 | 426 raise exceptions.InternalError('unknown flags: {flags}'.format( |
427 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
|
428 if defaults: |
3040 | 429 raise exceptions.InternalError(f'unused defaults: {defaults}') |
2532 | 430 |
431 return parent | |
2235
4db836386641
jp: added use_pubsub and use_pubsub_node_req arguments to CommandBase
Goffi <goffi@goffi.org>
parents:
2213
diff
changeset
|
432 |
817 | 433 def add_parser_options(self): |
3040 | 434 self.parser.add_argument( |
435 '--version', | |
436 action='version', | |
437 version=("{name} {version} {copyleft}".format( | |
438 name = PROG_NAME, | |
439 version = self.version, | |
440 copyleft = COPYLEFT)) | |
441 ) | |
817 | 442 |
2189
a25a256688e2
jp (base): output can now specify a default when registering
Goffi <goffi@goffi.org>
parents:
2188
diff
changeset
|
443 def register_output(self, type_, name, callback, description="", default=False): |
1950 | 444 if type_ not in C.OUTPUT_TYPES: |
3028 | 445 log.error("Invalid output type {}".format(type_)) |
1950 | 446 return |
447 self._outputs[type_][name] = {'callback': callback, | |
448 'description': description | |
449 } | |
2189
a25a256688e2
jp (base): output can now specify a default when registering
Goffi <goffi@goffi.org>
parents:
2188
diff
changeset
|
450 if default: |
a25a256688e2
jp (base): output can now specify a default when registering
Goffi <goffi@goffi.org>
parents:
2188
diff
changeset
|
451 if type_ in self.default_output: |
3040 | 452 self.disp( |
453 _(f'there is already a default output for {type_}, ignoring new one') | |
454 ) | |
2189
a25a256688e2
jp (base): output can now specify a default when registering
Goffi <goffi@goffi.org>
parents:
2188
diff
changeset
|
455 else: |
a25a256688e2
jp (base): output can now specify a default when registering
Goffi <goffi@goffi.org>
parents:
2188
diff
changeset
|
456 self.default_output[type_] = name |
a25a256688e2
jp (base): output can now specify a default when registering
Goffi <goffi@goffi.org>
parents:
2188
diff
changeset
|
457 |
1950 | 458 |
2187
4ec72927a222
jp (outputs): moved output options parsing and checking to base methods
Goffi <goffi@goffi.org>
parents:
2178
diff
changeset
|
459 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
|
460 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
|
461 options_dict = {} |
4ec72927a222
jp (outputs): moved output options parsing and checking to base methods
Goffi <goffi@goffi.org>
parents:
2178
diff
changeset
|
462 for option in options: |
4ec72927a222
jp (outputs): moved output options parsing and checking to base methods
Goffi <goffi@goffi.org>
parents:
2178
diff
changeset
|
463 try: |
3028 | 464 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
|
465 except ValueError: |
4ec72927a222
jp (outputs): moved output options parsing and checking to base methods
Goffi <goffi@goffi.org>
parents:
2178
diff
changeset
|
466 key, value = option, None |
4ec72927a222
jp (outputs): moved output options parsing and checking to base methods
Goffi <goffi@goffi.org>
parents:
2178
diff
changeset
|
467 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
|
468 return options_dict |
4ec72927a222
jp (outputs): moved output options parsing and checking to base methods
Goffi <goffi@goffi.org>
parents:
2178
diff
changeset
|
469 |
4ec72927a222
jp (outputs): moved output options parsing and checking to base methods
Goffi <goffi@goffi.org>
parents:
2178
diff
changeset
|
470 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
|
471 if not accepted_set.issuperset(options): |
3040 | 472 self.disp( |
473 _("The following output options are invalid: {invalid_options}").format( | |
3028 | 474 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
|
475 error=True) |
4ec72927a222
jp (outputs): moved output options parsing and checking to base methods
Goffi <goffi@goffi.org>
parents:
2178
diff
changeset
|
476 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
|
477 |
1950 | 478 def import_plugins(self): |
479 """Automaticaly import commands and outputs in jp | |
480 | |
817 | 481 looks from modules names cmd_*.py in jp path and import them |
482 """ | |
483 path = os.path.dirname(sat_frontends.jp.__file__) | |
1950 | 484 # XXX: outputs must be imported before commands as they are used for arguments |
3040 | 485 for type_, pattern in ((C.PLUGIN_OUTPUT, 'output_*.py'), |
486 (C.PLUGIN_CMD, 'cmd_*.py')): | |
487 modules = ( | |
488 os.path.splitext(module)[0] | |
489 for module in map(os.path.basename, iglob(os.path.join(path, pattern)))) | |
1950 | 490 for module_name in modules: |
2162
c9a67eb5bf72
jp (base): improved module import:
Goffi <goffi@goffi.org>
parents:
2161
diff
changeset
|
491 module_path = "sat_frontends.jp." + module_name |
1950 | 492 try: |
2162
c9a67eb5bf72
jp (base): improved module import:
Goffi <goffi@goffi.org>
parents:
2161
diff
changeset
|
493 module = import_module(module_path) |
1950 | 494 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
|
495 except ImportError as e: |
3040 | 496 self.disp( |
497 _(f"Can't import {module_path} plugin, ignoring it: {e}"), | |
498 error=True) | |
2162
c9a67eb5bf72
jp (base): improved module import:
Goffi <goffi@goffi.org>
parents:
2161
diff
changeset
|
499 except exceptions.CancelError: |
1950 | 500 continue |
2162
c9a67eb5bf72
jp (base): improved module import:
Goffi <goffi@goffi.org>
parents:
2161
diff
changeset
|
501 except exceptions.MissingModule as e: |
3028 | 502 self.disp(_("Missing module for plugin {name}: {missing}".format( |
2162
c9a67eb5bf72
jp (base): improved module import:
Goffi <goffi@goffi.org>
parents:
2161
diff
changeset
|
503 name = module_path, |
2188
052d560d0dce
jp (base): replaced log.warning by stderr prints
Goffi <goffi@goffi.org>
parents:
2187
diff
changeset
|
504 missing = e)), error=True) |
2162
c9a67eb5bf72
jp (base): improved module import:
Goffi <goffi@goffi.org>
parents:
2161
diff
changeset
|
505 |
0 | 506 |
1950 | 507 def import_plugin_module(self, module, type_): |
508 """add commands or outpus from a module to jp | |
817 | 509 |
1950 | 510 @param module: module containing commands or outputs |
511 @param type_(str): one of C_PLUGIN_* | |
817 | 512 """ |
513 try: | |
1950 | 514 class_names = getattr(module, '__{}__'.format(type_)) |
817 | 515 except AttributeError: |
3040 | 516 log.disp(_(f"Invalid plugin module [{type_}] {module}"), error=True) |
817 | 517 raise ImportError |
1950 | 518 else: |
519 for class_name in class_names: | |
520 cls = getattr(module, class_name) | |
521 cls(self) | |
817 | 522 |
2532 | 523 def get_xmpp_uri_from_http(self, http_url): |
524 """parse HTML page at http(s) URL, and looks for xmpp: uri""" | |
525 if http_url.startswith('https'): | |
3028 | 526 scheme = 'https' |
2532 | 527 elif http_url.startswith('http'): |
3028 | 528 scheme = 'http' |
2532 | 529 else: |
3028 | 530 raise exceptions.InternalError('An HTTP scheme is expected in this method') |
3040 | 531 self.disp(f"{scheme.upper()} URL found, trying to find associated xmpp: URI", 1) |
2532 | 532 # HTTP URL, we try to find xmpp: links |
533 try: | |
534 from lxml import etree | |
535 except ImportError: | |
3040 | 536 self.disp( |
537 "lxml module must be installed to use http(s) scheme, please install it " | |
538 "with \"pip install lxml\"", | |
539 error=True) | |
2672 | 540 self.quit(1) |
3028 | 541 import urllib.request, urllib.error, urllib.parse |
2532 | 542 parser = etree.HTMLParser() |
543 try: | |
3028 | 544 root = etree.parse(urllib.request.urlopen(http_url), parser) |
2532 | 545 except etree.XMLSyntaxError as e: |
3028 | 546 self.disp(_("Can't parse HTML page : {msg}").format(msg=e)) |
2532 | 547 links = [] |
548 else: | |
549 links = root.xpath("//link[@rel='alternate' and starts-with(@href, 'xmpp:')]") | |
550 if not links: | |
3040 | 551 self.disp( |
552 _('Could not find alternate "xmpp:" URI, can\'t find associated XMPP ' | |
553 'PubSub node/item'), | |
554 error=True) | |
2672 | 555 self.quit(1) |
2532 | 556 xmpp_uri = links[0].get('href') |
557 return xmpp_uri | |
558 | |
559 def parse_pubsub_args(self): | |
560 if self.args.pubsub_url is not None: | |
561 url = self.args.pubsub_url | |
562 | |
563 if url.startswith('http'): | |
564 # http(s) URL, we try to retrieve xmpp one from there | |
565 url = self.get_xmpp_uri_from_http(url) | |
566 | |
567 try: | |
568 uri_data = uri.parseXMPPUri(url) | |
569 except ValueError: | |
3028 | 570 self.parser.error(_('invalid XMPP URL: {url}').format(url=url)) |
2532 | 571 else: |
3028 | 572 if uri_data['type'] == 'pubsub': |
2532 | 573 # URL is alright, we only set data not already set by other options |
574 if not self.args.service: | |
3028 | 575 self.args.service = uri_data['path'] |
2532 | 576 if not self.args.node: |
3028 | 577 self.args.node = uri_data['node'] |
578 uri_item = uri_data.get('item') | |
2532 | 579 if uri_item: |
2556
340128e0b354
jp (base): fixed URI's item with use_pubsub
Goffi <goffi@goffi.org>
parents:
2552
diff
changeset
|
580 # there is an item in URI |
340128e0b354
jp (base): fixed URI's item with use_pubsub
Goffi <goffi@goffi.org>
parents:
2552
diff
changeset
|
581 # 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
|
582 # and item_last is not used either |
2532 | 583 try: |
2556
340128e0b354
jp (base): fixed URI's item with use_pubsub
Goffi <goffi@goffi.org>
parents:
2552
diff
changeset
|
584 item = self.args.item |
2532 | 585 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
|
586 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
|
587 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
|
588 except AttributeError: |
3040 | 589 self.disp( |
590 _("item specified in URL but not needed in command, " | |
591 "ignoring it"), | |
592 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
|
593 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
|
594 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
|
595 self.args.items = [uri_item] |
2532 | 596 else: |
2556
340128e0b354
jp (base): fixed URI's item with use_pubsub
Goffi <goffi@goffi.org>
parents:
2552
diff
changeset
|
597 if not item: |
340128e0b354
jp (base): fixed URI's item with use_pubsub
Goffi <goffi@goffi.org>
parents:
2552
diff
changeset
|
598 try: |
340128e0b354
jp (base): fixed URI's item with use_pubsub
Goffi <goffi@goffi.org>
parents:
2552
diff
changeset
|
599 item_last = self.args.item_last |
340128e0b354
jp (base): fixed URI's item with use_pubsub
Goffi <goffi@goffi.org>
parents:
2552
diff
changeset
|
600 except AttributeError: |
340128e0b354
jp (base): fixed URI's item with use_pubsub
Goffi <goffi@goffi.org>
parents:
2552
diff
changeset
|
601 item_last = False |
340128e0b354
jp (base): fixed URI's item with use_pubsub
Goffi <goffi@goffi.org>
parents:
2552
diff
changeset
|
602 if not item_last: |
340128e0b354
jp (base): fixed URI's item with use_pubsub
Goffi <goffi@goffi.org>
parents:
2552
diff
changeset
|
603 self.args.item = uri_item |
2532 | 604 else: |
3040 | 605 self.parser.error(_(f'XMPP URL is not a pubsub one: {url}')) |
2532 | 606 flags = self.args._cmd._pubsub_flags |
607 # we check required arguments here instead of using add_arguments' required option | |
608 # because the required argument can be set in URL | |
609 if C.SERVICE in flags and not self.args.service: | |
3028 | 610 self.parser.error(_("argument -s/--service is required")) |
2532 | 611 if C.NODE in flags and not self.args.node: |
3028 | 612 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
|
613 if C.ITEM in flags and not self.args.item: |
3028 | 614 self.parser.error(_("argument -i/--item is required")) |
2532 | 615 |
616 # FIXME: mutually groups can't be nested in a group and don't support title | |
617 # so we check conflict here. This may be fixed in Python 3, to be checked | |
618 try: | |
619 if self.args.item and self.args.item_last: | |
3040 | 620 self.parser.error( |
621 _("--item and --item-last can't be used at the same time")) | |
2532 | 622 except AttributeError: |
623 pass | |
624 | |
2775
2dfd5b1d39df
jp (base): fixed default values for --max-items and --max when using pubsub
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
625 try: |
2dfd5b1d39df
jp (base): fixed default values for --max-items and --max when using pubsub
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
626 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
|
627 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
|
628 except AttributeError: |
2dfd5b1d39df
jp (base): fixed default values for --max-items and --max when using pubsub
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
629 pass |
2dfd5b1d39df
jp (base): fixed default values for --max-items and --max when using pubsub
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
630 else: |
2dfd5b1d39df
jp (base): fixed default values for --max-items and --max when using pubsub
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
631 # 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
|
632 # 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
|
633 # 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
|
634 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
|
635 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
|
636 'rsm_index') |
2dfd5b1d39df
jp (base): fixed default values for --max-items and --max when using pubsub
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
637 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
|
638 # 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
|
639 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
|
640 else: |
2dfd5b1d39df
jp (base): fixed default values for --max-items and --max when using pubsub
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
641 # 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
|
642 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
|
643 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
|
644 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
|
645 |
3040 | 646 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
|
647 try: |
3040 | 648 await self.bridge.bridgeConnect() |
649 except Exception as e: | |
650 if isinstance(e, exceptions.BridgeExceptionNoService): | |
651 print((_("Can't connect to SàT backend, are you sure it's launched ?"))) | |
652 elif isinstance(e, exceptions.BridgeInitError): | |
653 print((_("Can't init bridge"))) | |
654 else: | |
655 print((_(f"Error while initialising bridge: {e}"))) | |
656 self.quit(C.EXIT_BRIDGE_ERROR, raise_exc=False) | |
657 return | |
658 self.version = await self.bridge.getVersion() | |
659 self._bridgeConnected() | |
660 self.import_plugins() | |
661 try: | |
662 self.args = self.parser.parse_args(args, namespace=None) | |
663 if self.args._cmd._use_pubsub: | |
664 self.parse_pubsub_args() | |
665 await self.args._cmd.run() | |
666 except SystemExit as e: | |
667 self.quit(e.code, raise_exc=False) | |
668 return | |
669 except QuitException: | |
670 return | |
817 | 671 |
3481
7892585b7e17
core (setup), jp, primitivus: update console scripts + classifiers:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
672 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
|
673 self.loop = JPLoop() |
3040 | 674 self.loop.run(self, args, namespace) |
0 | 675 |
3481
7892585b7e17
core (setup), jp, primitivus: update console scripts + classifiers:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
676 @classmethod |
7892585b7e17
core (setup), jp, primitivus: update console scripts + classifiers:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
677 def run(cls): |
7892585b7e17
core (setup), jp, primitivus: update console scripts + classifiers:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
678 cls()._run() |
7892585b7e17
core (setup), jp, primitivus: update console scripts + classifiers:
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
679 |
3040 | 680 def _read_stdin(self, stdin_fut): |
681 """Callback called by ainput to read stdin""" | |
682 line = sys.stdin.readline() | |
683 if line: | |
684 stdin_fut.set_result(line.rstrip(os.linesep)) | |
685 else: | |
686 stdin_fut.set_exception(EOFError()) | |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
687 |
3040 | 688 async def ainput(self, msg=''): |
689 """Asynchronous version of buildin "input" function""" | |
3407
2f0be2b7de68
jp: replace `no_lf` argument by `end` in `disp` (same as in `print`)
Goffi <goffi@goffi.org>
parents:
3378
diff
changeset
|
690 self.disp(msg, end=' ') |
3040 | 691 sys.stdout.flush() |
692 loop = asyncio.get_running_loop() | |
693 stdin_fut = loop.create_future() | |
694 loop.add_reader(sys.stdin, self._read_stdin, stdin_fut) | |
695 return await stdin_fut | |
696 | |
3409 | 697 async def confirm(self, message): |
698 """Request user to confirm action, return answer as boolean""" | |
699 res = await self.ainput(f"{message} (y/N)? ") | |
700 return res in ("y", "Y") | |
701 | |
3040 | 702 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
|
703 """Request user to confirm action, and quit if he doesn't""" |
3409 | 704 confirmed = await self.confirm(message) |
705 if not confirmed: | |
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
|
706 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
|
707 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
|
708 |
3040 | 709 def quitFromSignal(self, exit_code=0): |
710 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
|
711 |
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
|
712 /!\: 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
|
713 """ |
1918
01d56efd488b
jp: fixed traceback shown on a normal quit when a progression is finished
Goffi <goffi@goffi.org>
parents:
1884
diff
changeset
|
714 # 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
|
715 # so we use this little timeout trick to avoid it |
3040 | 716 self.loop.call_later(0, self.quit, exit_code) |
717 | |
718 def quit(self, exit_code=0, raise_exc=True): | |
719 """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
|
720 |
3040 | 721 This will stop the loop. |
722 @param exit_code(int): code to return when quitting the program | |
723 @param raise_exp(boolean): if True raise a QuitException to stop code execution | |
724 The default value should be used most of time. | |
725 """ | |
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
|
726 # 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
|
727 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
|
728 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
|
729 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
|
730 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
|
731 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
|
732 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
|
733 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
|
734 |
3040 | 735 self.loop.quit(exit_code) |
736 if raise_exc: | |
737 raise QuitException | |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
738 |
3040 | 739 async def check_jids(self, jids): |
814 | 740 """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
|
741 |
817 | 742 @param profile: profile name |
743 @param jids: list of jids | |
744 @return: List of jids | |
745 | |
814 | 746 """ |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
747 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
|
748 nodes2jid = {} |
393 | 749 |
3040 | 750 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
|
751 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
|
752 _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
|
753 try: |
1ae9aa94c351
jp: fixed bad try/except hidding errors + fixed bad management of jids without node
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
754 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
|
755 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
|
756 pass |
1ae9aa94c351
jp: fixed bad try/except hidding errors + fixed bad management of jids without node
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
757 |
1ae9aa94c351
jp: fixed bad try/except hidding errors + fixed bad management of jids without node
Goffi <goffi@goffi.org>
parents:
1290
diff
changeset
|
758 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
|
759 nodes2jid[_jid.node.lower()] = jid_s |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
760 |
817 | 761 def expand_jid(jid): |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
762 _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
|
763 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
|
764 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
|
765 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
|
766 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
|
767 else: |
b7c4bb2c0668
jp: - better expandJid: roster's jids' nodes are used after names to expand jid
Goffi <goffi@goffi.org>
parents:
480
diff
changeset
|
768 expanded = jid |
2588 | 769 return expanded |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
770 |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
771 def check(jid): |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
772 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
|
773 log.error (_("%s is not a valid JID !"), jid) |
817 | 774 self.quit(1) |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
775 |
814 | 776 dest_jids=[] |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
777 try: |
814 | 778 for i in range(len(jids)): |
817 | 779 dest_jids.append(expand_jid(jids[i])) |
814 | 780 check(dest_jids[i]) |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
781 except AttributeError: |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
782 pass |
0 | 783 |
814 | 784 return dest_jids |
0 | 785 |
3049
9839ce068140
jp: password is now prompted if needed:
Goffi <goffi@goffi.org>
parents:
3046
diff
changeset
|
786 async def a_pwd_input(self, msg=''): |
9839ce068140
jp: password is now prompted if needed:
Goffi <goffi@goffi.org>
parents:
3046
diff
changeset
|
787 """Like ainput but with echo disabled (useful for passwords)""" |
9839ce068140
jp: password is now prompted if needed:
Goffi <goffi@goffi.org>
parents:
3046
diff
changeset
|
788 # we disable echo, code adapted from getpass standard module which has been |
9839ce068140
jp: password is now prompted if needed:
Goffi <goffi@goffi.org>
parents:
3046
diff
changeset
|
789 # written by Piers Lauder (original), Guido van Rossum (Windows support and |
9839ce068140
jp: password is now prompted if needed:
Goffi <goffi@goffi.org>
parents:
3046
diff
changeset
|
790 # cleanup) and Gregory P. Smith (tty support & GetPassWarning), a big thanks |
9839ce068140
jp: password is now prompted if needed:
Goffi <goffi@goffi.org>
parents:
3046
diff
changeset
|
791 # to them (and for all the amazing work on Python). |
9839ce068140
jp: password is now prompted if needed:
Goffi <goffi@goffi.org>
parents:
3046
diff
changeset
|
792 stdin_fd = sys.stdin.fileno() |
9839ce068140
jp: password is now prompted if needed:
Goffi <goffi@goffi.org>
parents:
3046
diff
changeset
|
793 old = termios.tcgetattr(sys.stdin) |
9839ce068140
jp: password is now prompted if needed:
Goffi <goffi@goffi.org>
parents:
3046
diff
changeset
|
794 new = old[:] |
9839ce068140
jp: password is now prompted if needed:
Goffi <goffi@goffi.org>
parents:
3046
diff
changeset
|
795 new[3] &= ~termios.ECHO |
9839ce068140
jp: password is now prompted if needed:
Goffi <goffi@goffi.org>
parents:
3046
diff
changeset
|
796 tcsetattr_flags = termios.TCSAFLUSH |
9839ce068140
jp: password is now prompted if needed:
Goffi <goffi@goffi.org>
parents:
3046
diff
changeset
|
797 if hasattr(termios, 'TCSASOFT'): |
9839ce068140
jp: password is now prompted if needed:
Goffi <goffi@goffi.org>
parents:
3046
diff
changeset
|
798 tcsetattr_flags |= termios.TCSASOFT |
9839ce068140
jp: password is now prompted if needed:
Goffi <goffi@goffi.org>
parents:
3046
diff
changeset
|
799 try: |
9839ce068140
jp: password is now prompted if needed:
Goffi <goffi@goffi.org>
parents:
3046
diff
changeset
|
800 termios.tcsetattr(stdin_fd, tcsetattr_flags, new) |
9839ce068140
jp: password is now prompted if needed:
Goffi <goffi@goffi.org>
parents:
3046
diff
changeset
|
801 pwd = await self.ainput(msg=msg) |
9839ce068140
jp: password is now prompted if needed:
Goffi <goffi@goffi.org>
parents:
3046
diff
changeset
|
802 finally: |
9839ce068140
jp: password is now prompted if needed:
Goffi <goffi@goffi.org>
parents:
3046
diff
changeset
|
803 termios.tcsetattr(stdin_fd, tcsetattr_flags, old) |
9839ce068140
jp: password is now prompted if needed:
Goffi <goffi@goffi.org>
parents:
3046
diff
changeset
|
804 sys.stderr.flush() |
9839ce068140
jp: password is now prompted if needed:
Goffi <goffi@goffi.org>
parents:
3046
diff
changeset
|
805 self.disp('') |
9839ce068140
jp: password is now prompted if needed:
Goffi <goffi@goffi.org>
parents:
3046
diff
changeset
|
806 return pwd |
9839ce068140
jp: password is now prompted if needed:
Goffi <goffi@goffi.org>
parents:
3046
diff
changeset
|
807 |
9839ce068140
jp: password is now prompted if needed:
Goffi <goffi@goffi.org>
parents:
3046
diff
changeset
|
808 async def connectOrPrompt(self, method, err_msg=None): |
9839ce068140
jp: password is now prompted if needed:
Goffi <goffi@goffi.org>
parents:
3046
diff
changeset
|
809 """Try to connect/start profile session and prompt for password if needed |
9839ce068140
jp: password is now prompted if needed:
Goffi <goffi@goffi.org>
parents:
3046
diff
changeset
|
810 |
9839ce068140
jp: password is now prompted if needed:
Goffi <goffi@goffi.org>
parents:
3046
diff
changeset
|
811 @param method(callable): bridge method to either connect or start profile session |
9839ce068140
jp: password is now prompted if needed:
Goffi <goffi@goffi.org>
parents:
3046
diff
changeset
|
812 It will be called with password as sole argument, use lambda to do the call |
9839ce068140
jp: password is now prompted if needed:
Goffi <goffi@goffi.org>
parents:
3046
diff
changeset
|
813 properly |
9839ce068140
jp: password is now prompted if needed:
Goffi <goffi@goffi.org>
parents:
3046
diff
changeset
|
814 @param err_msg(str): message to show if connection fail |
9839ce068140
jp: password is now prompted if needed:
Goffi <goffi@goffi.org>
parents:
3046
diff
changeset
|
815 """ |
9839ce068140
jp: password is now prompted if needed:
Goffi <goffi@goffi.org>
parents:
3046
diff
changeset
|
816 password = self.args.pwd |
9839ce068140
jp: password is now prompted if needed:
Goffi <goffi@goffi.org>
parents:
3046
diff
changeset
|
817 while True: |
9839ce068140
jp: password is now prompted if needed:
Goffi <goffi@goffi.org>
parents:
3046
diff
changeset
|
818 try: |
9839ce068140
jp: password is now prompted if needed:
Goffi <goffi@goffi.org>
parents:
3046
diff
changeset
|
819 await method(password or '') |
9839ce068140
jp: password is now prompted if needed:
Goffi <goffi@goffi.org>
parents:
3046
diff
changeset
|
820 except Exception as e: |
9839ce068140
jp: password is now prompted if needed:
Goffi <goffi@goffi.org>
parents:
3046
diff
changeset
|
821 if ((isinstance(e, BridgeException) |
9839ce068140
jp: password is now prompted if needed:
Goffi <goffi@goffi.org>
parents:
3046
diff
changeset
|
822 and e.classname == 'PasswordError' |
9839ce068140
jp: password is now prompted if needed:
Goffi <goffi@goffi.org>
parents:
3046
diff
changeset
|
823 and self.args.pwd is None)): |
9839ce068140
jp: password is now prompted if needed:
Goffi <goffi@goffi.org>
parents:
3046
diff
changeset
|
824 if password is not None: |
9839ce068140
jp: password is now prompted if needed:
Goffi <goffi@goffi.org>
parents:
3046
diff
changeset
|
825 self.disp(A.color(C.A_WARNING, _("invalid password"))) |
9839ce068140
jp: password is now prompted if needed:
Goffi <goffi@goffi.org>
parents:
3046
diff
changeset
|
826 password = await self.a_pwd_input( |
9839ce068140
jp: password is now prompted if needed:
Goffi <goffi@goffi.org>
parents:
3046
diff
changeset
|
827 _("please enter profile password:")) |
9839ce068140
jp: password is now prompted if needed:
Goffi <goffi@goffi.org>
parents:
3046
diff
changeset
|
828 else: |
9839ce068140
jp: password is now prompted if needed:
Goffi <goffi@goffi.org>
parents:
3046
diff
changeset
|
829 self.disp(err_msg.format(profile=self.profile, e=e), error=True) |
9839ce068140
jp: password is now prompted if needed:
Goffi <goffi@goffi.org>
parents:
3046
diff
changeset
|
830 self.quit(C.EXIT_ERROR) |
9839ce068140
jp: password is now prompted if needed:
Goffi <goffi@goffi.org>
parents:
3046
diff
changeset
|
831 else: |
9839ce068140
jp: password is now prompted if needed:
Goffi <goffi@goffi.org>
parents:
3046
diff
changeset
|
832 break |
9839ce068140
jp: password is now prompted if needed:
Goffi <goffi@goffi.org>
parents:
3046
diff
changeset
|
833 |
3040 | 834 async def connect_profile(self): |
835 """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
|
836 |
817 | 837 @exit: - 1 when profile is not connected and --connect is not set |
838 - 1 when the profile doesn't exists | |
839 - 1 when there is a connection error | |
840 """ | |
841 # FIXME: need better exit codes | |
842 | |
3040 | 843 self.profile = await self.bridge.profileNameGet(self.args.profile) |
0 | 844 |
817 | 845 if not self.profile: |
3040 | 846 log.error(_(f"The profile [{self.args.profile}] doesn't exist")) |
847 self.quit(C.EXIT_ERROR) | |
817 | 848 |
1594
313f2bb7841b
jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents:
1588
diff
changeset
|
849 try: |
313f2bb7841b
jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents:
1588
diff
changeset
|
850 start_session = self.args.start_session |
313f2bb7841b
jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents:
1588
diff
changeset
|
851 except AttributeError: |
313f2bb7841b
jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents:
1588
diff
changeset
|
852 pass |
313f2bb7841b
jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents:
1588
diff
changeset
|
853 else: |
313f2bb7841b
jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents:
1588
diff
changeset
|
854 if start_session: |
3049
9839ce068140
jp: password is now prompted if needed:
Goffi <goffi@goffi.org>
parents:
3046
diff
changeset
|
855 await self.connectOrPrompt( |
9839ce068140
jp: password is now prompted if needed:
Goffi <goffi@goffi.org>
parents:
3046
diff
changeset
|
856 lambda pwd: self.bridge.profileStartSession(pwd, self.profile), |
9839ce068140
jp: password is now prompted if needed:
Goffi <goffi@goffi.org>
parents:
3046
diff
changeset
|
857 err_msg="Can't start {profile}'s session: {e}" |
9839ce068140
jp: password is now prompted if needed:
Goffi <goffi@goffi.org>
parents:
3046
diff
changeset
|
858 ) |
1594
313f2bb7841b
jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents:
1588
diff
changeset
|
859 return |
3040 | 860 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
|
861 if not self.args.connect: |
3040 | 862 self.disp(_( |
863 f"Session for [{self.profile}] is not started, please start it " | |
864 f"before using jp, or use either --start-session or --connect " | |
865 f"option"), error=True) | |
1594
313f2bb7841b
jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents:
1588
diff
changeset
|
866 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
|
867 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
|
868 return |
313f2bb7841b
jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents:
1588
diff
changeset
|
869 |
313f2bb7841b
jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents:
1588
diff
changeset
|
870 |
1401
265ff2bd8d67
jp: fixed crash on commands using profile without "connect" option
Goffi <goffi@goffi.org>
parents:
1396
diff
changeset
|
871 if not hasattr(self.args, 'connect'): |
3040 | 872 # a profile can be present without connect option (e.g. on profile |
873 # creation/deletion) | |
1401
265ff2bd8d67
jp: fixed crash on commands using profile without "connect" option
Goffi <goffi@goffi.org>
parents:
1396
diff
changeset
|
874 return |
1588
823a385235ef
jp: fixed bad --connect option check
Goffi <goffi@goffi.org>
parents:
1544
diff
changeset
|
875 elif self.args.connect is True: # if connection is asked, we connect the profile |
3049
9839ce068140
jp: password is now prompted if needed:
Goffi <goffi@goffi.org>
parents:
3046
diff
changeset
|
876 await self.connectOrPrompt( |
9839ce068140
jp: password is now prompted if needed:
Goffi <goffi@goffi.org>
parents:
3046
diff
changeset
|
877 lambda pwd: self.bridge.connect(self.profile, pwd, {}), |
9839ce068140
jp: password is now prompted if needed:
Goffi <goffi@goffi.org>
parents:
3046
diff
changeset
|
878 err_msg = 'Can\'t connect profile "{profile!s}": {e}' |
9839ce068140
jp: password is now prompted if needed:
Goffi <goffi@goffi.org>
parents:
3046
diff
changeset
|
879 ) |
814 | 880 return |
1594
313f2bb7841b
jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents:
1588
diff
changeset
|
881 else: |
3040 | 882 if not await self.bridge.isConnected(self.profile): |
883 log.error( | |
884 _(f"Profile [{self.profile}] is not connected, please connect it " | |
885 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
|
886 self.quit(1) |
0 | 887 |
3040 | 888 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
|
889 """Return the full jid if possible (add main resource when find a bare jid)""" |
3408
19bc03743aeb
jp (file/send): don't add main resource to bare jid anymore:
Goffi <goffi@goffi.org>
parents:
3407
diff
changeset
|
890 # TODO: to be removed, bare jid should work with all commands, notably for file |
19bc03743aeb
jp (file/send): don't add main resource to bare jid anymore:
Goffi <goffi@goffi.org>
parents:
3407
diff
changeset
|
891 # as backend now handle jingles message initiation |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
892 _jid = JID(param_jid) |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
893 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
|
894 #if the resource is not given, we try to add the main resource |
3040 | 895 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
|
896 if main_resource: |
3040 | 897 return f"{_jid.bare}/{main_resource}" |
401
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
898 return param_jid |
b2caa2615c4c
jp roster name manegement + Pipe transfer
Goffi <goffi@goffi.org>
parents:
393
diff
changeset
|
899 |
814 | 900 |
817 | 901 class CommandBase(object): |
814 | 902 |
2155 | 903 def __init__(self, host, name, use_profile=True, use_output=False, extra_outputs=None, |
1950 | 904 need_connect=None, help=None, **kwargs): |
2098
e0066920a661
primitivus, jp: dynamic bridge + fixed D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
2086
diff
changeset
|
905 """Initialise CommandBase |
e0066920a661
primitivus, jp: dynamic bridge + fixed D-Bus bridge:
Goffi <goffi@goffi.org>
parents:
2086
diff
changeset
|
906 |
817 | 907 @param host: Jp instance |
1594
313f2bb7841b
jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents:
1588
diff
changeset
|
908 @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
|
909 @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
|
910 @param use_output(bool, unicode): if not False, add --output option |
2155 | 911 @param extra_outputs(dict): list of command specific outputs: |
912 key is output name ("default" to use as main output) | |
3040 | 913 value is a callable which will format the output (data will be used as only |
914 argument) | |
2155 | 915 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
|
916 @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
|
917 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
|
918 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
|
919 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
|
920 @param help(unicode): help message to display |
817 | 921 @param **kwargs: args passed to ArgumentParser |
1950 | 922 use_* are handled directly, they can be: |
923 - use_progress(bool): if True, add progress bar activation option | |
924 progress* signals will be handled | |
925 - use_verbose(bool): if True, add verbosity option | |
2532 | 926 - use_pubsub(bool): if True, add pubsub options |
927 mandatory arguments are controlled by pubsub_req | |
928 - use_draft(bool): if True, add draft handling options | |
929 ** other arguments ** | |
3040 | 930 - pubsub_flags(iterable[unicode]): tuple of flags to set pubsub options, |
931 can be: | |
2532 | 932 C.SERVICE: service is required |
933 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
|
934 C.ITEM: item is required |
2532 | 935 C.SINGLE_ITEM: only one item is allowed |
814 | 936 """ |
817 | 937 try: # If we have subcommands, host is a CommandBase and we need to use host.host |
938 self.host = host.host | |
939 except AttributeError: | |
940 self.host = host | |
941 | |
1950 | 942 # --profile option |
817 | 943 parents = kwargs.setdefault('parents', set()) |
944 if use_profile: | |
3040 | 945 # self.host.parents['profile'] is an ArgumentParser with profile connection |
946 # arguments | |
1594
313f2bb7841b
jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents:
1588
diff
changeset
|
947 if need_connect is None: |
313f2bb7841b
jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents:
1588
diff
changeset
|
948 need_connect = True |
3040 | 949 parents.add( |
950 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
|
951 else: |
313f2bb7841b
jp: profile session can now be started without connection:
Goffi <goffi@goffi.org>
parents:
1588
diff
changeset
|
952 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
|
953 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
|
954 # 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
|
955 # 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
|
956 |
1950 | 957 # --output option |
958 if use_output: | |
2155 | 959 if extra_outputs is None: |
960 extra_outputs = {} | |
961 self.extra_outputs = extra_outputs | |
1950 | 962 if use_output == True: |
963 use_output = C.OUTPUT_TEXT | |
964 assert use_output in C.OUTPUT_TYPES | |
965 self._output_type = use_output | |
966 output_parent = argparse.ArgumentParser(add_help=False) | |
2155 | 967 choices = set(self.host.getOutputChoices(use_output)) |
968 choices.update(extra_outputs) | |
1950 | 969 if not choices: |
3040 | 970 raise exceptions.InternalError( |
971 "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
|
972 try: |
a25a256688e2
jp (base): output can now specify a default when registering
Goffi <goffi@goffi.org>
parents:
2188
diff
changeset
|
973 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
|
974 except KeyError: |
3028 | 975 if 'default' in choices: |
976 default = 'default' | |
977 elif 'simple' in choices: | |
978 default = 'simple' | |
2189
a25a256688e2
jp (base): output can now specify a default when registering
Goffi <goffi@goffi.org>
parents:
2188
diff
changeset
|
979 else: |
a25a256688e2
jp (base): output can now specify a default when registering
Goffi <goffi@goffi.org>
parents:
2188
diff
changeset
|
980 default = list(choices)[0] |
3040 | 981 output_parent.add_argument( |
982 '--output', '-O', choices=sorted(choices), default=default, | |
983 help=_("select output format (default: {})".format(default))) | |
984 output_parent.add_argument( | |
985 '--output-option', '--oo', action="append", dest='output_opts', | |
986 default=[], help=_("output specific option")) | |
1950 | 987 parents.add(output_parent) |
2155 | 988 else: |
989 assert extra_outputs is None | |
817 | 990 |
2532 | 991 self._use_pubsub = kwargs.pop('use_pubsub', False) |
992 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
|
993 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
|
994 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
|
995 parents.add(self.host.make_pubsub_group(flags, defaults)) |
2532 | 996 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
|
997 |
1950 | 998 # other common options |
3028 | 999 use_opts = {k:v for k,v in kwargs.items() if k.startswith('use_')} |
1000 for param, do_use in use_opts.items(): | |
1950 | 1001 opt=param[4:] # if param is use_verbose, opt is verbose |
1002 if opt not in self.host.parents: | |
3028 | 1003 raise exceptions.InternalError("Unknown parent option {}".format(opt)) |
1950 | 1004 del kwargs[param] |
1005 if do_use: | |
1006 parents.add(self.host.parents[opt]) | |
1600 | 1007 |
817 | 1008 self.parser = host.subparsers.add_parser(name, help=help, **kwargs) |
1009 if hasattr(self, "subcommands"): | |
3040 | 1010 self.subparsers = self.parser.add_subparsers(dest='subcommand', required=True) |
817 | 1011 else: |
2532 | 1012 self.parser.set_defaults(_cmd=self) |
817 | 1013 self.add_parser_options() |
1014 | |
1015 @property | |
3046 | 1016 def sat_conf(self): |
1017 return self.host.sat_conf | |
1018 | |
1019 @property | |
817 | 1020 def args(self): |
1021 return self.host.args | |
1022 | |
1023 @property | |
1024 def profile(self): | |
1025 return self.host.profile | |
1026 | |
1027 @property | |
2490
b4bf282d6354
jp (base): added verbosity property to CommandBase
Goffi <goffi@goffi.org>
parents:
2486
diff
changeset
|
1028 def verbosity(self): |
b4bf282d6354
jp (base): added verbosity property to CommandBase
Goffi <goffi@goffi.org>
parents:
2486
diff
changeset
|
1029 return self.host.verbosity |
b4bf282d6354
jp (base): added verbosity property to CommandBase
Goffi <goffi@goffi.org>
parents:
2486
diff
changeset
|
1030 |
b4bf282d6354
jp (base): added verbosity property to CommandBase
Goffi <goffi@goffi.org>
parents:
2486
diff
changeset
|
1031 @property |
817 | 1032 def progress_id(self): |
1033 return self.host.progress_id | |
814 | 1034 |
3040 | 1035 async def set_progress_id(self, progress_id): |
1036 return await self.host.set_progress_id(progress_id) | |
817 | 1037 |
3040 | 1038 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
|
1039 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
|
1040 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
|
1041 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
|
1042 # 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
|
1043 # 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
|
1044 # 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
|
1045 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
|
1046 try: |
3040 | 1047 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
|
1048 except AttributeError: |
3040 | 1049 cache = self.host.progress_ids_cache = [] |
1050 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
|
1051 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
|
1052 if self.host.watch_progress and uid == self.progress_id: |
3040 | 1053 await self.onProgressStarted(metadata) |
1054 while True: | |
1055 await asyncio.sleep(PROGRESS_DELAY) | |
1056 cont = await self.progressUpdate() | |
1057 if not cont: | |
1058 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
|
1059 |
3040 | 1060 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
|
1061 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
|
1062 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
|
1063 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
|
1064 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
|
1065 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
|
1066 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
|
1067 pass |
3040 | 1068 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
|
1069 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
|
1070 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
|
1071 |
3040 | 1072 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
|
1073 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
|
1074 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
|
1075 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
|
1076 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
|
1077 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
|
1078 if self.host.quit_on_progress_end: |
3040 | 1079 await self.onProgressError(message) |
1080 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
|
1081 |
3040 | 1082 async def progressUpdate(self): |
1083 """This method is continualy called to update the progress bar | |
1084 | |
1085 @return (bool): False to stop being called | |
1086 """ | |
1087 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
|
1088 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
|
1089 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
|
1090 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
|
1091 except KeyError: |
3040 | 1092 self.disp(_("file size is not known, we can't show a progress bar"), 1, |
1093 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
|
1094 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
|
1095 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
|
1096 #first answer, we must construct the bar |
3040 | 1097 |
1098 # if the instance has a pbar_template attribute, it is used has model, | |
1099 # else default one is used | |
1100 # template is a list of part, where part can be either a str to show directly | |
1101 # or a list where first argument is a name of a progressbar widget, and others | |
1102 # are used as widget arguments | |
1103 try: | |
1104 template = self.pbar_template | |
1105 except AttributeError: | |
1106 template = [ | |
1107 _("Progress: "), ["Percentage"], " ", ["Bar"], " ", | |
1108 ["FileTransferSpeed"], " ", ["ETA"] | |
1109 ] | |
1110 | |
1111 widgets = [] | |
1112 for part in template: | |
1113 if isinstance(part, str): | |
1114 widgets.append(part) | |
1115 else: | |
1116 widget = getattr(progressbar, part.pop(0)) | |
1117 widgets.append(widget(*part)) | |
1118 | |
1119 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
|
1120 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
|
1121 |
39545dc527a1
jp: an onProgressUpdate method is now called on each progress update, allowing to handle metadata
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
1122 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
|
1123 |
39545dc527a1
jp: an onProgressUpdate method is now called on each progress update, allowing to handle metadata
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
1124 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
|
1125 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
|
1126 |
3040 | 1127 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
|
1128 |
39545dc527a1
jp: an onProgressUpdate method is now called on each progress update, allowing to handle metadata
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
1129 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
|
1130 |
3040 | 1131 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
|
1132 """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
|
1133 |
39545dc527a1
jp: an onProgressUpdate method is now called on each progress update, allowing to handle metadata
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
1134 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
|
1135 @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
|
1136 """ |
3028 | 1137 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
|
1138 |
3040 | 1139 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
|
1140 """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
|
1141 |
39545dc527a1
jp: an onProgressUpdate method is now called on each progress update, allowing to handle metadata
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
1142 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
|
1143 @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
|
1144 """ |
39545dc527a1
jp: an onProgressUpdate method is now called on each progress update, allowing to handle metadata
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
1145 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
|
1146 |
3040 | 1147 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
|
1148 """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
|
1149 |
39545dc527a1
jp: an onProgressUpdate method is now called on each progress update, allowing to handle metadata
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
1150 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
|
1151 @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
|
1152 """ |
3028 | 1153 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
|
1154 |
3040 | 1155 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
|
1156 """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
|
1157 |
39545dc527a1
jp: an onProgressUpdate method is now called on each progress update, allowing to handle metadata
Goffi <goffi@goffi.org>
parents:
1766
diff
changeset
|
1158 @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
|
1159 """ |
3040 | 1160 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
|
1161 |
3407
2f0be2b7de68
jp: replace `no_lf` argument by `end` in `disp` (same as in `print`)
Goffi <goffi@goffi.org>
parents:
3378
diff
changeset
|
1162 def disp(self, msg, verbosity=0, error=False, end='\n'): |
2f0be2b7de68
jp: replace `no_lf` argument by `end` in `disp` (same as in `print`)
Goffi <goffi@goffi.org>
parents:
3378
diff
changeset
|
1163 return self.host.disp(msg, verbosity, error, end) |
1605
0aded9648c5c
jp (base): added a self.disp method which manage verbosity and stderr + verbosity property
Goffi <goffi@goffi.org>
parents:
1604
diff
changeset
|
1164 |
1950 | 1165 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
|
1166 try: |
33b82250eadd
jp (base): raise InternalError if output is used when use_output is not declared
Goffi <goffi@goffi.org>
parents:
2189
diff
changeset
|
1167 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
|
1168 except AttributeError: |
3040 | 1169 raise exceptions.InternalError( |
1170 _('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
|
1171 return self.host.output(output_type, self.args.output, self.extra_outputs, data) |
1950 | 1172 |
2761
4b693ea24d5f
jp (base, pubsub, ticket): handle order-by:
Goffi <goffi@goffi.org>
parents:
2706
diff
changeset
|
1173 def getPubsubExtra(self, extra=None): |
4b693ea24d5f
jp (base, pubsub, ticket): handle order-by:
Goffi <goffi@goffi.org>
parents:
2706
diff
changeset
|
1174 """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
|
1175 |
4b693ea24d5f
jp (base, pubsub, ticket): handle order-by:
Goffi <goffi@goffi.org>
parents:
2706
diff
changeset
|
1176 @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
|
1177 @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
|
1178 """ |
4b693ea24d5f
jp (base, pubsub, ticket): handle order-by:
Goffi <goffi@goffi.org>
parents:
2706
diff
changeset
|
1179 if extra is None: |
4b693ea24d5f
jp (base, pubsub, ticket): handle order-by:
Goffi <goffi@goffi.org>
parents:
2706
diff
changeset
|
1180 extra = {} |
4b693ea24d5f
jp (base, pubsub, ticket): handle order-by:
Goffi <goffi@goffi.org>
parents:
2706
diff
changeset
|
1181 else: |
3028 | 1182 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
|
1183 if intersection: |
92af49cde255
jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents:
2761
diff
changeset
|
1184 raise exceptions.ConflictError( |
3028 | 1185 "given extra dict has conflicting keys with pubsub keys " |
1186 "{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
|
1187 |
92af49cde255
jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents:
2761
diff
changeset
|
1188 # RSM |
92af49cde255
jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents:
2761
diff
changeset
|
1189 |
3028 | 1190 for attribute in ('max', 'after', 'before', 'index'): |
1191 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
|
1192 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
|
1193 raise exceptions.ConflictError( |
3028 | 1194 "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
|
1195 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
|
1196 if value is not None: |
3028 | 1197 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
|
1198 |
92af49cde255
jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents:
2761
diff
changeset
|
1199 # MAM |
92af49cde255
jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents:
2761
diff
changeset
|
1200 |
3028 | 1201 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
|
1202 for key, value in self.args.mam_filters: |
3028 | 1203 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
|
1204 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
|
1205 raise exceptions.ConflictError( |
3028 | 1206 "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
|
1207 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
|
1208 |
92af49cde255
jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents:
2761
diff
changeset
|
1209 # Order-By |
92af49cde255
jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents:
2761
diff
changeset
|
1210 |
2761
4b693ea24d5f
jp (base, pubsub, ticket): handle order-by:
Goffi <goffi@goffi.org>
parents:
2706
diff
changeset
|
1211 try: |
4b693ea24d5f
jp (base, pubsub, ticket): handle order-by:
Goffi <goffi@goffi.org>
parents:
2706
diff
changeset
|
1212 order_by = self.args.order_by |
4b693ea24d5f
jp (base, pubsub, ticket): handle order-by:
Goffi <goffi@goffi.org>
parents:
2706
diff
changeset
|
1213 except AttributeError: |
4b693ea24d5f
jp (base, pubsub, ticket): handle order-by:
Goffi <goffi@goffi.org>
parents:
2706
diff
changeset
|
1214 pass |
4b693ea24d5f
jp (base, pubsub, ticket): handle order-by:
Goffi <goffi@goffi.org>
parents:
2706
diff
changeset
|
1215 else: |
4b693ea24d5f
jp (base, pubsub, ticket): handle order-by:
Goffi <goffi@goffi.org>
parents:
2706
diff
changeset
|
1216 if order_by is not None: |
4b693ea24d5f
jp (base, pubsub, ticket): handle order-by:
Goffi <goffi@goffi.org>
parents:
2706
diff
changeset
|
1217 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
|
1218 return extra |
4b693ea24d5f
jp (base, pubsub, ticket): handle order-by:
Goffi <goffi@goffi.org>
parents:
2706
diff
changeset
|
1219 |
817 | 1220 def add_parser_options(self): |
1221 try: | |
1222 subcommands = self.subcommands | |
1223 except AttributeError: | |
1224 # We don't have subcommands, the class need to implements add_parser_options | |
1225 raise NotImplementedError | |
1226 | |
1227 # now we add subcommands to ourself | |
1228 for cls in subcommands: | |
1229 cls(self) | |
814 | 1230 |
3040 | 1231 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
|
1232 """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
|
1233 |
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
|
1234 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
|
1235 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
|
1236 """ |
2155 | 1237 # we keep a reference to run command, it may be useful e.g. for outputs |
1238 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
|
1239 |
817 | 1240 try: |
1606
de785fcf9a7b
jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents:
1605
diff
changeset
|
1241 show_progress = self.args.progress |
817 | 1242 except AttributeError: |
1243 # the command doesn't use progress bar | |
1244 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
|
1245 else: |
1606
de785fcf9a7b
jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents:
1605
diff
changeset
|
1246 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
|
1247 self.host.watch_progress = True |
3040 | 1248 # we need to register the following signal even if we don't display the |
1249 # progress bar | |
1250 self.host.bridge.register_signal( | |
1251 "progressStarted", self.progressStartedHandler) | |
1252 self.host.bridge.register_signal( | |
1253 "progressFinished", self.progressFinishedHandler) | |
1254 self.host.bridge.register_signal( | |
1255 "progressError", self.progressErrorHandler) | |
817 | 1256 |
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
|
1257 if self.need_connect is not None: |
3040 | 1258 await self.host.connect_profile() |
1259 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
|
1260 |
3040 | 1261 async def start(self): |
1262 """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
|
1263 |
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
|
1264 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
|
1265 """ |
3040 | 1266 raise NotImplementedError |
817 | 1267 |
1268 | |
1269 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
|
1270 """Specialised commands which answer to specific actions |
817 | 1271 |
1606
de785fcf9a7b
jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents:
1605
diff
changeset
|
1272 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
|
1273 """ |
2409
d2ff5ff3de77
jp (ad-hoc): new "list" and "run" commands:
Goffi <goffi@goffi.org>
parents:
2368
diff
changeset
|
1274 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
|
1275 # 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
|
1276 # 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
|
1277 # 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
|
1278 |
2486
031aa3cc67ac
jp (base): fixed assertion error when using CommandAnswering
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1279 def __init__(self, *args, **kwargs): |
031aa3cc67ac
jp (base): fixed assertion error when using CommandAnswering
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1280 super(CommandAnswering, self).__init__(*args, **kwargs) |
031aa3cc67ac
jp (base): fixed assertion error when using CommandAnswering
Goffi <goffi@goffi.org>
parents:
2483
diff
changeset
|
1281 |
3040 | 1282 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
|
1283 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
|
1284 return |
1606
de785fcf9a7b
jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents:
1605
diff
changeset
|
1285 try: |
de785fcf9a7b
jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents:
1605
diff
changeset
|
1286 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
|
1287 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
|
1288 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
|
1289 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
|
1290 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
|
1291 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
|
1292 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
|
1293 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
|
1294 else: |
de785fcf9a7b
jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents:
1605
diff
changeset
|
1295 try: |
de785fcf9a7b
jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents:
1605
diff
changeset
|
1296 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
|
1297 except KeyError: |
de785fcf9a7b
jp (base, file): file command and progress fixes and adaptation to new API:
Goffi <goffi@goffi.org>
parents:
1605
diff
changeset
|
1298 pass |
0 | 1299 else: |
3040 | 1300 await callback(action_data, action_id, security_limit, profile) |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
1301 |
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
|
1302 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
|
1303 """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
|
1304 |
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
|
1305 @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
|
1306 """ |
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
|
1307 # 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
|
1308 # 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
|
1309 # 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
|
1310 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
|
1311 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
|
1312 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
|
1313 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
|
1314 |
3040 | 1315 async def start_answering(self): |
1316 """Auto reply to confirmation requests""" | |
2368
3865a772c360
jp (base): fixed CommandAnswering and progress bar
Goffi <goffi@goffi.org>
parents:
2313
diff
changeset
|
1317 self.host.bridge.register_signal("actionNew", self.onActionNew) |
3040 | 1318 actions = await self.host.bridge.actionsGet(self.profile) |
1623 | 1319 for action_data, action_id, security_limit in actions: |
3040 | 1320 await self.onActionNew(action_data, action_id, security_limit, self.profile) |