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