comparison sat_frontends/jp/cmd_ticket.py @ 2624:56f94936df1e

code style reformatting using black
author Goffi <goffi@goffi.org>
date Wed, 27 Jun 2018 20:14:46 +0200
parents 41db2f58c753
children 4b693ea24d5f
comparison
equal deleted inserted replaced
2623:49533de4540b 2624:56f94936df1e
26 import json 26 import json
27 import os 27 import os
28 28
29 __commands__ = ["Ticket"] 29 __commands__ = ["Ticket"]
30 30
31 FIELDS_MAP = u'mapping' 31 FIELDS_MAP = u"mapping"
32 32
33 33
34 class Get(base.CommandBase): 34 class Get(base.CommandBase):
35
36 def __init__(self, host): 35 def __init__(self, host):
37 base.CommandBase.__init__(self, host, 'get', use_verbose=True, 36 base.CommandBase.__init__(
38 use_pubsub=True, pubsub_flags={C.MULTI_ITEMS}, 37 self,
39 pubsub_defaults = {u'service': _(u'auto'), u'node': _(u'auto')}, 38 host,
40 use_output=C.OUTPUT_LIST_XMLUI, help=_(u'get tickets')) 39 "get",
41 self.need_loop=True 40 use_verbose=True,
41 use_pubsub=True,
42 pubsub_flags={C.MULTI_ITEMS},
43 pubsub_defaults={u"service": _(u"auto"), u"node": _(u"auto")},
44 use_output=C.OUTPUT_LIST_XMLUI,
45 help=_(u"get tickets"),
46 )
47 self.need_loop = True
42 48
43 def add_parser_options(self): 49 def add_parser_options(self):
44 pass 50 pass
45 51
46 def ticketsGetCb(self, tickets_data): 52 def ticketsGetCb(self, tickets_data):
51 self.host.bridge.ticketsGet( 57 self.host.bridge.ticketsGet(
52 self.args.service, 58 self.args.service,
53 self.args.node, 59 self.args.node,
54 self.args.max, 60 self.args.max,
55 self.args.items, 61 self.args.items,
56 u'', 62 u"",
57 {}, 63 {},
58 self.profile, 64 self.profile,
59 callback=self.ticketsGetCb, 65 callback=self.ticketsGetCb,
60 errback=partial(self.errback, 66 errback=partial(
61 msg=_(u"can't get tickets: {}"), 67 self.errback,
62 exit_code=C.EXIT_BRIDGE_ERRBACK)) 68 msg=_(u"can't get tickets: {}"),
69 exit_code=C.EXIT_BRIDGE_ERRBACK,
70 ),
71 )
63 72
64 def start(self): 73 def start(self):
65 common.URIFinder(self, os.getcwd(), 'tickets', self.getTickets, meta_map={}) 74 common.URIFinder(self, os.getcwd(), "tickets", self.getTickets, meta_map={})
66 75
67 76
68 class Import(base.CommandAnswering): 77 class Import(base.CommandAnswering):
69 # TODO: factorize with blog/import 78 # TODO: factorize with blog/import
70 79
71 def __init__(self, host): 80 def __init__(self, host):
72 super(Import, self).__init__(host, 'import', use_progress=True, help=_(u'import tickets from external software/dataset')) 81 super(Import, self).__init__(
73 self.need_loop=True 82 host,
83 "import",
84 use_progress=True,
85 help=_(u"import tickets from external software/dataset"),
86 )
87 self.need_loop = True
74 88
75 def add_parser_options(self): 89 def add_parser_options(self):
76 self.parser.add_argument("importer", type=base.unicode_decoder, nargs='?', help=_(u"importer name, nothing to display importers list")) 90 self.parser.add_argument(
77 self.parser.add_argument('-o', '--option', action='append', nargs=2, default=[], metavar=(u'NAME', u'VALUE'), 91 "importer",
78 help=_(u"importer specific options (see importer description)")) 92 type=base.unicode_decoder,
79 self.parser.add_argument('-m', '--map', action='append', nargs=2, default=[], metavar=(u'IMPORTED_FIELD', u'DEST_FIELD'), 93 nargs="?",
80 help=_(u"specified field in import data will be put in dest field (default: use same field name, or ignore if it doesn't exist)")) 94 help=_(u"importer name, nothing to display importers list"),
81 self.parser.add_argument('-s', '--service', type=base.unicode_decoder, default=u'', metavar=u'PUBSUB_SERVICE', 95 )
82 help=_(u"PubSub service where the items must be uploaded (default: server)")) 96 self.parser.add_argument(
83 self.parser.add_argument('-n', '--node', type=base.unicode_decoder, default=u'', metavar=u'PUBSUB_NODE', 97 "-o",
84 help=_(u"PubSub node where the items must be uploaded (default: tickets' defaults)")) 98 "--option",
85 self.parser.add_argument("location", type=base.unicode_decoder, nargs='?', 99 action="append",
86 help=_(u"importer data location (see importer description), nothing to show importer description")) 100 nargs=2,
101 default=[],
102 metavar=(u"NAME", u"VALUE"),
103 help=_(u"importer specific options (see importer description)"),
104 )
105 self.parser.add_argument(
106 "-m",
107 "--map",
108 action="append",
109 nargs=2,
110 default=[],
111 metavar=(u"IMPORTED_FIELD", u"DEST_FIELD"),
112 help=_(
113 u"specified field in import data will be put in dest field (default: use same field name, or ignore if it doesn't exist)"
114 ),
115 )
116 self.parser.add_argument(
117 "-s",
118 "--service",
119 type=base.unicode_decoder,
120 default=u"",
121 metavar=u"PUBSUB_SERVICE",
122 help=_(u"PubSub service where the items must be uploaded (default: server)"),
123 )
124 self.parser.add_argument(
125 "-n",
126 "--node",
127 type=base.unicode_decoder,
128 default=u"",
129 metavar=u"PUBSUB_NODE",
130 help=_(
131 u"PubSub node where the items must be uploaded (default: tickets' defaults)"
132 ),
133 )
134 self.parser.add_argument(
135 "location",
136 type=base.unicode_decoder,
137 nargs="?",
138 help=_(
139 u"importer data location (see importer description), nothing to show importer description"
140 ),
141 )
87 142
88 def onProgressStarted(self, metadata): 143 def onProgressStarted(self, metadata):
89 self.disp(_(u'Tickets upload started'),2) 144 self.disp(_(u"Tickets upload started"), 2)
90 145
91 def onProgressFinished(self, metadata): 146 def onProgressFinished(self, metadata):
92 self.disp(_(u'Tickets uploaded successfully'),2) 147 self.disp(_(u"Tickets uploaded successfully"), 2)
93 148
94 def onProgressError(self, error_msg): 149 def onProgressError(self, error_msg):
95 self.disp(_(u'Error while uploading tickets: {}').format(error_msg),error=True) 150 self.disp(_(u"Error while uploading tickets: {}").format(error_msg), error=True)
96 151
97 def error(self, failure): 152 def error(self, failure):
98 self.disp(_("Error while trying to upload tickets: {reason}").format(reason=failure), error=True) 153 self.disp(
154 _("Error while trying to upload tickets: {reason}").format(reason=failure),
155 error=True,
156 )
99 self.host.quit(1) 157 self.host.quit(1)
100 158
101 def start(self): 159 def start(self):
102 if self.args.location is None: 160 if self.args.location is None:
103 for name in ('option', 'service', 'node'): 161 for name in ("option", "service", "node"):
104 if getattr(self.args, name): 162 if getattr(self.args, name):
105 self.parser.error(_(u"{name} argument can't be used without location argument").format(name=name)) 163 self.parser.error(
164 _(
165 u"{name} argument can't be used without location argument"
166 ).format(name=name)
167 )
106 if self.args.importer is None: 168 if self.args.importer is None:
107 self.disp(u'\n'.join([u'{}: {}'.format(name, desc) for name, desc in self.host.bridge.ticketsImportList()])) 169 self.disp(
170 u"\n".join(
171 [
172 u"{}: {}".format(name, desc)
173 for name, desc in self.host.bridge.ticketsImportList()
174 ]
175 )
176 )
108 else: 177 else:
109 try: 178 try:
110 short_desc, long_desc = self.host.bridge.ticketsImportDesc(self.args.importer) 179 short_desc, long_desc = self.host.bridge.ticketsImportDesc(
180 self.args.importer
181 )
111 except Exception as e: 182 except Exception as e:
112 msg = [l for l in unicode(e).split('\n') if l][-1] # we only keep the last line 183 msg = [l for l in unicode(e).split("\n") if l][
184 -1
185 ] # we only keep the last line
113 self.disp(msg) 186 self.disp(msg)
114 self.host.quit(1) 187 self.host.quit(1)
115 else: 188 else:
116 self.disp(u"{name}: {short_desc}\n\n{long_desc}".format(name=self.args.importer, short_desc=short_desc, long_desc=long_desc)) 189 self.disp(
190 u"{name}: {short_desc}\n\n{long_desc}".format(
191 name=self.args.importer,
192 short_desc=short_desc,
193 long_desc=long_desc,
194 )
195 )
117 self.host.quit() 196 self.host.quit()
118 else: 197 else:
119 # we have a location, an import is requested 198 # we have a location, an import is requested
120 options = {key: value for key, value in self.args.option} 199 options = {key: value for key, value in self.args.option}
121 fields_map = dict(self.args.map) 200 fields_map = dict(self.args.map)
122 if fields_map: 201 if fields_map:
123 if FIELDS_MAP in options: 202 if FIELDS_MAP in options:
124 self.parser.error(_(u"fields_map must be specified either preencoded in --option or using --map, but not both at the same time")) 203 self.parser.error(
204 _(
205 u"fields_map must be specified either preencoded in --option or using --map, but not both at the same time"
206 )
207 )
125 options[FIELDS_MAP] = json.dumps(fields_map) 208 options[FIELDS_MAP] = json.dumps(fields_map)
209
126 def gotId(id_): 210 def gotId(id_):
127 self.progress_id = id_ 211 self.progress_id = id_
128 self.host.bridge.ticketsImport(self.args.importer, self.args.location, options, self.args.service, self.args.node, self.profile, 212
129 callback=gotId, errback=self.error) 213 self.host.bridge.ticketsImport(
214 self.args.importer,
215 self.args.location,
216 options,
217 self.args.service,
218 self.args.node,
219 self.profile,
220 callback=gotId,
221 errback=self.error,
222 )
130 223
131 224
132 class Ticket(base.CommandBase): 225 class Ticket(base.CommandBase):
133 subcommands = (Get, Import) 226 subcommands = (Get, Import)
134 227
135 def __init__(self, host): 228 def __init__(self, host):
136 super(Ticket, self).__init__(host, 'ticket', use_profile=False, help=_('tickets handling')) 229 super(Ticket, self).__init__(
230 host, "ticket", use_profile=False, help=_("tickets handling")
231 )