Mercurial > libervia-backend
annotate sat_frontends/jp/cmd_list.py @ 3512:d018d2d8a9e5
docker: separate build instructions for demo compose file
author | Goffi <goffi@goffi.org> |
---|---|
date | Fri, 23 Apr 2021 10:08:58 +0200 |
parents | 059742e925f2 |
children | 04283582966f |
rev | line source |
---|---|
3458 | 1 #!/usr/bin/env python3 |
2 | |
3 | |
4 # jp: a SàT command line tool | |
3479 | 5 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org) |
3458 | 6 |
7 # This program is free software: you can redistribute it and/or modify | |
8 # it under the terms of the GNU Affero General Public License as published by | |
9 # the Free Software Foundation, either version 3 of the License, or | |
10 # (at your option) any later version. | |
11 | |
12 # This program is distributed in the hope that it will be useful, | |
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 # GNU Affero General Public License for more details. | |
16 | |
17 # You should have received a copy of the GNU Affero General Public License | |
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 | |
20 | |
21 import json | |
22 import os | |
23 from sat.core.i18n import _ | |
24 from sat.tools.common import data_format | |
25 from sat_frontends.jp import common | |
26 from sat_frontends.jp.constants import Const as C | |
27 from . import base | |
28 | |
3459
8dc26e5edcd3
plugin tickets, merge_requests: renamed "tickets" feature to "lists":
Goffi <goffi@goffi.org>
parents:
3458
diff
changeset
|
29 __commands__ = ["List"] |
3458 | 30 |
31 FIELDS_MAP = "mapping" | |
32 | |
33 | |
34 class Get(base.CommandBase): | |
35 def __init__(self, host): | |
36 base.CommandBase.__init__( | |
37 self, | |
38 host, | |
39 "get", | |
40 use_verbose=True, | |
41 use_pubsub=True, | |
42 pubsub_flags={C.MULTI_ITEMS}, | |
43 pubsub_defaults={"service": _("auto"), "node": _("auto")}, | |
44 use_output=C.OUTPUT_LIST_XMLUI, | |
3459
8dc26e5edcd3
plugin tickets, merge_requests: renamed "tickets" feature to "lists":
Goffi <goffi@goffi.org>
parents:
3458
diff
changeset
|
45 help=_("get lists"), |
3458 | 46 ) |
47 | |
48 def add_parser_options(self): | |
49 pass | |
50 | |
51 async def start(self): | |
52 await common.fill_well_known_uri(self, os.getcwd(), "tickets", meta_map={}) | |
53 try: | |
3459
8dc26e5edcd3
plugin tickets, merge_requests: renamed "tickets" feature to "lists":
Goffi <goffi@goffi.org>
parents:
3458
diff
changeset
|
54 lists_data = data_format.deserialise( |
8dc26e5edcd3
plugin tickets, merge_requests: renamed "tickets" feature to "lists":
Goffi <goffi@goffi.org>
parents:
3458
diff
changeset
|
55 await self.host.bridge.listGet( |
3458 | 56 self.args.service, |
57 self.args.node, | |
58 self.args.max, | |
59 self.args.items, | |
60 "", | |
61 self.getPubsubExtra(), | |
62 self.profile, | |
63 ), | |
64 type_check=list | |
65 ) | |
66 except Exception as e: | |
3459
8dc26e5edcd3
plugin tickets, merge_requests: renamed "tickets" feature to "lists":
Goffi <goffi@goffi.org>
parents:
3458
diff
changeset
|
67 self.disp(f"can't get lists: {e}", error=True) |
3458 | 68 self.host.quit(C.EXIT_BRIDGE_ERRBACK) |
69 else: | |
3459
8dc26e5edcd3
plugin tickets, merge_requests: renamed "tickets" feature to "lists":
Goffi <goffi@goffi.org>
parents:
3458
diff
changeset
|
70 await self.output(lists_data[0]) |
3458 | 71 self.host.quit(C.EXIT_OK) |
72 | |
73 | |
3510
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
74 class Set(base.CommandBase): |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
75 |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
76 def __init__(self, host): |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
77 base.CommandBase.__init__( |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
78 self, |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
79 host, |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
80 "set", |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
81 use_pubsub=True, |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
82 pubsub_defaults={"service": _("auto"), "node": _("auto")}, |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
83 help=_("set a list item"), |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
84 ) |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
85 |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
86 def add_parser_options(self): |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
87 self.parser.add_argument( |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
88 "-f", |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
89 "--field", |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
90 action="append", |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
91 nargs="+", |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
92 dest="fields", |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
93 required=True, |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
94 metavar=("NAME", "VALUES"), |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
95 help=_("field(s) to set (required)"), |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
96 ) |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
97 self.parser.add_argument( |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
98 "-U", |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
99 "--update", |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
100 choices=("auto", "true", "false"), |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
101 default="auto", |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
102 help=_("update existing item instead of replacing it (DEFAULT: auto)"), |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
103 ) |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
104 self.parser.add_argument( |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
105 "item", |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
106 nargs="?", |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
107 default="", |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
108 help=_("id, URL of the item to update, or nothing for new item"), |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
109 ) |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
110 |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
111 async def start(self): |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
112 await common.fill_well_known_uri(self, os.getcwd(), "tickets", meta_map={}) |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
113 if self.args.update == "auto": |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
114 # we update if we have a item id specified |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
115 update = bool(self.args.item) |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
116 else: |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
117 update = C.bool(self.args.update) |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
118 |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
119 values = {} |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
120 |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
121 for field_data in self.args.fields: |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
122 values.setdefault(field_data[0], []).extend(field_data[1:]) |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
123 |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
124 extra = {"update": update} |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
125 |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
126 try: |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
127 item_id = await self.host.bridge.listSet( |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
128 self.args.service, |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
129 self.args.node, |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
130 values, |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
131 '', |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
132 self.args.item, |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
133 data_format.serialise(extra), |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
134 self.profile |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
135 ) |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
136 except Exception as e: |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
137 self.disp(f"can't set list item: {e}", error=True) |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
138 self.host.quit(C.EXIT_BRIDGE_ERRBACK) |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
139 else: |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
140 self.disp(f"item {str(item_id or self.args.item)!r} set successfully") |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
141 self.host.quit(C.EXIT_OK) |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
142 |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
143 |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
144 class Delete(base.CommandBase): |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
145 |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
146 def __init__(self, host): |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
147 base.CommandBase.__init__( |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
148 self, |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
149 host, |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
150 "delete", |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
151 use_pubsub=True, |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
152 pubsub_defaults={"service": _("auto"), "node": _("auto")}, |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
153 help=_("delete a list item"), |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
154 ) |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
155 |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
156 def add_parser_options(self): |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
157 self.parser.add_argument( |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
158 "-f", "--force", action="store_true", help=_("delete without confirmation") |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
159 ) |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
160 self.parser.add_argument( |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
161 "-N", "--notify", action="store_true", help=_("notify deletion") |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
162 ) |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
163 self.parser.add_argument( |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
164 "item", |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
165 help=_("id of the item to delete"), |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
166 ) |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
167 |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
168 async def start(self): |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
169 await common.fill_well_known_uri(self, os.getcwd(), "tickets", meta_map={}) |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
170 if not self.args.item: |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
171 self.parser.error(_("You need to specify a list item to delete")) |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
172 if not self.args.force: |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
173 message = _("Are you sure to delete list item {item_id} ?").format( |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
174 item_id=self.args.item |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
175 ) |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
176 await self.host.confirmOrQuit(message, _("item deletion cancelled")) |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
177 try: |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
178 await self.host.bridge.listDeleteItem( |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
179 self.args.service, |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
180 self.args.node, |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
181 self.args.item, |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
182 self.args.notify, |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
183 self.profile, |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
184 ) |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
185 except Exception as e: |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
186 self.disp(_(f"can't delete item: {e}"), error=True) |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
187 self.host.quit(C.EXIT_BRIDGE_ERRBACK) |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
188 else: |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
189 self.disp(_(f"item {self.args.item} has been deleted")) |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
190 self.host.quit(C.EXIT_OK) |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
191 |
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
192 |
3458 | 193 class Import(base.CommandBase): |
194 # TODO: factorize with blog/import | |
195 | |
196 def __init__(self, host): | |
197 super(Import, self).__init__( | |
198 host, | |
199 "import", | |
200 use_progress=True, | |
201 use_verbose=True, | |
202 help=_("import tickets from external software/dataset"), | |
203 ) | |
204 | |
205 def add_parser_options(self): | |
206 self.parser.add_argument( | |
207 "importer", | |
208 nargs="?", | |
209 help=_("importer name, nothing to display importers list"), | |
210 ) | |
211 self.parser.add_argument( | |
212 "-o", | |
213 "--option", | |
214 action="append", | |
215 nargs=2, | |
216 default=[], | |
217 metavar=("NAME", "VALUE"), | |
218 help=_("importer specific options (see importer description)"), | |
219 ) | |
220 self.parser.add_argument( | |
221 "-m", | |
222 "--map", | |
223 action="append", | |
224 nargs=2, | |
225 default=[], | |
226 metavar=("IMPORTED_FIELD", "DEST_FIELD"), | |
227 help=_( | |
228 "specified field in import data will be put in dest field (default: use " | |
229 "same field name, or ignore if it doesn't exist)" | |
230 ), | |
231 ) | |
232 self.parser.add_argument( | |
233 "-s", | |
234 "--service", | |
235 default="", | |
236 metavar="PUBSUB_SERVICE", | |
237 help=_("PubSub service where the items must be uploaded (default: server)"), | |
238 ) | |
239 self.parser.add_argument( | |
240 "-n", | |
241 "--node", | |
242 default="", | |
243 metavar="PUBSUB_NODE", | |
244 help=_( | |
245 "PubSub node where the items must be uploaded (default: tickets' " | |
246 "defaults)" | |
247 ), | |
248 ) | |
249 self.parser.add_argument( | |
250 "location", | |
251 nargs="?", | |
252 help=_( | |
253 "importer data location (see importer description), nothing to show " | |
254 "importer description" | |
255 ), | |
256 ) | |
257 | |
258 async def onProgressStarted(self, metadata): | |
259 self.disp(_("Tickets upload started"), 2) | |
260 | |
261 async def onProgressFinished(self, metadata): | |
262 self.disp(_("Tickets uploaded successfully"), 2) | |
263 | |
264 async def onProgressError(self, error_msg): | |
265 self.disp(_(f"Error while uploading tickets: {error_msg}"), error=True) | |
266 | |
267 async def start(self): | |
268 if self.args.location is None: | |
269 # no location, the list of importer or description is requested | |
270 for name in ("option", "service", "node"): | |
271 if getattr(self.args, name): | |
272 self.parser.error( | |
273 _(f"{name} argument can't be used without location argument")) | |
274 if self.args.importer is None: | |
275 self.disp( | |
276 "\n".join( | |
277 [ | |
278 f"{name}: {desc}" | |
279 for name, desc in await self.host.bridge.ticketsImportList() | |
280 ] | |
281 ) | |
282 ) | |
283 else: | |
284 try: | |
285 short_desc, long_desc = await self.host.bridge.ticketsImportDesc( | |
286 self.args.importer | |
287 ) | |
288 except Exception as e: | |
289 self.disp(f"can't get importer description: {e}", error=True) | |
290 self.host.quit(C.EXIT_BRIDGE_ERRBACK) | |
291 else: | |
292 self.disp(f"{name}: {short_desc}\n\n{long_desc}") | |
293 self.host.quit() | |
294 else: | |
295 # we have a location, an import is requested | |
296 | |
297 if self.args.progress: | |
298 # we use a custom progress bar template as we want a counter | |
299 self.pbar_template = [ | |
300 _("Progress: "), ["Percentage"], " ", ["Bar"], " ", | |
301 ["Counter"], " ", ["ETA"] | |
302 ] | |
303 | |
304 options = {key: value for key, value in self.args.option} | |
305 fields_map = dict(self.args.map) | |
306 if fields_map: | |
307 if FIELDS_MAP in options: | |
308 self.parser.error( | |
309 _("fields_map must be specified either preencoded in --option or " | |
310 "using --map, but not both at the same time") | |
311 ) | |
312 options[FIELDS_MAP] = json.dumps(fields_map) | |
313 | |
314 try: | |
315 progress_id = await self.host.bridge.ticketsImport( | |
316 self.args.importer, | |
317 self.args.location, | |
318 options, | |
319 self.args.service, | |
320 self.args.node, | |
321 self.profile, | |
322 ) | |
323 except Exception as e: | |
324 self.disp( | |
325 _(f"Error while trying to import tickets: {e}"), | |
326 error=True, | |
327 ) | |
328 self.host.quit(C.EXIT_BRIDGE_ERRBACK) | |
329 else: | |
330 await self.set_progress_id(progress_id) | |
331 | |
332 | |
3459
8dc26e5edcd3
plugin tickets, merge_requests: renamed "tickets" feature to "lists":
Goffi <goffi@goffi.org>
parents:
3458
diff
changeset
|
333 class List(base.CommandBase): |
3510
059742e925f2
jp (list): implement `set` and `delete` subcommands.
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
334 subcommands = (Get, Set, Delete, Import) |
3458 | 335 |
336 def __init__(self, host): | |
3459
8dc26e5edcd3
plugin tickets, merge_requests: renamed "tickets" feature to "lists":
Goffi <goffi@goffi.org>
parents:
3458
diff
changeset
|
337 super(List, self).__init__( |
8dc26e5edcd3
plugin tickets, merge_requests: renamed "tickets" feature to "lists":
Goffi <goffi@goffi.org>
parents:
3458
diff
changeset
|
338 host, "list", use_profile=False, help=_("pubsub lists handling") |
3458 | 339 ) |