annotate sat_frontends/jp/cmd_blog.py @ 2696:5849dcaab99d

plugin XEP-0359: Unique and Stable Stanza IDs implementation, first draft
author Goffi <goffi@goffi.org>
date Sat, 01 Dec 2018 09:57:25 +0100
parents f69c1b260e49
children 4b693ea24d5f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1960
3e168cde7a7d jp: fixed shebang python call
Goffi <goffi@goffi.org>
parents: 1929
diff changeset
1 #!/usr/bin/env python2
1827
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
2483
0046283a285d dates update
Goffi <goffi@goffi.org>
parents: 2458
diff changeset
5 # Copyright (C) 2009-2018 Jérôme Poisson (goffi@goffi.org)
1827
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 _
2157
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
23 from sat_frontends.jp.constants import Const as C
2269
606ff34d30f2 jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents: 2267
diff changeset
24 from sat_frontends.jp import common
2157
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
25 from sat.tools.common.ansi import ANSI as A
2164
63d191c05ecd jp (blog): set default template and data mapping for the new template output
Goffi <goffi@goffi.org>
parents: 2161
diff changeset
26 from sat.tools.common import data_objects
2327
dad500e7ae50 jp (blog/get): added "url" key, which compute xmpp: URL of items
Goffi <goffi@goffi.org>
parents: 2325
diff changeset
27 from sat.tools.common import uri
1866
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
28 from sat.tools import config
1872
df1ca137b0cb jp (blog/edit): editor arguments can now be specified on sat.conf, and default on are applied for known editors:
Goffi <goffi@goffi.org>
parents: 1871
diff changeset
29 from ConfigParser import NoSectionError, NoOptionError
2458
4841ad6a5db4 jp (blog): added "set" command to publish content from stdin without editing
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
30 from functools import partial
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
31 import json
1887
16527dd5a81b blog (jp/edit): added --preview option to launch a "blog preview" in parallel
Goffi <goffi@goffi.org>
parents: 1886
diff changeset
32 import sys
1866
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
33 import os.path
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
34 import os
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
35 import time
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
36 import tempfile
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
37 import subprocess
2458
4841ad6a5db4 jp (blog): added "set" command to publish content from stdin without editing
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
38 import codecs
1919
d3354c80bd1f core (tools): moved common to a separate package, and put data method in a data_format module
Goffi <goffi@goffi.org>
parents: 1912
diff changeset
39 from sat.tools.common import data_format
1866
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
40
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
41 __commands__ = ["Blog"]
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
42
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
43 SYNTAX_XHTML = u"xhtml"
1872
df1ca137b0cb jp (blog/edit): editor arguments can now be specified on sat.conf, and default on are applied for known editors:
Goffi <goffi@goffi.org>
parents: 1871
diff changeset
44 # extensions to use with known syntaxes
df1ca137b0cb jp (blog/edit): editor arguments can now be specified on sat.conf, and default on are applied for known editors:
Goffi <goffi@goffi.org>
parents: 1871
diff changeset
45 SYNTAX_EXT = {
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
46 "": "txt", # used when the syntax is not found
2330
74c1dbabeec8 jp (blog/edit): pretty format XHTML before editing it
Goffi <goffi@goffi.org>
parents: 2327
diff changeset
47 SYNTAX_XHTML: "xhtml",
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
48 "markdown": "md",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
49 }
1872
df1ca137b0cb jp (blog/edit): editor arguments can now be specified on sat.conf, and default on are applied for known editors:
Goffi <goffi@goffi.org>
parents: 1871
diff changeset
50
df1ca137b0cb jp (blog/edit): editor arguments can now be specified on sat.conf, and default on are applied for known editors:
Goffi <goffi@goffi.org>
parents: 1871
diff changeset
51
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
52 CONF_SYNTAX_EXT = u"syntax_ext_dict"
2273
5f0dbf42aa9c jp (blog, common): various fixes in common and blog:
Goffi <goffi@goffi.org>
parents: 2271
diff changeset
53 BLOG_TMP_DIR = u"blog"
1868
28b29381db75 jp (blog/edit): added metadata handling through a .json file (named like content temporary file, but with extension replaced by "_metadata.json"). Modification to this file before the end of edition will be taken into account.
Goffi <goffi@goffi.org>
parents: 1866
diff changeset
54 # key to remove from metadata tmp file if they exist
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
55 KEY_TO_REMOVE_METADATA = (
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
56 "id",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
57 "content",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
58 "content_xhtml",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
59 "comments_node",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
60 "comments_service",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
61 "updated",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
62 )
1866
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
63
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
64 URL_REDIRECT_PREFIX = "url_redirect_"
1882
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
65 INOTIFY_INSTALL = '"pip install inotify"'
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
66 MB_KEYS = (
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
67 u"id",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
68 u"url",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
69 u"atom_id",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
70 u"updated",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
71 u"published",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
72 u"language",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
73 u"comments", # this key is used for all comments* keys
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
74 u"tags", # this key is used for all tag* keys
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
75 u"author",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
76 u"author_jid",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
77 u"author_email",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
78 u"author_jid_verified",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
79 u"content",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
80 u"content_xhtml",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
81 u"title",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
82 u"title_xhtml",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
83 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
84 OUTPUT_OPT_NO_HEADER = u"no-header"
1827
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
85
1866
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
86
2532
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2485
diff changeset
87 def guessSyntaxFromPath(host, sat_conf, path):
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2485
diff changeset
88 """Return syntax guessed according to filename extension
1877
a97db84c048d jp (blog): moved common method in a BlogCommon class + added "current" as an item keyword for blog/edit
Goffi <goffi@goffi.org>
parents: 1876
diff changeset
89
2532
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2485
diff changeset
90 @param sat_conf(ConfigParser.ConfigParser): instance opened on sat configuration
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2485
diff changeset
91 @param path(str): path to the content file
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2485
diff changeset
92 @return(unicode): syntax to use
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2485
diff changeset
93 """
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2485
diff changeset
94 # we first try to guess syntax with extension
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
95 ext = os.path.splitext(path)[1][1:] # we get extension without the '.'
2532
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2485
diff changeset
96 if ext:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
97 for k, v in SYNTAX_EXT.iteritems():
2532
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2485
diff changeset
98 if k and ext == v:
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2485
diff changeset
99 return k
1877
a97db84c048d jp (blog): moved common method in a BlogCommon class + added "current" as an item keyword for blog/edit
Goffi <goffi@goffi.org>
parents: 1876
diff changeset
100
2532
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2485
diff changeset
101 # if not found, we use current syntax
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2485
diff changeset
102 return host.bridge.getParamA("Syntax", "Composition", "value", host.profile)
1874
658824755a0c jp (blog): preview command, first draft
Goffi <goffi@goffi.org>
parents: 1873
diff changeset
103
658824755a0c jp (blog): preview command, first draft
Goffi <goffi@goffi.org>
parents: 1873
diff changeset
104
2458
4841ad6a5db4 jp (blog): added "set" command to publish content from stdin without editing
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
105 class BlogPublishCommon(object):
4841ad6a5db4 jp (blog): added "set" command to publish content from stdin without editing
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
106 """handle common option for publising commands (Set and Edit)"""
4841ad6a5db4 jp (blog): added "set" command to publish content from stdin without editing
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
107
4841ad6a5db4 jp (blog): added "set" command to publish content from stdin without editing
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
108 def add_parser_options(self):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
109 self.parser.add_argument(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
110 "-T", "--title", type=base.unicode_decoder, help=_(u"title of the item")
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
111 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
112 self.parser.add_argument(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
113 "-t",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
114 "--tag",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
115 type=base.unicode_decoder,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
116 action="append",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
117 help=_(u"tag (category) of your item"),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
118 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
119 self.parser.add_argument(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
120 "-C", "--comments", action="store_true", help=_(u"enable comments")
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
121 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
122 self.parser.add_argument(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
123 "-S",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
124 "--syntax",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
125 type=base.unicode_decoder,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
126 help=_(u"syntax to use (default: get profile's default syntax)"),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
127 )
2458
4841ad6a5db4 jp (blog): added "set" command to publish content from stdin without editing
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
128
4841ad6a5db4 jp (blog): added "set" command to publish content from stdin without editing
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
129 def setMbDataContent(self, content, mb_data):
4841ad6a5db4 jp (blog): added "set" command to publish content from stdin without editing
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
130 if self.args.syntax is None:
4841ad6a5db4 jp (blog): added "set" command to publish content from stdin without editing
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
131 # default syntax has been used
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
132 mb_data["content_rich"] = content
2458
4841ad6a5db4 jp (blog): added "set" command to publish content from stdin without editing
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
133 elif self.current_syntax == SYNTAX_XHTML:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
134 mb_data["content_xhtml"] = content
2458
4841ad6a5db4 jp (blog): added "set" command to publish content from stdin without editing
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
135 else:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
136 mb_data["content_xhtml"] = self.host.bridge.syntaxConvert(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
137 content, self.current_syntax, SYNTAX_XHTML, False, self.profile
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
138 )
2458
4841ad6a5db4 jp (blog): added "set" command to publish content from stdin without editing
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
139
4841ad6a5db4 jp (blog): added "set" command to publish content from stdin without editing
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
140 def setMbDataFromArgs(self, mb_data):
4841ad6a5db4 jp (blog): added "set" command to publish content from stdin without editing
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
141 """set microblog metadata according to command line options
4841ad6a5db4 jp (blog): added "set" command to publish content from stdin without editing
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
142
4841ad6a5db4 jp (blog): added "set" command to publish content from stdin without editing
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
143 if metadata already exist, it will be overwritten
4841ad6a5db4 jp (blog): added "set" command to publish content from stdin without editing
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
144 """
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
145 mb_data["allow_comments"] = C.boolConst(self.args.comments)
2458
4841ad6a5db4 jp (blog): added "set" command to publish content from stdin without editing
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
146 if self.args.tag:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
147 data_format.iter2dict("tag", self.args.tag, mb_data, check_conflict=False)
2458
4841ad6a5db4 jp (blog): added "set" command to publish content from stdin without editing
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
148 if self.args.title is not None:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
149 mb_data["title"] = self.args.title
2458
4841ad6a5db4 jp (blog): added "set" command to publish content from stdin without editing
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
150
4841ad6a5db4 jp (blog): added "set" command to publish content from stdin without editing
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
151
2532
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2485
diff changeset
152 class Set(base.CommandBase, BlogPublishCommon):
2458
4841ad6a5db4 jp (blog): added "set" command to publish content from stdin without editing
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
153 def __init__(self, host):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
154 base.CommandBase.__init__(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
155 self,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
156 host,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
157 "set",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
158 use_pubsub=True,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
159 pubsub_flags={C.SINGLE_ITEM},
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
160 help=_(u"publish a new blog item or update an existing one"),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
161 )
2458
4841ad6a5db4 jp (blog): added "set" command to publish content from stdin without editing
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
162 BlogPublishCommon.__init__(self)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
163 self.need_loop = True
2458
4841ad6a5db4 jp (blog): added "set" command to publish content from stdin without editing
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
164
4841ad6a5db4 jp (blog): added "set" command to publish content from stdin without editing
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
165 def add_parser_options(self):
4841ad6a5db4 jp (blog): added "set" command to publish content from stdin without editing
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
166 BlogPublishCommon.add_parser_options(self)
4841ad6a5db4 jp (blog): added "set" command to publish content from stdin without editing
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
167
4841ad6a5db4 jp (blog): added "set" command to publish content from stdin without editing
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
168 def mbSendCb(self):
4841ad6a5db4 jp (blog): added "set" command to publish content from stdin without editing
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
169 self.disp(u"Item published")
4841ad6a5db4 jp (blog): added "set" command to publish content from stdin without editing
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
170 self.host.quit(C.EXIT_OK)
4841ad6a5db4 jp (blog): added "set" command to publish content from stdin without editing
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
171
4841ad6a5db4 jp (blog): added "set" command to publish content from stdin without editing
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
172 def start(self):
4841ad6a5db4 jp (blog): added "set" command to publish content from stdin without editing
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
173 self.pubsub_item = self.args.item
4841ad6a5db4 jp (blog): added "set" command to publish content from stdin without editing
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
174 mb_data = {}
4841ad6a5db4 jp (blog): added "set" command to publish content from stdin without editing
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
175 self.setMbDataFromArgs(mb_data)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
176 content = codecs.getreader("utf-8")(sys.stdin).read()
2458
4841ad6a5db4 jp (blog): added "set" command to publish content from stdin without editing
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
177 self.setMbDataContent(content, mb_data)
4841ad6a5db4 jp (blog): added "set" command to publish content from stdin without editing
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
178
4841ad6a5db4 jp (blog): added "set" command to publish content from stdin without editing
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
179 self.host.bridge.mbSend(
4841ad6a5db4 jp (blog): added "set" command to publish content from stdin without editing
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
180 self.args.service,
4841ad6a5db4 jp (blog): added "set" command to publish content from stdin without editing
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
181 self.args.node,
4841ad6a5db4 jp (blog): added "set" command to publish content from stdin without editing
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
182 mb_data,
4841ad6a5db4 jp (blog): added "set" command to publish content from stdin without editing
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
183 self.profile,
4841ad6a5db4 jp (blog): added "set" command to publish content from stdin without editing
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
184 callback=self.exitCb,
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
185 errback=partial(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
186 self.errback,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
187 msg=_(u"can't send item: {}"),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
188 exit_code=C.EXIT_BRIDGE_ERRBACK,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
189 ),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
190 )
2458
4841ad6a5db4 jp (blog): added "set" command to publish content from stdin without editing
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
191
4841ad6a5db4 jp (blog): added "set" command to publish content from stdin without editing
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
192
2532
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2485
diff changeset
193 class Get(base.CommandBase):
2164
63d191c05ecd jp (blog): set default template and data mapping for the new template output
Goffi <goffi@goffi.org>
parents: 2161
diff changeset
194 TEMPLATE = u"blog/articles.html"
2157
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
195
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
196 def __init__(self, host):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
197 extra_outputs = {"default": self.default_output, "fancy": self.fancy_output}
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
198 base.CommandBase.__init__(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
199 self,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
200 host,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
201 "get",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
202 use_verbose=True,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
203 use_pubsub=True,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
204 pubsub_flags={C.MULTI_ITEMS},
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
205 use_output=C.OUTPUT_COMPLEX,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
206 extra_outputs=extra_outputs,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
207 help=_(u"get blog item(s)"),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
208 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
209 self.need_loop = True
2157
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
210
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
211 def add_parser_options(self):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
212 #  TODO: a key(s) argument to select keys to display
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
213 self.parser.add_argument(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
214 "-k",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
215 "--key",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
216 type=base.unicode_decoder,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
217 action="append",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
218 dest="keys",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
219 help=_(u"microblog data key(s) to display (default: depend of verbosity)"),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
220 )
2157
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
221 # TODO: add MAM filters
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
222
2164
63d191c05ecd jp (blog): set default template and data mapping for the new template output
Goffi <goffi@goffi.org>
parents: 2161
diff changeset
223 def template_data_mapping(self, data):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
224 return {u"items": data_objects.BlogItems(data)}
2164
63d191c05ecd jp (blog): set default template and data mapping for the new template output
Goffi <goffi@goffi.org>
parents: 2161
diff changeset
225
2157
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
226 def format_comments(self, item, keys):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
227 comments_data = data_format.dict2iterdict(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
228 u"comments", item, (u"node", u"service"), pop=True
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
229 )
2157
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
230 lines = []
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
231 for data in comments_data:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
232 lines.append(data[u"comments"])
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
233 for k in (u"node", u"service"):
2157
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
234 if OUTPUT_OPT_NO_HEADER in self.args.output_opts:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
235 header = u""
2157
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
236 else:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
237 header = C.A_HEADER + k + u": " + A.RESET
2157
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
238 lines.append(header + data[k])
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
239 return u"\n".join(lines)
2157
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
240
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
241 def format_tags(self, item, keys):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
242 tags = data_format.dict2iter("tag", item, pop=True)
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
243 return u", ".join(tags)
2157
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
244
2158
970a348d3fe9 jp (blog/get): fancy output prints author, published and updated if verbosity > 1 and tags if verbosity > 2 + format published and updated
Goffi <goffi@goffi.org>
parents: 2157
diff changeset
245 def format_updated(self, item, keys):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
246 return self.format_time(item["updated"])
2158
970a348d3fe9 jp (blog/get): fancy output prints author, published and updated if verbosity > 1 and tags if verbosity > 2 + format published and updated
Goffi <goffi@goffi.org>
parents: 2157
diff changeset
247
970a348d3fe9 jp (blog/get): fancy output prints author, published and updated if verbosity > 1 and tags if verbosity > 2 + format published and updated
Goffi <goffi@goffi.org>
parents: 2157
diff changeset
248 def format_published(self, item, keys):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
249 return self.format_time(item["published"])
2158
970a348d3fe9 jp (blog/get): fancy output prints author, published and updated if verbosity > 1 and tags if verbosity > 2 + format published and updated
Goffi <goffi@goffi.org>
parents: 2157
diff changeset
250
2327
dad500e7ae50 jp (blog/get): added "url" key, which compute xmpp: URL of items
Goffi <goffi@goffi.org>
parents: 2325
diff changeset
251 def format_url(self, item, keys):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
252 return uri.buildXMPPUri(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
253 u"pubsub",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
254 subtype=u"microblog",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
255 path=self.metadata[u"service"],
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
256 node=self.metadata[u"node"],
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
257 item=item[u"id"],
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
258 )
2327
dad500e7ae50 jp (blog/get): added "url" key, which compute xmpp: URL of items
Goffi <goffi@goffi.org>
parents: 2325
diff changeset
259
2157
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
260 def get_keys(self):
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
261 """return keys to display according to verbosity or explicit key request"""
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
262 verbosity = self.args.verbose
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
263 if self.args.keys:
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
264 if not set(MB_KEYS).issuperset(self.args.keys):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
265 self.disp(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
266 u"following keys are invalid: {invalid}.\n"
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
267 u"Valid keys are: {valid}.".format(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
268 invalid=u", ".join(set(self.args.keys).difference(MB_KEYS)),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
269 valid=u", ".join(sorted(MB_KEYS)),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
270 ),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
271 error=True,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
272 )
2157
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
273 self.host.quit(C.EXIT_BAD_ARG)
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
274 return self.args.keys
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
275 else:
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
276 if verbosity == 0:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
277 return (u"title", u"content")
2157
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
278 elif verbosity == 1:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
279 return (
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
280 u"title",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
281 u"tags",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
282 u"author",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
283 u"author_jid",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
284 u"author_email",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
285 u"author_jid_verified",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
286 u"published",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
287 u"updated",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
288 u"content",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
289 )
2157
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
290 else:
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
291 return MB_KEYS
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
292
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
293 def default_output(self, data):
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
294 """simple key/value output"""
2327
dad500e7ae50 jp (blog/get): added "url" key, which compute xmpp: URL of items
Goffi <goffi@goffi.org>
parents: 2325
diff changeset
295 items, self.metadata = data
2157
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
296 keys = self.get_keys()
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
297
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
298 #  k_cb use format_[key] methods for complex formattings
2157
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
299 k_cb = {}
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
300 for k in keys:
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
301 try:
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
302 callback = getattr(self, "format_" + k)
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
303 except AttributeError:
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
304 pass
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
305 else:
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
306 k_cb[k] = callback
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
307 for idx, item in enumerate(items):
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
308 for k in keys:
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
309 if k not in item and k not in k_cb:
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
310 continue
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
311 if OUTPUT_OPT_NO_HEADER in self.args.output_opts:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
312 header = ""
2157
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
313 else:
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
314 header = u"{k_fmt}{key}:{k_fmt_e} {sep}".format(
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
315 k_fmt=C.A_HEADER,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
316 key=k,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
317 k_fmt_e=A.RESET,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
318 sep=u"\n" if "content" in k else u"",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
319 )
2157
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
320 value = k_cb[k](item, keys) if k in k_cb else item[k]
2161
62dfa6e02f54 jp (base, blog, output(std)): fixed bad unicode handling in stdout, use of jp.disp instead of simple print
Goffi <goffi@goffi.org>
parents: 2158
diff changeset
321 self.disp(header + value)
2157
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
322 # we want a separation line after each item but the last one
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
323 if idx < len(items) - 1:
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
324 print(u"")
2157
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
325
2158
970a348d3fe9 jp (blog/get): fancy output prints author, published and updated if verbosity > 1 and tags if verbosity > 2 + format published and updated
Goffi <goffi@goffi.org>
parents: 2157
diff changeset
326 def format_time(self, timestamp):
970a348d3fe9 jp (blog/get): fancy output prints author, published and updated if verbosity > 1 and tags if verbosity > 2 + format published and updated
Goffi <goffi@goffi.org>
parents: 2157
diff changeset
327 """return formatted date for timestamp
970a348d3fe9 jp (blog/get): fancy output prints author, published and updated if verbosity > 1 and tags if verbosity > 2 + format published and updated
Goffi <goffi@goffi.org>
parents: 2157
diff changeset
328
970a348d3fe9 jp (blog/get): fancy output prints author, published and updated if verbosity > 1 and tags if verbosity > 2 + format published and updated
Goffi <goffi@goffi.org>
parents: 2157
diff changeset
329 @param timestamp(str,int,float): unix timestamp
970a348d3fe9 jp (blog/get): fancy output prints author, published and updated if verbosity > 1 and tags if verbosity > 2 + format published and updated
Goffi <goffi@goffi.org>
parents: 2157
diff changeset
330 @return (unicode): formatted date
970a348d3fe9 jp (blog/get): fancy output prints author, published and updated if verbosity > 1 and tags if verbosity > 2 + format published and updated
Goffi <goffi@goffi.org>
parents: 2157
diff changeset
331 """
970a348d3fe9 jp (blog/get): fancy output prints author, published and updated if verbosity > 1 and tags if verbosity > 2 + format published and updated
Goffi <goffi@goffi.org>
parents: 2157
diff changeset
332 fmt = u"%d/%m/%Y %H:%M:%S"
970a348d3fe9 jp (blog/get): fancy output prints author, published and updated if verbosity > 1 and tags if verbosity > 2 + format published and updated
Goffi <goffi@goffi.org>
parents: 2157
diff changeset
333 return time.strftime(fmt, time.localtime(float(timestamp)))
970a348d3fe9 jp (blog/get): fancy output prints author, published and updated if verbosity > 1 and tags if verbosity > 2 + format published and updated
Goffi <goffi@goffi.org>
parents: 2157
diff changeset
334
2157
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
335 def fancy_output(self, data):
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
336 """display blog is a nice to read way
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
337
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
338 this output doesn't use keys filter
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
339 """
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
340 # thanks to http://stackoverflow.com/a/943921
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
341 rows, columns = map(int, os.popen("stty size", "r").read().split())
2157
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
342 items, metadata = data
2158
970a348d3fe9 jp (blog/get): fancy output prints author, published and updated if verbosity > 1 and tags if verbosity > 2 + format published and updated
Goffi <goffi@goffi.org>
parents: 2157
diff changeset
343 verbosity = self.args.verbose
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
344 sep = A.color(A.FG_BLUE, columns * u"▬")
2157
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
345 if items:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
346 print(u"\n" + sep + "\n")
2157
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
347
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
348 for idx, item in enumerate(items):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
349 title = item.get(u"title")
2158
970a348d3fe9 jp (blog/get): fancy output prints author, published and updated if verbosity > 1 and tags if verbosity > 2 + format published and updated
Goffi <goffi@goffi.org>
parents: 2157
diff changeset
350 if verbosity > 0:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
351 author = item[u"author"]
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
352 published, updated = item[u"published"], item.get("updated")
2158
970a348d3fe9 jp (blog/get): fancy output prints author, published and updated if verbosity > 1 and tags if verbosity > 2 + format published and updated
Goffi <goffi@goffi.org>
parents: 2157
diff changeset
353 else:
970a348d3fe9 jp (blog/get): fancy output prints author, published and updated if verbosity > 1 and tags if verbosity > 2 + format published and updated
Goffi <goffi@goffi.org>
parents: 2157
diff changeset
354 author = published = updated = None
970a348d3fe9 jp (blog/get): fancy output prints author, published and updated if verbosity > 1 and tags if verbosity > 2 + format published and updated
Goffi <goffi@goffi.org>
parents: 2157
diff changeset
355 if verbosity > 1:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
356 tags = list(data_format.dict2iter("tag", item, pop=True))
2158
970a348d3fe9 jp (blog/get): fancy output prints author, published and updated if verbosity > 1 and tags if verbosity > 2 + format published and updated
Goffi <goffi@goffi.org>
parents: 2157
diff changeset
357 else:
970a348d3fe9 jp (blog/get): fancy output prints author, published and updated if verbosity > 1 and tags if verbosity > 2 + format published and updated
Goffi <goffi@goffi.org>
parents: 2157
diff changeset
358 tags = None
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
359 content = item.get(u"content")
2157
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
360
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
361 if title:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
362 print(A.color(A.BOLD, A.FG_CYAN, item[u"title"]))
2158
970a348d3fe9 jp (blog/get): fancy output prints author, published and updated if verbosity > 1 and tags if verbosity > 2 + format published and updated
Goffi <goffi@goffi.org>
parents: 2157
diff changeset
363 meta = []
970a348d3fe9 jp (blog/get): fancy output prints author, published and updated if verbosity > 1 and tags if verbosity > 2 + format published and updated
Goffi <goffi@goffi.org>
parents: 2157
diff changeset
364 if author:
970a348d3fe9 jp (blog/get): fancy output prints author, published and updated if verbosity > 1 and tags if verbosity > 2 + format published and updated
Goffi <goffi@goffi.org>
parents: 2157
diff changeset
365 meta.append(A.color(A.FG_YELLOW, author))
970a348d3fe9 jp (blog/get): fancy output prints author, published and updated if verbosity > 1 and tags if verbosity > 2 + format published and updated
Goffi <goffi@goffi.org>
parents: 2157
diff changeset
366 if published:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
367 meta.append(A.color(A.FG_YELLOW, u"on ", self.format_time(published)))
2158
970a348d3fe9 jp (blog/get): fancy output prints author, published and updated if verbosity > 1 and tags if verbosity > 2 + format published and updated
Goffi <goffi@goffi.org>
parents: 2157
diff changeset
368 if updated != published:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
369 meta.append(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
370 A.color(A.FG_YELLOW, u"(updated on ", self.format_time(updated), u")")
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
371 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
372 print(u" ".join(meta))
2157
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
373 if tags:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
374 print(A.color(A.FG_MAGENTA, u", ".join(tags)))
2157
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
375 if (title or tags) and content:
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
376 print("")
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
377 if content:
2161
62dfa6e02f54 jp (base, blog, output(std)): fixed bad unicode handling in stdout, use of jp.disp instead of simple print
Goffi <goffi@goffi.org>
parents: 2158
diff changeset
378 self.disp(content)
2157
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
379
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
380 print(u"\n" + sep + "\n")
2157
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
381
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
382 def mbGetCb(self, mb_result):
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
383 self.output(mb_result)
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
384 self.host.quit(C.EXIT_OK)
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
385
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
386 def mbGetEb(self, failure_):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
387 self.disp(u"can't get blog items: {reason}".format(reason=failure_), error=True)
2157
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
388 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
389
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
390 def start(self):
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
391 self.host.bridge.mbGet(
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
392 self.args.service,
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
393 self.args.node,
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
394 self.args.max,
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
395 self.args.items,
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
396 {},
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
397 self.profile,
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
398 callback=self.mbGetCb,
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
399 errback=self.mbGetEb,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
400 )
2157
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
401
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
402
2532
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2485
diff changeset
403 class Edit(base.CommandBase, BlogPublishCommon, common.BaseEdit):
1866
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
404 def __init__(self, host):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
405 base.CommandBase.__init__(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
406 self,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
407 host,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
408 "edit",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
409 use_pubsub=True,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
410 pubsub_flags={C.SINGLE_ITEM},
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
411 use_draft=True,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
412 use_verbose=True,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
413 help=_(u"edit an existing or new blog post"),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
414 )
2458
4841ad6a5db4 jp (blog): added "set" command to publish content from stdin without editing
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
415 BlogPublishCommon.__init__(self)
2269
606ff34d30f2 jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents: 2267
diff changeset
416 common.BaseEdit.__init__(self, self.host, BLOG_TMP_DIR, use_metadata=True)
1866
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
417
2532
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2485
diff changeset
418 @property
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2485
diff changeset
419 def current_syntax(self):
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2485
diff changeset
420 if self._current_syntax is None:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
421 self._current_syntax = self.host.bridge.getParamA(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
422 "Syntax", "Composition", "value", self.profile
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
423 )
2532
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2485
diff changeset
424 return self._current_syntax
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2485
diff changeset
425
1866
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
426 def add_parser_options(self):
2458
4841ad6a5db4 jp (blog): added "set" command to publish content from stdin without editing
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
427 BlogPublishCommon.add_parser_options(self)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
428 self.parser.add_argument(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
429 "-P",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
430 "--preview",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
431 action="store_true",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
432 help=_(u"launch a blog preview in parallel"),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
433 )
1866
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
434
1872
df1ca137b0cb jp (blog/edit): editor arguments can now be specified on sat.conf, and default on are applied for known editors:
Goffi <goffi@goffi.org>
parents: 1871
diff changeset
435 def buildMetadataFile(self, content_file_path, mb_data=None):
1868
28b29381db75 jp (blog/edit): added metadata handling through a .json file (named like content temporary file, but with extension replaced by "_metadata.json"). Modification to this file before the end of edition will be taken into account.
Goffi <goffi@goffi.org>
parents: 1866
diff changeset
436 """Build a metadata file using json
28b29381db75 jp (blog/edit): added metadata handling through a .json file (named like content temporary file, but with extension replaced by "_metadata.json"). Modification to this file before the end of edition will be taken into account.
Goffi <goffi@goffi.org>
parents: 1866
diff changeset
437
1872
df1ca137b0cb jp (blog/edit): editor arguments can now be specified on sat.conf, and default on are applied for known editors:
Goffi <goffi@goffi.org>
parents: 1871
diff changeset
438 The file is named after content_file_path, with extension replaced by _metadata.json
df1ca137b0cb jp (blog/edit): editor arguments can now be specified on sat.conf, and default on are applied for known editors:
Goffi <goffi@goffi.org>
parents: 1871
diff changeset
439 @param content_file_path(str): path to the temporary file which will contain the body
1868
28b29381db75 jp (blog/edit): added metadata handling through a .json file (named like content temporary file, but with extension replaced by "_metadata.json"). Modification to this file before the end of edition will be taken into account.
Goffi <goffi@goffi.org>
parents: 1866
diff changeset
440 @param mb_data(dict, None): microblog metadata (for existing items)
28b29381db75 jp (blog/edit): added metadata handling through a .json file (named like content temporary file, but with extension replaced by "_metadata.json"). Modification to this file before the end of edition will be taken into account.
Goffi <goffi@goffi.org>
parents: 1866
diff changeset
441 @return (tuple[dict, str]): merged metadata put originaly in metadata file
28b29381db75 jp (blog/edit): added metadata handling through a .json file (named like content temporary file, but with extension replaced by "_metadata.json"). Modification to this file before the end of edition will be taken into account.
Goffi <goffi@goffi.org>
parents: 1866
diff changeset
442 and path to temporary metadata file
28b29381db75 jp (blog/edit): added metadata handling through a .json file (named like content temporary file, but with extension replaced by "_metadata.json"). Modification to this file before the end of edition will be taken into account.
Goffi <goffi@goffi.org>
parents: 1866
diff changeset
443 """
28b29381db75 jp (blog/edit): added metadata handling through a .json file (named like content temporary file, but with extension replaced by "_metadata.json"). Modification to this file before the end of edition will be taken into account.
Goffi <goffi@goffi.org>
parents: 1866
diff changeset
444 # we first construct metadata from edited item ones and CLI argumments
1876
1088bf7b28e7 jp (blog/edit): metadata file is reused if it already exists
Goffi <goffi@goffi.org>
parents: 1874
diff changeset
445 # or re-use the existing one if it exists
2269
606ff34d30f2 jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents: 2267
diff changeset
446 meta_file_path = os.path.splitext(content_file_path)[0] + common.METADATA_SUFF
1876
1088bf7b28e7 jp (blog/edit): metadata file is reused if it already exists
Goffi <goffi@goffi.org>
parents: 1874
diff changeset
447 if os.path.exists(meta_file_path):
1088bf7b28e7 jp (blog/edit): metadata file is reused if it already exists
Goffi <goffi@goffi.org>
parents: 1874
diff changeset
448 self.disp(u"Metadata file already exists, we re-use it")
1088bf7b28e7 jp (blog/edit): metadata file is reused if it already exists
Goffi <goffi@goffi.org>
parents: 1874
diff changeset
449 try:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
450 with open(meta_file_path, "rb") as f:
1876
1088bf7b28e7 jp (blog/edit): metadata file is reused if it already exists
Goffi <goffi@goffi.org>
parents: 1874
diff changeset
451 mb_data = json.load(f)
1088bf7b28e7 jp (blog/edit): metadata file is reused if it already exists
Goffi <goffi@goffi.org>
parents: 1874
diff changeset
452 except (OSError, IOError, ValueError) as e:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
453 self.disp(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
454 u"Can't read existing metadata file at {path}, aborting: {reason}".format(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
455 path=meta_file_path, reason=e
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
456 ),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
457 error=True,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
458 )
1876
1088bf7b28e7 jp (blog/edit): metadata file is reused if it already exists
Goffi <goffi@goffi.org>
parents: 1874
diff changeset
459 self.host.quit(1)
1088bf7b28e7 jp (blog/edit): metadata file is reused if it already exists
Goffi <goffi@goffi.org>
parents: 1874
diff changeset
460 else:
1088bf7b28e7 jp (blog/edit): metadata file is reused if it already exists
Goffi <goffi@goffi.org>
parents: 1874
diff changeset
461 mb_data = {} if mb_data is None else mb_data.copy()
1088bf7b28e7 jp (blog/edit): metadata file is reused if it already exists
Goffi <goffi@goffi.org>
parents: 1874
diff changeset
462
1088bf7b28e7 jp (blog/edit): metadata file is reused if it already exists
Goffi <goffi@goffi.org>
parents: 1874
diff changeset
463 # in all cases, we want to remove unwanted keys
1868
28b29381db75 jp (blog/edit): added metadata handling through a .json file (named like content temporary file, but with extension replaced by "_metadata.json"). Modification to this file before the end of edition will be taken into account.
Goffi <goffi@goffi.org>
parents: 1866
diff changeset
464 for key in KEY_TO_REMOVE_METADATA:
28b29381db75 jp (blog/edit): added metadata handling through a .json file (named like content temporary file, but with extension replaced by "_metadata.json"). Modification to this file before the end of edition will be taken into account.
Goffi <goffi@goffi.org>
parents: 1866
diff changeset
465 try:
28b29381db75 jp (blog/edit): added metadata handling through a .json file (named like content temporary file, but with extension replaced by "_metadata.json"). Modification to this file before the end of edition will be taken into account.
Goffi <goffi@goffi.org>
parents: 1866
diff changeset
466 del mb_data[key]
28b29381db75 jp (blog/edit): added metadata handling through a .json file (named like content temporary file, but with extension replaced by "_metadata.json"). Modification to this file before the end of edition will be taken into account.
Goffi <goffi@goffi.org>
parents: 1866
diff changeset
467 except KeyError:
28b29381db75 jp (blog/edit): added metadata handling through a .json file (named like content temporary file, but with extension replaced by "_metadata.json"). Modification to this file before the end of edition will be taken into account.
Goffi <goffi@goffi.org>
parents: 1866
diff changeset
468 pass
1876
1088bf7b28e7 jp (blog/edit): metadata file is reused if it already exists
Goffi <goffi@goffi.org>
parents: 1874
diff changeset
469 # and override metadata with command-line arguments
2458
4841ad6a5db4 jp (blog): added "set" command to publish content from stdin without editing
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
470 self.setMbDataFromArgs(mb_data)
1868
28b29381db75 jp (blog/edit): added metadata handling through a .json file (named like content temporary file, but with extension replaced by "_metadata.json"). Modification to this file before the end of edition will be taken into account.
Goffi <goffi@goffi.org>
parents: 1866
diff changeset
471
1876
1088bf7b28e7 jp (blog/edit): metadata file is reused if it already exists
Goffi <goffi@goffi.org>
parents: 1874
diff changeset
472 # then we create the file and write metadata there, as JSON dict
1868
28b29381db75 jp (blog/edit): added metadata handling through a .json file (named like content temporary file, but with extension replaced by "_metadata.json"). Modification to this file before the end of edition will be taken into account.
Goffi <goffi@goffi.org>
parents: 1866
diff changeset
473 # XXX: if we port jp one day on Windows, O_BINARY may need to be added here
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
474 with os.fdopen(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
475 os.open(meta_file_path, os.O_RDWR | os.O_CREAT | os.O_TRUNC, 0o600), "w+b"
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
476 ) as f:
1868
28b29381db75 jp (blog/edit): added metadata handling through a .json file (named like content temporary file, but with extension replaced by "_metadata.json"). Modification to this file before the end of edition will be taken into account.
Goffi <goffi@goffi.org>
parents: 1866
diff changeset
477 # we need to use an intermediate unicode buffer to write to the file unicode without escaping characters
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
478 unicode_dump = json.dumps(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
479 mb_data,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
480 ensure_ascii=False,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
481 indent=4,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
482 separators=(",", ": "),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
483 sort_keys=True,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
484 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
485 f.write(unicode_dump.encode("utf-8"))
1868
28b29381db75 jp (blog/edit): added metadata handling through a .json file (named like content temporary file, but with extension replaced by "_metadata.json"). Modification to this file before the end of edition will be taken into account.
Goffi <goffi@goffi.org>
parents: 1866
diff changeset
486
28b29381db75 jp (blog/edit): added metadata handling through a .json file (named like content temporary file, but with extension replaced by "_metadata.json"). Modification to this file before the end of edition will be taken into account.
Goffi <goffi@goffi.org>
parents: 1866
diff changeset
487 return mb_data, meta_file_path
28b29381db75 jp (blog/edit): added metadata handling through a .json file (named like content temporary file, but with extension replaced by "_metadata.json"). Modification to this file before the end of edition will be taken into account.
Goffi <goffi@goffi.org>
parents: 1866
diff changeset
488
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
489 def edit(self, content_file_path, content_file_obj, mb_data=None):
1866
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
490 """Edit the file contening the content using editor, and publish it"""
1868
28b29381db75 jp (blog/edit): added metadata handling through a .json file (named like content temporary file, but with extension replaced by "_metadata.json"). Modification to this file before the end of edition will be taken into account.
Goffi <goffi@goffi.org>
parents: 1866
diff changeset
491 # we first create metadata file
2270
07caa12be945 jp (blog, common): added --force-item option to avoid magic and use argument as actual item id:
Goffi <goffi@goffi.org>
parents: 2269
diff changeset
492 meta_ori, meta_file_path = self.buildMetadataFile(content_file_path, mb_data)
1866
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
493
1887
16527dd5a81b blog (jp/edit): added --preview option to launch a "blog preview" in parallel
Goffi <goffi@goffi.org>
parents: 1886
diff changeset
494 # do we need a preview ?
16527dd5a81b blog (jp/edit): added --preview option to launch a "blog preview" in parallel
Goffi <goffi@goffi.org>
parents: 1886
diff changeset
495 if self.args.preview:
16527dd5a81b blog (jp/edit): added --preview option to launch a "blog preview" in parallel
Goffi <goffi@goffi.org>
parents: 1886
diff changeset
496 self.disp(u"Preview requested, launching it", 1)
16527dd5a81b blog (jp/edit): added --preview option to launch a "blog preview" in parallel
Goffi <goffi@goffi.org>
parents: 1886
diff changeset
497 # we redirect outputs to /dev/null to avoid console pollution in editor
16527dd5a81b blog (jp/edit): added --preview option to launch a "blog preview" in parallel
Goffi <goffi@goffi.org>
parents: 1886
diff changeset
498 # if user wants to see messages, (s)he can call "blog preview" directly
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
499 DEVNULL = open(os.devnull, "wb")
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
500 subprocess.Popen(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
501 [
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
502 sys.argv[0],
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
503 "blog",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
504 "preview",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
505 "--inotify",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
506 "true",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
507 "-p",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
508 self.profile,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
509 content_file_path,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
510 ],
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
511 stdout=DEVNULL,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
512 stderr=subprocess.STDOUT,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
513 )
1887
16527dd5a81b blog (jp/edit): added --preview option to launch a "blog preview" in parallel
Goffi <goffi@goffi.org>
parents: 1886
diff changeset
514
2269
606ff34d30f2 jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents: 2267
diff changeset
515 # we launch editor
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
516 self.runEditor(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
517 "blog_editor_args",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
518 content_file_path,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
519 content_file_obj,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
520 meta_file_path=meta_file_path,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
521 meta_ori=meta_ori,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
522 )
2269
606ff34d30f2 jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents: 2267
diff changeset
523
606ff34d30f2 jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents: 2267
diff changeset
524 def publish(self, content, mb_data):
2458
4841ad6a5db4 jp (blog): added "set" command to publish content from stdin without editing
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
525 self.setMbDataContent(content, mb_data)
1866
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
526
2270
07caa12be945 jp (blog, common): added --force-item option to avoid magic and use argument as actual item id:
Goffi <goffi@goffi.org>
parents: 2269
diff changeset
527 if self.pubsub_item is not None:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
528 mb_data["id"] = self.pubsub_item
2269
606ff34d30f2 jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents: 2267
diff changeset
529
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
530 self.host.bridge.mbSend(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
531 self.pubsub_service, self.pubsub_node, mb_data, self.profile
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
532 )
2269
606ff34d30f2 jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents: 2267
diff changeset
533 self.disp(u"Blog item published")
1866
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
534
2269
606ff34d30f2 jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents: 2267
diff changeset
535 def getTmpSuff(self):
606ff34d30f2 jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents: 2267
diff changeset
536 # we get current syntax to determine file extension
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
537 return SYNTAX_EXT.get(self.current_syntax, SYNTAX_EXT[""])
1866
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
538
2269
606ff34d30f2 jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents: 2267
diff changeset
539 def getItemData(self, service, node, item):
606ff34d30f2 jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents: 2267
diff changeset
540 items = [item] if item is not None else []
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
541 mb_data = self.host.bridge.mbGet(service, node, 1, items, {}, self.profile)[0][0]
2269
606ff34d30f2 jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents: 2267
diff changeset
542 try:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
543 content = mb_data["content_xhtml"]
2269
606ff34d30f2 jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents: 2267
diff changeset
544 except KeyError:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
545 content = mb_data["content"]
2269
606ff34d30f2 jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents: 2267
diff changeset
546 if content:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
547 content = self.host.bridge.syntaxConvert(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
548 content, "text", SYNTAX_XHTML, False, self.profile
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
549 )
2330
74c1dbabeec8 jp (blog/edit): pretty format XHTML before editing it
Goffi <goffi@goffi.org>
parents: 2327
diff changeset
550 if content and self.current_syntax != SYNTAX_XHTML:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
551 content = self.host.bridge.syntaxConvert(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
552 content, SYNTAX_XHTML, self.current_syntax, False, self.profile
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
553 )
2330
74c1dbabeec8 jp (blog/edit): pretty format XHTML before editing it
Goffi <goffi@goffi.org>
parents: 2327
diff changeset
554 if content and self.current_syntax == SYNTAX_XHTML:
74c1dbabeec8 jp (blog/edit): pretty format XHTML before editing it
Goffi <goffi@goffi.org>
parents: 2327
diff changeset
555 try:
74c1dbabeec8 jp (blog/edit): pretty format XHTML before editing it
Goffi <goffi@goffi.org>
parents: 2327
diff changeset
556 from lxml import etree
74c1dbabeec8 jp (blog/edit): pretty format XHTML before editing it
Goffi <goffi@goffi.org>
parents: 2327
diff changeset
557 except ImportError:
74c1dbabeec8 jp (blog/edit): pretty format XHTML before editing it
Goffi <goffi@goffi.org>
parents: 2327
diff changeset
558 self.disp(_(u"You need lxml to edit pretty XHTML"))
74c1dbabeec8 jp (blog/edit): pretty format XHTML before editing it
Goffi <goffi@goffi.org>
parents: 2327
diff changeset
559 else:
74c1dbabeec8 jp (blog/edit): pretty format XHTML before editing it
Goffi <goffi@goffi.org>
parents: 2327
diff changeset
560 parser = etree.XMLParser(remove_blank_text=True)
74c1dbabeec8 jp (blog/edit): pretty format XHTML before editing it
Goffi <goffi@goffi.org>
parents: 2327
diff changeset
561 root = etree.fromstring(content, parser)
74c1dbabeec8 jp (blog/edit): pretty format XHTML before editing it
Goffi <goffi@goffi.org>
parents: 2327
diff changeset
562 content = etree.tostring(root, encoding=unicode, pretty_print=True)
74c1dbabeec8 jp (blog/edit): pretty format XHTML before editing it
Goffi <goffi@goffi.org>
parents: 2327
diff changeset
563
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
564 return content, mb_data, mb_data["id"]
1866
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
565
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
566 def start(self):
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
567 # if there are user defined extension, we use them
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
568 SYNTAX_EXT.update(config.getConfig(self.sat_conf, "jp", CONF_SYNTAX_EXT, {}))
2532
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2485
diff changeset
569 self._current_syntax = self.args.syntax
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2485
diff changeset
570 if self._current_syntax is not None:
2325
760df3a58c24 jp (blog/edit): syntax modifications:
Goffi <goffi@goffi.org>
parents: 2284
diff changeset
571 try:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
572 self._current_syntax = self.args.syntax = self.host.bridge.syntaxGet(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
573 self.current_syntax
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
574 )
2325
760df3a58c24 jp (blog/edit): syntax modifications:
Goffi <goffi@goffi.org>
parents: 2284
diff changeset
575 except Exception as e:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
576 if "NotFound" in unicode(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
577 e
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
578 ): #  FIXME: there is not good way to check bridge errors
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
579 self.parser.error(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
580 _(u"unknown syntax requested ({syntax})").format(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
581 syntax=self.args.syntax
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
582 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
583 )
2325
760df3a58c24 jp (blog/edit): syntax modifications:
Goffi <goffi@goffi.org>
parents: 2284
diff changeset
584 else:
760df3a58c24 jp (blog/edit): syntax modifications:
Goffi <goffi@goffi.org>
parents: 2284
diff changeset
585 raise e
2225
301bb52c8715 jp (blog): service and node can now be specified for edit command
Goffi <goffi@goffi.org>
parents: 2164
diff changeset
586
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
587 (
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
588 self.pubsub_service,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
589 self.pubsub_node,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
590 self.pubsub_item,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
591 content_file_path,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
592 content_file_obj,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
593 mb_data,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
594 ) = self.getItemPath()
1925
53b51866747f jp (blog/edit): HTTP(S) and XMPP URLs can now be directly used in blog/edit command
Goffi <goffi@goffi.org>
parents: 1924
diff changeset
595
2269
606ff34d30f2 jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents: 2267
diff changeset
596 self.edit(content_file_path, content_file_obj, mb_data=mb_data)
1827
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
597
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
598
2532
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2485
diff changeset
599 class Preview(base.CommandBase):
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2485
diff changeset
600 # TODO: need to be rewritten with template output
1874
658824755a0c jp (blog): preview command, first draft
Goffi <goffi@goffi.org>
parents: 1873
diff changeset
601
658824755a0c jp (blog): preview command, first draft
Goffi <goffi@goffi.org>
parents: 1873
diff changeset
602 def __init__(self, host):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
603 base.CommandBase.__init__(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
604 self, host, "preview", use_verbose=True, help=_(u"preview a blog content")
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
605 )
1874
658824755a0c jp (blog): preview command, first draft
Goffi <goffi@goffi.org>
parents: 1873
diff changeset
606
658824755a0c jp (blog): preview command, first draft
Goffi <goffi@goffi.org>
parents: 1873
diff changeset
607 def add_parser_options(self):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
608 self.parser.add_argument(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
609 "--inotify",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
610 type=str,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
611 choices=("auto", "true", "false"),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
612 default=u"auto",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
613 help=_(u"use inotify to handle preview"),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
614 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
615 self.parser.add_argument(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
616 "file",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
617 type=base.unicode_decoder,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
618 nargs="?",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
619 default=u"current",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
620 help=_(u"path to the content file"),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
621 )
1874
658824755a0c jp (blog): preview command, first draft
Goffi <goffi@goffi.org>
parents: 1873
diff changeset
622
1882
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
623 def showPreview(self):
2271
2fae89f30b8d jp (blog): minor typo fix
Goffi <goffi@goffi.org>
parents: 2270
diff changeset
624 # we implement showPreview here so we don't have to import webbrowser and urllib
1882
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
625 # when preview is not used
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
626 url = "file:{}".format(self.urllib.quote(self.preview_file_path))
1882
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
627 self.webbrowser.open_new_tab(url)
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
628
1885
edd8dc8df1b9 jp (blog/preview): open and preview commands can be changed in sat.conf with "blog_preview_open_cmd" and "blog_preview_update_cmd" in [jp] section + don't convert syntax when content is empty
Goffi <goffi@goffi.org>
parents: 1883
diff changeset
629 def _launchPreviewExt(self, cmd_line, opt_name):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
630 url = "file:{}".format(self.urllib.quote(self.preview_file_path))
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
631 args = common.parse_args(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
632 self.host, cmd_line, url=url, preview_file=self.preview_file_path
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
633 )
1885
edd8dc8df1b9 jp (blog/preview): open and preview commands can be changed in sat.conf with "blog_preview_open_cmd" and "blog_preview_update_cmd" in [jp] section + don't convert syntax when content is empty
Goffi <goffi@goffi.org>
parents: 1883
diff changeset
634 if not args:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
635 self.disp(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
636 u'Couln\'t find command in "{name}", abording'.format(name=opt_name),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
637 error=True,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
638 )
1885
edd8dc8df1b9 jp (blog/preview): open and preview commands can be changed in sat.conf with "blog_preview_open_cmd" and "blog_preview_update_cmd" in [jp] section + don't convert syntax when content is empty
Goffi <goffi@goffi.org>
parents: 1883
diff changeset
639 self.host.quit(1)
edd8dc8df1b9 jp (blog/preview): open and preview commands can be changed in sat.conf with "blog_preview_open_cmd" and "blog_preview_update_cmd" in [jp] section + don't convert syntax when content is empty
Goffi <goffi@goffi.org>
parents: 1883
diff changeset
640 subprocess.Popen(args)
edd8dc8df1b9 jp (blog/preview): open and preview commands can be changed in sat.conf with "blog_preview_open_cmd" and "blog_preview_update_cmd" in [jp] section + don't convert syntax when content is empty
Goffi <goffi@goffi.org>
parents: 1883
diff changeset
641
edd8dc8df1b9 jp (blog/preview): open and preview commands can be changed in sat.conf with "blog_preview_open_cmd" and "blog_preview_update_cmd" in [jp] section + don't convert syntax when content is empty
Goffi <goffi@goffi.org>
parents: 1883
diff changeset
642 def openPreviewExt(self):
edd8dc8df1b9 jp (blog/preview): open and preview commands can be changed in sat.conf with "blog_preview_open_cmd" and "blog_preview_update_cmd" in [jp] section + don't convert syntax when content is empty
Goffi <goffi@goffi.org>
parents: 1883
diff changeset
643 self._launchPreviewExt(self.open_cb_cmd, "blog_preview_open_cmd")
edd8dc8df1b9 jp (blog/preview): open and preview commands can be changed in sat.conf with "blog_preview_open_cmd" and "blog_preview_update_cmd" in [jp] section + don't convert syntax when content is empty
Goffi <goffi@goffi.org>
parents: 1883
diff changeset
644
edd8dc8df1b9 jp (blog/preview): open and preview commands can be changed in sat.conf with "blog_preview_open_cmd" and "blog_preview_update_cmd" in [jp] section + don't convert syntax when content is empty
Goffi <goffi@goffi.org>
parents: 1883
diff changeset
645 def updatePreviewExt(self):
edd8dc8df1b9 jp (blog/preview): open and preview commands can be changed in sat.conf with "blog_preview_open_cmd" and "blog_preview_update_cmd" in [jp] section + don't convert syntax when content is empty
Goffi <goffi@goffi.org>
parents: 1883
diff changeset
646 self._launchPreviewExt(self.update_cb_cmd, "blog_preview_update_cmd")
edd8dc8df1b9 jp (blog/preview): open and preview commands can be changed in sat.conf with "blog_preview_open_cmd" and "blog_preview_update_cmd" in [jp] section + don't convert syntax when content is empty
Goffi <goffi@goffi.org>
parents: 1883
diff changeset
647
1882
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
648 def updateContent(self):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
649 with open(self.content_file_path, "rb") as f:
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
650 content = f.read().decode("utf-8-sig")
2330
74c1dbabeec8 jp (blog/edit): pretty format XHTML before editing it
Goffi <goffi@goffi.org>
parents: 2327
diff changeset
651 if content and self.syntax != SYNTAX_XHTML:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
652 # we use safe=True because we want to have a preview as close as possible
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
653 # to what the people will see
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
654 content = self.host.bridge.syntaxConvert(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
655 content, self.syntax, SYNTAX_XHTML, True, self.profile
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
656 )
1882
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
657
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
658 xhtml = (
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
659 u'<html xmlns="http://www.w3.org/1999/xhtml">'
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
660 u'<head><meta http-equiv="Content-Type" content="text/html;charset=utf-8" />'
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
661 u"</head>"
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
662 u"<body>{}</body>"
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
663 u"</html>"
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
664 ).format(content)
1882
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
665
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
666 with open(self.preview_file_path, "wb") as f:
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
667 f.write(xhtml.encode("utf-8"))
1877
a97db84c048d jp (blog): moved common method in a BlogCommon class + added "current" as an item keyword for blog/edit
Goffi <goffi@goffi.org>
parents: 1876
diff changeset
668
1874
658824755a0c jp (blog): preview command, first draft
Goffi <goffi@goffi.org>
parents: 1873
diff changeset
669 def start(self):
1882
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
670 import webbrowser
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
671 import urllib
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
672
1882
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
673 self.webbrowser, self.urllib = webbrowser, urllib
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
674
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
675 if self.args.inotify != "false":
1882
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
676 try:
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
677 import inotify.adapters
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
678 import inotify.constants
1886
f3db27508b31 jp (blog/preview): inotify improvments:
Goffi <goffi@goffi.org>
parents: 1885
diff changeset
679 from inotify.calls import InotifyError
1882
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
680 except ImportError:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
681 if self.args.inotify == "auto":
1882
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
682 inotify = None
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
683 self.disp(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
684 u"inotify module not found, deactivating feature. You can install"
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
685 u" it with {install}".format(install=INOTIFY_INSTALL)
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
686 )
1882
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
687 else:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
688 self.disp(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
689 u"inotify not found, can't activate the feature! Please install "
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
690 u"it with {install}".format(install=INOTIFY_INSTALL),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
691 error=True,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
692 )
1882
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
693 self.host.quit(1)
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
694 else:
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
695 # we deactivate logging in inotify, which is quite annoying
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
696 try:
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
697 inotify.adapters._LOGGER.setLevel(40)
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
698 except AttributeError:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
699 self.disp(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
700 u"Logger doesn't exists, inotify may have chanded", error=True
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
701 )
1882
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
702 else:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
703 inotify = None
1882
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
704
1874
658824755a0c jp (blog): preview command, first draft
Goffi <goffi@goffi.org>
parents: 1873
diff changeset
705 sat_conf = config.parseMainConf()
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
706 SYNTAX_EXT.update(config.getConfig(sat_conf, "jp", CONF_SYNTAX_EXT, {}))
1874
658824755a0c jp (blog): preview command, first draft
Goffi <goffi@goffi.org>
parents: 1873
diff changeset
707
1885
edd8dc8df1b9 jp (blog/preview): open and preview commands can be changed in sat.conf with "blog_preview_open_cmd" and "blog_preview_update_cmd" in [jp] section + don't convert syntax when content is empty
Goffi <goffi@goffi.org>
parents: 1883
diff changeset
708 try:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
709 self.open_cb_cmd = config.getConfig(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
710 sat_conf, "jp", "blog_preview_open_cmd", Exception
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
711 )
1885
edd8dc8df1b9 jp (blog/preview): open and preview commands can be changed in sat.conf with "blog_preview_open_cmd" and "blog_preview_update_cmd" in [jp] section + don't convert syntax when content is empty
Goffi <goffi@goffi.org>
parents: 1883
diff changeset
712 except (NoOptionError, NoSectionError):
edd8dc8df1b9 jp (blog/preview): open and preview commands can be changed in sat.conf with "blog_preview_open_cmd" and "blog_preview_update_cmd" in [jp] section + don't convert syntax when content is empty
Goffi <goffi@goffi.org>
parents: 1883
diff changeset
713 self.open_cb_cmd = None
edd8dc8df1b9 jp (blog/preview): open and preview commands can be changed in sat.conf with "blog_preview_open_cmd" and "blog_preview_update_cmd" in [jp] section + don't convert syntax when content is empty
Goffi <goffi@goffi.org>
parents: 1883
diff changeset
714 open_cb = self.showPreview
edd8dc8df1b9 jp (blog/preview): open and preview commands can be changed in sat.conf with "blog_preview_open_cmd" and "blog_preview_update_cmd" in [jp] section + don't convert syntax when content is empty
Goffi <goffi@goffi.org>
parents: 1883
diff changeset
715 else:
edd8dc8df1b9 jp (blog/preview): open and preview commands can be changed in sat.conf with "blog_preview_open_cmd" and "blog_preview_update_cmd" in [jp] section + don't convert syntax when content is empty
Goffi <goffi@goffi.org>
parents: 1883
diff changeset
716 open_cb = self.openPreviewExt
edd8dc8df1b9 jp (blog/preview): open and preview commands can be changed in sat.conf with "blog_preview_open_cmd" and "blog_preview_update_cmd" in [jp] section + don't convert syntax when content is empty
Goffi <goffi@goffi.org>
parents: 1883
diff changeset
717
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
718 self.update_cb_cmd = config.getConfig(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
719 sat_conf, "jp", "blog_preview_update_cmd", self.open_cb_cmd
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
720 )
1885
edd8dc8df1b9 jp (blog/preview): open and preview commands can be changed in sat.conf with "blog_preview_open_cmd" and "blog_preview_update_cmd" in [jp] section + don't convert syntax when content is empty
Goffi <goffi@goffi.org>
parents: 1883
diff changeset
721 if self.update_cb_cmd is None:
edd8dc8df1b9 jp (blog/preview): open and preview commands can be changed in sat.conf with "blog_preview_open_cmd" and "blog_preview_update_cmd" in [jp] section + don't convert syntax when content is empty
Goffi <goffi@goffi.org>
parents: 1883
diff changeset
722 update_cb = self.showPreview
edd8dc8df1b9 jp (blog/preview): open and preview commands can be changed in sat.conf with "blog_preview_open_cmd" and "blog_preview_update_cmd" in [jp] section + don't convert syntax when content is empty
Goffi <goffi@goffi.org>
parents: 1883
diff changeset
723 else:
edd8dc8df1b9 jp (blog/preview): open and preview commands can be changed in sat.conf with "blog_preview_open_cmd" and "blog_preview_update_cmd" in [jp] section + don't convert syntax when content is empty
Goffi <goffi@goffi.org>
parents: 1883
diff changeset
724 update_cb = self.updatePreviewExt
1882
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
725
1874
658824755a0c jp (blog): preview command, first draft
Goffi <goffi@goffi.org>
parents: 1873
diff changeset
726 # which file do we need to edit?
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
727 if self.args.file == "current":
1921
b111f6589da4 jp (blog): drafts are now put in a sub dir per profile + added a security which keep the last 10 drafts before removing them (all profiles included).
Goffi <goffi@goffi.org>
parents: 1919
diff changeset
728 self.content_file_path = self.getCurrentFile(sat_conf, self.profile)
1874
658824755a0c jp (blog): preview command, first draft
Goffi <goffi@goffi.org>
parents: 1873
diff changeset
729 else:
1885
edd8dc8df1b9 jp (blog/preview): open and preview commands can be changed in sat.conf with "blog_preview_open_cmd" and "blog_preview_update_cmd" in [jp] section + don't convert syntax when content is empty
Goffi <goffi@goffi.org>
parents: 1883
diff changeset
730 self.content_file_path = os.path.abspath(self.args.file)
1874
658824755a0c jp (blog): preview command, first draft
Goffi <goffi@goffi.org>
parents: 1873
diff changeset
731
2598
0b6adc2672d9 jp (blog/preview): fixed guessSyntaxFromPath call
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
732 self.syntax = guessSyntaxFromPath(self.host, sat_conf, self.content_file_path)
1882
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
733
1877
a97db84c048d jp (blog): moved common method in a BlogCommon class + added "current" as an item keyword for blog/edit
Goffi <goffi@goffi.org>
parents: 1876
diff changeset
734 # at this point the syntax is converted, we can display the preview
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
735 preview_file = tempfile.NamedTemporaryFile(suffix=".xhtml", delete=False)
1882
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
736 self.preview_file_path = preview_file.name
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
737 preview_file.close()
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
738 self.updateContent()
1877
a97db84c048d jp (blog): moved common method in a BlogCommon class + added "current" as an item keyword for blog/edit
Goffi <goffi@goffi.org>
parents: 1876
diff changeset
739
1882
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
740 if inotify is None:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
741 # XXX: we don't delete file automatically because browser need it
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
742 # (and webbrowser.open can return before it is read)
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
743 self.disp(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
744 u"temporary file created at {}\nthis file will NOT BE DELETED "
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
745 u"AUTOMATICALLY, please delete it yourself when you have finished".format(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
746 self.preview_file_path
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
747 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
748 )
1882
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
749 open_cb()
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
750 else:
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
751 open_cb()
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
752 i = inotify.adapters.Inotify(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
753 block_duration_s=60
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
754 ) # no need for 1 s duraction, inotify drive actions here
1886
f3db27508b31 jp (blog/preview): inotify improvments:
Goffi <goffi@goffi.org>
parents: 1885
diff changeset
755
f3db27508b31 jp (blog/preview): inotify improvments:
Goffi <goffi@goffi.org>
parents: 1885
diff changeset
756 def add_watch():
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
757 i.add_watch(
2639
266f871fcb4b jp (blog): fixed path encoding for inotify
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
758 self.content_file_path.encode('utf-8'),
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
759 mask=inotify.constants.IN_CLOSE_WRITE
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
760 | inotify.constants.IN_DELETE_SELF
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
761 | inotify.constants.IN_MOVE_SELF,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
762 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
763
1886
f3db27508b31 jp (blog/preview): inotify improvments:
Goffi <goffi@goffi.org>
parents: 1885
diff changeset
764 add_watch()
f3db27508b31 jp (blog/preview): inotify improvments:
Goffi <goffi@goffi.org>
parents: 1885
diff changeset
765
1882
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
766 try:
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
767 for event in i.event_gen():
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
768 if event is not None:
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
769 self.disp(u"Content updated", 1)
1893
4f252b72b193 jp (blog/preview): change watch for IN_MOVE_SELF event, needed at least for Emacs
Goffi <goffi@goffi.org>
parents: 1888
diff changeset
770 if {"IN_DELETE_SELF", "IN_MOVE_SELF"}.intersection(event[1]):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
771 self.disp(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
772 u"{} event catched, changing the watch".format(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
773 ", ".join(event[1])
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
774 ),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
775 2,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
776 )
1893
4f252b72b193 jp (blog/preview): change watch for IN_MOVE_SELF event, needed at least for Emacs
Goffi <goffi@goffi.org>
parents: 1888
diff changeset
777 try:
4f252b72b193 jp (blog/preview): change watch for IN_MOVE_SELF event, needed at least for Emacs
Goffi <goffi@goffi.org>
parents: 1888
diff changeset
778 add_watch()
4f252b72b193 jp (blog/preview): change watch for IN_MOVE_SELF event, needed at least for Emacs
Goffi <goffi@goffi.org>
parents: 1888
diff changeset
779 except InotifyError:
4f252b72b193 jp (blog/preview): change watch for IN_MOVE_SELF event, needed at least for Emacs
Goffi <goffi@goffi.org>
parents: 1888
diff changeset
780 # if the new file is not here yet we can have an error
4f252b72b193 jp (blog/preview): change watch for IN_MOVE_SELF event, needed at least for Emacs
Goffi <goffi@goffi.org>
parents: 1888
diff changeset
781 # as a workaround, we do a little rest
4f252b72b193 jp (blog/preview): change watch for IN_MOVE_SELF event, needed at least for Emacs
Goffi <goffi@goffi.org>
parents: 1888
diff changeset
782 time.sleep(1)
4f252b72b193 jp (blog/preview): change watch for IN_MOVE_SELF event, needed at least for Emacs
Goffi <goffi@goffi.org>
parents: 1888
diff changeset
783 add_watch()
1882
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
784 self.updateContent()
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
785 update_cb()
1886
f3db27508b31 jp (blog/preview): inotify improvments:
Goffi <goffi@goffi.org>
parents: 1885
diff changeset
786 except InotifyError:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
787 self.disp(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
788 u"Can't catch inotify events, as the file been deleted?", error=True
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
789 )
1882
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
790 finally:
ed444e9aa078 jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents: 1881
diff changeset
791 os.unlink(self.preview_file_path)
1886
f3db27508b31 jp (blog/preview): inotify improvments:
Goffi <goffi@goffi.org>
parents: 1885
diff changeset
792 try:
f3db27508b31 jp (blog/preview): inotify improvments:
Goffi <goffi@goffi.org>
parents: 1885
diff changeset
793 i.remove_watch(self.content_file_path)
f3db27508b31 jp (blog/preview): inotify improvments:
Goffi <goffi@goffi.org>
parents: 1885
diff changeset
794 except InotifyError:
f3db27508b31 jp (blog/preview): inotify improvments:
Goffi <goffi@goffi.org>
parents: 1885
diff changeset
795 pass
1874
658824755a0c jp (blog): preview command, first draft
Goffi <goffi@goffi.org>
parents: 1873
diff changeset
796
658824755a0c jp (blog): preview command, first draft
Goffi <goffi@goffi.org>
parents: 1873
diff changeset
797
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
798 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
799 def __init__(self, host):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
800 super(Import, self).__init__(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
801 host,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
802 "import",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
803 use_pubsub=True,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
804 use_progress=True,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
805 help=_(u"import an external blog"),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
806 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
807 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
808
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
809 def add_parser_options(self):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
810 self.parser.add_argument(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
811 "importer",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
812 type=base.unicode_decoder,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
813 nargs="?",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
814 help=_(u"importer name, nothing to display importers list"),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
815 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
816 self.parser.add_argument(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
817 "--host", type=base.unicode_decoder, help=_(u"original blog host")
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
818 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
819 self.parser.add_argument(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
820 "--no-images-upload",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
821 action="store_true",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
822 help=_(u"do *NOT* upload images (default: do upload images)"),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
823 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
824 self.parser.add_argument(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
825 "--upload-ignore-host",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
826 help=_(u"do not upload images from this host (default: upload all images)"),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
827 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
828 self.parser.add_argument(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
829 "--ignore-tls-errors",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
830 action="store_true",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
831 help=_("ignore invalide TLS certificate for uploads"),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
832 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
833 self.parser.add_argument(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
834 "-o",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
835 "--option",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
836 action="append",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
837 nargs=2,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
838 default=[],
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
839 metavar=(u"NAME", u"VALUE"),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
840 help=_(u"importer specific options (see importer description)"),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
841 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
842 self.parser.add_argument(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
843 "location",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
844 type=base.unicode_decoder,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
845 nargs="?",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
846 help=_(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
847 u"importer data location (see importer description), nothing to show "
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
848 u"importer description"
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
849 ),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
850 )
1827
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
851
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
852 def onProgressStarted(self, metadata):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
853 self.disp(_(u"Blog upload started"), 2)
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
854
6209de5e3e25 jp (blog): import now show progression + value to add to sat.conf for URLs redirection
Goffi <goffi@goffi.org>
parents: 1827
diff changeset
855 def onProgressFinished(self, metadata):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
856 self.disp(_(u"Blog uploaded successfully"), 2)
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
857 redirections = {
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
858 k[len(URL_REDIRECT_PREFIX) :]: v
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
859 for k, v in metadata.iteritems()
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
860 if k.startswith(URL_REDIRECT_PREFIX)
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
861 }
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
862 if redirections:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
863 conf = u"\n".join(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
864 [
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
865 u"url_redirections_dict = {}".format(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
866 # we need to add ' ' before each new line
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
867 # and to double each '%' for ConfigParser
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
868 u"\n ".join(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
869 json.dumps(redirections, indent=1, separators=(",", ": "))
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
870 .replace(u"%", u"%%")
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
871 .split(u"\n")
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
872 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
873 ),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
874 ]
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
875 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
876 self.disp(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
877 _(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
878 u"\nTo redirect old URLs to new ones, put the following lines in your"
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
879 u" sat.conf file, in [libervia] section:\n\n{conf}".format(conf=conf)
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
880 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
881 )
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
882
6209de5e3e25 jp (blog): import now show progression + value to add to sat.conf for URLs redirection
Goffi <goffi@goffi.org>
parents: 1827
diff changeset
883 def onProgressError(self, error_msg):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
884 self.disp(_(u"Error while uploading blog: {}").format(error_msg), error=True)
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
885
6209de5e3e25 jp (blog): import now show progression + value to add to sat.conf for URLs redirection
Goffi <goffi@goffi.org>
parents: 1827
diff changeset
886 def error(self, failure):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
887 self.disp(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
888 _("Error while trying to upload a blog: {reason}").format(reason=failure),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
889 error=True,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
890 )
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
891 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
892
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
893 def start(self):
1827
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
894 if self.args.location is None:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
895 for name in ("option", "service", "no_images_upload"):
1827
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
896 if getattr(self.args, name):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
897 self.parser.error(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
898 _(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
899 u"{name} argument can't be used without location argument"
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
900 ).format(name=name)
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
901 )
1827
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
902 if self.args.importer is None:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
903 self.disp(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
904 u"\n".join(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
905 [
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
906 u"{}: {}".format(name, desc)
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
907 for name, desc in self.host.bridge.blogImportList()
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
908 ]
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
909 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
910 )
1827
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
911 else:
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
912 try:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
913 short_desc, long_desc = self.host.bridge.blogImportDesc(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
914 self.args.importer
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
915 )
1827
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
916 except Exception as e:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
917 msg = [l for l in unicode(e).split("\n") if l][
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
918 -1
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
919 ] # we only keep the last line
2161
62dfa6e02f54 jp (base, blog, output(std)): fixed bad unicode handling in stdout, use of jp.disp instead of simple print
Goffi <goffi@goffi.org>
parents: 2158
diff changeset
920 self.disp(msg)
1827
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
921 self.host.quit(1)
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
922 else:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
923 self.disp(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
924 u"{name}: {short_desc}\n\n{long_desc}".format(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
925 name=self.args.importer,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
926 short_desc=short_desc,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
927 long_desc=long_desc,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
928 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
929 )
1827
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
930 self.host.quit()
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
931 else:
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
932 # 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
933 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
934 if self.args.host:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
935 options["host"] = self.args.host
1827
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
936 if self.args.ignore_tls_errors:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
937 options["ignore_tls_errors"] = C.BOOL_TRUE
1827
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
938 if self.args.no_images_upload:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
939 options["upload_images"] = C.BOOL_FALSE
1827
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
940 if self.args.upload_ignore_host:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
941 self.parser.error(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
942 u"upload-ignore-host option can't be used when no-images-upload "
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
943 u"is set"
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
944 )
1827
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
945 elif self.args.upload_ignore_host:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
946 options["upload_ignore_host"] = self.args.upload_ignore_host
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
947
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
948 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
949 self.progress_id = id_
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
950
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
951 self.host.bridge.blogImport(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
952 self.args.importer,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
953 self.args.location,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
954 options,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
955 self.args.service,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
956 self.args.node,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
957 self.profile,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
958 callback=gotId,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
959 errback=self.error,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
960 )
1827
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
961
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
962
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
963 class Blog(base.CommandBase):
2458
4841ad6a5db4 jp (blog): added "set" command to publish content from stdin without editing
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
964 subcommands = (Set, Get, Edit, Preview, Import)
1827
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
965
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
966 def __init__(self, host):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
967 super(Blog, self).__init__(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
968 host, "blog", use_profile=False, help=_("blog/microblog management")
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
969 )