# HG changeset patch # User Goffi # Date 1513033964 -3600 # Node ID 4841ad6a5db4ad362586354772558b5ea577567a # Parent 49aa1e3a90b8d9119720803abafbdf25db733880 jp (blog): added "set" command to publish content from stdin without editing diff -r 49aa1e3a90b8 -r 4841ad6a5db4 frontends/src/jp/cmd_blog.py --- a/frontends/src/jp/cmd_blog.py Tue Dec 12 00:11:27 2017 +0100 +++ b/frontends/src/jp/cmd_blog.py Tue Dec 12 00:12:44 2017 +0100 @@ -27,6 +27,7 @@ from sat.tools.common import uri from sat.tools import config from ConfigParser import NoSectionError, NoOptionError +from functools import partial import json import sys import os.path @@ -34,6 +35,7 @@ import time import tempfile import subprocess +import codecs from sat.tools.common import data_format __commands__ = ["Blog"] @@ -101,6 +103,72 @@ return self.host.bridge.getParamA("Syntax", "Composition", "value", self.profile) +class BlogPublishCommon(object): + """handle common option for publising commands (Set and Edit)""" + + def add_parser_options(self): + self.addServiceNodeArgs() + self.parser.add_argument("-T", '--title', type=base.unicode_decoder, help=_(u"title of the item")) + self.parser.add_argument("-t", '--tag', type=base.unicode_decoder, action='append', help=_(u"tag (category) of your item")) + self.parser.add_argument("-C", "--comments", action='store_true', help=_(u"enable comments")) + self.parser.add_argument("-S", '--syntax', type=base.unicode_decoder, help=_(u"syntax to use (default: get profile's default syntax)")) + + def setMbDataContent(self, content, mb_data): + if self.args.syntax is None: + # default syntax has been used + mb_data['content_rich'] = content + elif self.current_syntax == SYNTAX_XHTML: + mb_data['content_xhtml'] = content + else: + mb_data['content_xhtml'] = self.host.bridge.syntaxConvert(content, self.current_syntax, SYNTAX_XHTML, False, self.profile) + + def setMbDataFromArgs(self, mb_data): + """set microblog metadata according to command line options + + if metadata already exist, it will be overwritten + """ + mb_data['allow_comments'] = C.boolConst(self.args.comments) + if self.args.tag: + data_format.iter2dict('tag', self.args.tag, mb_data, check_conflict=False) + if self.args.title is not None: + mb_data['title'] = self.args.title + + +class Set(base.CommandBase, BlogCommon, BlogPublishCommon): + + def __init__(self, host): + base.CommandBase.__init__(self, host, 'set', help=_(u'publish a new blog item or update an existing one')) + BlogCommon.__init__(self, self.host) + BlogPublishCommon.__init__(self) + self.need_loop=True + + def add_parser_options(self): + BlogPublishCommon.add_parser_options(self) + self.parser.add_argument("item", type=base.unicode_decoder, nargs='?', default=None, help=_(u"id of the item to publish")) + + def mbSendCb(self): + self.disp(u"Item published") + self.host.quit(C.EXIT_OK) + + def start(self): + common.checkURI(self.args) + self.pubsub_item = self.args.item + mb_data = {} + self.setMbDataFromArgs(mb_data) + content = codecs.getreader('utf-8')(sys.stdin).read() + self.setMbDataContent(content, mb_data) + + self.host.bridge.mbSend( + self.args.service, + self.args.node, + mb_data, + self.profile, + callback=self.exitCb, + errback=partial(self.errback, + msg=_(u"can't send item: {}"), + exit_code=C.EXIT_BRIDGE_ERRBACK)) + + class Get(base.CommandBase, BlogCommon): TEMPLATE = u"blog/articles.html" @@ -283,21 +351,18 @@ errback=self.mbGetEb) -class Edit(base.CommandBase, BlogCommon, common.BaseEdit): +class Edit(base.CommandBase, BlogCommon, BlogPublishCommon, common.BaseEdit): def __init__(self, host): base.CommandBase.__init__(self, host, 'edit', use_verbose=True, help=_(u'edit an existing or new blog post')) BlogCommon.__init__(self, self.host) + BlogPublishCommon.__init__(self) common.BaseEdit.__init__(self, self.host, BLOG_TMP_DIR, use_metadata=True) def add_parser_options(self): - self.addServiceNodeArgs() - self.parser.add_argument("item", type=base.unicode_decoder, nargs='?', default=u'new', help=_(u"URL of the item to edit, or keyword")) + BlogPublishCommon.add_parser_options(self) self.parser.add_argument("-P", "--preview", action="store_true", help=_(u"launch a blog preview in parallel")) - self.parser.add_argument("-T", '--title', type=base.unicode_decoder, help=_(u"title of the item")) - self.parser.add_argument("-t", '--tag', type=base.unicode_decoder, action='append', help=_(u"tag (category) of your item")) - self.parser.add_argument("--no-comment", action='store_true', help=_(u"disable comments")) - self.parser.add_argument("-S", '--syntax', type=base.unicode_decoder, help=_(u"syntax to use (default: get profile's default syntax)")) + self.parser.add_argument("item", type=base.unicode_decoder, nargs='?', default=u'new', help=_(u"URL of the item to edit, or keyword")) common.BaseEdit.add_parser_options(self) def buildMetadataFile(self, content_file_path, mb_data=None): @@ -331,11 +396,7 @@ except KeyError: pass # and override metadata with command-line arguments - mb_data['allow_comments'] = C.boolConst(not self.args.no_comment) - if self.args.tag: - data_format.iter2dict('tag', self.args.tag, mb_data, check_conflict=False) - if self.args.title is not None: - mb_data['title'] = self.args.title + self.setMbDataFromArgs(mb_data) # then we create the file and write metadata there, as JSON dict # XXX: if we port jp one day on Windows, O_BINARY may need to be added here @@ -364,13 +425,7 @@ self.runEditor("blog_editor_args", content_file_path, content_file_obj, meta_file_path=meta_file_path, meta_ori=meta_ori) def publish(self, content, mb_data): - if self.args.syntax is None: - # default syntax has been used - mb_data['content_rich'] = content - elif self.current_syntax == SYNTAX_XHTML: - mb_data['content_xhtml'] = content - else: - mb_data['content_xhtml'] = self.host.bridge.syntaxConvert(content, self.current_syntax, SYNTAX_XHTML, False, self.profile) + self.setMbDataContent(content, mb_data) if self.pubsub_item is not None: mb_data['id'] = self.pubsub_item @@ -378,6 +433,7 @@ self.host.bridge.mbSend(self.pubsub_service, self.pubsub_node, mb_data, self.profile) self.disp(u"Blog item published") + def getTmpSuff(self): # we get current syntax to determine file extension if self.current_syntax is None: @@ -648,7 +704,7 @@ class Blog(base.CommandBase): - subcommands = (Get, Edit, Preview, Import) + subcommands = (Set, Get, Edit, Preview, Import) def __init__(self, host): super(Blog, self).__init__(host, 'blog', use_profile=False, help=_('blog/microblog management'))