annotate sat_frontends/jp/cmd_pubsub.py @ 3374:47755614b82a

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