annotate sat_frontends/jp/cmd_blog.py @ 3028:ab2696e34d29

Python 3 port: /!\ this is a huge commit /!\ starting from this commit, SàT is needs Python 3.6+ /!\ SàT maybe be instable or some feature may not work anymore, this will improve with time This patch port backend, bridge and frontends to Python 3. Roughly this has been done this way: - 2to3 tools has been applied (with python 3.7) - all references to python2 have been replaced with python3 (notably shebangs) - fixed files not handled by 2to3 (notably the shell script) - several manual fixes - fixed issues reported by Python 3 that where not handled in Python 2 - replaced "async" with "async_" when needed (it's a reserved word from Python 3.7) - replaced zope's "implements" with @implementer decorator - temporary hack to handle data pickled in database, as str or bytes may be returned, to be checked later - fixed hash comparison for password - removed some code which is not needed anymore with Python 3 - deactivated some code which needs to be checked (notably certificate validation) - tested with jp, fixed reported issues until some basic commands worked - ported Primitivus (after porting dependencies like urwid satext) - more manual fixes
author Goffi <goffi@goffi.org>
date Tue, 13 Aug 2019 19:08:41 +0200
parents d603550d5e99
children fee60f17ebac
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
2771
003b8b4b56a7 date update
Goffi <goffi@goffi.org>
parents: 2761
diff changeset
5 # Copyright (C) 2009-2019 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
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
21 from . import base
1827
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
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
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
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
43 SYNTAX_XHTML = "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 = {
2944
efb915a20d5d jp (blog/set): fixed missing current_syntax
Goffi <goffi@goffi.org>
parents: 2943
diff changeset
46 # FIXME: default syntax doesn't sounds needed, there should always be a syntax set
efb915a20d5d jp (blog/set): fixed missing current_syntax
Goffi <goffi@goffi.org>
parents: 2943
diff changeset
47 # by the plugin.
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
48 "": "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
49 SYNTAX_XHTML: "xhtml",
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
50 "markdown": "md",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
51 }
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
52
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
53
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
54 CONF_SYNTAX_EXT = "syntax_ext_dict"
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
55 BLOG_TMP_DIR = "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
56 # 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
57 KEY_TO_REMOVE_METADATA = (
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
58 "id",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
59 "content",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
60 "content_xhtml",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
61 "comments_node",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
62 "comments_service",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
63 "updated",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
64 )
1866
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
65
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
66 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
67 INOTIFY_INSTALL = '"pip install inotify"'
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
68 MB_KEYS = (
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
69 "id",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
70 "url",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
71 "atom_id",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
72 "updated",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
73 "published",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
74 "language",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
75 "comments", # this key is used for all comments* keys
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
76 "tags", # this key is used for all tag* keys
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
77 "author",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
78 "author_jid",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
79 "author_email",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
80 "author_jid_verified",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
81 "content",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
82 "content_xhtml",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
83 "title",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
84 "title_xhtml",
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
85 )
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
86 OUTPUT_OPT_NO_HEADER = "no-header"
1827
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
87
1866
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
88
2532
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2485
diff changeset
89 def guessSyntaxFromPath(host, sat_conf, path):
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2485
diff changeset
90 """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
91
2532
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2485
diff changeset
92 @param sat_conf(ConfigParser.ConfigParser): instance opened on sat configuration
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2485
diff changeset
93 @param path(str): path to the content file
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2485
diff changeset
94 @return(unicode): syntax to use
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2485
diff changeset
95 """
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2485
diff changeset
96 # we first try to guess syntax with extension
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
97 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
98 if ext:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
99 for k, v in SYNTAX_EXT.items():
2532
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2485
diff changeset
100 if k and ext == v:
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2485
diff changeset
101 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
102
2532
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2485
diff changeset
103 # if not found, we use current syntax
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2485
diff changeset
104 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
105
658824755a0c jp (blog): preview command, first draft
Goffi <goffi@goffi.org>
parents: 1873
diff changeset
106
2458
4841ad6a5db4 jp (blog): added "set" command to publish content from stdin without editing
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
107 class BlogPublishCommon(object):
4841ad6a5db4 jp (blog): added "set" command to publish content from stdin without editing
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
108 """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
109
2944
efb915a20d5d jp (blog/set): fixed missing current_syntax
Goffi <goffi@goffi.org>
parents: 2943
diff changeset
110 @property
efb915a20d5d jp (blog/set): fixed missing current_syntax
Goffi <goffi@goffi.org>
parents: 2943
diff changeset
111 def current_syntax(self):
efb915a20d5d jp (blog/set): fixed missing current_syntax
Goffi <goffi@goffi.org>
parents: 2943
diff changeset
112 if self._current_syntax is None:
efb915a20d5d jp (blog/set): fixed missing current_syntax
Goffi <goffi@goffi.org>
parents: 2943
diff changeset
113 self._current_syntax = self.host.bridge.getParamA(
efb915a20d5d jp (blog/set): fixed missing current_syntax
Goffi <goffi@goffi.org>
parents: 2943
diff changeset
114 "Syntax", "Composition", "value", self.profile
efb915a20d5d jp (blog/set): fixed missing current_syntax
Goffi <goffi@goffi.org>
parents: 2943
diff changeset
115 )
efb915a20d5d jp (blog/set): fixed missing current_syntax
Goffi <goffi@goffi.org>
parents: 2943
diff changeset
116 return self._current_syntax
efb915a20d5d jp (blog/set): fixed missing current_syntax
Goffi <goffi@goffi.org>
parents: 2943
diff changeset
117
2458
4841ad6a5db4 jp (blog): added "set" command to publish content from stdin without editing
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
118 def add_parser_options(self):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
119 self.parser.add_argument(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
120 "-T", "--title", help=_("title of the item")
2624
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 "-t",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
124 "--tag",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
125 action="append",
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
126 help=_("tag (category) of your item"),
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
127 )
2808
51c53fc4fc4a jp (blog/edit): changed comments handling:
Goffi <goffi@goffi.org>
parents: 2807
diff changeset
128
51c53fc4fc4a jp (blog/edit): changed comments handling:
Goffi <goffi@goffi.org>
parents: 2807
diff changeset
129 comments_group = self.parser.add_mutually_exclusive_group()
51c53fc4fc4a jp (blog/edit): changed comments handling:
Goffi <goffi@goffi.org>
parents: 2807
diff changeset
130 comments_group.add_argument(
51c53fc4fc4a jp (blog/edit): changed comments handling:
Goffi <goffi@goffi.org>
parents: 2807
diff changeset
131 "-C", "--comments", action="store_const", const=True, dest="comments",
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
132 help=_("enable comments (default: comments not enabled except if they "
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
133 "already exist)")
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
134 )
2808
51c53fc4fc4a jp (blog/edit): changed comments handling:
Goffi <goffi@goffi.org>
parents: 2807
diff changeset
135 comments_group.add_argument(
51c53fc4fc4a jp (blog/edit): changed comments handling:
Goffi <goffi@goffi.org>
parents: 2807
diff changeset
136 "--no-comments", action="store_const", const=False, dest="comments",
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
137 help=_("disable comments (will remove comments node if it exist)")
2808
51c53fc4fc4a jp (blog/edit): changed comments handling:
Goffi <goffi@goffi.org>
parents: 2807
diff changeset
138 )
51c53fc4fc4a jp (blog/edit): changed comments handling:
Goffi <goffi@goffi.org>
parents: 2807
diff changeset
139
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
140 self.parser.add_argument(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
141 "-S",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
142 "--syntax",
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
143 help=_("syntax to use (default: get profile's default syntax)"),
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
144 )
2458
4841ad6a5db4 jp (blog): added "set" command to publish content from stdin without editing
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
145
4841ad6a5db4 jp (blog): added "set" command to publish content from stdin without editing
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
146 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
147 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
148 # default syntax has been used
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
149 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
150 elif self.current_syntax == SYNTAX_XHTML:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
151 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
152 else:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
153 mb_data["content_xhtml"] = self.host.bridge.syntaxConvert(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
154 content, self.current_syntax, SYNTAX_XHTML, False, self.profile
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
155 )
2458
4841ad6a5db4 jp (blog): added "set" command to publish content from stdin without editing
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
156
4841ad6a5db4 jp (blog): added "set" command to publish content from stdin without editing
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
157 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
158 """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
159
4841ad6a5db4 jp (blog): added "set" command to publish content from stdin without editing
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
160 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
161 """
2807
0b7ce5daee9b plugin XEP-0277: blog items data are now entirely serialised before going to bridge:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
162 if self.args.comments is not None:
0b7ce5daee9b plugin XEP-0277: blog items data are now entirely serialised before going to bridge:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
163 mb_data["allow_comments"] = 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
164 if self.args.tag:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
165 mb_data['tags'] = self.args.tag
2458
4841ad6a5db4 jp (blog): added "set" command to publish content from stdin without editing
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
166 if self.args.title is not None:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
167 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
168
4841ad6a5db4 jp (blog): added "set" command to publish content from stdin without editing
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
169
2532
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2485
diff changeset
170 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
171 def __init__(self, host):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
172 base.CommandBase.__init__(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
173 self,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
174 host,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
175 "set",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
176 use_pubsub=True,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
177 pubsub_flags={C.SINGLE_ITEM},
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
178 help=_("publish a new blog item or update an existing one"),
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
179 )
2458
4841ad6a5db4 jp (blog): added "set" command to publish content from stdin without editing
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
180 BlogPublishCommon.__init__(self)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
181 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
182
4841ad6a5db4 jp (blog): added "set" command to publish content from stdin without editing
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
183 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
184 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
185
4841ad6a5db4 jp (blog): added "set" command to publish content from stdin without editing
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
186 def mbSendCb(self):
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
187 self.disp("Item published")
2458
4841ad6a5db4 jp (blog): added "set" command to publish content from stdin without editing
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
188 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
189
4841ad6a5db4 jp (blog): added "set" command to publish content from stdin without editing
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
190 def start(self):
2944
efb915a20d5d jp (blog/set): fixed missing current_syntax
Goffi <goffi@goffi.org>
parents: 2943
diff changeset
191 self._current_syntax = self.args.syntax
2458
4841ad6a5db4 jp (blog): added "set" command to publish content from stdin without editing
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
192 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
193 mb_data = {}
4841ad6a5db4 jp (blog): added "set" command to publish content from stdin without editing
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
194 self.setMbDataFromArgs(mb_data)
2910
b2f323237fce jp, plugin merge-requests: used u'' as default for item id in pubsub arguments + fixed some required arguments:
Goffi <goffi@goffi.org>
parents: 2876
diff changeset
195 if self.pubsub_item:
2876
84f82d014d06 jp (blog/set): fixed use of --item argument
Goffi <goffi@goffi.org>
parents: 2870
diff changeset
196 mb_data["id"] = self.pubsub_item
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
197 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
198 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
199
4841ad6a5db4 jp (blog): added "set" command to publish content from stdin without editing
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
200 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
201 self.args.service,
4841ad6a5db4 jp (blog): added "set" command to publish content from stdin without editing
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
202 self.args.node,
2807
0b7ce5daee9b plugin XEP-0277: blog items data are now entirely serialised before going to bridge:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
203 data_format.serialise(mb_data),
2458
4841ad6a5db4 jp (blog): added "set" command to publish content from stdin without editing
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
204 self.profile,
4841ad6a5db4 jp (blog): added "set" command to publish content from stdin without editing
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
205 callback=self.exitCb,
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
206 errback=partial(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
207 self.errback,
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
208 msg=_("can't send item: {}"),
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
209 exit_code=C.EXIT_BRIDGE_ERRBACK,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
210 ),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
211 )
2458
4841ad6a5db4 jp (blog): added "set" command to publish content from stdin without editing
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
212
4841ad6a5db4 jp (blog): added "set" command to publish content from stdin without editing
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
213
2532
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2485
diff changeset
214 class Get(base.CommandBase):
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
215 TEMPLATE = "blog/articles.html"
2157
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
216
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
217 def __init__(self, host):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
218 extra_outputs = {"default": self.default_output, "fancy": self.fancy_output}
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
219 base.CommandBase.__init__(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
220 self,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
221 host,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
222 "get",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
223 use_verbose=True,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
224 use_pubsub=True,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
225 pubsub_flags={C.MULTI_ITEMS},
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
226 use_output=C.OUTPUT_COMPLEX,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
227 extra_outputs=extra_outputs,
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
228 help=_("get blog item(s)"),
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
229 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
230 self.need_loop = True
2157
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
231
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
232 def add_parser_options(self):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
233 #  TODO: a key(s) argument to select keys to display
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
234 self.parser.add_argument(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
235 "-k",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
236 "--key",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
237 action="append",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
238 dest="keys",
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
239 help=_("microblog data key(s) to display (default: depend of verbosity)"),
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
240 )
2157
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
241 # TODO: add MAM filters
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
242
2164
63d191c05ecd jp (blog): set default template and data mapping for the new template output
Goffi <goffi@goffi.org>
parents: 2161
diff changeset
243 def template_data_mapping(self, data):
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
244 return {"items": data_objects.BlogItems(data, deserialise=False)}
2164
63d191c05ecd jp (blog): set default template and data mapping for the new template output
Goffi <goffi@goffi.org>
parents: 2161
diff changeset
245
2157
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
246 def format_comments(self, item, keys):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
247 comments_data = data_format.dict2iterdict(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
248 "comments", item, ("node", "service"), pop=True
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
249 )
2157
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
250 lines = []
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
251 for data in comments_data:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
252 lines.append(data["comments"])
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
253 for k in ("node", "service"):
2157
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
254 if OUTPUT_OPT_NO_HEADER in self.args.output_opts:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
255 header = ""
2157
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
256 else:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
257 header = C.A_HEADER + k + ": " + A.RESET
2157
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
258 lines.append(header + data[k])
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
259 return "\n".join(lines)
2157
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
260
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
261 def format_tags(self, item, keys):
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
262 tags = item.pop('tags', [])
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
263 return ", ".join(tags)
2157
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
264
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
265 def format_updated(self, item, keys):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
266 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
267
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
268 def format_published(self, item, keys):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
269 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
270
2327
dad500e7ae50 jp (blog/get): added "url" key, which compute xmpp: URL of items
Goffi <goffi@goffi.org>
parents: 2325
diff changeset
271 def format_url(self, item, keys):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
272 return uri.buildXMPPUri(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
273 "pubsub",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
274 subtype="microblog",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
275 path=self.metadata["service"],
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
276 node=self.metadata["node"],
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
277 item=item["id"],
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
278 )
2327
dad500e7ae50 jp (blog/get): added "url" key, which compute xmpp: URL of items
Goffi <goffi@goffi.org>
parents: 2325
diff changeset
279
2157
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
280 def get_keys(self):
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
281 """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
282 verbosity = self.args.verbose
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
283 if self.args.keys:
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
284 if not set(MB_KEYS).issuperset(self.args.keys):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
285 self.disp(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
286 "following keys are invalid: {invalid}.\n"
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
287 "Valid keys are: {valid}.".format(
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
288 invalid=", ".join(set(self.args.keys).difference(MB_KEYS)),
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
289 valid=", ".join(sorted(MB_KEYS)),
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
290 ),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
291 error=True,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
292 )
2157
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
293 self.host.quit(C.EXIT_BAD_ARG)
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
294 return self.args.keys
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
295 else:
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
296 if verbosity == 0:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
297 return ("title", "content")
2157
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
298 elif verbosity == 1:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
299 return (
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
300 "title",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
301 "tags",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
302 "author",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
303 "author_jid",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
304 "author_email",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
305 "author_jid_verified",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
306 "published",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
307 "updated",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
308 "content",
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
309 )
2157
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
310 else:
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
311 return MB_KEYS
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
312
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
313 def default_output(self, data):
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
314 """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
315 items, self.metadata = data
2157
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
316 keys = self.get_keys()
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
317
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
318 #  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
319 k_cb = {}
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
320 for k in keys:
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
321 try:
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
322 callback = getattr(self, "format_" + k)
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
323 except AttributeError:
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
324 pass
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
325 else:
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
326 k_cb[k] = callback
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
327 for idx, item in enumerate(items):
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
328 for k in keys:
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
329 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
330 continue
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
331 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
332 header = ""
2157
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
333 else:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
334 header = "{k_fmt}{key}:{k_fmt_e} {sep}".format(
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
335 k_fmt=C.A_HEADER,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
336 key=k,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
337 k_fmt_e=A.RESET,
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
338 sep="\n" if "content" in k else "",
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
339 )
2157
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
340 value = k_cb[k](item, keys) if k in k_cb else item[k]
2807
0b7ce5daee9b plugin XEP-0277: blog items data are now entirely serialised before going to bridge:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
341 if isinstance(value, bool):
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
342 value = str(value).lower()
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
343 self.disp(header + value)
2157
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
344 # 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
345 if idx < len(items) - 1:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
346 print("")
2157
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
347
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
348 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
349 """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
350
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
351 @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
352 @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
353 """
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
354 fmt = "%d/%m/%Y %H:%M:%S"
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
355 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
356
2157
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
357 def fancy_output(self, data):
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
358 """display blog is a nice to read way
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
359
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
360 this output doesn't use keys filter
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
361 """
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
362 # thanks to http://stackoverflow.com/a/943921
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
363 rows, columns = list(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
364 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
365 verbosity = self.args.verbose
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
366 sep = A.color(A.FG_BLUE, columns * "▬")
2157
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
367 if items:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
368 print(("\n" + sep + "\n"))
2157
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
369
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
370 for idx, item in enumerate(items):
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
371 title = item.get("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
372 if verbosity > 0:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
373 author = item["author"]
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
374 published, updated = item["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
375 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
376 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
377 if verbosity > 1:
2807
0b7ce5daee9b plugin XEP-0277: blog items data are now entirely serialised before going to bridge:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
378 tags = item.pop('tags', [])
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
379 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
380 tags = None
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
381 content = item.get("content")
2157
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
382
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
383 if title:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
384 print((A.color(A.BOLD, A.FG_CYAN, item["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
385 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
386 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
387 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
388 if published:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
389 meta.append(A.color(A.FG_YELLOW, "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
390 if updated != published:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
391 meta.append(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
392 A.color(A.FG_YELLOW, "(updated on ", self.format_time(updated), ")")
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
393 )
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
394 print((" ".join(meta)))
2157
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
395 if tags:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
396 print((A.color(A.FG_MAGENTA, ", ".join(tags))))
2157
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
397 if (title or tags) and content:
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
398 print("")
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
399 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
400 self.disp(content)
2157
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
401
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
402 print(("\n" + sep + "\n"))
2157
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
403
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
404 def mbGetCb(self, mb_result):
2807
0b7ce5daee9b plugin XEP-0277: blog items data are now entirely serialised before going to bridge:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
405 items, metadata = mb_result
0b7ce5daee9b plugin XEP-0277: blog items data are now entirely serialised before going to bridge:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
406 items = [data_format.deserialise(i) for i in items]
0b7ce5daee9b plugin XEP-0277: blog items data are now entirely serialised before going to bridge:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
407 mb_result = items, metadata
2157
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
408 self.output(mb_result)
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
409 self.host.quit(C.EXIT_OK)
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
410
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
411 def mbGetEb(self, failure_):
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
412 self.disp("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
413 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
414
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
415 def start(self):
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
416 self.host.bridge.mbGet(
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
417 self.args.service,
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
418 self.args.node,
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
419 self.args.max,
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
420 self.args.items,
2761
4b693ea24d5f jp (base, pubsub, ticket): handle order-by:
Goffi <goffi@goffi.org>
parents: 2673
diff changeset
421 self.getPubsubExtra(),
2157
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
422 self.profile,
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
423 callback=self.mbGetCb,
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
424 errback=self.mbGetEb,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
425 )
2157
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
426
b4a515e36631 jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents: 1969
diff changeset
427
2532
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2485
diff changeset
428 class Edit(base.CommandBase, BlogPublishCommon, common.BaseEdit):
1866
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
429 def __init__(self, host):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
430 base.CommandBase.__init__(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
431 self,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
432 host,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
433 "edit",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
434 use_pubsub=True,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
435 pubsub_flags={C.SINGLE_ITEM},
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
436 use_draft=True,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
437 use_verbose=True,
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
438 help=_("edit an existing or new blog post"),
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
439 )
2458
4841ad6a5db4 jp (blog): added "set" command to publish content from stdin without editing
Goffi <goffi@goffi.org>
parents: 2414
diff changeset
440 BlogPublishCommon.__init__(self)
2269
606ff34d30f2 jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents: 2267
diff changeset
441 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
442
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
443 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
444 BlogPublishCommon.add_parser_options(self)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
445 self.parser.add_argument(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
446 "-P",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
447 "--preview",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
448 action="store_true",
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
449 help=_("launch a blog preview in parallel"),
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
450 )
1866
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
451
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
452 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
453 """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
454
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
455 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
456 @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
457 @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
458 @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
459 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
460 """
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
461 # 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
462 # 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
463 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
464 if os.path.exists(meta_file_path):
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
465 self.disp("Metadata file already exists, we re-use it")
1876
1088bf7b28e7 jp (blog/edit): metadata file is reused if it already exists
Goffi <goffi@goffi.org>
parents: 1874
diff changeset
466 try:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
467 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
468 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
469 except (OSError, IOError, ValueError) as e:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
470 self.disp(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
471 "Can't read existing metadata file at {path}, aborting: {reason}".format(
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
472 path=meta_file_path, reason=e
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
473 ),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
474 error=True,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
475 )
1876
1088bf7b28e7 jp (blog/edit): metadata file is reused if it already exists
Goffi <goffi@goffi.org>
parents: 1874
diff changeset
476 self.host.quit(1)
1088bf7b28e7 jp (blog/edit): metadata file is reused if it already exists
Goffi <goffi@goffi.org>
parents: 1874
diff changeset
477 else:
1088bf7b28e7 jp (blog/edit): metadata file is reused if it already exists
Goffi <goffi@goffi.org>
parents: 1874
diff changeset
478 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
479
1088bf7b28e7 jp (blog/edit): metadata file is reused if it already exists
Goffi <goffi@goffi.org>
parents: 1874
diff changeset
480 # 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
481 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
482 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
483 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
484 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
485 pass
1876
1088bf7b28e7 jp (blog/edit): metadata file is reused if it already exists
Goffi <goffi@goffi.org>
parents: 1874
diff changeset
486 # 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
487 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
488
1876
1088bf7b28e7 jp (blog/edit): metadata file is reused if it already exists
Goffi <goffi@goffi.org>
parents: 1874
diff changeset
489 # 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
490 # 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
491 with os.fdopen(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
492 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
493 ) 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
494 # 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
495 unicode_dump = json.dumps(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
496 mb_data,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
497 ensure_ascii=False,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
498 indent=4,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
499 separators=(",", ": "),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
500 sort_keys=True,
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 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
503
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
504 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
505
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
506 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
507 """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
508 # 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
509 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
510
1887
16527dd5a81b blog (jp/edit): added --preview option to launch a "blog preview" in parallel
Goffi <goffi@goffi.org>
parents: 1886
diff changeset
511 # 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
512 if self.args.preview:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
513 self.disp("Preview requested, launching it", 1)
2998
a1b98772af6b Backed out changeset cf0dda0d4cda
Goffi <goffi@goffi.org>
parents: 2997
diff changeset
514 # we redirect outputs to /dev/null to avoid console pollution in editor
a1b98772af6b Backed out changeset cf0dda0d4cda
Goffi <goffi@goffi.org>
parents: 2997
diff changeset
515 # if user wants to see messages, (s)he can call "blog preview" directly
a1b98772af6b Backed out changeset cf0dda0d4cda
Goffi <goffi@goffi.org>
parents: 2997
diff changeset
516 DEVNULL = open(os.devnull, "wb")
a1b98772af6b Backed out changeset cf0dda0d4cda
Goffi <goffi@goffi.org>
parents: 2997
diff changeset
517 subprocess.Popen(
a1b98772af6b Backed out changeset cf0dda0d4cda
Goffi <goffi@goffi.org>
parents: 2997
diff changeset
518 [
a1b98772af6b Backed out changeset cf0dda0d4cda
Goffi <goffi@goffi.org>
parents: 2997
diff changeset
519 sys.argv[0],
a1b98772af6b Backed out changeset cf0dda0d4cda
Goffi <goffi@goffi.org>
parents: 2997
diff changeset
520 "blog",
a1b98772af6b Backed out changeset cf0dda0d4cda
Goffi <goffi@goffi.org>
parents: 2997
diff changeset
521 "preview",
a1b98772af6b Backed out changeset cf0dda0d4cda
Goffi <goffi@goffi.org>
parents: 2997
diff changeset
522 "--inotify",
a1b98772af6b Backed out changeset cf0dda0d4cda
Goffi <goffi@goffi.org>
parents: 2997
diff changeset
523 "true",
a1b98772af6b Backed out changeset cf0dda0d4cda
Goffi <goffi@goffi.org>
parents: 2997
diff changeset
524 "-p",
a1b98772af6b Backed out changeset cf0dda0d4cda
Goffi <goffi@goffi.org>
parents: 2997
diff changeset
525 self.profile,
a1b98772af6b Backed out changeset cf0dda0d4cda
Goffi <goffi@goffi.org>
parents: 2997
diff changeset
526 content_file_path,
a1b98772af6b Backed out changeset cf0dda0d4cda
Goffi <goffi@goffi.org>
parents: 2997
diff changeset
527 ],
a1b98772af6b Backed out changeset cf0dda0d4cda
Goffi <goffi@goffi.org>
parents: 2997
diff changeset
528 stdout=DEVNULL,
a1b98772af6b Backed out changeset cf0dda0d4cda
Goffi <goffi@goffi.org>
parents: 2997
diff changeset
529 stderr=subprocess.STDOUT,
a1b98772af6b Backed out changeset cf0dda0d4cda
Goffi <goffi@goffi.org>
parents: 2997
diff changeset
530 )
1887
16527dd5a81b blog (jp/edit): added --preview option to launch a "blog preview" in parallel
Goffi <goffi@goffi.org>
parents: 1886
diff changeset
531
2269
606ff34d30f2 jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents: 2267
diff changeset
532 # we launch editor
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
533 self.runEditor(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
534 "blog_editor_args",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
535 content_file_path,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
536 content_file_obj,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
537 meta_file_path=meta_file_path,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
538 meta_ori=meta_ori,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
539 )
2269
606ff34d30f2 jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents: 2267
diff changeset
540
606ff34d30f2 jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents: 2267
diff changeset
541 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
542 self.setMbDataContent(content, mb_data)
1866
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
543
2910
b2f323237fce jp, plugin merge-requests: used u'' as default for item id in pubsub arguments + fixed some required arguments:
Goffi <goffi@goffi.org>
parents: 2876
diff changeset
544 if self.pubsub_item:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
545 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
546
2807
0b7ce5daee9b plugin XEP-0277: blog items data are now entirely serialised before going to bridge:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
547 mb_data = data_format.serialise(mb_data)
0b7ce5daee9b plugin XEP-0277: blog items data are now entirely serialised before going to bridge:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
548
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
549 self.host.bridge.mbSend(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
550 self.pubsub_service, self.pubsub_node, mb_data, self.profile
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
551 )
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
552 self.disp("Blog item published")
1866
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
553
2269
606ff34d30f2 jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents: 2267
diff changeset
554 def getTmpSuff(self):
606ff34d30f2 jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents: 2267
diff changeset
555 # we get current syntax to determine file extension
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
556 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
557
2269
606ff34d30f2 jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents: 2267
diff changeset
558 def getItemData(self, service, node, item):
2945
6c264c224614 jp (blog/edit): fixed --last-item after recent change in defaut value of --item
Goffi <goffi@goffi.org>
parents: 2944
diff changeset
559 items = [item] if item else []
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
560 mb_data = self.host.bridge.mbGet(service, node, 1, items, {}, self.profile)[0][0]
2807
0b7ce5daee9b plugin XEP-0277: blog items data are now entirely serialised before going to bridge:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
561 mb_data = data_format.deserialise(mb_data)
2269
606ff34d30f2 jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents: 2267
diff changeset
562 try:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
563 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
564 except KeyError:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
565 content = mb_data["content"]
2269
606ff34d30f2 jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents: 2267
diff changeset
566 if content:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
567 content = self.host.bridge.syntaxConvert(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
568 content, "text", SYNTAX_XHTML, False, self.profile
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
569 )
2330
74c1dbabeec8 jp (blog/edit): pretty format XHTML before editing it
Goffi <goffi@goffi.org>
parents: 2327
diff changeset
570 if content and self.current_syntax != SYNTAX_XHTML:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
571 content = self.host.bridge.syntaxConvert(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
572 content, SYNTAX_XHTML, self.current_syntax, False, self.profile
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
573 )
2330
74c1dbabeec8 jp (blog/edit): pretty format XHTML before editing it
Goffi <goffi@goffi.org>
parents: 2327
diff changeset
574 if content and self.current_syntax == SYNTAX_XHTML:
2870
2f39682f6d40 jp (blog/edit): fixed handling of XHTML content when it is not already wrapped in <div>…</div>
Goffi <goffi@goffi.org>
parents: 2808
diff changeset
575 content = content.strip()
2f39682f6d40 jp (blog/edit): fixed handling of XHTML content when it is not already wrapped in <div>…</div>
Goffi <goffi@goffi.org>
parents: 2808
diff changeset
576 if not content.startswith('<div>'):
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
577 content = '<div>' + content + '</div>'
2330
74c1dbabeec8 jp (blog/edit): pretty format XHTML before editing it
Goffi <goffi@goffi.org>
parents: 2327
diff changeset
578 try:
74c1dbabeec8 jp (blog/edit): pretty format XHTML before editing it
Goffi <goffi@goffi.org>
parents: 2327
diff changeset
579 from lxml import etree
74c1dbabeec8 jp (blog/edit): pretty format XHTML before editing it
Goffi <goffi@goffi.org>
parents: 2327
diff changeset
580 except ImportError:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
581 self.disp(_("You need lxml to edit pretty XHTML"))
2330
74c1dbabeec8 jp (blog/edit): pretty format XHTML before editing it
Goffi <goffi@goffi.org>
parents: 2327
diff changeset
582 else:
74c1dbabeec8 jp (blog/edit): pretty format XHTML before editing it
Goffi <goffi@goffi.org>
parents: 2327
diff changeset
583 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
584 root = etree.fromstring(content, parser)
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
585 content = etree.tostring(root, encoding=str, pretty_print=True)
2330
74c1dbabeec8 jp (blog/edit): pretty format XHTML before editing it
Goffi <goffi@goffi.org>
parents: 2327
diff changeset
586
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
587 return content, mb_data, mb_data["id"]
1866
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
588
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
589 def start(self):
397ef87958b9 jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents: 1864
diff changeset
590 # if there are user defined extension, we use them
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
591 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
592 self._current_syntax = self.args.syntax
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2485
diff changeset
593 if self._current_syntax is not None:
2325
760df3a58c24 jp (blog/edit): syntax modifications:
Goffi <goffi@goffi.org>
parents: 2284
diff changeset
594 try:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
595 self._current_syntax = self.args.syntax = self.host.bridge.syntaxGet(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
596 self.current_syntax
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
597 )
2325
760df3a58c24 jp (blog/edit): syntax modifications:
Goffi <goffi@goffi.org>
parents: 2284
diff changeset
598 except Exception as e:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
599 if "NotFound" in str(
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
600 e
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
601 ): #  FIXME: there is not good way to check bridge errors
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
602 self.parser.error(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
603 _("unknown syntax requested ({syntax})").format(
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
604 syntax=self.args.syntax
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
605 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
606 )
2325
760df3a58c24 jp (blog/edit): syntax modifications:
Goffi <goffi@goffi.org>
parents: 2284
diff changeset
607 else:
760df3a58c24 jp (blog/edit): syntax modifications:
Goffi <goffi@goffi.org>
parents: 2284
diff changeset
608 raise e
2225
301bb52c8715 jp (blog): service and node can now be specified for edit command
Goffi <goffi@goffi.org>
parents: 2164
diff changeset
609
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
610 (
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
611 self.pubsub_service,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
612 self.pubsub_node,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
613 self.pubsub_item,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
614 content_file_path,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
615 content_file_obj,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
616 mb_data,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
617 ) = 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
618
2269
606ff34d30f2 jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents: 2267
diff changeset
619 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
620
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
621
2969
eafea3bcf111 jp (blog/preview): fixed call to getCurrentFile
Goffi <goffi@goffi.org>
parents: 2945
diff changeset
622 class Preview(base.CommandBase, common.BaseEdit):
2532
772447ec070f jp: pubsub options refactoring:
Goffi <goffi@goffi.org>
parents: 2485
diff changeset
623 # TODO: need to be rewritten with template output
1874
658824755a0c jp (blog): preview command, first draft
Goffi <goffi@goffi.org>
parents: 1873
diff changeset
624
658824755a0c jp (blog): preview command, first draft
Goffi <goffi@goffi.org>
parents: 1873
diff changeset
625 def __init__(self, host):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
626 base.CommandBase.__init__(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
627 self, host, "preview", use_verbose=True, help=_("preview a blog content")
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
628 )
2969
eafea3bcf111 jp (blog/preview): fixed call to getCurrentFile
Goffi <goffi@goffi.org>
parents: 2945
diff changeset
629 common.BaseEdit.__init__(self, self.host, BLOG_TMP_DIR, use_metadata=True)
1874
658824755a0c jp (blog): preview command, first draft
Goffi <goffi@goffi.org>
parents: 1873
diff changeset
630
658824755a0c jp (blog): preview command, first draft
Goffi <goffi@goffi.org>
parents: 1873
diff changeset
631 def add_parser_options(self):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
632 self.parser.add_argument(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
633 "--inotify",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
634 type=str,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
635 choices=("auto", "true", "false"),
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
636 default="auto",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
637 help=_("use inotify to handle preview"),
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
638 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
639 self.parser.add_argument(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
640 "file",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
641 nargs="?",
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
642 default="current",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
643 help=_("path to the content file"),
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
644 )
1874
658824755a0c jp (blog): preview command, first draft
Goffi <goffi@goffi.org>
parents: 1873
diff changeset
645
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
646 def showPreview(self):
2271
2fae89f30b8d jp (blog): minor typo fix
Goffi <goffi@goffi.org>
parents: 2270
diff changeset
647 # 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
648 # when preview is not used
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
649 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
650 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
651
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
652 def _launchPreviewExt(self, cmd_line, opt_name):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
653 url = "file:{}".format(self.urllib.quote(self.preview_file_path))
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
654 args = common.parse_args(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
655 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
656 )
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
657 if not args:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
658 self.disp(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
659 'Couln\'t find command in "{name}", abording'.format(name=opt_name),
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
660 error=True,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
661 )
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
662 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
663 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
664
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
665 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
666 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
667
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
668 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
669 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
670
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
671 def updateContent(self):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
672 with open(self.content_file_path, "rb") as f:
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
673 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
674 if content and self.syntax != SYNTAX_XHTML:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
675 # 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
676 # to what the people will see
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
677 content = self.host.bridge.syntaxConvert(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
678 content, self.syntax, SYNTAX_XHTML, True, self.profile
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
679 )
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
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
681 xhtml = (
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
682 '<html xmlns="http://www.w3.org/1999/xhtml">'
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
683 '<head><meta http-equiv="Content-Type" content="text/html;charset=utf-8" />'
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
684 "</head>"
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
685 "<body>{}</body>"
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
686 "</html>"
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
687 ).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
688
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
689 with open(self.preview_file_path, "wb") as f:
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
690 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
691
1874
658824755a0c jp (blog): preview command, first draft
Goffi <goffi@goffi.org>
parents: 1873
diff changeset
692 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
693 import webbrowser
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
694 import urllib.request, urllib.parse, urllib.error
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
695
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
696 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
697
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
698 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
699 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
700 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
701 import inotify.constants
1886
f3db27508b31 jp (blog/preview): inotify improvments:
Goffi <goffi@goffi.org>
parents: 1885
diff changeset
702 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
703 except ImportError:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
704 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
705 inotify = None
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
706 self.disp(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
707 "inotify module not found, deactivating feature. You can install"
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
708 " it with {install}".format(install=INOTIFY_INSTALL)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
709 )
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
710 else:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
711 self.disp(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
712 "inotify not found, can't activate the feature! Please install "
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
713 "it with {install}".format(install=INOTIFY_INSTALL),
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
714 error=True,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
715 )
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
716 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
717 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
718 # 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
719 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
720 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
721 except AttributeError:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
722 self.disp(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
723 "Logger doesn't exists, inotify may have chanded", error=True
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
724 )
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 else:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
726 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
727
1874
658824755a0c jp (blog): preview command, first draft
Goffi <goffi@goffi.org>
parents: 1873
diff changeset
728 sat_conf = config.parseMainConf()
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
729 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
730
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
731 try:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
732 self.open_cb_cmd = config.getConfig(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
733 sat_conf, "jp", "blog_preview_open_cmd", Exception
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
734 )
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
735 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
736 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
737 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
738 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
739 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
740
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
741 self.update_cb_cmd = config.getConfig(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
742 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
743 )
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
744 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
745 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
746 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
747 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
748
1874
658824755a0c jp (blog): preview command, first draft
Goffi <goffi@goffi.org>
parents: 1873
diff changeset
749 # which file do we need to edit?
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
750 if self.args.file == "current":
2969
eafea3bcf111 jp (blog/preview): fixed call to getCurrentFile
Goffi <goffi@goffi.org>
parents: 2945
diff changeset
751 self.content_file_path = self.getCurrentFile(self.profile)
1874
658824755a0c jp (blog): preview command, first draft
Goffi <goffi@goffi.org>
parents: 1873
diff changeset
752 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
753 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
754
2598
0b6adc2672d9 jp (blog/preview): fixed guessSyntaxFromPath call
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
755 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
756
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
757 # 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
758 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
759 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
760 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
761 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
762
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
763 if inotify is None:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
764 # 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
765 # (and webbrowser.open can return before it is read)
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
766 self.disp(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
767 "temporary file created at {}\nthis file will NOT BE DELETED "
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
768 "AUTOMATICALLY, please delete it yourself when you have finished".format(
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
769 self.preview_file_path
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
770 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
771 )
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
772 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
773 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
774 open_cb()
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
775 i = inotify.adapters.Inotify(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
776 block_duration_s=60
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
777 ) # 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
778
f3db27508b31 jp (blog/preview): inotify improvments:
Goffi <goffi@goffi.org>
parents: 1885
diff changeset
779 def add_watch():
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
780 i.add_watch(
2639
266f871fcb4b jp (blog): fixed path encoding for inotify
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
781 self.content_file_path.encode('utf-8'),
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
782 mask=inotify.constants.IN_CLOSE_WRITE
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
783 | inotify.constants.IN_DELETE_SELF
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
784 | inotify.constants.IN_MOVE_SELF,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
785 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
786
1886
f3db27508b31 jp (blog/preview): inotify improvments:
Goffi <goffi@goffi.org>
parents: 1885
diff changeset
787 add_watch()
f3db27508b31 jp (blog/preview): inotify improvments:
Goffi <goffi@goffi.org>
parents: 1885
diff changeset
788
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
789 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
790 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
791 if event is not None:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
792 self.disp("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
793 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
794 self.disp(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
795 "{} event catched, changing the watch".format(
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
796 ", ".join(event[1])
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
797 ),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
798 2,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
799 )
3000
d603550d5e99 jp (blog/preview): fixed watch restoring when file is moved/deleted:
Goffi <goffi@goffi.org>
parents: 2998
diff changeset
800 i.remove_watch(self.content_file_path)
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
801 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
802 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
803 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
804 # 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
805 # 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
806 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
807 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
808 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
809 update_cb()
1886
f3db27508b31 jp (blog/preview): inotify improvments:
Goffi <goffi@goffi.org>
parents: 1885
diff changeset
810 except InotifyError:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
811 self.disp(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
812 "Can't catch inotify events, as the file been deleted?", error=True
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
813 )
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
814 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
815 os.unlink(self.preview_file_path)
1886
f3db27508b31 jp (blog/preview): inotify improvments:
Goffi <goffi@goffi.org>
parents: 1885
diff changeset
816 try:
f3db27508b31 jp (blog/preview): inotify improvments:
Goffi <goffi@goffi.org>
parents: 1885
diff changeset
817 i.remove_watch(self.content_file_path)
f3db27508b31 jp (blog/preview): inotify improvments:
Goffi <goffi@goffi.org>
parents: 1885
diff changeset
818 except InotifyError:
f3db27508b31 jp (blog/preview): inotify improvments:
Goffi <goffi@goffi.org>
parents: 1885
diff changeset
819 pass
1874
658824755a0c jp (blog): preview command, first draft
Goffi <goffi@goffi.org>
parents: 1873
diff changeset
820
658824755a0c jp (blog): preview command, first draft
Goffi <goffi@goffi.org>
parents: 1873
diff changeset
821
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
822 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
823 def __init__(self, host):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
824 super(Import, self).__init__(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
825 host,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
826 "import",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
827 use_pubsub=True,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
828 use_progress=True,
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
829 help=_("import an external blog"),
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
830 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
831 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
832
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
833 def add_parser_options(self):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
834 self.parser.add_argument(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
835 "importer",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
836 nargs="?",
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
837 help=_("importer name, nothing to display importers list"),
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
838 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
839 self.parser.add_argument(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
840 "--host", help=_("original blog host")
2624
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 "--no-images-upload",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
844 action="store_true",
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
845 help=_("do *NOT* upload images (default: do upload images)"),
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
846 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
847 self.parser.add_argument(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
848 "--upload-ignore-host",
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
849 help=_("do not upload images from this host (default: upload all images)"),
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
850 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
851 self.parser.add_argument(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
852 "--ignore-tls-errors",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
853 action="store_true",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
854 help=_("ignore invalide TLS certificate for uploads"),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
855 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
856 self.parser.add_argument(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
857 "-o",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
858 "--option",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
859 action="append",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
860 nargs=2,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
861 default=[],
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
862 metavar=("NAME", "VALUE"),
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
863 help=_("importer specific options (see importer description)"),
2624
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 self.parser.add_argument(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
866 "location",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
867 nargs="?",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
868 help=_(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
869 "importer data location (see importer description), nothing to show "
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
870 "importer description"
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
871 ),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
872 )
1827
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
873
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
874 def onProgressStarted(self, metadata):
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
875 self.disp(_("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
876
6209de5e3e25 jp (blog): import now show progression + value to add to sat.conf for URLs redirection
Goffi <goffi@goffi.org>
parents: 1827
diff changeset
877 def onProgressFinished(self, metadata):
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
878 self.disp(_("Blog uploaded successfully"), 2)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
879 redirections = {
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
880 k[len(URL_REDIRECT_PREFIX) :]: v
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
881 for k, v in metadata.items()
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
882 if k.startswith(URL_REDIRECT_PREFIX)
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
883 }
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
884 if redirections:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
885 conf = "\n".join(
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
886 [
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
887 "url_redirections_dict = {}".format(
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
888 # we need to add ' ' before each new line
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
889 # and to double each '%' for ConfigParser
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
890 "\n ".join(
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
891 json.dumps(redirections, indent=1, separators=(",", ": "))
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
892 .replace("%", "%%")
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
893 .split("\n")
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
894 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
895 ),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
896 ]
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
897 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
898 self.disp(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
899 _(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
900 "\nTo redirect old URLs to new ones, put the following lines in your"
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
901 " sat.conf file, in [libervia] section:\n\n{conf}".format(conf=conf)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
902 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
903 )
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
904
6209de5e3e25 jp (blog): import now show progression + value to add to sat.conf for URLs redirection
Goffi <goffi@goffi.org>
parents: 1827
diff changeset
905 def onProgressError(self, error_msg):
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
906 self.disp(_("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
907
6209de5e3e25 jp (blog): import now show progression + value to add to sat.conf for URLs redirection
Goffi <goffi@goffi.org>
parents: 1827
diff changeset
908 def error(self, failure):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
909 self.disp(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
910 _("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
911 error=True,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
912 )
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
913 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
914
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
915 def start(self):
1827
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
916 if self.args.location is None:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
917 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
918 if getattr(self.args, name):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
919 self.parser.error(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
920 _(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
921 "{name} argument can't be used without location argument"
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
922 ).format(name=name)
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
923 )
1827
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
924 if self.args.importer is None:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
925 self.disp(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
926 "\n".join(
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
927 [
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
928 "{}: {}".format(name, desc)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
929 for name, desc in self.host.bridge.blogImportList()
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
930 ]
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
931 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
932 )
1827
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
933 else:
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
934 try:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
935 short_desc, long_desc = self.host.bridge.blogImportDesc(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
936 self.args.importer
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
937 )
1827
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
938 except Exception as e:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
939 msg = [l for l in str(e).split("\n") if l][
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
940 -1
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
941 ] # 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
942 self.disp(msg)
1827
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
943 self.host.quit(1)
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
944 else:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
945 self.disp(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
946 "{name}: {short_desc}\n\n{long_desc}".format(
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
947 name=self.args.importer,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
948 short_desc=short_desc,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
949 long_desc=long_desc,
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 )
1827
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
952 self.host.quit()
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
953 else:
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
954 # 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
955 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
956 if self.args.host:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
957 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
958 if self.args.ignore_tls_errors:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
959 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
960 if self.args.no_images_upload:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
961 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
962 if self.args.upload_ignore_host:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
963 self.parser.error(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
964 "upload-ignore-host option can't be used when no-images-upload "
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 3000
diff changeset
965 "is set"
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
966 )
1827
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
967 elif self.args.upload_ignore_host:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
968 options["upload_ignore_host"] = self.args.upload_ignore_host
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
969
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
970 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
971 self.progress_id = id_
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
972
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
973 self.host.bridge.blogImport(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
974 self.args.importer,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
975 self.args.location,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
976 options,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
977 self.args.service,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
978 self.args.node,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
979 self.profile,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
980 callback=gotId,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
981 errback=self.error,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
982 )
1827
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
983
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
984
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
985 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
986 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
987
74014a9cc203 jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
988 def __init__(self, host):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
989 super(Blog, self).__init__(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
990 host, "blog", use_profile=False, help=_("blog/microblog management")
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2598
diff changeset
991 )