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