Mercurial > libervia-backend
annotate sat_frontends/jp/cmd_pubsub.py @ 3040:fee60f17ebac
jp: jp asyncio port:
/!\ this commit is huge. Jp is temporarily not working with `dbus` bridge /!\
This patch implements the port of jp to asyncio, so it is now correctly using the bridge
asynchronously, and it can be used with bridges like `pb`. This also simplify the code,
notably for things which were previously implemented with many callbacks (like pagination
with RSM).
During the process, some behaviours have been modified/fixed, in jp and backends, check
diff for details.
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 25 Sep 2019 08:56:41 +0200 |
parents | ab2696e34d29 |
children | cea52c9ddfd9 |
rev | line source |
---|---|
2191
a1a8233f89e8
jp(pubsub/get): pubsub/get command, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1 #!/usr/bin/env python2 |
a1a8233f89e8
jp(pubsub/get): pubsub/get command, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
2 # -*- coding: utf-8 -*- |
a1a8233f89e8
jp(pubsub/get): pubsub/get command, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
3 |
a1a8233f89e8
jp(pubsub/get): pubsub/get command, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
4 # jp: a SàT command line tool |
2771 | 5 # Copyright (C) 2009-2019 Jérôme Poisson (goffi@goffi.org) |
2191
a1a8233f89e8
jp(pubsub/get): pubsub/get command, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
6 |
a1a8233f89e8
jp(pubsub/get): pubsub/get command, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
7 # This program is free software: you can redistribute it and/or modify |
a1a8233f89e8
jp(pubsub/get): pubsub/get command, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
8 # it under the terms of the GNU Affero General Public License as published by |
a1a8233f89e8
jp(pubsub/get): pubsub/get command, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
9 # the Free Software Foundation, either version 3 of the License, or |
a1a8233f89e8
jp(pubsub/get): pubsub/get command, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
10 # (at your option) any later version. |
a1a8233f89e8
jp(pubsub/get): pubsub/get command, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
11 |
a1a8233f89e8
jp(pubsub/get): pubsub/get command, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
12 # This program is distributed in the hope that it will be useful, |
a1a8233f89e8
jp(pubsub/get): pubsub/get command, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
a1a8233f89e8
jp(pubsub/get): pubsub/get command, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
a1a8233f89e8
jp(pubsub/get): pubsub/get command, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
15 # GNU Affero General Public License for more details. |
a1a8233f89e8
jp(pubsub/get): pubsub/get command, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
16 |
a1a8233f89e8
jp(pubsub/get): pubsub/get command, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
17 # You should have received a copy of the GNU Affero General Public License |
a1a8233f89e8
jp(pubsub/get): pubsub/get command, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
a1a8233f89e8
jp(pubsub/get): pubsub/get command, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
19 |
a1a8233f89e8
jp(pubsub/get): pubsub/get command, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
20 |
3040 | 21 import argparse |
22 import os.path | |
23 import re | |
24 import sys | |
25 import subprocess | |
26 import asyncio | |
3028 | 27 from . import base |
2191
a1a8233f89e8
jp(pubsub/get): pubsub/get command, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
28 from sat.core.i18n import _ |
2316 | 29 from sat.core import exceptions |
2191
a1a8233f89e8
jp(pubsub/get): pubsub/get command, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
30 from sat_frontends.jp.constants import Const as C |
2275
64e99bf0dfa2
jp (pubsub/edit): new edit subcommand, which work in a same way as for blog
Goffi <goffi@goffi.org>
parents:
2274
diff
changeset
|
31 from sat_frontends.jp import common |
2316 | 32 from sat_frontends.jp import arg_tools |
2777
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
33 from sat_frontends.jp import xml_tools |
2221
a6c9bc4d1de0
jp (pubsub/node): added create and delete commands
Goffi <goffi@goffi.org>
parents:
2214
diff
changeset
|
34 from functools import partial |
2224
87fcd4a7c7e4
jp (pubsub): added uri command to build pubsub URI
Goffi <goffi@goffi.org>
parents:
2221
diff
changeset
|
35 from sat.tools.common import uri |
2316 | 36 from sat.tools.common.ansi import ANSI as A |
37 from sat_frontends.tools import jid, strings | |
2191
a1a8233f89e8
jp(pubsub/get): pubsub/get command, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
38 |
a1a8233f89e8
jp(pubsub/get): pubsub/get command, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
39 __commands__ = ["Pubsub"] |
a1a8233f89e8
jp(pubsub/get): pubsub/get command, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
40 |
3028 | 41 PUBSUB_TMP_DIR = "pubsub" |
2351
3c0a3fae1862
jp (pubsub/node): added schema (set/edit/get) commands to manipulate PubSub node schema
Goffi <goffi@goffi.org>
parents:
2339
diff
changeset
|
42 PUBSUB_SCHEMA_TMP_DIR = PUBSUB_TMP_DIR + "_schema" |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
43 ALLOWED_SUBSCRIPTIONS_OWNER = ("subscribed", "pending", "none") |
2275
64e99bf0dfa2
jp (pubsub/edit): new edit subcommand, which work in a same way as for blog
Goffi <goffi@goffi.org>
parents:
2274
diff
changeset
|
44 |
2308
0b21d87c91cf
jp (pubsub/hook): added create/delete/list hook command to handle new Pubsub hook feature
Goffi <goffi@goffi.org>
parents:
2292
diff
changeset
|
45 # TODO: need to split this class in several modules, plugin should handle subcommands |
0b21d87c91cf
jp (pubsub/hook): added create/delete/list hook command to handle new Pubsub hook feature
Goffi <goffi@goffi.org>
parents:
2292
diff
changeset
|
46 |
2191
a1a8233f89e8
jp(pubsub/get): pubsub/get command, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
47 |
2235
4db836386641
jp: added use_pubsub and use_pubsub_node_req arguments to CommandBase
Goffi <goffi@goffi.org>
parents:
2224
diff
changeset
|
48 class NodeInfo(base.CommandBase): |
2197
e0e06391ce91
jp (pubsub): added pubsub/node/info command to retrieve node configuration
Goffi <goffi@goffi.org>
parents:
2195
diff
changeset
|
49 def __init__(self, host): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
50 base.CommandBase.__init__( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
51 self, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
52 host, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
53 "info", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
54 use_output=C.OUTPUT_DICT, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
55 use_pubsub=True, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
56 pubsub_flags={C.NODE}, |
3028 | 57 help=_("retrieve node configuration"), |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
58 ) |
2197
e0e06391ce91
jp (pubsub): added pubsub/node/info command to retrieve node configuration
Goffi <goffi@goffi.org>
parents:
2195
diff
changeset
|
59 |
e0e06391ce91
jp (pubsub): added pubsub/node/info command to retrieve node configuration
Goffi <goffi@goffi.org>
parents:
2195
diff
changeset
|
60 def add_parser_options(self): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
61 self.parser.add_argument( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
62 "-k", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
63 "--key", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
64 action="append", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
65 dest="keys", |
3028 | 66 help=_("data key to filter"), |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
67 ) |
2197
e0e06391ce91
jp (pubsub): added pubsub/node/info command to retrieve node configuration
Goffi <goffi@goffi.org>
parents:
2195
diff
changeset
|
68 |
e0e06391ce91
jp (pubsub): added pubsub/node/info command to retrieve node configuration
Goffi <goffi@goffi.org>
parents:
2195
diff
changeset
|
69 def removePrefix(self, key): |
3028 | 70 return key[7:] if key.startswith("pubsub#") else key |
2197
e0e06391ce91
jp (pubsub): added pubsub/node/info command to retrieve node configuration
Goffi <goffi@goffi.org>
parents:
2195
diff
changeset
|
71 |
e0e06391ce91
jp (pubsub): added pubsub/node/info command to retrieve node configuration
Goffi <goffi@goffi.org>
parents:
2195
diff
changeset
|
72 def filterKey(self, key): |
3028 | 73 return any((key == k or key == "pubsub#" + k) for k in self.args.keys) |
2197
e0e06391ce91
jp (pubsub): added pubsub/node/info command to retrieve node configuration
Goffi <goffi@goffi.org>
parents:
2195
diff
changeset
|
74 |
3040 | 75 async def start(self): |
76 try: | |
77 config_dict = await self.host.bridge.psNodeConfigurationGet( | |
78 self.args.service, | |
79 self.args.node, | |
80 self.profile, | |
81 ) | |
82 except Exception as e: | |
83 self.disp(f"can't get node configuration: {e}", error=True) | |
84 self.host.quit(C.EXIT_BRIDGE_ERRBACK) | |
85 else: | |
86 key_filter = (lambda k: True) if not self.args.keys else self.filterKey | |
87 config_dict = { | |
88 self.removePrefix(k): v for k, v in config_dict.items() if key_filter(k) | |
89 } | |
90 await self.output(config_dict) | |
91 self.host.quit() | |
2197
e0e06391ce91
jp (pubsub): added pubsub/node/info command to retrieve node configuration
Goffi <goffi@goffi.org>
parents:
2195
diff
changeset
|
92 |
2221
a6c9bc4d1de0
jp (pubsub/node): added create and delete commands
Goffi <goffi@goffi.org>
parents:
2214
diff
changeset
|
93 |
2235
4db836386641
jp: added use_pubsub and use_pubsub_node_req arguments to CommandBase
Goffi <goffi@goffi.org>
parents:
2224
diff
changeset
|
94 class NodeCreate(base.CommandBase): |
2221
a6c9bc4d1de0
jp (pubsub/node): added create and delete commands
Goffi <goffi@goffi.org>
parents:
2214
diff
changeset
|
95 def __init__(self, host): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
96 base.CommandBase.__init__( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
97 self, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
98 host, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
99 "create", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
100 use_output=C.OUTPUT_DICT, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
101 use_pubsub=True, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
102 pubsub_flags={C.NODE}, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
103 use_verbose=True, |
3028 | 104 help=_("create a node"), |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
105 ) |
2221
a6c9bc4d1de0
jp (pubsub/node): added create and delete commands
Goffi <goffi@goffi.org>
parents:
2214
diff
changeset
|
106 |
a6c9bc4d1de0
jp (pubsub/node): added create and delete commands
Goffi <goffi@goffi.org>
parents:
2214
diff
changeset
|
107 def add_parser_options(self): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
108 self.parser.add_argument( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
109 "-f", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
110 "--field", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
111 action="append", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
112 nargs=2, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
113 dest="fields", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
114 default=[], |
3028 | 115 metavar=("KEY", "VALUE"), |
116 help=_("configuration field to set"), | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
117 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
118 self.parser.add_argument( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
119 "-F", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
120 "--full-prefix", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
121 action="store_true", |
3028 | 122 help=_('don\'t prepend "pubsub#" prefix to field names'), |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
123 ) |
2221
a6c9bc4d1de0
jp (pubsub/node): added create and delete commands
Goffi <goffi@goffi.org>
parents:
2214
diff
changeset
|
124 |
3040 | 125 async def start(self): |
2221
a6c9bc4d1de0
jp (pubsub/node): added create and delete commands
Goffi <goffi@goffi.org>
parents:
2214
diff
changeset
|
126 if not self.args.full_prefix: |
3028 | 127 options = {"pubsub#" + k: v for k, v in self.args.fields} |
2221
a6c9bc4d1de0
jp (pubsub/node): added create and delete commands
Goffi <goffi@goffi.org>
parents:
2214
diff
changeset
|
128 else: |
a6c9bc4d1de0
jp (pubsub/node): added create and delete commands
Goffi <goffi@goffi.org>
parents:
2214
diff
changeset
|
129 options = dict(self.args.fields) |
3040 | 130 try: |
131 node_id = await self.host.bridge.psNodeCreate( | |
132 self.args.service, | |
133 self.args.node, | |
134 options, | |
135 self.profile, | |
136 ) | |
137 except Exception as e: | |
138 self.disp(msg=_(f"can't create node: {e}"), error=True) | |
139 self.host.quit(C.EXIT_BRIDGE_ERRBACK) | |
140 else: | |
141 if self.host.verbosity: | |
142 announce = _("node created successfully: ") | |
143 else: | |
144 announce = "" | |
145 self.disp(announce + node_id) | |
146 self.host.quit() | |
2221
a6c9bc4d1de0
jp (pubsub/node): added create and delete commands
Goffi <goffi@goffi.org>
parents:
2214
diff
changeset
|
147 |
a6c9bc4d1de0
jp (pubsub/node): added create and delete commands
Goffi <goffi@goffi.org>
parents:
2214
diff
changeset
|
148 |
2802
f61a50790fae
jp (pubsub/node): added purge command
Goffi <goffi@goffi.org>
parents:
2777
diff
changeset
|
149 class NodePurge(base.CommandBase): |
f61a50790fae
jp (pubsub/node): added purge command
Goffi <goffi@goffi.org>
parents:
2777
diff
changeset
|
150 |
f61a50790fae
jp (pubsub/node): added purge command
Goffi <goffi@goffi.org>
parents:
2777
diff
changeset
|
151 def __init__(self, host): |
f61a50790fae
jp (pubsub/node): added purge command
Goffi <goffi@goffi.org>
parents:
2777
diff
changeset
|
152 super(NodePurge, self).__init__( |
f61a50790fae
jp (pubsub/node): added purge command
Goffi <goffi@goffi.org>
parents:
2777
diff
changeset
|
153 host, |
f61a50790fae
jp (pubsub/node): added purge command
Goffi <goffi@goffi.org>
parents:
2777
diff
changeset
|
154 "purge", |
f61a50790fae
jp (pubsub/node): added purge command
Goffi <goffi@goffi.org>
parents:
2777
diff
changeset
|
155 use_pubsub=True, |
f61a50790fae
jp (pubsub/node): added purge command
Goffi <goffi@goffi.org>
parents:
2777
diff
changeset
|
156 pubsub_flags={C.NODE}, |
3028 | 157 help=_("purge a node (i.e. remove all items from it)"), |
2802
f61a50790fae
jp (pubsub/node): added purge command
Goffi <goffi@goffi.org>
parents:
2777
diff
changeset
|
158 ) |
f61a50790fae
jp (pubsub/node): added purge command
Goffi <goffi@goffi.org>
parents:
2777
diff
changeset
|
159 |
f61a50790fae
jp (pubsub/node): added purge command
Goffi <goffi@goffi.org>
parents:
2777
diff
changeset
|
160 def add_parser_options(self): |
f61a50790fae
jp (pubsub/node): added purge command
Goffi <goffi@goffi.org>
parents:
2777
diff
changeset
|
161 self.parser.add_argument( |
f61a50790fae
jp (pubsub/node): added purge command
Goffi <goffi@goffi.org>
parents:
2777
diff
changeset
|
162 "-f", |
f61a50790fae
jp (pubsub/node): added purge command
Goffi <goffi@goffi.org>
parents:
2777
diff
changeset
|
163 "--force", |
f61a50790fae
jp (pubsub/node): added purge command
Goffi <goffi@goffi.org>
parents:
2777
diff
changeset
|
164 action="store_true", |
3028 | 165 help=_("purge node without confirmation"), |
2802
f61a50790fae
jp (pubsub/node): added purge command
Goffi <goffi@goffi.org>
parents:
2777
diff
changeset
|
166 ) |
f61a50790fae
jp (pubsub/node): added purge command
Goffi <goffi@goffi.org>
parents:
2777
diff
changeset
|
167 |
3040 | 168 async def start(self): |
2802
f61a50790fae
jp (pubsub/node): added purge command
Goffi <goffi@goffi.org>
parents:
2777
diff
changeset
|
169 if not self.args.force: |
f61a50790fae
jp (pubsub/node): added purge command
Goffi <goffi@goffi.org>
parents:
2777
diff
changeset
|
170 if not self.args.service: |
3040 | 171 message = _( |
172 f"Are you sure to purge PEP node [{self.args.node}]? This will " | |
173 f"delete ALL items from it!") | |
2802
f61a50790fae
jp (pubsub/node): added purge command
Goffi <goffi@goffi.org>
parents:
2777
diff
changeset
|
174 else: |
f61a50790fae
jp (pubsub/node): added purge command
Goffi <goffi@goffi.org>
parents:
2777
diff
changeset
|
175 message = _( |
3040 | 176 f"Are you sure to delete node [{self.args.node}] on service " |
177 f"[{self.args.service}]? This will delete ALL items from it!") | |
178 await self.host.confirmOrQuit(message, _("node purge cancelled")) | |
2802
f61a50790fae
jp (pubsub/node): added purge command
Goffi <goffi@goffi.org>
parents:
2777
diff
changeset
|
179 |
3040 | 180 try: |
181 await self.host.bridge.psNodePurge( | |
182 self.args.service, | |
183 self.args.node, | |
184 self.profile, | |
185 ) | |
186 except Exception as e: | |
187 self.disp(msg=_(f"can't purge node: {e}"), error=True) | |
188 self.host.quit(C.EXIT_BRIDGE_ERRBACK) | |
189 else: | |
190 self.disp(_(f"node [{self.args.node}] purged successfully")) | |
191 self.host.quit() | |
2802
f61a50790fae
jp (pubsub/node): added purge command
Goffi <goffi@goffi.org>
parents:
2777
diff
changeset
|
192 |
f61a50790fae
jp (pubsub/node): added purge command
Goffi <goffi@goffi.org>
parents:
2777
diff
changeset
|
193 |
2235
4db836386641
jp: added use_pubsub and use_pubsub_node_req arguments to CommandBase
Goffi <goffi@goffi.org>
parents:
2224
diff
changeset
|
194 class NodeDelete(base.CommandBase): |
2221
a6c9bc4d1de0
jp (pubsub/node): added create and delete commands
Goffi <goffi@goffi.org>
parents:
2214
diff
changeset
|
195 def __init__(self, host): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
196 base.CommandBase.__init__( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
197 self, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
198 host, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
199 "delete", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
200 use_pubsub=True, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
201 pubsub_flags={C.NODE}, |
3028 | 202 help=_("delete a node"), |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
203 ) |
2221
a6c9bc4d1de0
jp (pubsub/node): added create and delete commands
Goffi <goffi@goffi.org>
parents:
2214
diff
changeset
|
204 |
a6c9bc4d1de0
jp (pubsub/node): added create and delete commands
Goffi <goffi@goffi.org>
parents:
2214
diff
changeset
|
205 def add_parser_options(self): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
206 self.parser.add_argument( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
207 "-f", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
208 "--force", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
209 action="store_true", |
3028 | 210 help=_("delete node without confirmation"), |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
211 ) |
2221
a6c9bc4d1de0
jp (pubsub/node): added create and delete commands
Goffi <goffi@goffi.org>
parents:
2214
diff
changeset
|
212 |
3040 | 213 async def start(self): |
2221
a6c9bc4d1de0
jp (pubsub/node): added create and delete commands
Goffi <goffi@goffi.org>
parents:
2214
diff
changeset
|
214 if not self.args.force: |
a6c9bc4d1de0
jp (pubsub/node): added create and delete commands
Goffi <goffi@goffi.org>
parents:
2214
diff
changeset
|
215 if not self.args.service: |
3040 | 216 message = _(f"Are you sure to delete PEP node [{self.args.node}] ?") |
2221
a6c9bc4d1de0
jp (pubsub/node): added create and delete commands
Goffi <goffi@goffi.org>
parents:
2214
diff
changeset
|
217 else: |
3040 | 218 message = _(f"Are you sure to delete node [{self.args.node}] on " |
219 f"service [{self.args.service}]?") | |
220 await self.host.confirmOrQuit(message, _("node deletion cancelled")) | |
2221
a6c9bc4d1de0
jp (pubsub/node): added create and delete commands
Goffi <goffi@goffi.org>
parents:
2214
diff
changeset
|
221 |
3040 | 222 try: |
223 await self.host.bridge.psNodeDelete( | |
224 self.args.service, | |
225 self.args.node, | |
226 self.profile, | |
227 ) | |
228 except Exception as e: | |
229 self.disp(f"can't delete node: {e}", error=True) | |
230 self.host.quit(C.EXIT_BRIDGE_ERRBACK) | |
231 else: | |
232 self.disp(_(f"node [{self.args.node}] deleted successfully")) | |
233 self.host.quit() | |
2221
a6c9bc4d1de0
jp (pubsub/node): added create and delete commands
Goffi <goffi@goffi.org>
parents:
2214
diff
changeset
|
234 |
a6c9bc4d1de0
jp (pubsub/node): added create and delete commands
Goffi <goffi@goffi.org>
parents:
2214
diff
changeset
|
235 |
2235
4db836386641
jp: added use_pubsub and use_pubsub_node_req arguments to CommandBase
Goffi <goffi@goffi.org>
parents:
2224
diff
changeset
|
236 class NodeSet(base.CommandBase): |
2199
ea0d0a4e2ad8
jp (pubsub/node): added set command
Goffi <goffi@goffi.org>
parents:
2197
diff
changeset
|
237 def __init__(self, host): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
238 base.CommandBase.__init__( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
239 self, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
240 host, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
241 "set", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
242 use_output=C.OUTPUT_DICT, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
243 use_pubsub=True, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
244 pubsub_flags={C.NODE}, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
245 use_verbose=True, |
3028 | 246 help=_("set node configuration"), |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
247 ) |
2199
ea0d0a4e2ad8
jp (pubsub/node): added set command
Goffi <goffi@goffi.org>
parents:
2197
diff
changeset
|
248 |
ea0d0a4e2ad8
jp (pubsub/node): added set command
Goffi <goffi@goffi.org>
parents:
2197
diff
changeset
|
249 def add_parser_options(self): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
250 self.parser.add_argument( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
251 "-f", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
252 "--field", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
253 action="append", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
254 nargs=2, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
255 dest="fields", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
256 required=True, |
3028 | 257 metavar=("KEY", "VALUE"), |
258 help=_("configuration field to set (required)"), | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
259 ) |
3040 | 260 self.parser.add_argument( |
261 "-F", | |
262 "--full-prefix", | |
263 action="store_true", | |
264 help=_('don\'t prepend "pubsub#" prefix to field names'), | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
265 ) |
2199
ea0d0a4e2ad8
jp (pubsub/node): added set command
Goffi <goffi@goffi.org>
parents:
2197
diff
changeset
|
266 |
ea0d0a4e2ad8
jp (pubsub/node): added set command
Goffi <goffi@goffi.org>
parents:
2197
diff
changeset
|
267 def getKeyName(self, k): |
3040 | 268 if self.args.full_prefix or k.startswith("pubsub#"): |
269 return k | |
270 else: | |
3028 | 271 return "pubsub#" + k |
2199
ea0d0a4e2ad8
jp (pubsub/node): added set command
Goffi <goffi@goffi.org>
parents:
2197
diff
changeset
|
272 |
3040 | 273 async def start(self): |
274 try: | |
275 await self.host.bridge.psNodeConfigurationSet( | |
276 self.args.service, | |
277 self.args.node, | |
278 {self.getKeyName(k): v for k, v in self.args.fields}, | |
279 self.profile, | |
280 ) | |
281 except Exception as e: | |
282 self.disp(f"can't set node configuration: {e}", error=True) | |
283 self.host.quit(C.EXIT_BRIDGE_ERRBACK) | |
284 else: | |
285 self.disp(_("node configuration successful"), 1) | |
286 self.host.quit() | |
2199
ea0d0a4e2ad8
jp (pubsub/node): added set command
Goffi <goffi@goffi.org>
parents:
2197
diff
changeset
|
287 |
2197
e0e06391ce91
jp (pubsub): added pubsub/node/info command to retrieve node configuration
Goffi <goffi@goffi.org>
parents:
2195
diff
changeset
|
288 |
2804
710de41da2f2
jp (pubsub/node): new "import" command, to publish many nodes from an XML file
Goffi <goffi@goffi.org>
parents:
2803
diff
changeset
|
289 class NodeImport(base.CommandBase): |
710de41da2f2
jp (pubsub/node): new "import" command, to publish many nodes from an XML file
Goffi <goffi@goffi.org>
parents:
2803
diff
changeset
|
290 |
710de41da2f2
jp (pubsub/node): new "import" command, to publish many nodes from an XML file
Goffi <goffi@goffi.org>
parents:
2803
diff
changeset
|
291 def __init__(self, host): |
710de41da2f2
jp (pubsub/node): new "import" command, to publish many nodes from an XML file
Goffi <goffi@goffi.org>
parents:
2803
diff
changeset
|
292 super(NodeImport, self).__init__( |
710de41da2f2
jp (pubsub/node): new "import" command, to publish many nodes from an XML file
Goffi <goffi@goffi.org>
parents:
2803
diff
changeset
|
293 host, |
710de41da2f2
jp (pubsub/node): new "import" command, to publish many nodes from an XML file
Goffi <goffi@goffi.org>
parents:
2803
diff
changeset
|
294 "import", |
710de41da2f2
jp (pubsub/node): new "import" command, to publish many nodes from an XML file
Goffi <goffi@goffi.org>
parents:
2803
diff
changeset
|
295 use_pubsub=True, |
710de41da2f2
jp (pubsub/node): new "import" command, to publish many nodes from an XML file
Goffi <goffi@goffi.org>
parents:
2803
diff
changeset
|
296 pubsub_flags={C.NODE}, |
3028 | 297 help=_("import raw XML to a node"), |
2804
710de41da2f2
jp (pubsub/node): new "import" command, to publish many nodes from an XML file
Goffi <goffi@goffi.org>
parents:
2803
diff
changeset
|
298 ) |
710de41da2f2
jp (pubsub/node): new "import" command, to publish many nodes from an XML file
Goffi <goffi@goffi.org>
parents:
2803
diff
changeset
|
299 |
710de41da2f2
jp (pubsub/node): new "import" command, to publish many nodes from an XML file
Goffi <goffi@goffi.org>
parents:
2803
diff
changeset
|
300 def add_parser_options(self): |
710de41da2f2
jp (pubsub/node): new "import" command, to publish many nodes from an XML file
Goffi <goffi@goffi.org>
parents:
2803
diff
changeset
|
301 self.parser.add_argument( |
710de41da2f2
jp (pubsub/node): new "import" command, to publish many nodes from an XML file
Goffi <goffi@goffi.org>
parents:
2803
diff
changeset
|
302 "--admin", |
710de41da2f2
jp (pubsub/node): new "import" command, to publish many nodes from an XML file
Goffi <goffi@goffi.org>
parents:
2803
diff
changeset
|
303 action="store_true", |
3028 | 304 help=_("do a pubsub admin request, needed to change publisher"), |
2804
710de41da2f2
jp (pubsub/node): new "import" command, to publish many nodes from an XML file
Goffi <goffi@goffi.org>
parents:
2803
diff
changeset
|
305 ) |
710de41da2f2
jp (pubsub/node): new "import" command, to publish many nodes from an XML file
Goffi <goffi@goffi.org>
parents:
2803
diff
changeset
|
306 self.parser.add_argument( |
710de41da2f2
jp (pubsub/node): new "import" command, to publish many nodes from an XML file
Goffi <goffi@goffi.org>
parents:
2803
diff
changeset
|
307 "import_file", |
3028 | 308 type=argparse.FileType(), |
309 help=_("path to the XML file with data to import. The file must contain " | |
310 "whole XML of each item to import."), | |
2804
710de41da2f2
jp (pubsub/node): new "import" command, to publish many nodes from an XML file
Goffi <goffi@goffi.org>
parents:
2803
diff
changeset
|
311 ) |
710de41da2f2
jp (pubsub/node): new "import" command, to publish many nodes from an XML file
Goffi <goffi@goffi.org>
parents:
2803
diff
changeset
|
312 |
3040 | 313 async def start(self): |
2804
710de41da2f2
jp (pubsub/node): new "import" command, to publish many nodes from an XML file
Goffi <goffi@goffi.org>
parents:
2803
diff
changeset
|
314 try: |
710de41da2f2
jp (pubsub/node): new "import" command, to publish many nodes from an XML file
Goffi <goffi@goffi.org>
parents:
2803
diff
changeset
|
315 element, etree = xml_tools.etreeParse(self, self.args.import_file, |
710de41da2f2
jp (pubsub/node): new "import" command, to publish many nodes from an XML file
Goffi <goffi@goffi.org>
parents:
2803
diff
changeset
|
316 reraise=True) |
710de41da2f2
jp (pubsub/node): new "import" command, to publish many nodes from an XML file
Goffi <goffi@goffi.org>
parents:
2803
diff
changeset
|
317 except Exception as e: |
710de41da2f2
jp (pubsub/node): new "import" command, to publish many nodes from an XML file
Goffi <goffi@goffi.org>
parents:
2803
diff
changeset
|
318 from lxml.etree import XMLSyntaxError |
710de41da2f2
jp (pubsub/node): new "import" command, to publish many nodes from an XML file
Goffi <goffi@goffi.org>
parents:
2803
diff
changeset
|
319 if isinstance(e, XMLSyntaxError) and e.code == 5: |
710de41da2f2
jp (pubsub/node): new "import" command, to publish many nodes from an XML file
Goffi <goffi@goffi.org>
parents:
2803
diff
changeset
|
320 # we have extra content, this probaby means that item are not wrapped |
710de41da2f2
jp (pubsub/node): new "import" command, to publish many nodes from an XML file
Goffi <goffi@goffi.org>
parents:
2803
diff
changeset
|
321 # so we wrap them here and try again |
710de41da2f2
jp (pubsub/node): new "import" command, to publish many nodes from an XML file
Goffi <goffi@goffi.org>
parents:
2803
diff
changeset
|
322 self.args.import_file.seek(0) |
710de41da2f2
jp (pubsub/node): new "import" command, to publish many nodes from an XML file
Goffi <goffi@goffi.org>
parents:
2803
diff
changeset
|
323 xml_buf = "<import>" + self.args.import_file.read() + "</import>" |
710de41da2f2
jp (pubsub/node): new "import" command, to publish many nodes from an XML file
Goffi <goffi@goffi.org>
parents:
2803
diff
changeset
|
324 element, etree = xml_tools.etreeParse(self, xml_buf) |
710de41da2f2
jp (pubsub/node): new "import" command, to publish many nodes from an XML file
Goffi <goffi@goffi.org>
parents:
2803
diff
changeset
|
325 |
710de41da2f2
jp (pubsub/node): new "import" command, to publish many nodes from an XML file
Goffi <goffi@goffi.org>
parents:
2803
diff
changeset
|
326 # we reverse element as we expect to have most recently published element first |
710de41da2f2
jp (pubsub/node): new "import" command, to publish many nodes from an XML file
Goffi <goffi@goffi.org>
parents:
2803
diff
changeset
|
327 # TODO: make this more explicit and add an option |
710de41da2f2
jp (pubsub/node): new "import" command, to publish many nodes from an XML file
Goffi <goffi@goffi.org>
parents:
2803
diff
changeset
|
328 element[:] = reversed(element) |
710de41da2f2
jp (pubsub/node): new "import" command, to publish many nodes from an XML file
Goffi <goffi@goffi.org>
parents:
2803
diff
changeset
|
329 |
710de41da2f2
jp (pubsub/node): new "import" command, to publish many nodes from an XML file
Goffi <goffi@goffi.org>
parents:
2803
diff
changeset
|
330 if not all([i.tag == '{http://jabber.org/protocol/pubsub}item' for i in element]): |
710de41da2f2
jp (pubsub/node): new "import" command, to publish many nodes from an XML file
Goffi <goffi@goffi.org>
parents:
2803
diff
changeset
|
331 self.disp( |
3028 | 332 _("You are not using list of pubsub items, we can't import this file"), |
2804
710de41da2f2
jp (pubsub/node): new "import" command, to publish many nodes from an XML file
Goffi <goffi@goffi.org>
parents:
2803
diff
changeset
|
333 error=True) |
710de41da2f2
jp (pubsub/node): new "import" command, to publish many nodes from an XML file
Goffi <goffi@goffi.org>
parents:
2803
diff
changeset
|
334 self.host.quit(C.EXIT_DATA_ERROR) |
3040 | 335 return |
2804
710de41da2f2
jp (pubsub/node): new "import" command, to publish many nodes from an XML file
Goffi <goffi@goffi.org>
parents:
2803
diff
changeset
|
336 |
3040 | 337 items = [etree.tostring(i, encoding="unicode") for i in element] |
2804
710de41da2f2
jp (pubsub/node): new "import" command, to publish many nodes from an XML file
Goffi <goffi@goffi.org>
parents:
2803
diff
changeset
|
338 if self.args.admin: |
3040 | 339 method = self.host.bridge.psAdminItemsSend |
340 else: | |
341 self.disp(_("Items are imported without using admin mode, publisher can't " | |
342 "be changed")) | |
343 method = self.host.bridge.psItemsSend | |
344 | |
345 try: | |
346 items_ids = await method( | |
2804
710de41da2f2
jp (pubsub/node): new "import" command, to publish many nodes from an XML file
Goffi <goffi@goffi.org>
parents:
2803
diff
changeset
|
347 self.args.service, |
710de41da2f2
jp (pubsub/node): new "import" command, to publish many nodes from an XML file
Goffi <goffi@goffi.org>
parents:
2803
diff
changeset
|
348 self.args.node, |
710de41da2f2
jp (pubsub/node): new "import" command, to publish many nodes from an XML file
Goffi <goffi@goffi.org>
parents:
2803
diff
changeset
|
349 items, |
3028 | 350 "", |
2804
710de41da2f2
jp (pubsub/node): new "import" command, to publish many nodes from an XML file
Goffi <goffi@goffi.org>
parents:
2803
diff
changeset
|
351 self.profile, |
710de41da2f2
jp (pubsub/node): new "import" command, to publish many nodes from an XML file
Goffi <goffi@goffi.org>
parents:
2803
diff
changeset
|
352 ) |
3040 | 353 except Exception as e: |
354 self.disp(f"can't send items: {e}", error=True) | |
355 self.host.quit(C.EXIT_BRIDGE_ERRBACK) | |
2804
710de41da2f2
jp (pubsub/node): new "import" command, to publish many nodes from an XML file
Goffi <goffi@goffi.org>
parents:
2803
diff
changeset
|
356 else: |
3040 | 357 if items_ids: |
358 self.disp(_('items published with id(s) {items_ids}').format( | |
359 items_ids=', '.join(items_ids))) | |
360 else: | |
361 self.disp(_('items published')) | |
362 self.host.quit() | |
2804
710de41da2f2
jp (pubsub/node): new "import" command, to publish many nodes from an XML file
Goffi <goffi@goffi.org>
parents:
2803
diff
changeset
|
363 |
710de41da2f2
jp (pubsub/node): new "import" command, to publish many nodes from an XML file
Goffi <goffi@goffi.org>
parents:
2803
diff
changeset
|
364 |
2235
4db836386641
jp: added use_pubsub and use_pubsub_node_req arguments to CommandBase
Goffi <goffi@goffi.org>
parents:
2224
diff
changeset
|
365 class NodeAffiliationsGet(base.CommandBase): |
2204
afc703419186
jp (pubsub): added affiliations and node/affiliations/get:
Goffi <goffi@goffi.org>
parents:
2199
diff
changeset
|
366 def __init__(self, host): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
367 base.CommandBase.__init__( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
368 self, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
369 host, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
370 "get", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
371 use_output=C.OUTPUT_DICT, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
372 use_pubsub=True, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
373 pubsub_flags={C.NODE}, |
3028 | 374 help=_("retrieve node affiliations (for node owner)"), |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
375 ) |
2204
afc703419186
jp (pubsub): added affiliations and node/affiliations/get:
Goffi <goffi@goffi.org>
parents:
2199
diff
changeset
|
376 |
afc703419186
jp (pubsub): added affiliations and node/affiliations/get:
Goffi <goffi@goffi.org>
parents:
2199
diff
changeset
|
377 def add_parser_options(self): |
2235
4db836386641
jp: added use_pubsub and use_pubsub_node_req arguments to CommandBase
Goffi <goffi@goffi.org>
parents:
2224
diff
changeset
|
378 pass |
2204
afc703419186
jp (pubsub): added affiliations and node/affiliations/get:
Goffi <goffi@goffi.org>
parents:
2199
diff
changeset
|
379 |
3040 | 380 async def start(self): |
381 try: | |
382 affiliations = await self.host.bridge.psNodeAffiliationsGet( | |
383 self.args.service, | |
384 self.args.node, | |
385 self.profile, | |
386 ) | |
387 except Exception as e: | |
388 self.disp(f"can't get node affiliations: {e}", error=True) | |
389 self.host.quit(C.EXIT_BRIDGE_ERRBACK) | |
390 else: | |
391 await self.output(affiliations) | |
392 self.host.quit() | |
2204
afc703419186
jp (pubsub): added affiliations and node/affiliations/get:
Goffi <goffi@goffi.org>
parents:
2199
diff
changeset
|
393 |
afc703419186
jp (pubsub): added affiliations and node/affiliations/get:
Goffi <goffi@goffi.org>
parents:
2199
diff
changeset
|
394 |
2235
4db836386641
jp: added use_pubsub and use_pubsub_node_req arguments to CommandBase
Goffi <goffi@goffi.org>
parents:
2224
diff
changeset
|
395 class NodeAffiliationsSet(base.CommandBase): |
2207
d662bdd682b2
jp (pubsub/node) added pubsub/node/affiliations/set command
Goffi <goffi@goffi.org>
parents:
2204
diff
changeset
|
396 def __init__(self, host): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
397 base.CommandBase.__init__( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
398 self, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
399 host, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
400 "set", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
401 use_pubsub=True, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
402 pubsub_flags={C.NODE}, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
403 use_verbose=True, |
3028 | 404 help=_("set affiliations (for node owner)"), |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
405 ) |
2207
d662bdd682b2
jp (pubsub/node) added pubsub/node/affiliations/set command
Goffi <goffi@goffi.org>
parents:
2204
diff
changeset
|
406 |
d662bdd682b2
jp (pubsub/node) added pubsub/node/affiliations/set command
Goffi <goffi@goffi.org>
parents:
2204
diff
changeset
|
407 def add_parser_options(self): |
d662bdd682b2
jp (pubsub/node) added pubsub/node/affiliations/set command
Goffi <goffi@goffi.org>
parents:
2204
diff
changeset
|
408 # XXX: we use optional argument syntax for a required one because list of list of 2 elements |
d662bdd682b2
jp (pubsub/node) added pubsub/node/affiliations/set command
Goffi <goffi@goffi.org>
parents:
2204
diff
changeset
|
409 # (uses to construct dicts) don't work with positional arguments |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
410 self.parser.add_argument( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
411 "-a", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
412 "--affiliation", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
413 dest="affiliations", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
414 metavar=("JID", "AFFILIATION"), |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
415 required=True, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
416 action="append", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
417 nargs=2, |
3028 | 418 help=_("entity/affiliation couple(s)"), |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
419 ) |
2207
d662bdd682b2
jp (pubsub/node) added pubsub/node/affiliations/set command
Goffi <goffi@goffi.org>
parents:
2204
diff
changeset
|
420 |
3040 | 421 async def start(self): |
2207
d662bdd682b2
jp (pubsub/node) added pubsub/node/affiliations/set command
Goffi <goffi@goffi.org>
parents:
2204
diff
changeset
|
422 affiliations = dict(self.args.affiliations) |
3040 | 423 try: |
424 await self.host.bridge.psNodeAffiliationsSet( | |
425 self.args.service, | |
426 self.args.node, | |
427 affiliations, | |
428 self.profile, | |
429 ) | |
430 except Exception as e: | |
431 self.disp(f"can't set node affiliations: {e}", error=True) | |
432 self.host.quit(C.EXIT_BRIDGE_ERRBACK) | |
433 else: | |
434 self.disp(_("affiliations have been set"), 1) | |
435 self.host.quit() | |
2207
d662bdd682b2
jp (pubsub/node) added pubsub/node/affiliations/set command
Goffi <goffi@goffi.org>
parents:
2204
diff
changeset
|
436 |
d662bdd682b2
jp (pubsub/node) added pubsub/node/affiliations/set command
Goffi <goffi@goffi.org>
parents:
2204
diff
changeset
|
437 |
2204
afc703419186
jp (pubsub): added affiliations and node/affiliations/get:
Goffi <goffi@goffi.org>
parents:
2199
diff
changeset
|
438 class NodeAffiliations(base.CommandBase): |
2207
d662bdd682b2
jp (pubsub/node) added pubsub/node/affiliations/set command
Goffi <goffi@goffi.org>
parents:
2204
diff
changeset
|
439 subcommands = (NodeAffiliationsGet, NodeAffiliationsSet) |
2204
afc703419186
jp (pubsub): added affiliations and node/affiliations/get:
Goffi <goffi@goffi.org>
parents:
2199
diff
changeset
|
440 |
afc703419186
jp (pubsub): added affiliations and node/affiliations/get:
Goffi <goffi@goffi.org>
parents:
2199
diff
changeset
|
441 def __init__(self, host): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
442 super(NodeAffiliations, self).__init__( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
443 host, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
444 "affiliations", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
445 use_profile=False, |
3028 | 446 help=_("set or retrieve node affiliations"), |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
447 ) |
2204
afc703419186
jp (pubsub): added affiliations and node/affiliations/get:
Goffi <goffi@goffi.org>
parents:
2199
diff
changeset
|
448 |
afc703419186
jp (pubsub): added affiliations and node/affiliations/get:
Goffi <goffi@goffi.org>
parents:
2199
diff
changeset
|
449 |
2339
d94e932be8b3
jp (pubsub/node): added subscriptions subcommand:
Goffi <goffi@goffi.org>
parents:
2320
diff
changeset
|
450 class NodeSubscriptionsGet(base.CommandBase): |
d94e932be8b3
jp (pubsub/node): added subscriptions subcommand:
Goffi <goffi@goffi.org>
parents:
2320
diff
changeset
|
451 def __init__(self, host): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
452 base.CommandBase.__init__( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
453 self, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
454 host, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
455 "get", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
456 use_output=C.OUTPUT_DICT, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
457 use_pubsub=True, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
458 pubsub_flags={C.NODE}, |
3028 | 459 help=_("retrieve node subscriptions (for node owner)"), |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
460 ) |
2339
d94e932be8b3
jp (pubsub/node): added subscriptions subcommand:
Goffi <goffi@goffi.org>
parents:
2320
diff
changeset
|
461 |
d94e932be8b3
jp (pubsub/node): added subscriptions subcommand:
Goffi <goffi@goffi.org>
parents:
2320
diff
changeset
|
462 def add_parser_options(self): |
d94e932be8b3
jp (pubsub/node): added subscriptions subcommand:
Goffi <goffi@goffi.org>
parents:
2320
diff
changeset
|
463 pass |
d94e932be8b3
jp (pubsub/node): added subscriptions subcommand:
Goffi <goffi@goffi.org>
parents:
2320
diff
changeset
|
464 |
3040 | 465 async def start(self): |
466 try: | |
467 subscriptions = await self.host.bridge.psNodeSubscriptionsGet( | |
468 self.args.service, | |
469 self.args.node, | |
470 self.profile, | |
471 ) | |
472 except Exception as e: | |
473 self.disp(f"can't get node subscriptions: {e}", error=True) | |
474 self.host.quit(C.EXIT_BRIDGE_ERRBACK) | |
475 else: | |
476 await self.output(subscriptions) | |
477 self.host.quit() | |
2339
d94e932be8b3
jp (pubsub/node): added subscriptions subcommand:
Goffi <goffi@goffi.org>
parents:
2320
diff
changeset
|
478 |
d94e932be8b3
jp (pubsub/node): added subscriptions subcommand:
Goffi <goffi@goffi.org>
parents:
2320
diff
changeset
|
479 |
d94e932be8b3
jp (pubsub/node): added subscriptions subcommand:
Goffi <goffi@goffi.org>
parents:
2320
diff
changeset
|
480 class StoreSubscriptionAction(argparse.Action): |
d94e932be8b3
jp (pubsub/node): added subscriptions subcommand:
Goffi <goffi@goffi.org>
parents:
2320
diff
changeset
|
481 """Action which handle subscription parameter for owner |
d94e932be8b3
jp (pubsub/node): added subscriptions subcommand:
Goffi <goffi@goffi.org>
parents:
2320
diff
changeset
|
482 |
d94e932be8b3
jp (pubsub/node): added subscriptions subcommand:
Goffi <goffi@goffi.org>
parents:
2320
diff
changeset
|
483 list is given by pairs: jid and subscription state |
d94e932be8b3
jp (pubsub/node): added subscriptions subcommand:
Goffi <goffi@goffi.org>
parents:
2320
diff
changeset
|
484 if subscription state is not specified, it default to "subscribed" |
d94e932be8b3
jp (pubsub/node): added subscriptions subcommand:
Goffi <goffi@goffi.org>
parents:
2320
diff
changeset
|
485 """ |
d94e932be8b3
jp (pubsub/node): added subscriptions subcommand:
Goffi <goffi@goffi.org>
parents:
2320
diff
changeset
|
486 |
d94e932be8b3
jp (pubsub/node): added subscriptions subcommand:
Goffi <goffi@goffi.org>
parents:
2320
diff
changeset
|
487 def __call__(self, parser, namespace, values, option_string): |
d94e932be8b3
jp (pubsub/node): added subscriptions subcommand:
Goffi <goffi@goffi.org>
parents:
2320
diff
changeset
|
488 dest_dict = getattr(namespace, self.dest) |
d94e932be8b3
jp (pubsub/node): added subscriptions subcommand:
Goffi <goffi@goffi.org>
parents:
2320
diff
changeset
|
489 while values: |
d94e932be8b3
jp (pubsub/node): added subscriptions subcommand:
Goffi <goffi@goffi.org>
parents:
2320
diff
changeset
|
490 jid_s = values.pop(0) |
d94e932be8b3
jp (pubsub/node): added subscriptions subcommand:
Goffi <goffi@goffi.org>
parents:
2320
diff
changeset
|
491 try: |
d94e932be8b3
jp (pubsub/node): added subscriptions subcommand:
Goffi <goffi@goffi.org>
parents:
2320
diff
changeset
|
492 subscription = values.pop(0) |
d94e932be8b3
jp (pubsub/node): added subscriptions subcommand:
Goffi <goffi@goffi.org>
parents:
2320
diff
changeset
|
493 except IndexError: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
494 subscription = "subscribed" |
2339
d94e932be8b3
jp (pubsub/node): added subscriptions subcommand:
Goffi <goffi@goffi.org>
parents:
2320
diff
changeset
|
495 if subscription not in ALLOWED_SUBSCRIPTIONS_OWNER: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
496 parser.error( |
3028 | 497 _("subscription must be one of {}").format( |
498 ", ".join(ALLOWED_SUBSCRIPTIONS_OWNER) | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
499 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
500 ) |
2339
d94e932be8b3
jp (pubsub/node): added subscriptions subcommand:
Goffi <goffi@goffi.org>
parents:
2320
diff
changeset
|
501 dest_dict[jid_s] = subscription |
d94e932be8b3
jp (pubsub/node): added subscriptions subcommand:
Goffi <goffi@goffi.org>
parents:
2320
diff
changeset
|
502 |
d94e932be8b3
jp (pubsub/node): added subscriptions subcommand:
Goffi <goffi@goffi.org>
parents:
2320
diff
changeset
|
503 |
d94e932be8b3
jp (pubsub/node): added subscriptions subcommand:
Goffi <goffi@goffi.org>
parents:
2320
diff
changeset
|
504 class NodeSubscriptionsSet(base.CommandBase): |
d94e932be8b3
jp (pubsub/node): added subscriptions subcommand:
Goffi <goffi@goffi.org>
parents:
2320
diff
changeset
|
505 def __init__(self, host): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
506 base.CommandBase.__init__( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
507 self, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
508 host, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
509 "set", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
510 use_pubsub=True, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
511 pubsub_flags={C.NODE}, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
512 use_verbose=True, |
3028 | 513 help=_("set/modify subscriptions (for node owner)"), |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
514 ) |
2339
d94e932be8b3
jp (pubsub/node): added subscriptions subcommand:
Goffi <goffi@goffi.org>
parents:
2320
diff
changeset
|
515 |
d94e932be8b3
jp (pubsub/node): added subscriptions subcommand:
Goffi <goffi@goffi.org>
parents:
2320
diff
changeset
|
516 def add_parser_options(self): |
d94e932be8b3
jp (pubsub/node): added subscriptions subcommand:
Goffi <goffi@goffi.org>
parents:
2320
diff
changeset
|
517 # XXX: we use optional argument syntax for a required one because list of list of 2 elements |
d94e932be8b3
jp (pubsub/node): added subscriptions subcommand:
Goffi <goffi@goffi.org>
parents:
2320
diff
changeset
|
518 # (uses to construct dicts) don't work with positional arguments |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
519 self.parser.add_argument( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
520 "-S", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
521 "--subscription", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
522 dest="subscriptions", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
523 default={}, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
524 nargs="+", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
525 metavar=("JID [SUSBSCRIPTION]"), |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
526 required=True, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
527 action=StoreSubscriptionAction, |
3028 | 528 help=_("entity/subscription couple(s)"), |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
529 ) |
2339
d94e932be8b3
jp (pubsub/node): added subscriptions subcommand:
Goffi <goffi@goffi.org>
parents:
2320
diff
changeset
|
530 |
3040 | 531 async def start(self): |
532 try: | |
533 self.host.bridge.psNodeSubscriptionsSet( | |
534 self.args.service, | |
535 self.args.node, | |
536 self.args.subscriptions, | |
537 self.profile, | |
538 ) | |
539 except Exception as e: | |
540 self.disp(f"can't set node subscriptions: {e}", error=True) | |
541 self.host.quit(C.EXIT_BRIDGE_ERRBACK) | |
542 else: | |
543 self.disp(_("subscriptions have been set"), 1) | |
544 self.host.quit() | |
2339
d94e932be8b3
jp (pubsub/node): added subscriptions subcommand:
Goffi <goffi@goffi.org>
parents:
2320
diff
changeset
|
545 |
d94e932be8b3
jp (pubsub/node): added subscriptions subcommand:
Goffi <goffi@goffi.org>
parents:
2320
diff
changeset
|
546 |
d94e932be8b3
jp (pubsub/node): added subscriptions subcommand:
Goffi <goffi@goffi.org>
parents:
2320
diff
changeset
|
547 class NodeSubscriptions(base.CommandBase): |
d94e932be8b3
jp (pubsub/node): added subscriptions subcommand:
Goffi <goffi@goffi.org>
parents:
2320
diff
changeset
|
548 subcommands = (NodeSubscriptionsGet, NodeSubscriptionsSet) |
d94e932be8b3
jp (pubsub/node): added subscriptions subcommand:
Goffi <goffi@goffi.org>
parents:
2320
diff
changeset
|
549 |
d94e932be8b3
jp (pubsub/node): added subscriptions subcommand:
Goffi <goffi@goffi.org>
parents:
2320
diff
changeset
|
550 def __init__(self, host): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
551 super(NodeSubscriptions, self).__init__( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
552 host, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
553 "subscriptions", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
554 use_profile=False, |
3028 | 555 help=_("get or modify node subscriptions"), |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
556 ) |
2339
d94e932be8b3
jp (pubsub/node): added subscriptions subcommand:
Goffi <goffi@goffi.org>
parents:
2320
diff
changeset
|
557 |
d94e932be8b3
jp (pubsub/node): added subscriptions subcommand:
Goffi <goffi@goffi.org>
parents:
2320
diff
changeset
|
558 |
2351
3c0a3fae1862
jp (pubsub/node): added schema (set/edit/get) commands to manipulate PubSub node schema
Goffi <goffi@goffi.org>
parents:
2339
diff
changeset
|
559 class NodeSchemaSet(base.CommandBase): |
3c0a3fae1862
jp (pubsub/node): added schema (set/edit/get) commands to manipulate PubSub node schema
Goffi <goffi@goffi.org>
parents:
2339
diff
changeset
|
560 def __init__(self, host): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
561 base.CommandBase.__init__( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
562 self, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
563 host, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
564 "set", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
565 use_pubsub=True, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
566 pubsub_flags={C.NODE}, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
567 use_verbose=True, |
3028 | 568 help=_("set/replace a schema"), |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
569 ) |
2351
3c0a3fae1862
jp (pubsub/node): added schema (set/edit/get) commands to manipulate PubSub node schema
Goffi <goffi@goffi.org>
parents:
2339
diff
changeset
|
570 |
3c0a3fae1862
jp (pubsub/node): added schema (set/edit/get) commands to manipulate PubSub node schema
Goffi <goffi@goffi.org>
parents:
2339
diff
changeset
|
571 def add_parser_options(self): |
3028 | 572 self.parser.add_argument("schema", help=_("schema to set (must be XML)")) |
2351
3c0a3fae1862
jp (pubsub/node): added schema (set/edit/get) commands to manipulate PubSub node schema
Goffi <goffi@goffi.org>
parents:
2339
diff
changeset
|
573 |
3040 | 574 async def start(self): |
575 try: | |
576 await self.host.bridge.psSchemaSet( | |
577 self.args.service, | |
578 self.args.node, | |
579 self.args.schema, | |
580 self.profile, | |
581 ) | |
582 except Exception as e: | |
583 self.disp(f"can't set schema: {e}", error=True) | |
584 self.host.quit(C.EXIT_BRIDGE_ERRBACK) | |
585 else: | |
586 self.disp(_("schema has been set"), 1) | |
587 self.host.quit() | |
2351
3c0a3fae1862
jp (pubsub/node): added schema (set/edit/get) commands to manipulate PubSub node schema
Goffi <goffi@goffi.org>
parents:
2339
diff
changeset
|
588 |
3c0a3fae1862
jp (pubsub/node): added schema (set/edit/get) commands to manipulate PubSub node schema
Goffi <goffi@goffi.org>
parents:
2339
diff
changeset
|
589 |
3c0a3fae1862
jp (pubsub/node): added schema (set/edit/get) commands to manipulate PubSub node schema
Goffi <goffi@goffi.org>
parents:
2339
diff
changeset
|
590 class NodeSchemaEdit(base.CommandBase, common.BaseEdit): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
591 use_items = False |
2351
3c0a3fae1862
jp (pubsub/node): added schema (set/edit/get) commands to manipulate PubSub node schema
Goffi <goffi@goffi.org>
parents:
2339
diff
changeset
|
592 |
3c0a3fae1862
jp (pubsub/node): added schema (set/edit/get) commands to manipulate PubSub node schema
Goffi <goffi@goffi.org>
parents:
2339
diff
changeset
|
593 def __init__(self, host): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
594 base.CommandBase.__init__( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
595 self, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
596 host, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
597 "edit", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
598 use_pubsub=True, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
599 pubsub_flags={C.NODE}, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
600 use_draft=True, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
601 use_verbose=True, |
3028 | 602 help=_("edit a schema"), |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
603 ) |
2351
3c0a3fae1862
jp (pubsub/node): added schema (set/edit/get) commands to manipulate PubSub node schema
Goffi <goffi@goffi.org>
parents:
2339
diff
changeset
|
604 common.BaseEdit.__init__(self, self.host, PUBSUB_SCHEMA_TMP_DIR) |
3c0a3fae1862
jp (pubsub/node): added schema (set/edit/get) commands to manipulate PubSub node schema
Goffi <goffi@goffi.org>
parents:
2339
diff
changeset
|
605 |
3c0a3fae1862
jp (pubsub/node): added schema (set/edit/get) commands to manipulate PubSub node schema
Goffi <goffi@goffi.org>
parents:
2339
diff
changeset
|
606 def add_parser_options(self): |
2532 | 607 pass |
2351
3c0a3fae1862
jp (pubsub/node): added schema (set/edit/get) commands to manipulate PubSub node schema
Goffi <goffi@goffi.org>
parents:
2339
diff
changeset
|
608 |
3040 | 609 async def publish(self, schema): |
610 try: | |
611 await self.host.bridge.psSchemaSet( | |
612 self.args.service, | |
613 self.args.node, | |
614 schema, | |
615 self.profile, | |
616 ) | |
617 except Exception as e: | |
618 self.disp(f"can't set schema: {e}", error=True) | |
619 self.host.quit(C.EXIT_BRIDGE_ERRBACK) | |
620 else: | |
621 self.disp(_("schema has been set"), 1) | |
622 self.host.quit() | |
2351
3c0a3fae1862
jp (pubsub/node): added schema (set/edit/get) commands to manipulate PubSub node schema
Goffi <goffi@goffi.org>
parents:
2339
diff
changeset
|
623 |
3040 | 624 async def psSchemaGetCb(self, schema): |
2351
3c0a3fae1862
jp (pubsub/node): added schema (set/edit/get) commands to manipulate PubSub node schema
Goffi <goffi@goffi.org>
parents:
2339
diff
changeset
|
625 try: |
3c0a3fae1862
jp (pubsub/node): added schema (set/edit/get) commands to manipulate PubSub node schema
Goffi <goffi@goffi.org>
parents:
2339
diff
changeset
|
626 from lxml import etree |
3c0a3fae1862
jp (pubsub/node): added schema (set/edit/get) commands to manipulate PubSub node schema
Goffi <goffi@goffi.org>
parents:
2339
diff
changeset
|
627 except ImportError: |
3028 | 628 self.disp('lxml module must be installed to use edit, please install it ' |
629 'with "pip install lxml"', | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
630 error=True, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
631 ) |
2351
3c0a3fae1862
jp (pubsub/node): added schema (set/edit/get) commands to manipulate PubSub node schema
Goffi <goffi@goffi.org>
parents:
2339
diff
changeset
|
632 self.host.quit(1) |
3c0a3fae1862
jp (pubsub/node): added schema (set/edit/get) commands to manipulate PubSub node schema
Goffi <goffi@goffi.org>
parents:
2339
diff
changeset
|
633 content_file_obj, content_file_path = self.getTmpFile() |
2359
47516e90d26a
jp (pubsub/node/schema): check that a schema exists before parsing it (fix issue on new schema edition)
Goffi <goffi@goffi.org>
parents:
2353
diff
changeset
|
634 schema = schema.strip() |
47516e90d26a
jp (pubsub/node/schema): check that a schema exists before parsing it (fix issue on new schema edition)
Goffi <goffi@goffi.org>
parents:
2353
diff
changeset
|
635 if schema: |
47516e90d26a
jp (pubsub/node/schema): check that a schema exists before parsing it (fix issue on new schema edition)
Goffi <goffi@goffi.org>
parents:
2353
diff
changeset
|
636 parser = etree.XMLParser(remove_blank_text=True) |
47516e90d26a
jp (pubsub/node/schema): check that a schema exists before parsing it (fix issue on new schema edition)
Goffi <goffi@goffi.org>
parents:
2353
diff
changeset
|
637 schema_elt = etree.fromstring(schema, parser) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
638 content_file_obj.write( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
639 etree.tostring(schema_elt, encoding="utf-8", pretty_print=True) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
640 ) |
2359
47516e90d26a
jp (pubsub/node/schema): check that a schema exists before parsing it (fix issue on new schema edition)
Goffi <goffi@goffi.org>
parents:
2353
diff
changeset
|
641 content_file_obj.seek(0) |
3040 | 642 await self.runEditor("pubsub_schema_editor_args", content_file_path, content_file_obj) |
2351
3c0a3fae1862
jp (pubsub/node): added schema (set/edit/get) commands to manipulate PubSub node schema
Goffi <goffi@goffi.org>
parents:
2339
diff
changeset
|
643 |
3040 | 644 async def start(self): |
645 try: | |
646 schema = await self.host.bridge.psSchemaGet( | |
647 self.args.service, | |
648 self.args.node, | |
649 self.profile, | |
650 ) | |
651 except Exception as e: | |
652 self.disp(f"can't edit schema: {e}", error=True) | |
653 self.host.quit(C.EXIT_BRIDGE_ERRBACK) | |
654 else: | |
655 await self.psSchemaGetCb(schema) | |
2351
3c0a3fae1862
jp (pubsub/node): added schema (set/edit/get) commands to manipulate PubSub node schema
Goffi <goffi@goffi.org>
parents:
2339
diff
changeset
|
656 |
3c0a3fae1862
jp (pubsub/node): added schema (set/edit/get) commands to manipulate PubSub node schema
Goffi <goffi@goffi.org>
parents:
2339
diff
changeset
|
657 |
3c0a3fae1862
jp (pubsub/node): added schema (set/edit/get) commands to manipulate PubSub node schema
Goffi <goffi@goffi.org>
parents:
2339
diff
changeset
|
658 class NodeSchemaGet(base.CommandBase): |
3c0a3fae1862
jp (pubsub/node): added schema (set/edit/get) commands to manipulate PubSub node schema
Goffi <goffi@goffi.org>
parents:
2339
diff
changeset
|
659 def __init__(self, host): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
660 base.CommandBase.__init__( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
661 self, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
662 host, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
663 "get", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
664 use_output=C.OUTPUT_XML, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
665 use_pubsub=True, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
666 pubsub_flags={C.NODE}, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
667 use_verbose=True, |
3028 | 668 help=_("get schema"), |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
669 ) |
2351
3c0a3fae1862
jp (pubsub/node): added schema (set/edit/get) commands to manipulate PubSub node schema
Goffi <goffi@goffi.org>
parents:
2339
diff
changeset
|
670 |
3c0a3fae1862
jp (pubsub/node): added schema (set/edit/get) commands to manipulate PubSub node schema
Goffi <goffi@goffi.org>
parents:
2339
diff
changeset
|
671 def add_parser_options(self): |
3c0a3fae1862
jp (pubsub/node): added schema (set/edit/get) commands to manipulate PubSub node schema
Goffi <goffi@goffi.org>
parents:
2339
diff
changeset
|
672 pass |
3c0a3fae1862
jp (pubsub/node): added schema (set/edit/get) commands to manipulate PubSub node schema
Goffi <goffi@goffi.org>
parents:
2339
diff
changeset
|
673 |
3040 | 674 async def start(self): |
675 try: | |
676 schema = await self.host.bridge.psSchemaGet( | |
677 self.args.service, | |
678 self.args.node, | |
679 self.profile, | |
680 ) | |
681 except Exception as e: | |
682 self.disp(f"can't get schema: {e}", error=True) | |
683 self.host.quit(C.EXIT_BRIDGE_ERRBACK) | |
684 else: | |
685 if schema: | |
686 await self.output(schema) | |
687 self.host.quit() | |
688 else: | |
689 self.disp(_("no schema found"), 1) | |
690 self.host.quit(1) | |
2351
3c0a3fae1862
jp (pubsub/node): added schema (set/edit/get) commands to manipulate PubSub node schema
Goffi <goffi@goffi.org>
parents:
2339
diff
changeset
|
691 |
3c0a3fae1862
jp (pubsub/node): added schema (set/edit/get) commands to manipulate PubSub node schema
Goffi <goffi@goffi.org>
parents:
2339
diff
changeset
|
692 |
3c0a3fae1862
jp (pubsub/node): added schema (set/edit/get) commands to manipulate PubSub node schema
Goffi <goffi@goffi.org>
parents:
2339
diff
changeset
|
693 class NodeSchema(base.CommandBase): |
3c0a3fae1862
jp (pubsub/node): added schema (set/edit/get) commands to manipulate PubSub node schema
Goffi <goffi@goffi.org>
parents:
2339
diff
changeset
|
694 subcommands = (NodeSchemaSet, NodeSchemaEdit, NodeSchemaGet) |
3c0a3fae1862
jp (pubsub/node): added schema (set/edit/get) commands to manipulate PubSub node schema
Goffi <goffi@goffi.org>
parents:
2339
diff
changeset
|
695 |
3c0a3fae1862
jp (pubsub/node): added schema (set/edit/get) commands to manipulate PubSub node schema
Goffi <goffi@goffi.org>
parents:
2339
diff
changeset
|
696 def __init__(self, host): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
697 super(NodeSchema, self).__init__( |
3028 | 698 host, "schema", use_profile=False, help=_("data schema manipulation") |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
699 ) |
2351
3c0a3fae1862
jp (pubsub/node): added schema (set/edit/get) commands to manipulate PubSub node schema
Goffi <goffi@goffi.org>
parents:
2339
diff
changeset
|
700 |
3c0a3fae1862
jp (pubsub/node): added schema (set/edit/get) commands to manipulate PubSub node schema
Goffi <goffi@goffi.org>
parents:
2339
diff
changeset
|
701 |
2197
e0e06391ce91
jp (pubsub): added pubsub/node/info command to retrieve node configuration
Goffi <goffi@goffi.org>
parents:
2195
diff
changeset
|
702 class Node(base.CommandBase): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
703 subcommands = ( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
704 NodeInfo, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
705 NodeCreate, |
2802
f61a50790fae
jp (pubsub/node): added purge command
Goffi <goffi@goffi.org>
parents:
2777
diff
changeset
|
706 NodePurge, |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
707 NodeDelete, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
708 NodeSet, |
2804
710de41da2f2
jp (pubsub/node): new "import" command, to publish many nodes from an XML file
Goffi <goffi@goffi.org>
parents:
2803
diff
changeset
|
709 NodeImport, |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
710 NodeAffiliations, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
711 NodeSubscriptions, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
712 NodeSchema, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
713 ) |
2197
e0e06391ce91
jp (pubsub): added pubsub/node/info command to retrieve node configuration
Goffi <goffi@goffi.org>
parents:
2195
diff
changeset
|
714 |
e0e06391ce91
jp (pubsub): added pubsub/node/info command to retrieve node configuration
Goffi <goffi@goffi.org>
parents:
2195
diff
changeset
|
715 def __init__(self, host): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
716 super(Node, self).__init__( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
717 host, "node", use_profile=False, help=_("node handling") |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
718 ) |
2197
e0e06391ce91
jp (pubsub): added pubsub/node/info command to retrieve node configuration
Goffi <goffi@goffi.org>
parents:
2195
diff
changeset
|
719 |
e0e06391ce91
jp (pubsub): added pubsub/node/info command to retrieve node configuration
Goffi <goffi@goffi.org>
parents:
2195
diff
changeset
|
720 |
2439
6908fe4c6eca
jp (pubsub): added new "set" command tu publish or update an item in a non-interactive way (while "edit" do it interactively).
Goffi <goffi@goffi.org>
parents:
2416
diff
changeset
|
721 class Set(base.CommandBase): |
6908fe4c6eca
jp (pubsub): added new "set" command tu publish or update an item in a non-interactive way (while "edit" do it interactively).
Goffi <goffi@goffi.org>
parents:
2416
diff
changeset
|
722 def __init__(self, host): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
723 base.CommandBase.__init__( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
724 self, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
725 host, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
726 "set", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
727 use_pubsub=True, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
728 pubsub_flags={C.NODE}, |
3028 | 729 help=_("publish a new item or update an existing one"), |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
730 ) |
2439
6908fe4c6eca
jp (pubsub): added new "set" command tu publish or update an item in a non-interactive way (while "edit" do it interactively).
Goffi <goffi@goffi.org>
parents:
2416
diff
changeset
|
731 |
6908fe4c6eca
jp (pubsub): added new "set" command tu publish or update an item in a non-interactive way (while "edit" do it interactively).
Goffi <goffi@goffi.org>
parents:
2416
diff
changeset
|
732 def add_parser_options(self): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
733 self.parser.add_argument( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
734 "item", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
735 nargs="?", |
3028 | 736 default="", |
737 help=_("id, URL of the item to update, keyword, or nothing for new item"), | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
738 ) |
2439
6908fe4c6eca
jp (pubsub): added new "set" command tu publish or update an item in a non-interactive way (while "edit" do it interactively).
Goffi <goffi@goffi.org>
parents:
2416
diff
changeset
|
739 |
3040 | 740 async def start(self): |
2777
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
741 element, etree = xml_tools.etreeParse(self, sys.stdin) |
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
742 element = xml_tools.getPayload(self, element) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
743 payload = etree.tostring(element, encoding="unicode") |
2439
6908fe4c6eca
jp (pubsub): added new "set" command tu publish or update an item in a non-interactive way (while "edit" do it interactively).
Goffi <goffi@goffi.org>
parents:
2416
diff
changeset
|
744 |
3040 | 745 try: |
746 published_id = await self.host.bridge.psItemSend( | |
747 self.args.service, | |
748 self.args.node, | |
749 payload, | |
750 self.args.item, | |
751 {}, | |
752 self.profile, | |
753 ) | |
754 except Exception as e: | |
755 self.disp(_(f"can't send item: {e}"), error=True) | |
756 self.host.quit(C.EXIT_BRIDGE_ERRBACK) | |
757 else: | |
758 if published_id: | |
759 self.disp("Item published at {pub_id}".format(pub_id=published_id)) | |
760 else: | |
761 self.disp("Item published") | |
762 self.host.quit(C.EXIT_OK) | |
2439
6908fe4c6eca
jp (pubsub): added new "set" command tu publish or update an item in a non-interactive way (while "edit" do it interactively).
Goffi <goffi@goffi.org>
parents:
2416
diff
changeset
|
763 |
6908fe4c6eca
jp (pubsub): added new "set" command tu publish or update an item in a non-interactive way (while "edit" do it interactively).
Goffi <goffi@goffi.org>
parents:
2416
diff
changeset
|
764 |
2235
4db836386641
jp: added use_pubsub and use_pubsub_node_req arguments to CommandBase
Goffi <goffi@goffi.org>
parents:
2224
diff
changeset
|
765 class Get(base.CommandBase): |
2191
a1a8233f89e8
jp(pubsub/get): pubsub/get command, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
766 def __init__(self, host): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
767 base.CommandBase.__init__( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
768 self, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
769 host, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
770 "get", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
771 use_output=C.OUTPUT_LIST_XML, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
772 use_pubsub=True, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
773 pubsub_flags={C.NODE, C.MULTI_ITEMS}, |
3028 | 774 help=_("get pubsub item(s)"), |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
775 ) |
2191
a1a8233f89e8
jp(pubsub/get): pubsub/get command, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
776 |
a1a8233f89e8
jp(pubsub/get): pubsub/get command, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
777 def add_parser_options(self): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
778 self.parser.add_argument( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
779 "-S", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
780 "--sub-id", |
3028 | 781 default="", |
782 help=_("subscription id"), | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
783 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
784 # TODO: a key(s) argument to select keys to display |
2191
a1a8233f89e8
jp(pubsub/get): pubsub/get command, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
785 # TODO: add MAM filters |
a1a8233f89e8
jp(pubsub/get): pubsub/get command, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
786 |
3040 | 787 async def start(self): |
788 try: | |
789 ps_result = await self.host.bridge.psItemsGet( | |
790 self.args.service, | |
791 self.args.node, | |
792 self.args.max, | |
793 self.args.items, | |
794 self.args.sub_id, | |
795 self.getPubsubExtra(), | |
796 self.profile, | |
797 ) | |
798 except Exception as e: | |
799 self.disp(f"can't get pubsub items: {e}", error=True) | |
800 self.host.quit(C.EXIT_BRIDGE_ERRBACK) | |
801 else: | |
802 await self.output(ps_result[0]) | |
803 self.host.quit(C.EXIT_OK) | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
804 |
2191
a1a8233f89e8
jp(pubsub/get): pubsub/get command, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
805 |
2281
4af1805cc6df
jp (pubsub/delete): delete command implementation (to delete an item)
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
806 class Delete(base.CommandBase): |
4af1805cc6df
jp (pubsub/delete): delete command implementation (to delete an item)
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
807 def __init__(self, host): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
808 base.CommandBase.__init__( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
809 self, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
810 host, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
811 "delete", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
812 use_pubsub=True, |
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:
2805
diff
changeset
|
813 pubsub_flags={C.NODE, C.ITEM, C.SINGLE_ITEM}, |
3028 | 814 help=_("delete an item"), |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
815 ) |
2281
4af1805cc6df
jp (pubsub/delete): delete command implementation (to delete an item)
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
816 |
4af1805cc6df
jp (pubsub/delete): delete command implementation (to delete an item)
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
817 def add_parser_options(self): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
818 self.parser.add_argument( |
3028 | 819 "-f", "--force", action="store_true", help=_("delete without confirmation") |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
820 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
821 self.parser.add_argument( |
3028 | 822 "-N", "--notify", action="store_true", help=_("notify deletion") |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
823 ) |
2281
4af1805cc6df
jp (pubsub/delete): delete command implementation (to delete an item)
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
824 |
3040 | 825 async def start(self): |
2281
4af1805cc6df
jp (pubsub/delete): delete command implementation (to delete an item)
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
826 if not self.args.item: |
3028 | 827 self.parser.error(_("You need to specify an item to delete")) |
2281
4af1805cc6df
jp (pubsub/delete): delete command implementation (to delete an item)
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
828 if not self.args.force: |
3028 | 829 message = _("Are you sure to delete item {item_id} ?").format( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
830 item_id=self.args.item |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
831 ) |
3040 | 832 await self.host.confirmOrQuit(message, _("item deletion cancelled")) |
833 try: | |
834 await self.host.bridge.psRetractItem( | |
835 self.args.service, | |
836 self.args.node, | |
837 self.args.item, | |
838 self.args.notify, | |
839 self.profile, | |
840 ) | |
841 except Exception as e: | |
842 self.disp(_(f"can't delete item: {e}"), error=True) | |
843 self.host.quit(C.EXIT_BRIDGE_ERRBACK) | |
844 else: | |
845 self.disp(_(f"item {self.args.item} has been deleted")) | |
846 self.host.quit(C.EXIT_OK) | |
2281
4af1805cc6df
jp (pubsub/delete): delete command implementation (to delete an item)
Goffi <goffi@goffi.org>
parents:
2280
diff
changeset
|
847 |
2191
a1a8233f89e8
jp(pubsub/get): pubsub/get command, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
848 |
2275
64e99bf0dfa2
jp (pubsub/edit): new edit subcommand, which work in a same way as for blog
Goffi <goffi@goffi.org>
parents:
2274
diff
changeset
|
849 class Edit(base.CommandBase, common.BaseEdit): |
64e99bf0dfa2
jp (pubsub/edit): new edit subcommand, which work in a same way as for blog
Goffi <goffi@goffi.org>
parents:
2274
diff
changeset
|
850 def __init__(self, host): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
851 base.CommandBase.__init__( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
852 self, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
853 host, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
854 "edit", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
855 use_verbose=True, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
856 use_pubsub=True, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
857 pubsub_flags={C.NODE, C.SINGLE_ITEM}, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
858 use_draft=True, |
3028 | 859 help=_("edit an existing or new pubsub item"), |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
860 ) |
2275
64e99bf0dfa2
jp (pubsub/edit): new edit subcommand, which work in a same way as for blog
Goffi <goffi@goffi.org>
parents:
2274
diff
changeset
|
861 common.BaseEdit.__init__(self, self.host, PUBSUB_TMP_DIR) |
64e99bf0dfa2
jp (pubsub/edit): new edit subcommand, which work in a same way as for blog
Goffi <goffi@goffi.org>
parents:
2274
diff
changeset
|
862 |
64e99bf0dfa2
jp (pubsub/edit): new edit subcommand, which work in a same way as for blog
Goffi <goffi@goffi.org>
parents:
2274
diff
changeset
|
863 def add_parser_options(self): |
2532 | 864 pass |
2275
64e99bf0dfa2
jp (pubsub/edit): new edit subcommand, which work in a same way as for blog
Goffi <goffi@goffi.org>
parents:
2274
diff
changeset
|
865 |
3040 | 866 async def publish(self, content): |
867 published_id = await self.host.bridge.psItemSend( | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
868 self.pubsub_service, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
869 self.pubsub_node, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
870 content, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
871 self.pubsub_item or "", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
872 {}, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
873 self.profile, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
874 ) |
2275
64e99bf0dfa2
jp (pubsub/edit): new edit subcommand, which work in a same way as for blog
Goffi <goffi@goffi.org>
parents:
2274
diff
changeset
|
875 if published_id: |
3028 | 876 self.disp("Item published at {pub_id}".format(pub_id=published_id)) |
2275
64e99bf0dfa2
jp (pubsub/edit): new edit subcommand, which work in a same way as for blog
Goffi <goffi@goffi.org>
parents:
2274
diff
changeset
|
877 else: |
3028 | 878 self.disp("Item published") |
2275
64e99bf0dfa2
jp (pubsub/edit): new edit subcommand, which work in a same way as for blog
Goffi <goffi@goffi.org>
parents:
2274
diff
changeset
|
879 |
3040 | 880 async def getItemData(self, service, node, item): |
2275
64e99bf0dfa2
jp (pubsub/edit): new edit subcommand, which work in a same way as for blog
Goffi <goffi@goffi.org>
parents:
2274
diff
changeset
|
881 try: |
64e99bf0dfa2
jp (pubsub/edit): new edit subcommand, which work in a same way as for blog
Goffi <goffi@goffi.org>
parents:
2274
diff
changeset
|
882 from lxml import etree |
64e99bf0dfa2
jp (pubsub/edit): new edit subcommand, which work in a same way as for blog
Goffi <goffi@goffi.org>
parents:
2274
diff
changeset
|
883 except ImportError: |
3028 | 884 self.disp('lxml module must be installed to use edit, please install it ' |
885 'with "pip install lxml"', | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
886 error=True, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
887 ) |
2275
64e99bf0dfa2
jp (pubsub/edit): new edit subcommand, which work in a same way as for blog
Goffi <goffi@goffi.org>
parents:
2274
diff
changeset
|
888 self.host.quit(1) |
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:
2805
diff
changeset
|
889 items = [item] if item else [] |
3040 | 890 item_raw = (await self.host.bridge.psItemsGet( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
891 service, node, 1, items, "", {}, self.profile |
3040 | 892 ))[0][0] |
893 parser = etree.XMLParser(remove_blank_text=True, recover=True) | |
2275
64e99bf0dfa2
jp (pubsub/edit): new edit subcommand, which work in a same way as for blog
Goffi <goffi@goffi.org>
parents:
2274
diff
changeset
|
894 item_elt = etree.fromstring(item_raw, parser) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
895 item_id = item_elt.get("id") |
2275
64e99bf0dfa2
jp (pubsub/edit): new edit subcommand, which work in a same way as for blog
Goffi <goffi@goffi.org>
parents:
2274
diff
changeset
|
896 try: |
64e99bf0dfa2
jp (pubsub/edit): new edit subcommand, which work in a same way as for blog
Goffi <goffi@goffi.org>
parents:
2274
diff
changeset
|
897 payload = item_elt[0] |
64e99bf0dfa2
jp (pubsub/edit): new edit subcommand, which work in a same way as for blog
Goffi <goffi@goffi.org>
parents:
2274
diff
changeset
|
898 except IndexError: |
3028 | 899 self.disp(_("Item has not payload"), 1) |
900 return "" | |
2280
4bc9a2c2d6c9
jp (pubsub, common): fixed last item edition (keep item id instead of creating a new one)
Goffi <goffi@goffi.org>
parents:
2279
diff
changeset
|
901 return etree.tostring(payload, encoding="unicode", pretty_print=True), item_id |
2275
64e99bf0dfa2
jp (pubsub/edit): new edit subcommand, which work in a same way as for blog
Goffi <goffi@goffi.org>
parents:
2274
diff
changeset
|
902 |
3040 | 903 async def start(self): |
904 (self.pubsub_service, | |
905 self.pubsub_node, | |
906 self.pubsub_item, | |
907 content_file_path, | |
908 content_file_obj) = await self.getItemPath() | |
909 await self.runEditor("pubsub_editor_args", content_file_path, content_file_obj) | |
910 self.host.quit() | |
2275
64e99bf0dfa2
jp (pubsub/edit): new edit subcommand, which work in a same way as for blog
Goffi <goffi@goffi.org>
parents:
2274
diff
changeset
|
911 |
64e99bf0dfa2
jp (pubsub/edit): new edit subcommand, which work in a same way as for blog
Goffi <goffi@goffi.org>
parents:
2274
diff
changeset
|
912 |
2353
ebc0dfe9c0ca
jp (pubsub): added subscribe, unsubscribe and subscriptions methods:
Goffi <goffi@goffi.org>
parents:
2351
diff
changeset
|
913 class Subscribe(base.CommandBase): |
ebc0dfe9c0ca
jp (pubsub): added subscribe, unsubscribe and subscriptions methods:
Goffi <goffi@goffi.org>
parents:
2351
diff
changeset
|
914 def __init__(self, host): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
915 base.CommandBase.__init__( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
916 self, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
917 host, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
918 "subscribe", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
919 use_pubsub=True, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
920 pubsub_flags={C.NODE}, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
921 use_verbose=True, |
3028 | 922 help=_("subscribe to a node"), |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
923 ) |
2353
ebc0dfe9c0ca
jp (pubsub): added subscribe, unsubscribe and subscriptions methods:
Goffi <goffi@goffi.org>
parents:
2351
diff
changeset
|
924 |
ebc0dfe9c0ca
jp (pubsub): added subscribe, unsubscribe and subscriptions methods:
Goffi <goffi@goffi.org>
parents:
2351
diff
changeset
|
925 def add_parser_options(self): |
ebc0dfe9c0ca
jp (pubsub): added subscribe, unsubscribe and subscriptions methods:
Goffi <goffi@goffi.org>
parents:
2351
diff
changeset
|
926 pass |
ebc0dfe9c0ca
jp (pubsub): added subscribe, unsubscribe and subscriptions methods:
Goffi <goffi@goffi.org>
parents:
2351
diff
changeset
|
927 |
3040 | 928 async def start(self): |
929 try: | |
930 sub_id = await self.host.bridge.psSubscribe( | |
931 self.args.service, | |
932 self.args.node, | |
933 {}, | |
934 self.profile, | |
935 ) | |
936 except Exception as e: | |
937 self.disp(_(f"can't subscribe to node: {e}"), error=True) | |
938 self.host.quit(C.EXIT_BRIDGE_ERRBACK) | |
939 else: | |
940 self.disp(_("subscription done"), 1) | |
941 if sub_id: | |
942 self.disp(_("subscription id: {sub_id}").format(sub_id=sub_id)) | |
943 self.host.quit() | |
2353
ebc0dfe9c0ca
jp (pubsub): added subscribe, unsubscribe and subscriptions methods:
Goffi <goffi@goffi.org>
parents:
2351
diff
changeset
|
944 |
ebc0dfe9c0ca
jp (pubsub): added subscribe, unsubscribe and subscriptions methods:
Goffi <goffi@goffi.org>
parents:
2351
diff
changeset
|
945 |
ebc0dfe9c0ca
jp (pubsub): added subscribe, unsubscribe and subscriptions methods:
Goffi <goffi@goffi.org>
parents:
2351
diff
changeset
|
946 class Unsubscribe(base.CommandBase): |
3040 | 947 # FIXME: check why we get a a NodeNotFound on subscribe just after unsubscribe |
2353
ebc0dfe9c0ca
jp (pubsub): added subscribe, unsubscribe and subscriptions methods:
Goffi <goffi@goffi.org>
parents:
2351
diff
changeset
|
948 |
ebc0dfe9c0ca
jp (pubsub): added subscribe, unsubscribe and subscriptions methods:
Goffi <goffi@goffi.org>
parents:
2351
diff
changeset
|
949 def __init__(self, host): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
950 base.CommandBase.__init__( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
951 self, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
952 host, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
953 "unsubscribe", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
954 use_pubsub=True, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
955 pubsub_flags={C.NODE}, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
956 use_verbose=True, |
3028 | 957 help=_("unsubscribe from a node"), |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
958 ) |
2353
ebc0dfe9c0ca
jp (pubsub): added subscribe, unsubscribe and subscriptions methods:
Goffi <goffi@goffi.org>
parents:
2351
diff
changeset
|
959 |
ebc0dfe9c0ca
jp (pubsub): added subscribe, unsubscribe and subscriptions methods:
Goffi <goffi@goffi.org>
parents:
2351
diff
changeset
|
960 def add_parser_options(self): |
ebc0dfe9c0ca
jp (pubsub): added subscribe, unsubscribe and subscriptions methods:
Goffi <goffi@goffi.org>
parents:
2351
diff
changeset
|
961 pass |
ebc0dfe9c0ca
jp (pubsub): added subscribe, unsubscribe and subscriptions methods:
Goffi <goffi@goffi.org>
parents:
2351
diff
changeset
|
962 |
3040 | 963 async def start(self): |
964 try: | |
965 await self.host.bridge.psUnsubscribe( | |
966 self.args.service, | |
967 self.args.node, | |
968 self.profile, | |
969 ) | |
970 except Exception as e: | |
971 self.disp(_(f"can't unsubscribe from node: {e}"), error=True) | |
972 self.host.quit(C.EXIT_BRIDGE_ERRBACK) | |
973 else: | |
974 self.disp(_("subscription removed"), 1) | |
975 self.host.quit() | |
2353
ebc0dfe9c0ca
jp (pubsub): added subscribe, unsubscribe and subscriptions methods:
Goffi <goffi@goffi.org>
parents:
2351
diff
changeset
|
976 |
ebc0dfe9c0ca
jp (pubsub): added subscribe, unsubscribe and subscriptions methods:
Goffi <goffi@goffi.org>
parents:
2351
diff
changeset
|
977 |
ebc0dfe9c0ca
jp (pubsub): added subscribe, unsubscribe and subscriptions methods:
Goffi <goffi@goffi.org>
parents:
2351
diff
changeset
|
978 class Subscriptions(base.CommandBase): |
ebc0dfe9c0ca
jp (pubsub): added subscribe, unsubscribe and subscriptions methods:
Goffi <goffi@goffi.org>
parents:
2351
diff
changeset
|
979 def __init__(self, host): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
980 base.CommandBase.__init__( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
981 self, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
982 host, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
983 "subscriptions", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
984 use_output=C.OUTPUT_LIST_DICT, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
985 use_pubsub=True, |
3028 | 986 help=_("retrieve all subscriptions on a service"), |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
987 ) |
2353
ebc0dfe9c0ca
jp (pubsub): added subscribe, unsubscribe and subscriptions methods:
Goffi <goffi@goffi.org>
parents:
2351
diff
changeset
|
988 |
ebc0dfe9c0ca
jp (pubsub): added subscribe, unsubscribe and subscriptions methods:
Goffi <goffi@goffi.org>
parents:
2351
diff
changeset
|
989 def add_parser_options(self): |
ebc0dfe9c0ca
jp (pubsub): added subscribe, unsubscribe and subscriptions methods:
Goffi <goffi@goffi.org>
parents:
2351
diff
changeset
|
990 pass |
ebc0dfe9c0ca
jp (pubsub): added subscribe, unsubscribe and subscriptions methods:
Goffi <goffi@goffi.org>
parents:
2351
diff
changeset
|
991 |
3040 | 992 async def start(self): |
993 try: | |
994 subscriptions = await self.host.bridge.psSubscriptionsGet( | |
995 self.args.service, | |
996 self.args.node, | |
997 self.profile, | |
998 ) | |
999 except Exception as e: | |
1000 self.disp(_(f"can't retrieve subscriptions: {e}"), error=True) | |
1001 self.host.quit(C.EXIT_BRIDGE_ERRBACK) | |
1002 else: | |
1003 await self.output(subscriptions) | |
1004 self.host.quit() | |
2353
ebc0dfe9c0ca
jp (pubsub): added subscribe, unsubscribe and subscriptions methods:
Goffi <goffi@goffi.org>
parents:
2351
diff
changeset
|
1005 |
ebc0dfe9c0ca
jp (pubsub): added subscribe, unsubscribe and subscriptions methods:
Goffi <goffi@goffi.org>
parents:
2351
diff
changeset
|
1006 |
2235
4db836386641
jp: added use_pubsub and use_pubsub_node_req arguments to CommandBase
Goffi <goffi@goffi.org>
parents:
2224
diff
changeset
|
1007 class Affiliations(base.CommandBase): |
2204
afc703419186
jp (pubsub): added affiliations and node/affiliations/get:
Goffi <goffi@goffi.org>
parents:
2199
diff
changeset
|
1008 def __init__(self, host): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1009 base.CommandBase.__init__( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1010 self, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1011 host, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1012 "affiliations", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1013 use_output=C.OUTPUT_DICT, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1014 use_pubsub=True, |
3028 | 1015 help=_("retrieve all affiliations on a service"), |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1016 ) |
2204
afc703419186
jp (pubsub): added affiliations and node/affiliations/get:
Goffi <goffi@goffi.org>
parents:
2199
diff
changeset
|
1017 |
afc703419186
jp (pubsub): added affiliations and node/affiliations/get:
Goffi <goffi@goffi.org>
parents:
2199
diff
changeset
|
1018 def add_parser_options(self): |
2235
4db836386641
jp: added use_pubsub and use_pubsub_node_req arguments to CommandBase
Goffi <goffi@goffi.org>
parents:
2224
diff
changeset
|
1019 pass |
2204
afc703419186
jp (pubsub): added affiliations and node/affiliations/get:
Goffi <goffi@goffi.org>
parents:
2199
diff
changeset
|
1020 |
3040 | 1021 async def start(self): |
1022 try: | |
1023 affiliations = await self.host.bridge.psAffiliationsGet( | |
1024 self.args.service, | |
1025 self.args.node, | |
1026 self.profile, | |
1027 ) | |
1028 except Exception as e: | |
1029 self.disp( | |
1030 f"can't get node affiliations: {e}", error=True | |
1031 ) | |
1032 self.host.quit(C.EXIT_BRIDGE_ERRBACK) | |
1033 else: | |
1034 await self.output(affiliations) | |
1035 self.host.quit() | |
2204
afc703419186
jp (pubsub): added affiliations and node/affiliations/get:
Goffi <goffi@goffi.org>
parents:
2199
diff
changeset
|
1036 |
afc703419186
jp (pubsub): added affiliations and node/affiliations/get:
Goffi <goffi@goffi.org>
parents:
2199
diff
changeset
|
1037 |
2316 | 1038 class Search(base.CommandBase): |
3040 | 1039 """This command do a search without using MAM |
2764
92af49cde255
jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents:
2761
diff
changeset
|
1040 |
92af49cde255
jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents:
2761
diff
changeset
|
1041 This commands checks every items it finds by itself, |
92af49cde255
jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents:
2761
diff
changeset
|
1042 so it may be heavy in resources both for server and client |
92af49cde255
jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents:
2761
diff
changeset
|
1043 """ |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1044 |
2316 | 1045 RE_FLAGS = re.MULTILINE | re.UNICODE |
3028 | 1046 EXEC_ACTIONS = ("exec", "external") |
2316 | 1047 |
1048 def __init__(self, host): | |
2764
92af49cde255
jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents:
2761
diff
changeset
|
1049 # FIXME: C.NO_MAX is not needed here, and this can be globally removed from consts |
92af49cde255
jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents:
2761
diff
changeset
|
1050 # the only interest is to change the help string, but this can be explained |
92af49cde255
jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents:
2761
diff
changeset
|
1051 # extensively in man pages (max is for each node found) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1052 base.CommandBase.__init__( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1053 self, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1054 host, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1055 "search", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1056 use_output=C.OUTPUT_XML, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1057 use_pubsub=True, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1058 pubsub_flags={C.MULTI_ITEMS, C.NO_MAX}, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1059 use_verbose=True, |
3028 | 1060 help=_("search items corresponding to filters"), |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1061 ) |
2316 | 1062 |
1063 @property | |
1064 def etree(self): | |
1065 """load lxml.etree only if needed""" | |
1066 if self._etree is None: | |
1067 from lxml import etree | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1068 |
2316 | 1069 self._etree = etree |
1070 return self._etree | |
1071 | |
1072 def filter_opt(self, value, type_): | |
1073 return (type_, value) | |
1074 | |
1075 def filter_flag(self, value, type_): | |
1076 value = C.bool(value) | |
1077 return (type_, value) | |
1078 | |
1079 def add_parser_options(self): | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1080 self.parser.add_argument( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1081 "-D", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1082 "--max-depth", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1083 type=int, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1084 default=0, |
3028 | 1085 help=_("maximum depth of recursion (will search linked nodes if > 0, " |
1086 "DEFAULT: 0)"), | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1087 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1088 self.parser.add_argument( |
2764
92af49cde255
jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents:
2761
diff
changeset
|
1089 "-M", |
92af49cde255
jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents:
2761
diff
changeset
|
1090 "--node-max", |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1091 type=int, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1092 default=30, |
3028 | 1093 help=_("maximum number of items to get per node ({} to get all items, " |
1094 "DEFAULT: 30)".format( C.NO_LIMIT)), | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1095 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1096 self.parser.add_argument( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1097 "-N", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1098 "--namespace", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1099 action="append", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1100 nargs=2, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1101 default=[], |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1102 metavar="NAME NAMESPACE", |
3028 | 1103 help=_("namespace to use for xpath"), |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1104 ) |
2316 | 1105 |
1106 # filters | |
3028 | 1107 filter_text = partial(self.filter_opt, type_="text") |
1108 filter_re = partial(self.filter_opt, type_="regex") | |
1109 filter_xpath = partial(self.filter_opt, type_="xpath") | |
1110 filter_python = partial(self.filter_opt, type_="python") | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1111 filters = self.parser.add_argument_group( |
3028 | 1112 _("filters"), |
1113 _("only items corresponding to following filters will be kept"), | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1114 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1115 filters.add_argument( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1116 "-t", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1117 "--text", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1118 action="append", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1119 dest="filters", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1120 type=filter_text, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1121 metavar="TEXT", |
3028 | 1122 help=_("full text filter, item must contain this string (XML included)"), |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1123 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1124 filters.add_argument( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1125 "-r", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1126 "--regex", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1127 action="append", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1128 dest="filters", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1129 type=filter_re, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1130 metavar="EXPRESSION", |
3028 | 1131 help=_("like --text but using a regular expression"), |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1132 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1133 filters.add_argument( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1134 "-x", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1135 "--xpath", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1136 action="append", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1137 dest="filters", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1138 type=filter_xpath, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1139 metavar="XPATH", |
3028 | 1140 help=_("filter items which has elements matching this xpath"), |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1141 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1142 filters.add_argument( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1143 "-P", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1144 "--python", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1145 action="append", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1146 dest="filters", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1147 type=filter_python, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1148 metavar="PYTHON_CODE", |
3028 | 1149 help=_('Python expression which much return a bool (True to keep item, ' |
1150 'False to reject it). "item" is raw text item, "item_xml" is ' | |
1151 'lxml\'s etree.Element' | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1152 ), |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1153 ) |
2316 | 1154 |
1155 # filters flags | |
3028 | 1156 flag_case = partial(self.filter_flag, type_="ignore-case") |
1157 flag_invert = partial(self.filter_flag, type_="invert") | |
1158 flag_dotall = partial(self.filter_flag, type_="dotall") | |
1159 flag_matching = partial(self.filter_flag, type_="only-matching") | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1160 flags = self.parser.add_argument_group( |
3028 | 1161 _("filters flags"), |
1162 _("filters modifiers (change behaviour of following filters)"), | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1163 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1164 flags.add_argument( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1165 "-C", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1166 "--ignore-case", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1167 action="append", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1168 dest="filters", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1169 type=flag_case, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1170 const=("ignore-case", True), |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1171 nargs="?", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1172 metavar="BOOLEAN", |
3028 | 1173 help=_("(don't) ignore case in following filters (DEFAULT: case sensitive)"), |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1174 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1175 flags.add_argument( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1176 "-I", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1177 "--invert", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1178 action="append", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1179 dest="filters", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1180 type=flag_invert, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1181 const=("invert", True), |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1182 nargs="?", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1183 metavar="BOOLEAN", |
3028 | 1184 help=_("(don't) invert effect of following filters (DEFAULT: don't invert)"), |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1185 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1186 flags.add_argument( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1187 "-A", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1188 "--dot-all", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1189 action="append", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1190 dest="filters", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1191 type=flag_dotall, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1192 const=("dotall", True), |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1193 nargs="?", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1194 metavar="BOOLEAN", |
3028 | 1195 help=_("(don't) use DOTALL option for regex (DEFAULT: don't use)"), |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1196 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1197 flags.add_argument( |
2764
92af49cde255
jp (base): MAM and RSM arguments can now be used for pubsub commands:
Goffi <goffi@goffi.org>
parents:
2761
diff
changeset
|
1198 "-k", |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1199 "--only-matching", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1200 action="append", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1201 dest="filters", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1202 type=flag_matching, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1203 const=("only-matching", True), |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1204 nargs="?", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1205 metavar="BOOLEAN", |
3028 | 1206 help=_("keep only the matching part of the item"), |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1207 ) |
2316 | 1208 |
1209 # action | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1210 self.parser.add_argument( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1211 "action", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1212 default="print", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1213 nargs="?", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1214 choices=("print", "exec", "external"), |
3028 | 1215 help=_("action to do on found items (DEFAULT: print)"), |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1216 ) |
2316 | 1217 self.parser.add_argument("command", nargs=argparse.REMAINDER) |
1218 | |
3040 | 1219 async def getItems(self, depth, service, node, items): |
2316 | 1220 self.to_get += 1 |
3040 | 1221 try: |
1222 items_data = await self.host.bridge.psItemsGet( | |
1223 service, | |
1224 node, | |
1225 self.args.node_max, | |
1226 items, | |
1227 "", | |
1228 self.getPubsubExtra(), | |
1229 self.profile, | |
1230 ) | |
1231 except Exception as e: | |
1232 self.disp( | |
1233 f"can't get pubsub items at {service} (node: {node}): {e}", | |
1234 error=True, | |
1235 ) | |
1236 self.to_get -= 1 | |
1237 else: | |
1238 await self.search(items_data, depth) | |
2316 | 1239 |
1240 def _checkPubsubURL(self, match, found_nodes): | |
1241 """check that the matched URL is an xmpp: one | |
1242 | |
1243 @param found_nodes(list[unicode]): found_nodes | |
1244 this list will be filled while xmpp: URIs are discovered | |
1245 """ | |
1246 url = match.group(0) | |
3028 | 1247 if url.startswith("xmpp"): |
2316 | 1248 try: |
1249 url_data = uri.parseXMPPUri(url) | |
1250 except ValueError: | |
1251 return | |
3028 | 1252 if url_data["type"] == "pubsub": |
1253 found_node = {"service": url_data["path"], "node": url_data["node"]} | |
1254 if "item" in url_data: | |
1255 found_node["item"] = url_data["item"] | |
2316 | 1256 found_nodes.append(found_node) |
1257 | |
3040 | 1258 async def getSubNodes(self, item, depth): |
2316 | 1259 """look for pubsub URIs in item, and getItems on the linked nodes""" |
1260 found_nodes = [] | |
1261 checkURI = partial(self._checkPubsubURL, found_nodes=found_nodes) | |
1262 strings.RE_URL.sub(checkURI, item) | |
1263 for data in found_nodes: | |
3040 | 1264 await self.getItems( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1265 depth + 1, |
3028 | 1266 data["service"], |
1267 data["node"], | |
1268 [data["item"]] if "item" in data else [], | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1269 ) |
2316 | 1270 |
1271 def parseXml(self, item): | |
1272 try: | |
1273 return self.etree.fromstring(item) | |
1274 except self.etree.XMLSyntaxError: | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1275 self.disp( |
3028 | 1276 _("item doesn't looks like XML, you have probably used --only-matching " |
1277 "somewhere before and we have no more XML"), | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1278 error=True, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1279 ) |
2316 | 1280 self.host.quit(C.EXIT_BAD_ARG) |
1281 | |
1282 def filter(self, item): | |
1283 """apply filters given on command line | |
1284 | |
1285 if only-matching is used, item may be modified | |
1286 @return (tuple[bool, unicode]): a tuple with: | |
1287 - keep: True if item passed the filters | |
1288 - item: it is returned in case of modifications | |
1289 """ | |
1290 ignore_case = False | |
1291 invert = False | |
1292 dotall = False | |
1293 only_matching = False | |
1294 item_xml = None | |
1295 for type_, value in self.args.filters: | |
1296 keep = True | |
1297 | |
1298 ## filters | |
1299 | |
3028 | 1300 if type_ == "text": |
2316 | 1301 if ignore_case: |
1302 if value.lower() not in item.lower(): | |
1303 keep = False | |
1304 else: | |
1305 if value not in item: | |
1306 keep = False | |
1307 if keep and only_matching: | |
1308 # doesn't really make sens to keep a fixed string | |
1309 # so we raise an error | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1310 self.host.disp( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1311 _( |
3028 | 1312 "--only-matching used with fixed --text string, are you sure?" |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1313 ), |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1314 error=True, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1315 ) |
2316 | 1316 self.host.quit(C.EXIT_BAD_ARG) |
3028 | 1317 elif type_ == "regex": |
2316 | 1318 flags = self.RE_FLAGS |
1319 if ignore_case: | |
1320 flags |= re.IGNORECASE | |
1321 if dotall: | |
1322 flags |= re.DOTALL | |
1323 match = re.search(value, item, flags) | |
1324 keep = match != None | |
1325 if keep and only_matching: | |
1326 item = match.group() | |
1327 item_xml = None | |
3028 | 1328 elif type_ == "xpath": |
2316 | 1329 if item_xml is None: |
1330 item_xml = self.parseXml(item) | |
1331 try: | |
1332 elts = item_xml.xpath(value, namespaces=self.args.namespace) | |
1333 except self.etree.XPathEvalError as e: | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1334 self.disp( |
3028 | 1335 _("can't use xpath: {reason}").format(reason=e), error=True |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1336 ) |
2316 | 1337 self.host.quit(C.EXIT_BAD_ARG) |
1338 keep = bool(elts) | |
1339 if keep and only_matching: | |
1340 item_xml = elts[0] | |
1341 try: | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1342 item = self.etree.tostring(item_xml, encoding="unicode") |
2316 | 1343 except TypeError: |
1344 # we have a string only, not an element | |
3028 | 1345 item = str(item_xml) |
2316 | 1346 item_xml = None |
3028 | 1347 elif type_ == "python": |
2316 | 1348 if item_xml is None: |
1349 item_xml = self.parseXml(item) | |
3040 | 1350 cmd_ns = { |
1351 "etree": self.etree, | |
1352 "item": item, | |
1353 "item_xml": item_xml | |
1354 } | |
2316 | 1355 try: |
1356 keep = eval(value, cmd_ns) | |
1357 except SyntaxError as e: | |
3028 | 1358 self.disp(str(e), error=True) |
2316 | 1359 self.host.quit(C.EXIT_BAD_ARG) |
1360 | |
1361 ## flags | |
1362 | |
3028 | 1363 elif type_ == "ignore-case": |
2316 | 1364 ignore_case = value |
3028 | 1365 elif type_ == "invert": |
2316 | 1366 invert = value |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1367 # we need to continue, else loop would end here |
2316 | 1368 continue |
3028 | 1369 elif type_ == "dotall": |
2316 | 1370 dotall = value |
3028 | 1371 elif type_ == "only-matching": |
2316 | 1372 only_matching = value |
1373 else: | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1374 raise exceptions.InternalError( |
3028 | 1375 _("unknown filter type {type}").format(type=type_) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1376 ) |
2316 | 1377 |
1378 if invert: | |
1379 keep = not keep | |
1380 if not keep: | |
1381 return False, item | |
1382 | |
1383 return True, item | |
1384 | |
3040 | 1385 async def doItemAction(self, item, metadata): |
2316 | 1386 """called when item has been kepts and the action need to be done |
1387 | |
1388 @param item(unicode): accepted item | |
1389 """ | |
1390 action = self.args.action | |
3028 | 1391 if action == "print" or self.host.verbosity > 0: |
2316 | 1392 try: |
3040 | 1393 await self.output(item) |
2316 | 1394 except self.etree.XMLSyntaxError: |
1395 # item is not valid XML, but a string | |
1396 # can happen when --only-matching is used | |
1397 self.disp(item) | |
1398 if action in self.EXEC_ACTIONS: | |
1399 item_elt = self.parseXml(item) | |
3028 | 1400 if action == "exec": |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1401 use = { |
3028 | 1402 "service": metadata["service"], |
1403 "node": metadata["node"], | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1404 "item": item_elt.get("id"), |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1405 "profile": self.profile, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1406 } |
2317
f4e05600577b
jp (arg_tools): args is not modified anymore in get_use_args + fixed args returned + parser_args are returned separatly (return is now a tuple)
Goffi <goffi@goffi.org>
parents:
2316
diff
changeset
|
1407 # we need to send a copy of self.args.command |
f4e05600577b
jp (arg_tools): args is not modified anymore in get_use_args + fixed args returned + parser_args are returned separatly (return is now a tuple)
Goffi <goffi@goffi.org>
parents:
2316
diff
changeset
|
1408 # else it would be modified |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1409 parser_args, use_args = arg_tools.get_use_args( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1410 self.host, self.args.command, use, verbose=self.host.verbosity > 1 |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1411 ) |
2317
f4e05600577b
jp (arg_tools): args is not modified anymore in get_use_args + fixed args returned + parser_args are returned separatly (return is now a tuple)
Goffi <goffi@goffi.org>
parents:
2316
diff
changeset
|
1412 cmd_args = sys.argv[0:1] + parser_args + use_args |
2316 | 1413 else: |
1414 cmd_args = self.args.command | |
1415 | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1416 self.disp( |
3028 | 1417 "COMMAND: {command}".format( |
1418 command=" ".join([arg_tools.escape(a) for a in cmd_args]) | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1419 ), |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1420 2, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1421 ) |
3028 | 1422 if action == "exec": |
3040 | 1423 p = await asyncio.create_subprocess_exec(*cmd_args) |
1424 ret = await p.wait() | |
2316 | 1425 else: |
3040 | 1426 p = await asyncio.create_subprocess_exec(*cmd_args, |
1427 stdin=subprocess.PIPE) | |
1428 await p.communicate(item.encode(sys.getfilesystemencoding())) | |
1429 ret = p.returncode | |
2316 | 1430 if ret != 0: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1431 self.disp( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1432 A.color( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1433 C.A_FAILURE, |
3040 | 1434 _(f"executed command failed with exit code {ret}"), |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1435 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1436 ) |
2316 | 1437 |
3040 | 1438 async def search(self, items_data, depth): |
2316 | 1439 """callback of getItems |
1440 | |
1441 this method filters items, get sub nodes if needed, | |
1442 do the requested action, and exit the command when everything is done | |
1443 @param items_data(tuple): result of getItems | |
1444 @param depth(int): current depth level | |
1445 0 for first node, 1 for first children, and so on | |
1446 """ | |
1447 items, metadata = items_data | |
1448 for item in items: | |
1449 if depth < self.args.max_depth: | |
3040 | 1450 await self.getSubNodes(item, depth) |
2316 | 1451 keep, item = self.filter(item) |
1452 if not keep: | |
1453 continue | |
3040 | 1454 await self.doItemAction(item, metadata) |
2316 | 1455 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1456 # we check if we got all getItems results |
2316 | 1457 self.to_get -= 1 |
1458 if self.to_get == 0: | |
1459 # yes, we can quit | |
1460 self.host.quit() | |
1461 assert self.to_get > 0 | |
1462 | |
3040 | 1463 async def start(self): |
2316 | 1464 if self.args.command: |
1465 if self.args.action not in self.EXEC_ACTIONS: | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1466 self.parser.error( |
3028 | 1467 _("Command can only be used with {actions} actions").format( |
1468 actions=", ".join(self.EXEC_ACTIONS) | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1469 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1470 ) |
2316 | 1471 else: |
1472 if self.args.action in self.EXEC_ACTIONS: | |
3028 | 1473 self.parser.error(_("you need to specify a command to execute")) |
2316 | 1474 if not self.args.node: |
1475 # TODO: handle get service affiliations when node is not set | |
3028 | 1476 self.parser.error(_("empty node is not handled yet")) |
2316 | 1477 # to_get is increased on each get and decreased on each answer |
1478 # when it reach 0 again, the command is finished | |
1479 self.to_get = 0 | |
1480 self._etree = None | |
1481 if self.args.filters is None: | |
1482 self.args.filters = [] | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1483 self.args.namespace = dict( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1484 self.args.namespace + [("pubsub", "http://jabber.org/protocol/pubsub")] |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1485 ) |
3040 | 1486 await self.getItems(0, self.args.service, self.args.node, self.args.items) |
2316 | 1487 |
1488 | |
2777
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1489 class Transform(base.CommandBase): |
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1490 def __init__(self, host): |
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1491 base.CommandBase.__init__( |
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1492 self, |
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1493 host, |
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1494 "transform", |
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1495 use_pubsub=True, |
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1496 pubsub_flags={C.NODE, C.MULTI_ITEMS}, |
3028 | 1497 help=_("modify items of a node using an external command/script"), |
2777
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1498 ) |
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1499 |
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1500 def add_parser_options(self): |
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1501 self.parser.add_argument( |
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1502 "--apply", |
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1503 action="store_true", |
3028 | 1504 help=_("apply transformation (DEFAULT: do a dry run)"), |
2777
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1505 ) |
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1506 self.parser.add_argument( |
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1507 "--admin", |
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1508 action="store_true", |
3028 | 1509 help=_("do a pubsub admin request, needed to change publisher"), |
2777
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1510 ) |
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1511 self.parser.add_argument( |
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1512 "-I", |
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1513 "--ignore_errors", |
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1514 action="store_true", |
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1515 help=_( |
3028 | 1516 "if command return a non zero exit code, ignore the item and continue"), |
2777
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1517 ) |
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1518 self.parser.add_argument( |
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1519 "-A", |
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1520 "--all", |
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1521 action="store_true", |
3028 | 1522 help=_("get all items by looping over all pages using RSM") |
2777
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1523 ) |
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1524 self.parser.add_argument( |
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1525 "command_path", |
3028 | 1526 help=_("path to the command to use. Will be called repetitivly with an " |
1527 "item as input. Output (full item XML) will be used as new one. " | |
1528 'Return "DELETE" string to delete the item, and "SKIP" to ignore it'), | |
2777
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1529 ) |
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1530 |
3040 | 1531 async def psItemsSendCb(self, item_ids, metadata): |
2803
d4a9a60bc650
jp (pubsub/transform): use new psItemsSend method, it is not needed anymore to send items one by one when --admin is not used
Goffi <goffi@goffi.org>
parents:
2802
diff
changeset
|
1532 if item_ids: |
3028 | 1533 self.disp(_('items published with ids {item_ids}').format( |
1534 item_ids=', '.join(item_ids))) | |
2803
d4a9a60bc650
jp (pubsub/transform): use new psItemsSend method, it is not needed anymore to send items one by one when --admin is not used
Goffi <goffi@goffi.org>
parents:
2802
diff
changeset
|
1535 else: |
3028 | 1536 self.disp(_('items published')) |
2777
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1537 if self.args.all: |
3040 | 1538 return await self.handleNextPage(metadata) |
2777
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1539 else: |
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1540 self.host.quit() |
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1541 |
3040 | 1542 async def handleNextPage(self, metadata): |
2777
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1543 """Retrieve new page through RSM or quit if we're in the last page |
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1544 |
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1545 use to handle --all option |
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1546 @param metadata(dict): metadata as returned by psItemsGet |
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1547 """ |
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1548 try: |
3028 | 1549 last = metadata['rsm_last'] |
1550 index = int(metadata['rsm_index']) | |
1551 count = int(metadata['rsm_count']) | |
2777
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1552 except KeyError: |
3028 | 1553 self.disp(_("Can't retrieve all items, RSM metadata not available"), |
2777
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1554 error=True) |
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1555 self.host.quit(C.EXIT_MISSING_FEATURE) |
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1556 except ValueError as e: |
3028 | 1557 self.disp(_("Can't retrieve all items, bad RSM metadata: {msg}") |
2777
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1558 .format(msg=e), error=True) |
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1559 self.host.quit(C.EXIT_ERROR) |
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1560 |
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1561 if index + self.args.rsm_max >= count: |
3028 | 1562 self.disp(_('All items transformed')) |
2777
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1563 self.host.quit(0) |
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1564 |
3028 | 1565 self.disp(_('Retrieving next page ({page_idx}/{page_total})').format( |
2777
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1566 page_idx = int(index/self.args.rsm_max) + 1, |
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1567 page_total = int(count/self.args.rsm_max), |
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1568 ) |
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1569 ) |
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1570 |
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1571 extra = self.getPubsubExtra() |
3028 | 1572 extra['rsm_after'] = last |
3040 | 1573 try: |
1574 ps_result = await self.host.bridge.psItemsGet( | |
1575 self.args.service, | |
1576 self.args.node, | |
1577 self.args.rsm_max, | |
1578 self.args.items, | |
1579 "", | |
1580 extra, | |
1581 self.profile, | |
1582 ) | |
1583 except Exception as e: | |
1584 self.disp( | |
1585 f"can't retrieve items: {e}", error=True | |
1586 ) | |
1587 self.host.quit(C.EXIT_BRIDGE_ERRBACK) | |
1588 else: | |
1589 await self.psItemsGetCb(ps_result) | |
2777
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1590 |
3040 | 1591 async def psItemsGetCb(self, ps_result): |
2777
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1592 items, metadata = ps_result |
3040 | 1593 encoding = 'utf-8' |
2803
d4a9a60bc650
jp (pubsub/transform): use new psItemsSend method, it is not needed anymore to send items one by one when --admin is not used
Goffi <goffi@goffi.org>
parents:
2802
diff
changeset
|
1594 new_items = [] |
2777
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1595 |
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1596 for item in items: |
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1597 if self.check_duplicates: |
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1598 # this is used when we are not ordering by creation |
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1599 # to avoid infinite loop |
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1600 item_elt, __ = xml_tools.etreeParse(self, item) |
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1601 item_id = item_elt.get('id') |
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1602 if item_id in self.items_ids: |
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1603 self.disp(_( |
3028 | 1604 "Duplicate found on item {item_id}, we have probably handled " |
1605 "all items.").format(item_id=item_id)) | |
2777
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1606 self.host.quit() |
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1607 self.items_ids.append(item_id) |
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1608 |
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1609 # we launch the command to filter the item |
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1610 try: |
3040 | 1611 p = await asyncio.create_subprocess_exec( |
1612 self.args.command_path, | |
1613 stdin=subprocess.PIPE, | |
1614 stdout=subprocess.PIPE) | |
2777
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1615 except OSError as e: |
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1616 exit_code = C.EXIT_CMD_NOT_FOUND if e.errno == 2 else C.EXIT_ERROR |
3040 | 1617 self.disp(f"Can't execute the command: {e}", error=True) |
2777
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1618 self.host.quit(exit_code) |
3040 | 1619 encoding = "utf-8" |
1620 cmd_std_out, cmd_std_err = await p.communicate(item.encode(encoding)) | |
1621 ret = p.returncode | |
2777
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1622 if ret != 0: |
3040 | 1623 self.disp(f"The command returned a non zero status while parsing the " |
1624 f"following item:\n\n{item}", error=True) | |
2777
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1625 if self.args.ignore_errors: |
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1626 continue |
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1627 else: |
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1628 self.host.quit(C.EXIT_CMD_ERROR) |
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1629 if cmd_std_err is not None: |
3040 | 1630 cmd_std_err = cmd_std_err.decode(encoding, errors='ignore') |
2777
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1631 self.disp(cmd_std_err, error=True) |
3040 | 1632 cmd_std_out = cmd_std_out.decode(encoding).strip() |
2777
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1633 if cmd_std_out == "DELETE": |
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1634 item_elt, __ = xml_tools.etreeParse(self, item) |
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1635 item_id = item_elt.get('id') |
3040 | 1636 self.disp(_(f"Deleting item {item_id}")) |
2777
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1637 if self.args.apply: |
3040 | 1638 try: |
1639 await self.host.bridge.psRetractItem( | |
1640 self.args.service, | |
1641 self.args.node, | |
1642 item_id, | |
1643 False, | |
1644 self.profile, | |
1645 ) | |
1646 except Exception as e: | |
1647 self.disp( | |
1648 f"can't delete item {item_id}: {e}", error=True | |
1649 ) | |
1650 self.host.quit(C.EXIT_BRIDGE_ERRBACK) | |
2777
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1651 continue |
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1652 elif cmd_std_out == "SKIP": |
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1653 item_elt, __ = xml_tools.etreeParse(self, item) |
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1654 item_id = item_elt.get('id') |
3028 | 1655 self.disp(_("Skipping item {item_id}").format(item_id=item_id)) |
2777
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1656 continue |
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1657 element, etree = xml_tools.etreeParse(self, cmd_std_out) |
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1658 |
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1659 # at this point command has been run and we have a etree.Element object |
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1660 if element.tag not in ("item", "{http://jabber.org/protocol/pubsub}item"): |
3028 | 1661 self.disp("your script must return a whole item, this is not:\n{xml}" |
2777
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1662 .format(xml=etree.tostring(element, encoding="unicode")), error=True) |
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1663 self.host.quit(C.EXIT_DATA_ERROR) |
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1664 |
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1665 if not self.args.apply: |
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1666 # we have a dry run, we just display filtered items |
3028 | 1667 serialised = etree.tostring(element, encoding='unicode', |
2777
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1668 pretty_print=True) |
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1669 self.disp(serialised) |
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1670 else: |
2803
d4a9a60bc650
jp (pubsub/transform): use new psItemsSend method, it is not needed anymore to send items one by one when --admin is not used
Goffi <goffi@goffi.org>
parents:
2802
diff
changeset
|
1671 new_items.append(etree.tostring(element, encoding="unicode")) |
2777
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1672 |
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1673 if not self.args.apply: |
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1674 # on dry run we have nothing to wait for, we can quit |
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1675 if self.args.all: |
3040 | 1676 return await self.handleNextPage(metadata) |
2777
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1677 self.host.quit() |
2803
d4a9a60bc650
jp (pubsub/transform): use new psItemsSend method, it is not needed anymore to send items one by one when --admin is not used
Goffi <goffi@goffi.org>
parents:
2802
diff
changeset
|
1678 else: |
d4a9a60bc650
jp (pubsub/transform): use new psItemsSend method, it is not needed anymore to send items one by one when --admin is not used
Goffi <goffi@goffi.org>
parents:
2802
diff
changeset
|
1679 if self.args.admin: |
3040 | 1680 bridge_method = self.host.bridge.psAdminItemsSend |
1681 else: | |
1682 bridge_method = self.host.bridge.psItemsSend | |
1683 | |
1684 try: | |
1685 ps_result = await bridge_method( | |
2803
d4a9a60bc650
jp (pubsub/transform): use new psItemsSend method, it is not needed anymore to send items one by one when --admin is not used
Goffi <goffi@goffi.org>
parents:
2802
diff
changeset
|
1686 self.args.service, |
d4a9a60bc650
jp (pubsub/transform): use new psItemsSend method, it is not needed anymore to send items one by one when --admin is not used
Goffi <goffi@goffi.org>
parents:
2802
diff
changeset
|
1687 self.args.node, |
d4a9a60bc650
jp (pubsub/transform): use new psItemsSend method, it is not needed anymore to send items one by one when --admin is not used
Goffi <goffi@goffi.org>
parents:
2802
diff
changeset
|
1688 new_items, |
3028 | 1689 "", |
2803
d4a9a60bc650
jp (pubsub/transform): use new psItemsSend method, it is not needed anymore to send items one by one when --admin is not used
Goffi <goffi@goffi.org>
parents:
2802
diff
changeset
|
1690 self.profile, |
d4a9a60bc650
jp (pubsub/transform): use new psItemsSend method, it is not needed anymore to send items one by one when --admin is not used
Goffi <goffi@goffi.org>
parents:
2802
diff
changeset
|
1691 ) |
3040 | 1692 except Exception as e: |
1693 self.disp(f"can't send item: {e}", error=True) | |
1694 self.host.quit(C.EXIT_BRIDGE_ERRBACK) | |
2803
d4a9a60bc650
jp (pubsub/transform): use new psItemsSend method, it is not needed anymore to send items one by one when --admin is not used
Goffi <goffi@goffi.org>
parents:
2802
diff
changeset
|
1695 else: |
3040 | 1696 await self.psItemsSendCb(ps_result, metadata=metadata) |
2777
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1697 |
3040 | 1698 async def start(self): |
2777
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1699 if self.args.all and self.args.order_by != C.ORDER_BY_CREATION: |
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1700 self.check_duplicates = True |
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1701 self.items_ids = [] |
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1702 self.disp(A.color( |
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1703 A.FG_RED, A.BOLD, |
3028 | 1704 '/!\\ "--all" should be used with "--order-by creation" /!\\\n', |
2777
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1705 A.RESET, |
3028 | 1706 "We'll update items, so order may change during transformation,\n" |
1707 "we'll try to mitigate that by stopping on first duplicate,\n" | |
1708 "but this method is not safe, and some items may be missed.\n---\n")) | |
2777
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1709 else: |
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1710 self.check_duplicates = False |
3040 | 1711 |
1712 try: | |
1713 ps_result = await self.host.bridge.psItemsGet( | |
1714 self.args.service, | |
1715 self.args.node, | |
1716 self.args.max, | |
1717 self.args.items, | |
1718 "", | |
1719 self.getPubsubExtra(), | |
1720 self.profile, | |
1721 ) | |
1722 except Exception as e: | |
1723 self.disp(f"can't retrieve items: {e}", error=True) | |
1724 self.host.quit(C.EXIT_BRIDGE_ERRBACK) | |
1725 else: | |
1726 await self.psItemsGetCb(ps_result) | |
2777
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1727 |
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1728 |
2235
4db836386641
jp: added use_pubsub and use_pubsub_node_req arguments to CommandBase
Goffi <goffi@goffi.org>
parents:
2224
diff
changeset
|
1729 class Uri(base.CommandBase): |
2224
87fcd4a7c7e4
jp (pubsub): added uri command to build pubsub URI
Goffi <goffi@goffi.org>
parents:
2221
diff
changeset
|
1730 def __init__(self, host): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1731 base.CommandBase.__init__( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1732 self, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1733 host, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1734 "uri", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1735 use_profile=False, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1736 use_pubsub=True, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1737 pubsub_flags={C.NODE, C.SINGLE_ITEM}, |
3028 | 1738 help=_("build URI"), |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1739 ) |
2224
87fcd4a7c7e4
jp (pubsub): added uri command to build pubsub URI
Goffi <goffi@goffi.org>
parents:
2221
diff
changeset
|
1740 |
87fcd4a7c7e4
jp (pubsub): added uri command to build pubsub URI
Goffi <goffi@goffi.org>
parents:
2221
diff
changeset
|
1741 def add_parser_options(self): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1742 self.parser.add_argument( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1743 "-p", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1744 "--profile", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1745 default=C.PROF_KEY_DEFAULT, |
3028 | 1746 help=_("profile (used when no server is specified)"), |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1747 ) |
2224
87fcd4a7c7e4
jp (pubsub): added uri command to build pubsub URI
Goffi <goffi@goffi.org>
parents:
2221
diff
changeset
|
1748 |
87fcd4a7c7e4
jp (pubsub): added uri command to build pubsub URI
Goffi <goffi@goffi.org>
parents:
2221
diff
changeset
|
1749 def display_uri(self, jid_): |
2239
17502e74c046
jp (pubsub/uri): fixed URI generation
Goffi <goffi@goffi.org>
parents:
2235
diff
changeset
|
1750 uri_args = {} |
2224
87fcd4a7c7e4
jp (pubsub): added uri command to build pubsub URI
Goffi <goffi@goffi.org>
parents:
2221
diff
changeset
|
1751 if not self.args.service: |
87fcd4a7c7e4
jp (pubsub): added uri command to build pubsub URI
Goffi <goffi@goffi.org>
parents:
2221
diff
changeset
|
1752 self.args.service = jid.JID(jid_).bare |
87fcd4a7c7e4
jp (pubsub): added uri command to build pubsub URI
Goffi <goffi@goffi.org>
parents:
2221
diff
changeset
|
1753 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1754 for key in ("node", "service", "item"): |
2224
87fcd4a7c7e4
jp (pubsub): added uri command to build pubsub URI
Goffi <goffi@goffi.org>
parents:
2221
diff
changeset
|
1755 value = getattr(self.args, key) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1756 if key == "service": |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1757 key = "path" |
2224
87fcd4a7c7e4
jp (pubsub): added uri command to build pubsub URI
Goffi <goffi@goffi.org>
parents:
2221
diff
changeset
|
1758 if value: |
87fcd4a7c7e4
jp (pubsub): added uri command to build pubsub URI
Goffi <goffi@goffi.org>
parents:
2221
diff
changeset
|
1759 uri_args[key] = value |
3028 | 1760 self.disp(uri.buildXMPPUri("pubsub", **uri_args)) |
2224
87fcd4a7c7e4
jp (pubsub): added uri command to build pubsub URI
Goffi <goffi@goffi.org>
parents:
2221
diff
changeset
|
1761 self.host.quit() |
87fcd4a7c7e4
jp (pubsub): added uri command to build pubsub URI
Goffi <goffi@goffi.org>
parents:
2221
diff
changeset
|
1762 |
3040 | 1763 async def start(self): |
2224
87fcd4a7c7e4
jp (pubsub): added uri command to build pubsub URI
Goffi <goffi@goffi.org>
parents:
2221
diff
changeset
|
1764 if not self.args.service: |
3040 | 1765 try: |
1766 jid_ = await self.host.bridge.asyncGetParamA( | |
1767 "JabberID", | |
1768 "Connection", | |
1769 profile_key=self.args.profile | |
1770 ) | |
1771 except Exception as e: | |
1772 self.disp(f"can't retrieve jid: {e}", error=True) | |
1773 self.host.quit(C.EXIT_BRIDGE_ERRBACK) | |
1774 else: | |
1775 self.display_uri(jid_) | |
2224
87fcd4a7c7e4
jp (pubsub): added uri command to build pubsub URI
Goffi <goffi@goffi.org>
parents:
2221
diff
changeset
|
1776 else: |
87fcd4a7c7e4
jp (pubsub): added uri command to build pubsub URI
Goffi <goffi@goffi.org>
parents:
2221
diff
changeset
|
1777 self.display_uri(None) |
87fcd4a7c7e4
jp (pubsub): added uri command to build pubsub URI
Goffi <goffi@goffi.org>
parents:
2221
diff
changeset
|
1778 |
87fcd4a7c7e4
jp (pubsub): added uri command to build pubsub URI
Goffi <goffi@goffi.org>
parents:
2221
diff
changeset
|
1779 |
2308
0b21d87c91cf
jp (pubsub/hook): added create/delete/list hook command to handle new Pubsub hook feature
Goffi <goffi@goffi.org>
parents:
2292
diff
changeset
|
1780 class HookCreate(base.CommandBase): |
0b21d87c91cf
jp (pubsub/hook): added create/delete/list hook command to handle new Pubsub hook feature
Goffi <goffi@goffi.org>
parents:
2292
diff
changeset
|
1781 def __init__(self, host): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1782 base.CommandBase.__init__( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1783 self, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1784 host, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1785 "create", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1786 use_pubsub=True, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1787 pubsub_flags={C.NODE}, |
3028 | 1788 help=_("create a Pubsub hook"), |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1789 ) |
2308
0b21d87c91cf
jp (pubsub/hook): added create/delete/list hook command to handle new Pubsub hook feature
Goffi <goffi@goffi.org>
parents:
2292
diff
changeset
|
1790 |
0b21d87c91cf
jp (pubsub/hook): added create/delete/list hook command to handle new Pubsub hook feature
Goffi <goffi@goffi.org>
parents:
2292
diff
changeset
|
1791 def add_parser_options(self): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1792 self.parser.add_argument( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1793 "-t", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1794 "--type", |
3028 | 1795 default="python", |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1796 choices=("python", "python_file", "python_code"), |
3028 | 1797 help=_("hook type"), |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1798 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1799 self.parser.add_argument( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1800 "-P", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1801 "--persistent", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1802 action="store_true", |
3028 | 1803 help=_("make hook persistent across restarts"), |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1804 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1805 self.parser.add_argument( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1806 "hook_arg", |
3028 | 1807 help=_("argument of the hook (depend of the type)"), |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1808 ) |
2308
0b21d87c91cf
jp (pubsub/hook): added create/delete/list hook command to handle new Pubsub hook feature
Goffi <goffi@goffi.org>
parents:
2292
diff
changeset
|
1809 |
0b21d87c91cf
jp (pubsub/hook): added create/delete/list hook command to handle new Pubsub hook feature
Goffi <goffi@goffi.org>
parents:
2292
diff
changeset
|
1810 @staticmethod |
0b21d87c91cf
jp (pubsub/hook): added create/delete/list hook command to handle new Pubsub hook feature
Goffi <goffi@goffi.org>
parents:
2292
diff
changeset
|
1811 def checkArgs(self): |
3028 | 1812 if self.args.type == "python_file": |
2308
0b21d87c91cf
jp (pubsub/hook): added create/delete/list hook command to handle new Pubsub hook feature
Goffi <goffi@goffi.org>
parents:
2292
diff
changeset
|
1813 self.args.hook_arg = os.path.abspath(self.args.hook_arg) |
0b21d87c91cf
jp (pubsub/hook): added create/delete/list hook command to handle new Pubsub hook feature
Goffi <goffi@goffi.org>
parents:
2292
diff
changeset
|
1814 if not os.path.isfile(self.args.hook_arg): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1815 self.parser.error( |
3028 | 1816 _("{path} is not a file").format(path=self.args.hook_arg) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1817 ) |
2308
0b21d87c91cf
jp (pubsub/hook): added create/delete/list hook command to handle new Pubsub hook feature
Goffi <goffi@goffi.org>
parents:
2292
diff
changeset
|
1818 |
3040 | 1819 async def start(self): |
2308
0b21d87c91cf
jp (pubsub/hook): added create/delete/list hook command to handle new Pubsub hook feature
Goffi <goffi@goffi.org>
parents:
2292
diff
changeset
|
1820 self.checkArgs(self) |
3040 | 1821 try: |
1822 await self.host.bridge.psHookAdd( | |
1823 self.args.service, | |
1824 self.args.node, | |
1825 self.args.type, | |
1826 self.args.hook_arg, | |
1827 self.args.persistent, | |
1828 self.profile, | |
1829 ) | |
1830 except Exception as e: | |
1831 self.disp(f"can't create hook: {e}", error=True) | |
1832 self.host.quit(C.EXIT_BRIDGE_ERRBACK) | |
1833 else: | |
1834 self.host.quit() | |
2308
0b21d87c91cf
jp (pubsub/hook): added create/delete/list hook command to handle new Pubsub hook feature
Goffi <goffi@goffi.org>
parents:
2292
diff
changeset
|
1835 |
0b21d87c91cf
jp (pubsub/hook): added create/delete/list hook command to handle new Pubsub hook feature
Goffi <goffi@goffi.org>
parents:
2292
diff
changeset
|
1836 |
0b21d87c91cf
jp (pubsub/hook): added create/delete/list hook command to handle new Pubsub hook feature
Goffi <goffi@goffi.org>
parents:
2292
diff
changeset
|
1837 class HookDelete(base.CommandBase): |
0b21d87c91cf
jp (pubsub/hook): added create/delete/list hook command to handle new Pubsub hook feature
Goffi <goffi@goffi.org>
parents:
2292
diff
changeset
|
1838 def __init__(self, host): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1839 base.CommandBase.__init__( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1840 self, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1841 host, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1842 "delete", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1843 use_pubsub=True, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1844 pubsub_flags={C.NODE}, |
3028 | 1845 help=_("delete a Pubsub hook"), |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1846 ) |
2308
0b21d87c91cf
jp (pubsub/hook): added create/delete/list hook command to handle new Pubsub hook feature
Goffi <goffi@goffi.org>
parents:
2292
diff
changeset
|
1847 |
0b21d87c91cf
jp (pubsub/hook): added create/delete/list hook command to handle new Pubsub hook feature
Goffi <goffi@goffi.org>
parents:
2292
diff
changeset
|
1848 def add_parser_options(self): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1849 self.parser.add_argument( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1850 "-t", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1851 "--type", |
3028 | 1852 default="", |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1853 choices=("", "python", "python_file", "python_code"), |
3028 | 1854 help=_("hook type to remove, empty to remove all (DEFAULT: remove all)"), |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1855 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1856 self.parser.add_argument( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1857 "-a", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1858 "--arg", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1859 dest="hook_arg", |
3028 | 1860 default="", |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1861 help=_( |
3028 | 1862 "argument of the hook to remove, empty to remove all (DEFAULT: remove all)" |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1863 ), |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1864 ) |
2308
0b21d87c91cf
jp (pubsub/hook): added create/delete/list hook command to handle new Pubsub hook feature
Goffi <goffi@goffi.org>
parents:
2292
diff
changeset
|
1865 |
3040 | 1866 async def start(self): |
2308
0b21d87c91cf
jp (pubsub/hook): added create/delete/list hook command to handle new Pubsub hook feature
Goffi <goffi@goffi.org>
parents:
2292
diff
changeset
|
1867 HookCreate.checkArgs(self) |
3040 | 1868 try: |
1869 nb_deleted = await self.host.bridge.psHookRemove( | |
1870 self.args.service, | |
1871 self.args.node, | |
1872 self.args.type, | |
1873 self.args.hook_arg, | |
1874 self.profile, | |
1875 ) | |
1876 except Exception as e: | |
1877 self.disp(f"can't delete hook: {e}", error=True) | |
1878 self.host.quit(C.EXIT_BRIDGE_ERRBACK) | |
1879 else: | |
1880 self.disp(_(f"{nb_deleted} hook(s) have been deleted")) | |
1881 self.host.quit() | |
2308
0b21d87c91cf
jp (pubsub/hook): added create/delete/list hook command to handle new Pubsub hook feature
Goffi <goffi@goffi.org>
parents:
2292
diff
changeset
|
1882 |
0b21d87c91cf
jp (pubsub/hook): added create/delete/list hook command to handle new Pubsub hook feature
Goffi <goffi@goffi.org>
parents:
2292
diff
changeset
|
1883 |
0b21d87c91cf
jp (pubsub/hook): added create/delete/list hook command to handle new Pubsub hook feature
Goffi <goffi@goffi.org>
parents:
2292
diff
changeset
|
1884 class HookList(base.CommandBase): |
0b21d87c91cf
jp (pubsub/hook): added create/delete/list hook command to handle new Pubsub hook feature
Goffi <goffi@goffi.org>
parents:
2292
diff
changeset
|
1885 def __init__(self, host): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1886 base.CommandBase.__init__( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1887 self, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1888 host, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1889 "list", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1890 use_output=C.OUTPUT_LIST_DICT, |
3028 | 1891 help=_("list hooks of a profile"), |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1892 ) |
2308
0b21d87c91cf
jp (pubsub/hook): added create/delete/list hook command to handle new Pubsub hook feature
Goffi <goffi@goffi.org>
parents:
2292
diff
changeset
|
1893 |
0b21d87c91cf
jp (pubsub/hook): added create/delete/list hook command to handle new Pubsub hook feature
Goffi <goffi@goffi.org>
parents:
2292
diff
changeset
|
1894 def add_parser_options(self): |
0b21d87c91cf
jp (pubsub/hook): added create/delete/list hook command to handle new Pubsub hook feature
Goffi <goffi@goffi.org>
parents:
2292
diff
changeset
|
1895 pass |
0b21d87c91cf
jp (pubsub/hook): added create/delete/list hook command to handle new Pubsub hook feature
Goffi <goffi@goffi.org>
parents:
2292
diff
changeset
|
1896 |
3040 | 1897 async def start(self): |
1898 try: | |
1899 data = await self.host.bridge.psHookList( | |
1900 self.profile, | |
1901 ) | |
1902 except Exception as e: | |
1903 self.disp(f"can't list hooks: {e}", error=True) | |
1904 self.host.quit(C.EXIT_BRIDGE_ERRBACK) | |
1905 else: | |
1906 if not data: | |
1907 self.disp(_("No hook found.")) | |
1908 await self.output(data) | |
1909 self.host.quit() | |
2308
0b21d87c91cf
jp (pubsub/hook): added create/delete/list hook command to handle new Pubsub hook feature
Goffi <goffi@goffi.org>
parents:
2292
diff
changeset
|
1910 |
0b21d87c91cf
jp (pubsub/hook): added create/delete/list hook command to handle new Pubsub hook feature
Goffi <goffi@goffi.org>
parents:
2292
diff
changeset
|
1911 |
0b21d87c91cf
jp (pubsub/hook): added create/delete/list hook command to handle new Pubsub hook feature
Goffi <goffi@goffi.org>
parents:
2292
diff
changeset
|
1912 class Hook(base.CommandBase): |
0b21d87c91cf
jp (pubsub/hook): added create/delete/list hook command to handle new Pubsub hook feature
Goffi <goffi@goffi.org>
parents:
2292
diff
changeset
|
1913 subcommands = (HookCreate, HookDelete, HookList) |
0b21d87c91cf
jp (pubsub/hook): added create/delete/list hook command to handle new Pubsub hook feature
Goffi <goffi@goffi.org>
parents:
2292
diff
changeset
|
1914 |
0b21d87c91cf
jp (pubsub/hook): added create/delete/list hook command to handle new Pubsub hook feature
Goffi <goffi@goffi.org>
parents:
2292
diff
changeset
|
1915 def __init__(self, host): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1916 super(Hook, self).__init__( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1917 host, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1918 "hook", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1919 use_profile=False, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1920 use_verbose=True, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1921 help=_("trigger action on Pubsub notifications"), |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1922 ) |
2308
0b21d87c91cf
jp (pubsub/hook): added create/delete/list hook command to handle new Pubsub hook feature
Goffi <goffi@goffi.org>
parents:
2292
diff
changeset
|
1923 |
0b21d87c91cf
jp (pubsub/hook): added create/delete/list hook command to handle new Pubsub hook feature
Goffi <goffi@goffi.org>
parents:
2292
diff
changeset
|
1924 |
2191
a1a8233f89e8
jp(pubsub/get): pubsub/get command, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1925 class Pubsub(base.CommandBase): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1926 subcommands = ( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1927 Set, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1928 Get, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1929 Delete, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1930 Edit, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1931 Subscribe, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1932 Unsubscribe, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1933 Subscriptions, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1934 Node, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1935 Affiliations, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1936 Search, |
2777
ff1b40823b07
jp (pubsub): new "transform" command:
Goffi <goffi@goffi.org>
parents:
2773
diff
changeset
|
1937 Transform, |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1938 Hook, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1939 Uri, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1940 ) |
2191
a1a8233f89e8
jp(pubsub/get): pubsub/get command, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1941 |
a1a8233f89e8
jp(pubsub/get): pubsub/get command, first draft
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1942 def __init__(self, host): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1943 super(Pubsub, self).__init__( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1944 host, "pubsub", use_profile=False, help=_("PubSub nodes/items management") |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
1945 ) |