# HG changeset patch # User Goffi # Date 1453656241 -3600 # Node ID 6209de5e3e25d13599713a296ca1ed9f856be5a6 # Parent a123e881f9e5c215442035d55c384d6af216a73c jp (blog): import now show progression + value to add to sat.conf for URLs redirection diff -r a123e881f9e5 -r 6209de5e3e25 frontends/src/jp/cmd_blog.py --- a/frontends/src/jp/cmd_blog.py Sat Jan 23 20:06:01 2016 +0100 +++ b/frontends/src/jp/cmd_blog.py Sun Jan 24 18:24:01 2016 +0100 @@ -21,13 +21,15 @@ import base from sat.core.i18n import _ from sat.core.constants import Const as C +import json +URL_REDIRECT_PREFIX = 'url_redirect_' __commands__ = ["Blog"] class Import(base.CommandBase): def __init__(self, host): - super(Import, self).__init__(host, 'import', help=_(u'import an external blog')) + super(Import, self).__init__(host, 'import', use_progress=True, help=_(u'import an external blog')) def add_parser_options(self): self.parser.add_argument("importer", type=base.unicode_decoder, nargs='?', help=_(u"importer name, nothing to display importers list")) @@ -42,9 +44,34 @@ self.parser.add_argument("location", type=base.unicode_decoder, nargs='?', help=_(u"importer data location (see importer description), nothing to show importer description")) + def onProgressStarted(self, metadata): + self.disp(_(u'Blog upload started'),2) + + def onProgressFinished(self, metadata): + self.disp(_(u'Blog uploaded successfully'),2) + redirections = {k[len(URL_REDIRECT_PREFIX):]:v for k,v in metadata.iteritems() + if k.startswith(URL_REDIRECT_PREFIX)} + if redirections: + conf = u'\n'.join([ + u'url_redirections_profile = {}'.format(self.profile), + u"url_redirections_dict = {}".format( + # we need to add ' ' before each new line and to double each '%' for ConfigParser + u'\n '.join(json.dumps(redirections, indent=1, separators=(',',': ')).replace(u'%', u'%%').split(u'\n'))), + ]) + self.disp(_(u'\nTo redirect old URLs to new ones, put the following lines in your sat.conf file, in [libervia] section:\n\n{conf}'.format(conf=conf))) + + def onProgressError(self, error_msg): + self.disp(_(u'Error while uploading blog: {}').format(error_msg),error=True) + + def error(self, failure): + self.disp(_("Error while trying to upload a blog: {reason}").format(reason=failure), error=True) + self.host.quit(1) + def connected(self): """Send files to jabber contact""" + self.need_loop=True super(Import, self).connected() + if self.args.location is None: for name in ('option', 'service', 'no_images_upload'): if getattr(self.args, name): @@ -74,11 +101,10 @@ self.parser.error(u"upload-ignore-host option can't be used when no-images-upload is set") elif self.args.upload_ignore_host: options['upload_ignore_host'] = self.args.upload_ignore_host - try: - self.host.bridge.blogImport(self.args.importer, self.args.location, options, self.args.service, self.profile) - except Exception as e: - print e - self.host.quit(1) + def gotId(id_): + self.progress_id = id_ + self.host.bridge.blogImport(self.args.importer, self.args.location, options, self.args.service, self.profile, + callback=gotId, errback=self.error) class Blog(base.CommandBase):