Mercurial > libervia-backend
annotate frontends/src/jp/cmd_blog.py @ 1866:397ef87958b9
jp (blog): edit command, first draft:
- can edit a new or existing item, by default edit a new item
- item are selected with a shortcut, for now only "new" and "last" are handled. URI handling is planed, specially xmpp: URIs
- file are edited using EDITOR env variable, or editor in [jp] section in sat.conf
- current syntax is used, file extension is choosed according to syntax, to make syntax coloration and other goodies in editor available
- if file is empty or not modified, nothing is published
- for now, title, tags and commend desactivation are handled with optional arguments, but other are planed, and a metadata system should come soon
author | Goffi <goffi@goffi.org> |
---|---|
date | Tue, 01 Mar 2016 01:54:21 +0100 |
parents | 96ba685162f6 |
children | 28b29381db75 |
rev | line source |
---|---|
1827
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1 #! /usr/bin/python |
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
2 # -*- coding: utf-8 -*- |
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
3 |
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
4 # jp: a SàT command line tool |
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
5 # Copyright (C) 2009-2016 Jérôme Poisson (goffi@goffi.org) |
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
6 |
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
7 # This program is free software: you can redistribute it and/or modify |
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
8 # it under the terms of the GNU Affero General Public License as published by |
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
9 # the Free Software Foundation, either version 3 of the License, or |
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
10 # (at your option) any later version. |
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
11 |
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
12 # This program is distributed in the hope that it will be useful, |
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
15 # GNU Affero General Public License for more details. |
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
16 |
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
17 # You should have received a copy of the GNU Affero General Public License |
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
19 |
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
20 |
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
21 import base |
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
22 from sat.core.i18n import _ |
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
23 from sat.core.constants import Const as C |
1866
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
24 from sat.tools import config |
1834
6209de5e3e25
jp (blog): import now show progression + value to add to sat.conf for URLs redirection
Goffi <goffi@goffi.org>
parents:
1827
diff
changeset
|
25 import json |
1866
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
26 import os.path |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
27 import os |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
28 import time |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
29 import tempfile |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
30 import subprocess |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
31 from sat.tools import common |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
32 |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
33 __commands__ = ["Blog"] |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
34 |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
35 SYNTAX_EXT = { '': 'txt', # used when the syntax is not found |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
36 "XHTML": "xhtml", |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
37 "markdown": "md" |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
38 } |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
39 CONF_SYNTAX_EXT = 'syntax_ext_dict' |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
40 BLOG_TMP_DIR="blog" |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
41 |
1834
6209de5e3e25
jp (blog): import now show progression + value to add to sat.conf for URLs redirection
Goffi <goffi@goffi.org>
parents:
1827
diff
changeset
|
42 URL_REDIRECT_PREFIX = 'url_redirect_' |
1827
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
43 |
1866
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
44 |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
45 class Edit(base.CommandBase): |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
46 |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
47 def __init__(self, host): |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
48 super(Edit, self).__init__(host, 'edit', use_verbose=True, help=_(u'edit an existing or new blog post')) |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
49 |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
50 def add_parser_options(self): |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
51 self.parser.add_argument("item", type=base.unicode_decoder, nargs='?', default=u'new', help=_(u"URL of the item to edit, or keyword")) |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
52 self.parser.add_argument("-T", '--title', type=base.unicode_decoder, help=_(u"Title of the item")) |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
53 self.parser.add_argument("-t", '--tag', type=base.unicode_decoder, action='append', help=_(u"tag (category) of your item")) |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
54 self.parser.add_argument("--no-comment", action='store_true', help=_(u"disable comments")) |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
55 |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
56 def getTmpFile(self, sat_conf, tmp_suff): |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
57 local_dir = config.getConfig(sat_conf, '', 'local_dir', Exception) |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
58 tmp_dir = os.path.join(local_dir, BLOG_TMP_DIR) |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
59 if not os.path.exists(tmp_dir): |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
60 try: |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
61 os.makedirs(tmp_dir) |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
62 except OSError as e: |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
63 self.disp(u"Can't create {path} directory: {reason}".format( |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
64 path=tmp_dir, reason=e), error=True) |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
65 self.host.quit(1) |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
66 |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
67 try: |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
68 return tempfile.mkstemp(suffix=tmp_suff, |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
69 prefix=time.strftime('blog_%Y-%m-%d_%H:%M:%S_'), |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
70 dir=tmp_dir, text=True) |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
71 except OSError as e: |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
72 self.disp(u"Can't create temporary file: {reason}".format(reason=e), error=True) |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
73 self.host.quit(1) |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
74 |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
75 def edit(self, sat_conf, tmp_file, tmp_file_obj, item_id=None): |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
76 """Edit the file contening the content using editor, and publish it""" |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
77 # we first calculate hash to check for modifications |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
78 import hashlib |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
79 tmp_file_obj.seek(0) |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
80 ori_hash = hashlib.sha1(tmp_file_obj.read()).digest() |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
81 tmp_file_obj.close() |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
82 |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
83 # the we launch editor |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
84 editor = config.getConfig(sat_conf, 'jp', 'editor') or os.getenv('EDITOR', 'vi') |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
85 editor_exit = subprocess.call([editor, tmp_file]) |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
86 |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
87 # we send the file if edition was a success |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
88 if editor_exit != 0: |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
89 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( |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
90 path=tmp_file), error=True) |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
91 else: |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
92 with open(tmp_file, 'rb') as f: |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
93 content = f.read() |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
94 |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
95 if len(content) == 0: |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
96 self.disp(u"Content is empty, cancelling the blog edition") |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
97 |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
98 # time to re-check the hash |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
99 elif ori_hash == hashlib.sha1(content).digest(): |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
100 self.disp(u"The content has not been modified, cancelling the blog edition") |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
101 |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
102 else: |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
103 # we can now send the blog |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
104 mb_data = { |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
105 'content_rich': content.decode('utf-8'), |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
106 'allow_comments': C.boolConst(not self.args.no_comment), |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
107 } |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
108 if item_id: |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
109 mb_data['id'] = item_id |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
110 if self.args.tag: |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
111 common.iter2dict('tag', self.args.tag, mb_data) |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
112 |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
113 if self.args.title is not None: |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
114 mb_data['title'] = self.args.title |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
115 try: |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
116 self.host.bridge.mbSend('', '', mb_data, self.profile) |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
117 except Exception as e: |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
118 self.disp(u"Error while sending your blog, the temporary file has been kept at {path}: {reason}".format( |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
119 path=tmp_file, reason=e), error=True) |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
120 self.host.quit(1) |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
121 |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
122 os.unlink(tmp_file) |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
123 |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
124 def start(self): |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
125 # we get current syntax to determine file extension |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
126 current_syntax = self.host.bridge.getParamA("Syntax", "Composition", "value", self.profile) |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
127 self.disp(u"Current syntax: {}".format(current_syntax), 1) |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
128 sat_conf = config.parseMainConf() |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
129 # if there are user defined extension, we use them |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
130 SYNTAX_EXT.update(config.getConfig(sat_conf, 'jp', CONF_SYNTAX_EXT, {})) |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
131 |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
132 # we now create a temporary file |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
133 tmp_suff = '.' + SYNTAX_EXT.get(current_syntax, SYNTAX_EXT['']) |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
134 fd, tmp_file = self.getTmpFile(sat_conf, tmp_suff) |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
135 |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
136 item_lower = self.args.item.lower() |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
137 if item_lower == 'new': |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
138 self.disp(u'Editing a new blog item', 2) |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
139 self.edit(sat_conf, tmp_file, os.fdopen(fd)) |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
140 elif item_lower == 'last': |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
141 self.disp(u'Editing last published item', 2) |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
142 try: |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
143 mb_data = self.host.bridge.mbGet('', '', 1, [], {}, self.profile)[0][0] |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
144 except Exception as e: |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
145 self.disp(u"Error while retrieving last comment: {}".format(e)) |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
146 self.host.quit(1) |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
147 |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
148 content = mb_data['content_xhtml'] |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
149 if current_syntax != 'XHTML': |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
150 content = self.host.bridge.syntaxConvert(content, 'XHTML', current_syntax, False, self.profile) |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
151 f = os.fdopen(fd, 'w+b') |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
152 f.write(content.encode('utf-8')) |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
153 f.seek(0) |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
154 self.edit(sat_conf, tmp_file, f, mb_data['id']) |
1827
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
155 |
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
156 |
1840
9eabf7fadfdd
frontends (jp): onActionNew displays xmlui message if no meta_type is defined and xmlui is defined
souliane <souliane@mailoo.org>
parents:
1839
diff
changeset
|
157 class Import(base.CommandAnswering): |
1827
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
158 def __init__(self, host): |
1834
6209de5e3e25
jp (blog): import now show progression + value to add to sat.conf for URLs redirection
Goffi <goffi@goffi.org>
parents:
1827
diff
changeset
|
159 super(Import, self).__init__(host, 'import', use_progress=True, help=_(u'import an external blog')) |
1864
96ba685162f6
jp: all commands now use the new start method and set need_loop in __init__ when needed
Goffi <goffi@goffi.org>
parents:
1840
diff
changeset
|
160 self.need_loop=True |
1827
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
161 |
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
162 def add_parser_options(self): |
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
163 self.parser.add_argument("importer", type=base.unicode_decoder, nargs='?', help=_(u"importer name, nothing to display importers list")) |
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
164 self.parser.add_argument('--host', type=base.unicode_decoder, help=_(u"original blog host")) |
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
165 self.parser.add_argument('--no-images-upload', action='store_true', help=_(u"do *NOT* upload images (default: do upload images)")) |
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
166 self.parser.add_argument('--upload-ignore-host', help=_(u"do not upload images from this host (default: upload all images)")) |
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
167 self.parser.add_argument("--ignore-tls-errors", action="store_true", help=_("ignore invalide TLS certificate for uploads")) |
1839
cdecf553e051
frontends (jp/blog), plugin blog_import: fixes:
souliane <souliane@mailoo.org>
parents:
1834
diff
changeset
|
168 self.parser.add_argument('-o', '--option', action='append', nargs=2, default=[], metavar=(u'NAME', u'VALUE'), |
1827
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
169 help=_(u"importer specific options (see importer description)")) |
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
170 self.parser.add_argument('--service', type=base.unicode_decoder, default=u'', metavar=u'PUBSUB_SERVICE', |
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
171 help=_(u"PubSub service where the items must be uploaded (default: server)")) |
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
172 self.parser.add_argument("location", type=base.unicode_decoder, nargs='?', |
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
173 help=_(u"importer data location (see importer description), nothing to show importer description")) |
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
174 |
1834
6209de5e3e25
jp (blog): import now show progression + value to add to sat.conf for URLs redirection
Goffi <goffi@goffi.org>
parents:
1827
diff
changeset
|
175 def onProgressStarted(self, metadata): |
6209de5e3e25
jp (blog): import now show progression + value to add to sat.conf for URLs redirection
Goffi <goffi@goffi.org>
parents:
1827
diff
changeset
|
176 self.disp(_(u'Blog upload started'),2) |
6209de5e3e25
jp (blog): import now show progression + value to add to sat.conf for URLs redirection
Goffi <goffi@goffi.org>
parents:
1827
diff
changeset
|
177 |
6209de5e3e25
jp (blog): import now show progression + value to add to sat.conf for URLs redirection
Goffi <goffi@goffi.org>
parents:
1827
diff
changeset
|
178 def onProgressFinished(self, metadata): |
6209de5e3e25
jp (blog): import now show progression + value to add to sat.conf for URLs redirection
Goffi <goffi@goffi.org>
parents:
1827
diff
changeset
|
179 self.disp(_(u'Blog uploaded successfully'),2) |
6209de5e3e25
jp (blog): import now show progression + value to add to sat.conf for URLs redirection
Goffi <goffi@goffi.org>
parents:
1827
diff
changeset
|
180 redirections = {k[len(URL_REDIRECT_PREFIX):]:v for k,v in metadata.iteritems() |
6209de5e3e25
jp (blog): import now show progression + value to add to sat.conf for URLs redirection
Goffi <goffi@goffi.org>
parents:
1827
diff
changeset
|
181 if k.startswith(URL_REDIRECT_PREFIX)} |
6209de5e3e25
jp (blog): import now show progression + value to add to sat.conf for URLs redirection
Goffi <goffi@goffi.org>
parents:
1827
diff
changeset
|
182 if redirections: |
6209de5e3e25
jp (blog): import now show progression + value to add to sat.conf for URLs redirection
Goffi <goffi@goffi.org>
parents:
1827
diff
changeset
|
183 conf = u'\n'.join([ |
6209de5e3e25
jp (blog): import now show progression + value to add to sat.conf for URLs redirection
Goffi <goffi@goffi.org>
parents:
1827
diff
changeset
|
184 u'url_redirections_profile = {}'.format(self.profile), |
6209de5e3e25
jp (blog): import now show progression + value to add to sat.conf for URLs redirection
Goffi <goffi@goffi.org>
parents:
1827
diff
changeset
|
185 u"url_redirections_dict = {}".format( |
6209de5e3e25
jp (blog): import now show progression + value to add to sat.conf for URLs redirection
Goffi <goffi@goffi.org>
parents:
1827
diff
changeset
|
186 # we need to add ' ' before each new line and to double each '%' for ConfigParser |
6209de5e3e25
jp (blog): import now show progression + value to add to sat.conf for URLs redirection
Goffi <goffi@goffi.org>
parents:
1827
diff
changeset
|
187 u'\n '.join(json.dumps(redirections, indent=1, separators=(',',': ')).replace(u'%', u'%%').split(u'\n'))), |
6209de5e3e25
jp (blog): import now show progression + value to add to sat.conf for URLs redirection
Goffi <goffi@goffi.org>
parents:
1827
diff
changeset
|
188 ]) |
6209de5e3e25
jp (blog): import now show progression + value to add to sat.conf for URLs redirection
Goffi <goffi@goffi.org>
parents:
1827
diff
changeset
|
189 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))) |
6209de5e3e25
jp (blog): import now show progression + value to add to sat.conf for URLs redirection
Goffi <goffi@goffi.org>
parents:
1827
diff
changeset
|
190 |
6209de5e3e25
jp (blog): import now show progression + value to add to sat.conf for URLs redirection
Goffi <goffi@goffi.org>
parents:
1827
diff
changeset
|
191 def onProgressError(self, error_msg): |
6209de5e3e25
jp (blog): import now show progression + value to add to sat.conf for URLs redirection
Goffi <goffi@goffi.org>
parents:
1827
diff
changeset
|
192 self.disp(_(u'Error while uploading blog: {}').format(error_msg),error=True) |
6209de5e3e25
jp (blog): import now show progression + value to add to sat.conf for URLs redirection
Goffi <goffi@goffi.org>
parents:
1827
diff
changeset
|
193 |
6209de5e3e25
jp (blog): import now show progression + value to add to sat.conf for URLs redirection
Goffi <goffi@goffi.org>
parents:
1827
diff
changeset
|
194 def error(self, failure): |
6209de5e3e25
jp (blog): import now show progression + value to add to sat.conf for URLs redirection
Goffi <goffi@goffi.org>
parents:
1827
diff
changeset
|
195 self.disp(_("Error while trying to upload a blog: {reason}").format(reason=failure), error=True) |
6209de5e3e25
jp (blog): import now show progression + value to add to sat.conf for URLs redirection
Goffi <goffi@goffi.org>
parents:
1827
diff
changeset
|
196 self.host.quit(1) |
6209de5e3e25
jp (blog): import now show progression + value to add to sat.conf for URLs redirection
Goffi <goffi@goffi.org>
parents:
1827
diff
changeset
|
197 |
1864
96ba685162f6
jp: all commands now use the new start method and set need_loop in __init__ when needed
Goffi <goffi@goffi.org>
parents:
1840
diff
changeset
|
198 def start(self): |
1827
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
199 if self.args.location is None: |
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
200 for name in ('option', 'service', 'no_images_upload'): |
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
201 if getattr(self.args, name): |
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
202 self.parser.error(_(u"{name} argument can't be used without location argument").format(name=name)) |
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
203 if self.args.importer is None: |
1839
cdecf553e051
frontends (jp/blog), plugin blog_import: fixes:
souliane <souliane@mailoo.org>
parents:
1834
diff
changeset
|
204 print u'\n'.join([u'{}: {}'.format(name, desc) for name, desc in self.host.bridge.blogImportList()]) |
1827
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
205 else: |
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
206 try: |
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
207 short_desc, long_desc = self.host.bridge.blogImportDesc(self.args.importer) |
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
208 except Exception as e: |
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
209 msg = [l for l in unicode(e).split('\n') if l][-1] # we only keep the last line |
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
210 print msg |
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
211 self.host.quit(1) |
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
212 else: |
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
213 print u"{name}: {short_desc}\n\n{long_desc}".format(name=self.args.importer, short_desc=short_desc, long_desc=long_desc) |
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
214 self.host.quit() |
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
215 else: |
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
216 # we have a location, an import is requested |
1839
cdecf553e051
frontends (jp/blog), plugin blog_import: fixes:
souliane <souliane@mailoo.org>
parents:
1834
diff
changeset
|
217 options = {key: value for key, value in self.args.option} |
1827
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
218 if self.args.host: |
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
219 options['host'] = self.args.host |
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
220 if self.args.ignore_tls_errors: |
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
221 options['ignore_tls_errors'] = C.BOOL_TRUE |
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
222 if self.args.no_images_upload: |
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
223 options['upload_images'] = C.BOOL_FALSE |
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
224 if self.args.upload_ignore_host: |
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
225 self.parser.error(u"upload-ignore-host option can't be used when no-images-upload is set") |
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
226 elif self.args.upload_ignore_host: |
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
227 options['upload_ignore_host'] = self.args.upload_ignore_host |
1834
6209de5e3e25
jp (blog): import now show progression + value to add to sat.conf for URLs redirection
Goffi <goffi@goffi.org>
parents:
1827
diff
changeset
|
228 def gotId(id_): |
6209de5e3e25
jp (blog): import now show progression + value to add to sat.conf for URLs redirection
Goffi <goffi@goffi.org>
parents:
1827
diff
changeset
|
229 self.progress_id = id_ |
6209de5e3e25
jp (blog): import now show progression + value to add to sat.conf for URLs redirection
Goffi <goffi@goffi.org>
parents:
1827
diff
changeset
|
230 self.host.bridge.blogImport(self.args.importer, self.args.location, options, self.args.service, self.profile, |
6209de5e3e25
jp (blog): import now show progression + value to add to sat.conf for URLs redirection
Goffi <goffi@goffi.org>
parents:
1827
diff
changeset
|
231 callback=gotId, errback=self.error) |
1827
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
232 |
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
233 |
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
234 class Blog(base.CommandBase): |
1864
96ba685162f6
jp: all commands now use the new start method and set need_loop in __init__ when needed
Goffi <goffi@goffi.org>
parents:
1840
diff
changeset
|
235 subcommands = (Edit, Import) |
1827
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
236 |
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
237 def __init__(self, host): |
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
238 super(Blog, self).__init__(host, 'blog', use_profile=False, help=_('blog/microblog management')) |