view frontends/src/jp/cmd_blog.py @ 1870:c25077c87b1d

base (blog/edit): added a way to block publishing by adding "publish": "false" in metadata file
author Goffi <goffi@goffi.org>
date Wed, 02 Mar 2016 19:46:01 +0100
parents 28b29381db75
children 64a40adccba4
line wrap: on
line source

#! /usr/bin/python
# -*- coding: utf-8 -*-

# jp: a SàT command line tool
# Copyright (C) 2009-2016 Jérôme Poisson (goffi@goffi.org)

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.

# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


import base
from sat.core.i18n import _
from sat.core.constants import Const as C
from sat.tools import config
import json
import os.path
import os
import time
import tempfile
import subprocess
from sat.tools import common

__commands__ = ["Blog"]

SYNTAX_EXT = { '': 'txt', # used when the syntax is not found
               "XHTML": "xhtml",
               "markdown": "md"
               }
CONF_SYNTAX_EXT = 'syntax_ext_dict'
BLOG_TMP_DIR="blog"
# key to remove from metadata tmp file if they exist
KEY_TO_REMOVE_METADATA = ('id','content', 'content_rich', 'content_xhtml', 'comments_node', 'comments_service')

URL_REDIRECT_PREFIX = 'url_redirect_'


class Edit(base.CommandBase):

    def __init__(self, host):
        super(Edit, self).__init__(host, 'edit', use_verbose=True, help=_(u'edit an existing or new blog post'))

    def add_parser_options(self):
        self.parser.add_argument("item", type=base.unicode_decoder, nargs='?', default=u'new', help=_(u"URL of the item to edit, or keyword"))
        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"))

    def getTmpFile(self, sat_conf, tmp_suff):
        """Create a temporary file to received blog item body

        @param sat_conf(ConfigParser.ConfigParser): instance opened on sat configuration
        @param tmp_suff (str): suffix to use for the filename
        @return (tuple(file, str)): opened (w+b) file object and file path
        """
        local_dir = config.getConfig(sat_conf, '', 'local_dir', Exception)
        tmp_dir = os.path.join(local_dir, BLOG_TMP_DIR)
        if not os.path.exists(tmp_dir):
            try:
                os.makedirs(tmp_dir)
            except OSError as e:
                self.disp(u"Can't create {path} directory: {reason}".format(
                    path=tmp_dir, reason=e), error=True)
                self.host.quit(1)
        try:
            fd, path = tempfile.mkstemp(suffix=tmp_suff,
                prefix=time.strftime('blog_%Y-%m-%d_%H:%M:%S_'),
                dir=tmp_dir, text=True)
            return os.fdopen(fd, 'w+b'), path
        except OSError as e:
            self.disp(u"Can't create temporary file: {reason}".format(reason=e), error=True)
            self.host.quit(1)

    def buildMetadataFile(self, tmp_file_path, mb_data=None):
        """Build a metadata file using json

        The file is named after tmp_file_path, with extension replaced by _metadata.json
        @param tmp_file_path(str): path to the temporary file which will contain the body
        @param mb_data(dict, None): microblog metadata (for existing items)
        @return (tuple[dict, str]): merged metadata put originaly in metadata file
            and path to temporary metadata file
        """
        # we first construct metadata from edited item ones and CLI argumments
        mb_data = {} if mb_data is None else mb_data.copy()
        for key in KEY_TO_REMOVE_METADATA:
            try:
                del mb_data[key]
            except KeyError:
                pass
        mb_data['allow_comments'] = C.boolConst(not self.args.no_comment)
        if self.args.tag:
            common.iter2dict('tag', self.args.tag, mb_data)
        if self.args.title is not None:
            mb_data['title'] = self.args.title

        # the we create the file and write metadata there, as JSON dict
        meta_file_path = os.path.splitext(tmp_file_path)[0] + '_metadata.json'
        # XXX: if we port jp one day on Windows, O_BINARY may need to be added here
        if os.path.exists(meta_file_path):
            self.disp(u"metadata file {} already exists, this should not happen! Cancelling...", error=True)
        with os.fdopen(os.open(meta_file_path, os.O_RDWR | os.O_CREAT ,0o600), 'w+b') as f:
            # we need to use an intermediate unicode buffer to write to the file unicode without escaping characters
            unicode_dump = json.dumps(mb_data, ensure_ascii=False, indent=4, separators=(',', ': '), sort_keys=True)
            f.write(unicode_dump.encode('utf-8'))

        return mb_data, meta_file_path

    def edit(self, sat_conf, tmp_file_path, tmp_file_obj, mb_data=None):
        """Edit the file contening the content using editor, and publish it"""
        item_ori_mb_data = mb_data
        # we first create metadata file
        meta_ori, meta_file_path = self.buildMetadataFile(tmp_file_path, item_ori_mb_data)

        # then we calculate hashes to check for modifications
        import hashlib
        tmp_file_obj.seek(0)
        tmp_ori_hash = hashlib.sha1(tmp_file_obj.read()).digest()
        tmp_file_obj.close()

        # then we launch editor
        editor = config.getConfig(sat_conf, 'jp', 'editor') or os.getenv('EDITOR', 'vi')
        editor_exit = subprocess.call([editor, tmp_file_path])

        # we send the file if edition was a success
        if editor_exit != 0:
            self.disp(u"Editor exited with an error code, so temporary file has not be deleted, and blog item is not published.\nTou can find temporary file at {path}".format(
                path=tmp_file_path), error=True)
        else:
            try:
                with open(tmp_file_path, 'rb') as f:
                    content = f.read()
                with open(meta_file_path, 'rb') as f:
                    mb_data = json.load(f)
            except OSError:
                self.disp(u"Can write files at {file_path} and/or {meta_path}, have they been deleted? Cancelling edition".format(
                    file_path=tmp_file_path, meta_path=meta_file_path), error=True)
                self.host.quit(1)
            except ValueError:
                self.disp(u"Can't parse metadata, please check it is correct JSON format. Cancelling edition.\n" +
                    "You can find tmp file at {file_path} and temporary meta file at {meta_path}.".format(
                    file_path=tmp_file_path, meta_path=meta_file_path), error=True)
                self.host.quit(1)

            if not C.bool(mb_data.get('publish', "true")):
                self.disp(u'Publication blocked by "publish" key in metadata, cancelling edition.\n\n' +
                    "temporary file path:\t{file_path}\nmetadata file path:\t{meta_path}".format(
                    file_path=tmp_file_path, meta_path=meta_file_path), error=True)
                self.host.quit(0)

            if len(content) == 0:
                self.disp(u"Content is empty, cancelling the blog edition")

            # time to re-check the hash
            elif (tmp_ori_hash == hashlib.sha1(content).digest() and
                  meta_ori == mb_data):
                self.disp(u"The content has not been modified, cancelling the blog edition")

            else:
                # we can now send the blog
                mb_data['content_rich'] =  content.decode('utf-8')

                if item_ori_mb_data is not None:
                    mb_data['id'] = item_ori_mb_data['id']

                try:
                    self.host.bridge.mbSend('', '', mb_data, self.profile)
                except Exception as e:
                    self.disp(u"Error while sending your blog, the temporary files have been kept at {file_path} and {meta_path}: {reason}".format(
                        file_path=tmp_file_path, meta_path=meta_file_path, reason=e), error=True)
                    self.host.quit(1)

            os.unlink(tmp_file_path)
            os.unlink(meta_file_path)

    def start(self):
        # we get current syntax to determine file extension
        current_syntax = self.host.bridge.getParamA("Syntax", "Composition", "value", self.profile)
        self.disp(u"Current syntax: {}".format(current_syntax), 1)
        sat_conf = config.parseMainConf()
        # if there are user defined extension, we use them
        SYNTAX_EXT.update(config.getConfig(sat_conf, 'jp', CONF_SYNTAX_EXT, {}))

        # we now create a temporary file
        tmp_suff = '.' + SYNTAX_EXT.get(current_syntax, SYNTAX_EXT[''])
        tmp_file_obj, tmp_file_path = self.getTmpFile(sat_conf, tmp_suff)

        item_lower = self.args.item.lower()
        if item_lower == 'new':
            self.disp(u'Editing a new blog item', 2)
            self.edit(sat_conf, tmp_file_path, tmp_file_obj)
        elif item_lower == 'last':
            self.disp(u'Editing last published item', 2)
            try:
                mb_data = self.host.bridge.mbGet('', '', 1, [], {}, self.profile)[0][0]
            except Exception as e:
                self.disp(u"Error while retrieving last comment: {}".format(e))
                self.host.quit(1)

            content = mb_data['content_xhtml']
            if current_syntax != 'XHTML':
                content = self.host.bridge.syntaxConvert(content, 'XHTML', current_syntax, False, self.profile)
            tmp_file_obj.write(content.encode('utf-8'))
            tmp_file_obj.seek(0)
            self.edit(sat_conf, tmp_file_path, tmp_file_obj, mb_data=mb_data)


class Import(base.CommandAnswering):
    def __init__(self, host):
        super(Import, self).__init__(host, 'import', use_progress=True, help=_(u'import an external blog'))
        self.need_loop=True

    def add_parser_options(self):
        self.parser.add_argument("importer", type=base.unicode_decoder, nargs='?', help=_(u"importer name, nothing to display importers list"))
        self.parser.add_argument('--host', type=base.unicode_decoder, help=_(u"original blog host"))
        self.parser.add_argument('--no-images-upload', action='store_true', help=_(u"do *NOT* upload images (default: do upload images)"))
        self.parser.add_argument('--upload-ignore-host', help=_(u"do not upload images from this host (default: upload all images)"))
        self.parser.add_argument("--ignore-tls-errors", action="store_true", help=_("ignore invalide TLS certificate for uploads"))
        self.parser.add_argument('-o', '--option', action='append', nargs=2, default=[], metavar=(u'NAME', u'VALUE'),
            help=_(u"importer specific options (see importer description)"))
        self.parser.add_argument('--service', type=base.unicode_decoder, default=u'', metavar=u'PUBSUB_SERVICE',
            help=_(u"PubSub service where the items must be uploaded (default: server)"))
        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 start(self):
        if self.args.location is None:
            for name in ('option', 'service', 'no_images_upload'):
                if getattr(self.args, name):
                    self.parser.error(_(u"{name} argument can't be used without location argument").format(name=name))
            if self.args.importer is None:
                print u'\n'.join([u'{}: {}'.format(name, desc) for name, desc in self.host.bridge.blogImportList()])
            else:
                try:
                    short_desc, long_desc = self.host.bridge.blogImportDesc(self.args.importer)
                except Exception as e:
                    msg = [l for l in unicode(e).split('\n') if l][-1] # we only keep the last line
                    print msg
                    self.host.quit(1)
                else:
                    print u"{name}: {short_desc}\n\n{long_desc}".format(name=self.args.importer, short_desc=short_desc, long_desc=long_desc)
            self.host.quit()
        else:
            # we have a location, an import is requested
            options = {key: value for key, value in self.args.option}
            if self.args.host:
                options['host'] = self.args.host
            if self.args.ignore_tls_errors:
                options['ignore_tls_errors'] = C.BOOL_TRUE
            if self.args.no_images_upload:
                options['upload_images'] = C.BOOL_FALSE
                if self.args.upload_ignore_host:
                    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
            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):
    subcommands = (Edit, Import)

    def __init__(self, host):
        super(Blog, self).__init__(host, 'blog', use_profile=False, help=_('blog/microblog management'))