comparison frontends/src/jp/cmd_blog.py @ 1827:74014a9cc203

jp: added a blog/import command to handle new blog import plugin
author Goffi <goffi@goffi.org>
date Fri, 22 Jan 2016 20:24:17 +0100
parents
children 6209de5e3e25
comparison
equal deleted inserted replaced
1826:d80ccf4bf201 1827:74014a9cc203
1 #! /usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # jp: a SàT command line tool
5 # Copyright (C) 2009-2016 Jérôme Poisson (goffi@goffi.org)
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 base
22 from sat.core.i18n import _
23 from sat.core.constants import Const as C
24
25 __commands__ = ["Blog"]
26
27
28 class Import(base.CommandBase):
29 def __init__(self, host):
30 super(Import, self).__init__(host, 'import', help=_(u'import an external blog'))
31
32 def add_parser_options(self):
33 self.parser.add_argument("importer", type=base.unicode_decoder, nargs='?', help=_(u"importer name, nothing to display importers list"))
34 self.parser.add_argument('--host', type=base.unicode_decoder, help=_(u"original blog host"))
35 self.parser.add_argument('--no-images-upload', action='store_true', help=_(u"do *NOT* upload images (default: do upload images)"))
36 self.parser.add_argument('--upload-ignore-host', help=_(u"do not upload images from this host (default: upload all images)"))
37 self.parser.add_argument("--ignore-tls-errors", action="store_true", help=_("ignore invalide TLS certificate for uploads"))
38 self.parser.add_argument('-o', '--option', action='append', nargs=2, default={}, metavar=(u'NAME', u'VALUE'),
39 help=_(u"importer specific options (see importer description)"))
40 self.parser.add_argument('--service', type=base.unicode_decoder, default=u'', metavar=u'PUBSUB_SERVICE',
41 help=_(u"PubSub service where the items must be uploaded (default: server)"))
42 self.parser.add_argument("location", type=base.unicode_decoder, nargs='?',
43 help=_(u"importer data location (see importer description), nothing to show importer description"))
44
45 def connected(self):
46 """Send files to jabber contact"""
47 super(Import, self).connected()
48 if self.args.location is None:
49 for name in ('option', 'service', 'no_images_upload'):
50 if getattr(self.args, name):
51 self.parser.error(_(u"{name} argument can't be used without location argument").format(name=name))
52 if self.args.importer is None:
53 print u''.join([u'{}: {}'.format(name, desc) for name, desc in self.host.bridge.blogImportList()])
54 else:
55 try:
56 short_desc, long_desc = self.host.bridge.blogImportDesc(self.args.importer)
57 except Exception as e:
58 msg = [l for l in unicode(e).split('\n') if l][-1] # we only keep the last line
59 print msg
60 self.host.quit(1)
61 else:
62 print u"{name}: {short_desc}\n\n{long_desc}".format(name=self.args.importer, short_desc=short_desc, long_desc=long_desc)
63 self.host.quit()
64 else:
65 # we have a location, an import is requested
66 options = dict(self.args.option)
67 if self.args.host:
68 options['host'] = self.args.host
69 if self.args.ignore_tls_errors:
70 options['ignore_tls_errors'] = C.BOOL_TRUE
71 if self.args.no_images_upload:
72 options['upload_images'] = C.BOOL_FALSE
73 if self.args.upload_ignore_host:
74 self.parser.error(u"upload-ignore-host option can't be used when no-images-upload is set")
75 elif self.args.upload_ignore_host:
76 options['upload_ignore_host'] = self.args.upload_ignore_host
77 try:
78 self.host.bridge.blogImport(self.args.importer, self.args.location, options, self.args.service, self.profile)
79 except Exception as e:
80 print e
81 self.host.quit(1)
82
83
84 class Blog(base.CommandBase):
85 subcommands = (Import,)
86
87 def __init__(self, host):
88 super(Blog, self).__init__(host, 'blog', use_profile=False, help=_('blog/microblog management'))