annotate frontends/src/jp/cmd_ticket.py @ 2545:2df1ca79cb30

jp (merge-request/import): added import command
author Goffi <goffi@goffi.org>
date Fri, 30 Mar 2018 17:53:57 +0200
parents 0046283a285d
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2374
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/env python2
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
3
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
4 # jp: a SàT command line tool
2483
0046283a285d dates update
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
5 # Copyright (C) 2009-2018 Jérôme Poisson (goffi@goffi.org)
2374
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
6
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # This program is free software: you can redistribute it and/or modify
686f84ebb670 jp (ticket): ticket 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
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
9 # the Free Software Foundation, either version 3 of the License, or
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
10 # (at your option) any later version.
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
11
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # This program is distributed in the hope that it will be useful,
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
15 # GNU Affero General Public License for more details.
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
16
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
17 # You should have received a copy of the GNU Affero General Public License
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
19
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
20
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
21 import base
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
22 from sat.core.i18n import _
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
23 import json
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
24
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
25 __commands__ = ["Ticket"]
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
26
2396
66baa687c682 plugins tickets import, jp (ticket/import): implemented mapping:
Goffi <goffi@goffi.org>
parents: 2374
diff changeset
27 FIELDS_MAP = u'mapping'
2374
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
28
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
29
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
30 class Import(base.CommandAnswering):
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
31 # TODO: factorize with blog/import
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
32
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
33 def __init__(self, host):
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
34 super(Import, self).__init__(host, 'import', use_progress=True, help=_(u'import tickets from external software/dataset'))
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
35 self.need_loop=True
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
36
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
37 def add_parser_options(self):
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
38 self.parser.add_argument("importer", type=base.unicode_decoder, nargs='?', help=_(u"importer name, nothing to display importers list"))
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
39 self.parser.add_argument('-o', '--option', action='append', nargs=2, default=[], metavar=(u'NAME', u'VALUE'),
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
40 help=_(u"importer specific options (see importer description)"))
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
41 self.parser.add_argument('-m', '--map', action='append', nargs=2, default=[], metavar=(u'IMPORTED_FIELD', u'DEST_FIELD'),
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
42 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)"))
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
43 self.parser.add_argument('-s', '--service', type=base.unicode_decoder, default=u'', metavar=u'PUBSUB_SERVICE',
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
44 help=_(u"PubSub service where the items must be uploaded (default: server)"))
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
45 self.parser.add_argument('-n', '--node', type=base.unicode_decoder, default=u'', metavar=u'PUBSUB_NODE',
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
46 help=_(u"PubSub node where the items must be uploaded (default: tickets' defaults)"))
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
47 self.parser.add_argument("location", type=base.unicode_decoder, nargs='?',
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
48 help=_(u"importer data location (see importer description), nothing to show importer description"))
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
49
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
50 def onProgressStarted(self, metadata):
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
51 self.disp(_(u'Tickets upload started'),2)
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
52
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
53 def onProgressFinished(self, metadata):
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
54 self.disp(_(u'Tickets uploaded successfully'),2)
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
55
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
56 def onProgressError(self, error_msg):
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
57 self.disp(_(u'Error while uploading tickets: {}').format(error_msg),error=True)
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
58
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
59 def error(self, failure):
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
60 self.disp(_("Error while trying to upload tickets: {reason}").format(reason=failure), error=True)
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
61 self.host.quit(1)
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
62
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
63 def start(self):
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
64 if self.args.location is None:
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
65 for name in ('option', 'service', 'node'):
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
66 if getattr(self.args, name):
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
67 self.parser.error(_(u"{name} argument can't be used without location argument").format(name=name))
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
68 if self.args.importer is None:
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
69 self.disp(u'\n'.join([u'{}: {}'.format(name, desc) for name, desc in self.host.bridge.ticketsImportList()]))
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
70 else:
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
71 try:
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
72 short_desc, long_desc = self.host.bridge.ticketsImportDesc(self.args.importer)
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
73 except Exception as e:
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
74 msg = [l for l in unicode(e).split('\n') if l][-1] # we only keep the last line
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
75 self.disp(msg)
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
76 self.host.quit(1)
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
77 else:
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
78 self.disp(u"{name}: {short_desc}\n\n{long_desc}".format(name=self.args.importer, short_desc=short_desc, long_desc=long_desc))
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
79 self.host.quit()
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
80 else:
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
81 # we have a location, an import is requested
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
82 options = {key: value for key, value in self.args.option}
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
83 fields_map = dict(self.args.map)
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
84 if fields_map:
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
85 if FIELDS_MAP in options:
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
86 self.parser.error(_(u"fields_map must be specified either preencoded in --option or using --map, but not both at the same time"))
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
87 options[FIELDS_MAP] = json.dumps(fields_map)
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
88 def gotId(id_):
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
89 self.progress_id = id_
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
90 self.host.bridge.ticketsImport(self.args.importer, self.args.location, options, self.args.service, self.args.node, self.profile,
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
91 callback=gotId, errback=self.error)
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
92
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
93
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
94 class Ticket(base.CommandBase):
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
95 subcommands = (Import,)
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
96
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
97 def __init__(self, host):
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
98 super(Ticket, self).__init__(host, 'ticket', use_profile=False, help=_('tickets handling'))