annotate sat_frontends/jp/base.py @ 3046:d9f328374473

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