annotate sat_frontends/jp/cmd_ticket.py @ 2608:0883bac573fd

jp (forums/edit): fixed unicode when dumping in json
author Goffi <goffi@goffi.org>
date Sun, 03 Jun 2018 18:05:01 +0200
parents 41db2f58c753
children 56f94936df1e
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 _
2602
41db2f58c753 jp (ticket): new "get" subcommand, to retrieve ticket(s)
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
23 from sat_frontends.jp import common
41db2f58c753 jp (ticket): new "get" subcommand, to retrieve ticket(s)
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
24 from sat_frontends.jp.constants import Const as C
41db2f58c753 jp (ticket): new "get" subcommand, to retrieve ticket(s)
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
25 from functools import partial
2374
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
26 import json
2602
41db2f58c753 jp (ticket): new "get" subcommand, to retrieve ticket(s)
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
27 import os
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 __commands__ = ["Ticket"]
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
30
2396
66baa687c682 plugins tickets import, jp (ticket/import): implemented mapping:
Goffi <goffi@goffi.org>
parents: 2374
diff changeset
31 FIELDS_MAP = u'mapping'
2374
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
2602
41db2f58c753 jp (ticket): new "get" subcommand, to retrieve ticket(s)
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
34 class Get(base.CommandBase):
41db2f58c753 jp (ticket): new "get" subcommand, to retrieve ticket(s)
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
35
41db2f58c753 jp (ticket): new "get" subcommand, to retrieve ticket(s)
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
36 def __init__(self, host):
41db2f58c753 jp (ticket): new "get" subcommand, to retrieve ticket(s)
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
37 base.CommandBase.__init__(self, host, 'get', use_verbose=True,
41db2f58c753 jp (ticket): new "get" subcommand, to retrieve ticket(s)
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
38 use_pubsub=True, pubsub_flags={C.MULTI_ITEMS},
41db2f58c753 jp (ticket): new "get" subcommand, to retrieve ticket(s)
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
39 pubsub_defaults = {u'service': _(u'auto'), u'node': _(u'auto')},
41db2f58c753 jp (ticket): new "get" subcommand, to retrieve ticket(s)
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
40 use_output=C.OUTPUT_LIST_XMLUI, help=_(u'get tickets'))
41db2f58c753 jp (ticket): new "get" subcommand, to retrieve ticket(s)
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
41 self.need_loop=True
41db2f58c753 jp (ticket): new "get" subcommand, to retrieve ticket(s)
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
42
41db2f58c753 jp (ticket): new "get" subcommand, to retrieve ticket(s)
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
43 def add_parser_options(self):
41db2f58c753 jp (ticket): new "get" subcommand, to retrieve ticket(s)
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
44 pass
41db2f58c753 jp (ticket): new "get" subcommand, to retrieve ticket(s)
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
45
41db2f58c753 jp (ticket): new "get" subcommand, to retrieve ticket(s)
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
46 def ticketsGetCb(self, tickets_data):
41db2f58c753 jp (ticket): new "get" subcommand, to retrieve ticket(s)
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
47 self.output(tickets_data[0])
41db2f58c753 jp (ticket): new "get" subcommand, to retrieve ticket(s)
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
48 self.host.quit(C.EXIT_OK)
41db2f58c753 jp (ticket): new "get" subcommand, to retrieve ticket(s)
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
49
41db2f58c753 jp (ticket): new "get" subcommand, to retrieve ticket(s)
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
50 def getTickets(self):
41db2f58c753 jp (ticket): new "get" subcommand, to retrieve ticket(s)
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
51 self.host.bridge.ticketsGet(
41db2f58c753 jp (ticket): new "get" subcommand, to retrieve ticket(s)
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
52 self.args.service,
41db2f58c753 jp (ticket): new "get" subcommand, to retrieve ticket(s)
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
53 self.args.node,
41db2f58c753 jp (ticket): new "get" subcommand, to retrieve ticket(s)
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
54 self.args.max,
41db2f58c753 jp (ticket): new "get" subcommand, to retrieve ticket(s)
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
55 self.args.items,
41db2f58c753 jp (ticket): new "get" subcommand, to retrieve ticket(s)
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
56 u'',
41db2f58c753 jp (ticket): new "get" subcommand, to retrieve ticket(s)
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
57 {},
41db2f58c753 jp (ticket): new "get" subcommand, to retrieve ticket(s)
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
58 self.profile,
41db2f58c753 jp (ticket): new "get" subcommand, to retrieve ticket(s)
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
59 callback=self.ticketsGetCb,
41db2f58c753 jp (ticket): new "get" subcommand, to retrieve ticket(s)
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
60 errback=partial(self.errback,
41db2f58c753 jp (ticket): new "get" subcommand, to retrieve ticket(s)
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
61 msg=_(u"can't get tickets: {}"),
41db2f58c753 jp (ticket): new "get" subcommand, to retrieve ticket(s)
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
62 exit_code=C.EXIT_BRIDGE_ERRBACK))
41db2f58c753 jp (ticket): new "get" subcommand, to retrieve ticket(s)
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
63
41db2f58c753 jp (ticket): new "get" subcommand, to retrieve ticket(s)
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
64 def start(self):
41db2f58c753 jp (ticket): new "get" subcommand, to retrieve ticket(s)
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
65 common.URIFinder(self, os.getcwd(), 'tickets', self.getTickets, meta_map={})
41db2f58c753 jp (ticket): new "get" subcommand, to retrieve ticket(s)
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
66
41db2f58c753 jp (ticket): new "get" subcommand, to retrieve ticket(s)
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
67
2374
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
68 class Import(base.CommandAnswering):
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
69 # TODO: factorize with blog/import
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
70
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
71 def __init__(self, host):
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
72 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
73 self.need_loop=True
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
74
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
75 def add_parser_options(self):
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
76 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
77 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
78 help=_(u"importer specific options (see importer description)"))
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
79 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
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)"))
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
81 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
82 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
83 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
84 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
85 self.parser.add_argument("location", type=base.unicode_decoder, nargs='?',
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
86 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
87
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
88 def onProgressStarted(self, metadata):
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
89 self.disp(_(u'Tickets upload started'),2)
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
90
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
91 def onProgressFinished(self, metadata):
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
92 self.disp(_(u'Tickets uploaded successfully'),2)
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 def onProgressError(self, error_msg):
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
95 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
96
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
97 def error(self, failure):
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
98 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
99 self.host.quit(1)
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
100
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
101 def start(self):
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
102 if self.args.location is None:
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
103 for name in ('option', 'service', 'node'):
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
104 if getattr(self.args, name):
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
105 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
106 if self.args.importer is None:
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
107 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
108 else:
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
109 try:
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
110 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
111 except Exception as e:
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
112 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
113 self.disp(msg)
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
114 self.host.quit(1)
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
115 else:
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
116 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
117 self.host.quit()
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
118 else:
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
119 # we have a location, an import is requested
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
120 options = {key: value for key, value in self.args.option}
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
121 fields_map = dict(self.args.map)
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
122 if fields_map:
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
123 if FIELDS_MAP in options:
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
124 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
125 options[FIELDS_MAP] = json.dumps(fields_map)
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
126 def gotId(id_):
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
127 self.progress_id = id_
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
128 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
129 callback=gotId, errback=self.error)
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
130
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
131
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
132 class Ticket(base.CommandBase):
2602
41db2f58c753 jp (ticket): new "get" subcommand, to retrieve ticket(s)
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
133 subcommands = (Get, Import)
2374
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
134
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
135 def __init__(self, host):
686f84ebb670 jp (ticket): ticket command first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
136 super(Ticket, self).__init__(host, 'ticket', use_profile=False, help=_('tickets handling'))