comparison sat_frontends/jp/cmd_list.py @ 3510:059742e925f2

jp (list): implement `set` and `delete` subcommands.
author Goffi <goffi@goffi.org>
date Thu, 22 Apr 2021 18:21:33 +0200
parents be6d91572633
children 04283582966f
comparison
equal deleted inserted replaced
3509:b977c74f9c85 3510:059742e925f2
66 except Exception as e: 66 except Exception as e:
67 self.disp(f"can't get lists: {e}", error=True) 67 self.disp(f"can't get lists: {e}", error=True)
68 self.host.quit(C.EXIT_BRIDGE_ERRBACK) 68 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
69 else: 69 else:
70 await self.output(lists_data[0]) 70 await self.output(lists_data[0])
71 self.host.quit(C.EXIT_OK)
72
73
74 class Set(base.CommandBase):
75
76 def __init__(self, host):
77 base.CommandBase.__init__(
78 self,
79 host,
80 "set",
81 use_pubsub=True,
82 pubsub_defaults={"service": _("auto"), "node": _("auto")},
83 help=_("set a list item"),
84 )
85
86 def add_parser_options(self):
87 self.parser.add_argument(
88 "-f",
89 "--field",
90 action="append",
91 nargs="+",
92 dest="fields",
93 required=True,
94 metavar=("NAME", "VALUES"),
95 help=_("field(s) to set (required)"),
96 )
97 self.parser.add_argument(
98 "-U",
99 "--update",
100 choices=("auto", "true", "false"),
101 default="auto",
102 help=_("update existing item instead of replacing it (DEFAULT: auto)"),
103 )
104 self.parser.add_argument(
105 "item",
106 nargs="?",
107 default="",
108 help=_("id, URL of the item to update, or nothing for new item"),
109 )
110
111 async def start(self):
112 await common.fill_well_known_uri(self, os.getcwd(), "tickets", meta_map={})
113 if self.args.update == "auto":
114 # we update if we have a item id specified
115 update = bool(self.args.item)
116 else:
117 update = C.bool(self.args.update)
118
119 values = {}
120
121 for field_data in self.args.fields:
122 values.setdefault(field_data[0], []).extend(field_data[1:])
123
124 extra = {"update": update}
125
126 try:
127 item_id = await self.host.bridge.listSet(
128 self.args.service,
129 self.args.node,
130 values,
131 '',
132 self.args.item,
133 data_format.serialise(extra),
134 self.profile
135 )
136 except Exception as e:
137 self.disp(f"can't set list item: {e}", error=True)
138 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
139 else:
140 self.disp(f"item {str(item_id or self.args.item)!r} set successfully")
141 self.host.quit(C.EXIT_OK)
142
143
144 class Delete(base.CommandBase):
145
146 def __init__(self, host):
147 base.CommandBase.__init__(
148 self,
149 host,
150 "delete",
151 use_pubsub=True,
152 pubsub_defaults={"service": _("auto"), "node": _("auto")},
153 help=_("delete a list item"),
154 )
155
156 def add_parser_options(self):
157 self.parser.add_argument(
158 "-f", "--force", action="store_true", help=_("delete without confirmation")
159 )
160 self.parser.add_argument(
161 "-N", "--notify", action="store_true", help=_("notify deletion")
162 )
163 self.parser.add_argument(
164 "item",
165 help=_("id of the item to delete"),
166 )
167
168 async def start(self):
169 await common.fill_well_known_uri(self, os.getcwd(), "tickets", meta_map={})
170 if not self.args.item:
171 self.parser.error(_("You need to specify a list item to delete"))
172 if not self.args.force:
173 message = _("Are you sure to delete list item {item_id} ?").format(
174 item_id=self.args.item
175 )
176 await self.host.confirmOrQuit(message, _("item deletion cancelled"))
177 try:
178 await self.host.bridge.listDeleteItem(
179 self.args.service,
180 self.args.node,
181 self.args.item,
182 self.args.notify,
183 self.profile,
184 )
185 except Exception as e:
186 self.disp(_(f"can't delete item: {e}"), error=True)
187 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
188 else:
189 self.disp(_(f"item {self.args.item} has been deleted"))
71 self.host.quit(C.EXIT_OK) 190 self.host.quit(C.EXIT_OK)
72 191
73 192
74 class Import(base.CommandBase): 193 class Import(base.CommandBase):
75 # TODO: factorize with blog/import 194 # TODO: factorize with blog/import
210 else: 329 else:
211 await self.set_progress_id(progress_id) 330 await self.set_progress_id(progress_id)
212 331
213 332
214 class List(base.CommandBase): 333 class List(base.CommandBase):
215 subcommands = (Get, Import) 334 subcommands = (Get, Set, Delete, Import)
216 335
217 def __init__(self, host): 336 def __init__(self, host):
218 super(List, self).__init__( 337 super(List, self).__init__(
219 host, "list", use_profile=False, help=_("pubsub lists handling") 338 host, "list", use_profile=False, help=_("pubsub lists handling")
220 ) 339 )