Mercurial > libervia-backend
annotate sat_frontends/jp/cmd_blog.py @ 3794:a58715ffa445
core (constants): type hints
author | Goffi <goffi@goffi.org> |
---|---|
date | Fri, 17 Jun 2022 14:15:23 +0200 |
parents | a6dfd3db372b |
children | b337d7da72e5 |
rev | line source |
---|---|
3137 | 1 #!/usr/bin/env python3 |
2 | |
1827
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 |
3479 | 5 # Copyright (C) 2009-2021 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 |
3040 | 21 import json |
22 import sys | |
23 import os.path | |
24 import os | |
25 import tempfile | |
26 import subprocess | |
27 import asyncio | |
28 from asyncio.subprocess import DEVNULL | |
29 from pathlib import Path | |
3028 | 30 from . import base |
1827
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
31 from sat.core.i18n import _ |
2157
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
32 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
|
33 from sat_frontends.jp import common |
2157
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
34 from sat.tools.common.ansi import ANSI as A |
2327
dad500e7ae50
jp (blog/get): added "url" key, which compute xmpp: URL of items
Goffi <goffi@goffi.org>
parents:
2325
diff
changeset
|
35 from sat.tools.common import uri |
1866
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
36 from sat.tools import config |
3028 | 37 from configparser import NoSectionError, NoOptionError |
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
|
38 from sat.tools.common import data_format |
1866
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
39 |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
40 __commands__ = ["Blog"] |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
41 |
3028 | 42 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
|
43 # 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
|
44 SYNTAX_EXT = { |
2944
efb915a20d5d
jp (blog/set): fixed missing current_syntax
Goffi <goffi@goffi.org>
parents:
2943
diff
changeset
|
45 # 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
|
46 # by the plugin. |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
47 "": "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
|
48 SYNTAX_XHTML: "xhtml", |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
49 "markdown": "md", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
50 } |
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
|
51 |
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 |
3028 | 53 CONF_SYNTAX_EXT = "syntax_ext_dict" |
54 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
|
55 # 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
|
56 KEY_TO_REMOVE_METADATA = ( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
57 "id", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
58 "content", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
59 "content_xhtml", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
60 "comments_node", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
61 "comments_service", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
62 "updated", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
63 ) |
1866
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
64 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
65 URL_REDIRECT_PREFIX = "url_redirect_" |
3040 | 66 AIONOTIFY_INSTALL = '"pip install aionotify"' |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
67 MB_KEYS = ( |
3028 | 68 "id", |
69 "url", | |
70 "atom_id", | |
71 "updated", | |
72 "published", | |
73 "language", | |
74 "comments", # this key is used for all comments* keys | |
75 "tags", # this key is used for all tag* keys | |
76 "author", | |
77 "author_jid", | |
78 "author_email", | |
79 "author_jid_verified", | |
80 "content", | |
81 "content_xhtml", | |
82 "title", | |
83 "title_xhtml", | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
84 ) |
3028 | 85 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
|
86 |
1866
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
87 |
3040 | 88 async def guessSyntaxFromPath(host, sat_conf, path): |
2532 | 89 """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
|
90 |
2532 | 91 @param sat_conf(ConfigParser.ConfigParser): instance opened on sat configuration |
92 @param path(str): path to the content file | |
93 @return(unicode): syntax to use | |
94 """ | |
95 # we first try to guess syntax with extension | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
96 ext = os.path.splitext(path)[1][1:] # we get extension without the '.' |
2532 | 97 if ext: |
3028 | 98 for k, v in SYNTAX_EXT.items(): |
2532 | 99 if k and ext == v: |
100 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
|
101 |
3568
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
102 # if not found, we use current syntax |
3040 | 103 return await host.bridge.getParamA("Syntax", "Composition", "value", host.profile) |
1874
658824755a0c
jp (blog): preview command, first draft
Goffi <goffi@goffi.org>
parents:
1873
diff
changeset
|
104 |
658824755a0c
jp (blog): preview command, first draft
Goffi <goffi@goffi.org>
parents:
1873
diff
changeset
|
105 |
2458
4841ad6a5db4
jp (blog): added "set" command to publish content from stdin without editing
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
106 class BlogPublishCommon(object): |
4841ad6a5db4
jp (blog): added "set" command to publish content from stdin without editing
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
107 """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
|
108 |
3040 | 109 async def get_current_syntax(self): |
110 """Retrieve current_syntax | |
111 | |
112 Use default syntax if --syntax has not been used, else check given syntax. | |
113 Will set self.default_syntax_used to True if default syntax has been used | |
114 """ | |
115 if self.args.syntax is None: | |
116 self.default_syntax_used = True | |
117 return await self.host.bridge.getParamA( | |
2944
efb915a20d5d
jp (blog/set): fixed missing current_syntax
Goffi <goffi@goffi.org>
parents:
2943
diff
changeset
|
118 "Syntax", "Composition", "value", self.profile |
efb915a20d5d
jp (blog/set): fixed missing current_syntax
Goffi <goffi@goffi.org>
parents:
2943
diff
changeset
|
119 ) |
3040 | 120 else: |
121 self.default_syntax_used = False | |
122 try: | |
3342
9dbe2517d131
jp (blog/edit): fixed edition when syntax is specified
Goffi <goffi@goffi.org>
parents:
3308
diff
changeset
|
123 syntax = await self.host.bridge.syntaxGet(self.args.syntax) |
3040 | 124 self.current_syntax = self.args.syntax = syntax |
125 except Exception as e: | |
126 if e.classname == "NotFound": | |
3568
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
127 self.parser.error( |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
128 _("unknown syntax requested ({syntax})").format( |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
129 syntax=self.args.syntax |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
130 ) |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
131 ) |
3040 | 132 else: |
133 raise e | |
134 return self.args.syntax | |
2944
efb915a20d5d
jp (blog/set): fixed missing current_syntax
Goffi <goffi@goffi.org>
parents:
2943
diff
changeset
|
135 |
2458
4841ad6a5db4
jp (blog): added "set" command to publish content from stdin without editing
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
136 def add_parser_options(self): |
3568
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
137 self.parser.add_argument("-T", "--title", help=_("title of the item")) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
138 self.parser.add_argument( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
139 "-t", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
140 "--tag", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
141 action="append", |
3028 | 142 help=_("tag (category) of your item"), |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
143 ) |
3097
2a7dfe43b161
jp (blog): added --language to common publish argument
Goffi <goffi@goffi.org>
parents:
3046
diff
changeset
|
144 self.parser.add_argument( |
2a7dfe43b161
jp (blog): added --language to common publish argument
Goffi <goffi@goffi.org>
parents:
3046
diff
changeset
|
145 "-l", |
2a7dfe43b161
jp (blog): added --language to common publish argument
Goffi <goffi@goffi.org>
parents:
3046
diff
changeset
|
146 "--language", |
2a7dfe43b161
jp (blog): added --language to common publish argument
Goffi <goffi@goffi.org>
parents:
3046
diff
changeset
|
147 help=_("language of the item (ISO 639 code)"), |
2a7dfe43b161
jp (blog): added --language to common publish argument
Goffi <goffi@goffi.org>
parents:
3046
diff
changeset
|
148 ) |
2808
51c53fc4fc4a
jp (blog/edit): changed comments handling:
Goffi <goffi@goffi.org>
parents:
2807
diff
changeset
|
149 |
51c53fc4fc4a
jp (blog/edit): changed comments handling:
Goffi <goffi@goffi.org>
parents:
2807
diff
changeset
|
150 comments_group = self.parser.add_mutually_exclusive_group() |
51c53fc4fc4a
jp (blog/edit): changed comments handling:
Goffi <goffi@goffi.org>
parents:
2807
diff
changeset
|
151 comments_group.add_argument( |
3568
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
152 "-C", |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
153 "--comments", |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
154 action="store_const", |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
155 const=True, |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
156 dest="comments", |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
157 help=_( |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
158 "enable comments (default: comments not enabled except if they " |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
159 "already exist)" |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
160 ), |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
161 ) |
2808
51c53fc4fc4a
jp (blog/edit): changed comments handling:
Goffi <goffi@goffi.org>
parents:
2807
diff
changeset
|
162 comments_group.add_argument( |
3568
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
163 "--no-comments", |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
164 action="store_const", |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
165 const=False, |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
166 dest="comments", |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
167 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
|
168 ) |
51c53fc4fc4a
jp (blog/edit): changed comments handling:
Goffi <goffi@goffi.org>
parents:
2807
diff
changeset
|
169 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
170 self.parser.add_argument( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
171 "-S", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
172 "--syntax", |
3028 | 173 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
|
174 ) |
2458
4841ad6a5db4
jp (blog): added "set" command to publish content from stdin without editing
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
175 |
3040 | 176 async def setMbDataContent(self, content, mb_data): |
177 if self.default_syntax_used: | |
2458
4841ad6a5db4
jp (blog): added "set" command to publish content from stdin without editing
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
178 # default syntax has been used |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
179 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
|
180 elif self.current_syntax == SYNTAX_XHTML: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
181 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
|
182 else: |
3040 | 183 mb_data["content_xhtml"] = await self.host.bridge.syntaxConvert( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
184 content, self.current_syntax, SYNTAX_XHTML, False, self.profile |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
185 ) |
2458
4841ad6a5db4
jp (blog): added "set" command to publish content from stdin without editing
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
186 |
4841ad6a5db4
jp (blog): added "set" command to publish content from stdin without editing
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
187 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
|
188 """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
|
189 |
4841ad6a5db4
jp (blog): added "set" command to publish content from stdin without editing
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
190 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
|
191 """ |
2807
0b7ce5daee9b
plugin XEP-0277: blog items data are now entirely serialised before going to bridge:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
192 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
|
193 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
|
194 if self.args.tag: |
3568
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
195 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
|
196 if self.args.title is not None: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
197 mb_data["title"] = self.args.title |
3097
2a7dfe43b161
jp (blog): added --language to common publish argument
Goffi <goffi@goffi.org>
parents:
3046
diff
changeset
|
198 if self.args.language is not None: |
2a7dfe43b161
jp (blog): added --language to common publish argument
Goffi <goffi@goffi.org>
parents:
3046
diff
changeset
|
199 mb_data["language"] = self.args.language |
2458
4841ad6a5db4
jp (blog): added "set" command to publish content from stdin without editing
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
200 |
4841ad6a5db4
jp (blog): added "set" command to publish content from stdin without editing
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
201 |
2532 | 202 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
|
203 def __init__(self, host): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
204 base.CommandBase.__init__( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
205 self, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
206 host, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
207 "set", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
208 use_pubsub=True, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
209 pubsub_flags={C.SINGLE_ITEM}, |
3028 | 210 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
|
211 ) |
2458
4841ad6a5db4
jp (blog): added "set" command to publish content from stdin without editing
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
212 BlogPublishCommon.__init__(self) |
4841ad6a5db4
jp (blog): added "set" command to publish content from stdin without editing
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
213 |
4841ad6a5db4
jp (blog): added "set" command to publish content from stdin without editing
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
214 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
|
215 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
|
216 |
3040 | 217 async def start(self): |
218 self.current_syntax = await self.get_current_syntax() | |
2458
4841ad6a5db4
jp (blog): added "set" command to publish content from stdin without editing
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
219 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
|
220 mb_data = {} |
4841ad6a5db4
jp (blog): added "set" command to publish content from stdin without editing
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
221 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
|
222 if self.pubsub_item: |
2876
84f82d014d06
jp (blog/set): fixed use of --item argument
Goffi <goffi@goffi.org>
parents:
2870
diff
changeset
|
223 mb_data["id"] = self.pubsub_item |
3040 | 224 content = sys.stdin.read() |
225 await self.setMbDataContent(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
|
226 |
3040 | 227 try: |
228 await self.host.bridge.mbSend( | |
229 self.args.service, | |
230 self.args.node, | |
231 data_format.serialise(mb_data), | |
232 self.profile, | |
233 ) | |
234 except Exception as e: | |
3568
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
235 self.disp(f"can't send item: {e}", error=True) |
3040 | 236 self.host.quit(C.EXIT_BRIDGE_ERRBACK) |
237 else: | |
238 self.disp("Item published") | |
239 self.host.quit(C.EXIT_OK) | |
2458
4841ad6a5db4
jp (blog): added "set" command to publish content from stdin without editing
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
240 |
4841ad6a5db4
jp (blog): added "set" command to publish content from stdin without editing
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
241 |
2532 | 242 class Get(base.CommandBase): |
3028 | 243 TEMPLATE = "blog/articles.html" |
2157
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
244 |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
245 def __init__(self, host): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
246 extra_outputs = {"default": self.default_output, "fancy": self.fancy_output} |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
247 base.CommandBase.__init__( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
248 self, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
249 host, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
250 "get", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
251 use_verbose=True, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
252 use_pubsub=True, |
3600
1709f0a78f50
jp (base): add flag for `use_pubsub` to add cache skipping option
Goffi <goffi@goffi.org>
parents:
3586
diff
changeset
|
253 pubsub_flags={C.MULTI_ITEMS, C.CACHE}, |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
254 use_output=C.OUTPUT_COMPLEX, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
255 extra_outputs=extra_outputs, |
3028 | 256 help=_("get blog item(s)"), |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
257 ) |
2157
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
258 |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
259 def add_parser_options(self): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
260 # TODO: a key(s) argument to select keys to display |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
261 self.parser.add_argument( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
262 "-k", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
263 "--key", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
264 action="append", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
265 dest="keys", |
3028 | 266 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
|
267 ) |
2157
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
268 # TODO: add MAM filters |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
269 |
2164
63d191c05ecd
jp (blog): set default template and data mapping for the new template output
Goffi <goffi@goffi.org>
parents:
2161
diff
changeset
|
270 def template_data_mapping(self, data): |
3308
384283adcce1
plugins XEP-0059, XEP-0060, XEP-0277, XEP-0313: better serialisation:
Goffi <goffi@goffi.org>
parents:
3241
diff
changeset
|
271 items, blog_items = data |
3568
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
272 blog_items["items"] = items |
3308
384283adcce1
plugins XEP-0059, XEP-0060, XEP-0277, XEP-0313: better serialisation:
Goffi <goffi@goffi.org>
parents:
3241
diff
changeset
|
273 return {"blog_items": blog_items} |
2164
63d191c05ecd
jp (blog): set default template and data mapping for the new template output
Goffi <goffi@goffi.org>
parents:
2161
diff
changeset
|
274 |
2157
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
275 def format_comments(self, item, keys): |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
276 lines = [] |
3568
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
277 for data in item.get("comments", []): |
3520
8acaf857cd0a
jp (blog/get): fix comments node parsing
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
278 lines.append(data["uri"]) |
3028 | 279 for k in ("node", "service"): |
2157
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
280 if OUTPUT_OPT_NO_HEADER in self.args.output_opts: |
3028 | 281 header = "" |
2157
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
282 else: |
3520
8acaf857cd0a
jp (blog/get): fix comments node parsing
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
283 header = f"{C.A_HEADER}comments_{k}: {A.RESET}" |
2157
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
284 lines.append(header + data[k]) |
3028 | 285 return "\n".join(lines) |
2157
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
286 |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
287 def format_tags(self, item, keys): |
3568
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
288 tags = item.pop("tags", []) |
3028 | 289 return ", ".join(tags) |
2157
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
290 |
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
|
291 def format_updated(self, item, keys): |
3667
9ca19b317293
CLI: move `format_time` to `common` + add timezone
Goffi <goffi@goffi.org>
parents:
3631
diff
changeset
|
292 return common.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
|
293 |
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
|
294 def format_published(self, item, keys): |
3667
9ca19b317293
CLI: move `format_time` to `common` + add timezone
Goffi <goffi@goffi.org>
parents:
3631
diff
changeset
|
295 return common.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
|
296 |
2327
dad500e7ae50
jp (blog/get): added "url" key, which compute xmpp: URL of items
Goffi <goffi@goffi.org>
parents:
2325
diff
changeset
|
297 def format_url(self, item, keys): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
298 return uri.buildXMPPUri( |
3028 | 299 "pubsub", |
300 subtype="microblog", | |
301 path=self.metadata["service"], | |
302 node=self.metadata["node"], | |
303 item=item["id"], | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
304 ) |
2327
dad500e7ae50
jp (blog/get): added "url" key, which compute xmpp: URL of items
Goffi <goffi@goffi.org>
parents:
2325
diff
changeset
|
305 |
2157
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
306 def get_keys(self): |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
307 """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
|
308 verbosity = self.args.verbose |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
309 if self.args.keys: |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
310 if not set(MB_KEYS).issuperset(self.args.keys): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
311 self.disp( |
3028 | 312 "following keys are invalid: {invalid}.\n" |
313 "Valid keys are: {valid}.".format( | |
314 invalid=", ".join(set(self.args.keys).difference(MB_KEYS)), | |
315 valid=", ".join(sorted(MB_KEYS)), | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
316 ), |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
317 error=True, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
318 ) |
2157
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
319 self.host.quit(C.EXIT_BAD_ARG) |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
320 return self.args.keys |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
321 else: |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
322 if verbosity == 0: |
3028 | 323 return ("title", "content") |
2157
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
324 elif verbosity == 1: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
325 return ( |
3028 | 326 "title", |
327 "tags", | |
328 "author", | |
329 "author_jid", | |
330 "author_email", | |
331 "author_jid_verified", | |
332 "published", | |
333 "updated", | |
334 "content", | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
335 ) |
2157
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
336 else: |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
337 return MB_KEYS |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
338 |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
339 def default_output(self, data): |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
340 """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
|
341 items, self.metadata = data |
2157
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
342 keys = self.get_keys() |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
343 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
344 # 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
|
345 k_cb = {} |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
346 for k in keys: |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
347 try: |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
348 callback = getattr(self, "format_" + k) |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
349 except AttributeError: |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
350 pass |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
351 else: |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
352 k_cb[k] = callback |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
353 for idx, item in enumerate(items): |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
354 for k in keys: |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
355 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
|
356 continue |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
357 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
|
358 header = "" |
2157
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
359 else: |
3028 | 360 header = "{k_fmt}{key}:{k_fmt_e} {sep}".format( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
361 k_fmt=C.A_HEADER, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
362 key=k, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
363 k_fmt_e=A.RESET, |
3028 | 364 sep="\n" if "content" in k else "", |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
365 ) |
2157
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
366 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
|
367 if isinstance(value, bool): |
3028 | 368 value = str(value).lower() |
3727
a6dfd3db372b
cli (blog/get): fix default output when a value is None
Goffi <goffi@goffi.org>
parents:
3667
diff
changeset
|
369 self.disp(header + (value or "")) |
3568
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
370 # 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
|
371 if idx < len(items) - 1: |
3028 | 372 print("") |
2157
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
373 |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
374 def fancy_output(self, data): |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
375 """display blog is a nice to read way |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
376 |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
377 this output doesn't use keys filter |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
378 """ |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
379 # thanks to http://stackoverflow.com/a/943921 |
3028 | 380 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
|
381 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
|
382 verbosity = self.args.verbose |
3028 | 383 sep = A.color(A.FG_BLUE, columns * "▬") |
2157
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
384 if items: |
3028 | 385 print(("\n" + sep + "\n")) |
2157
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
386 |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
387 for idx, item in enumerate(items): |
3028 | 388 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
|
389 if verbosity > 0: |
3028 | 390 author = item["author"] |
391 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
|
392 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
|
393 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
|
394 if verbosity > 1: |
3568
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
395 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
|
396 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
|
397 tags = None |
3028 | 398 content = item.get("content") |
2157
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
399 |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
400 if title: |
3028 | 401 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
|
402 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
|
403 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
|
404 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
|
405 if published: |
3667
9ca19b317293
CLI: move `format_time` to `common` + add timezone
Goffi <goffi@goffi.org>
parents:
3631
diff
changeset
|
406 meta.append(A.color(A.FG_YELLOW, "on ", common.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
|
407 if updated != published: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
408 meta.append( |
3667
9ca19b317293
CLI: move `format_time` to `common` + add timezone
Goffi <goffi@goffi.org>
parents:
3631
diff
changeset
|
409 A.color(A.FG_YELLOW, "(updated on ", common.format_time(updated), ")") |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
410 ) |
3028 | 411 print((" ".join(meta))) |
2157
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
412 if tags: |
3028 | 413 print((A.color(A.FG_MAGENTA, ", ".join(tags)))) |
2157
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
414 if (title or tags) and content: |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
415 print("") |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
416 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
|
417 self.disp(content) |
2157
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
418 |
3028 | 419 print(("\n" + sep + "\n")) |
2157
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
420 |
3040 | 421 async def start(self): |
422 try: | |
3308
384283adcce1
plugins XEP-0059, XEP-0060, XEP-0277, XEP-0313: better serialisation:
Goffi <goffi@goffi.org>
parents:
3241
diff
changeset
|
423 mb_data = data_format.deserialise( |
384283adcce1
plugins XEP-0059, XEP-0060, XEP-0277, XEP-0313: better serialisation:
Goffi <goffi@goffi.org>
parents:
3241
diff
changeset
|
424 await self.host.bridge.mbGet( |
384283adcce1
plugins XEP-0059, XEP-0060, XEP-0277, XEP-0313: better serialisation:
Goffi <goffi@goffi.org>
parents:
3241
diff
changeset
|
425 self.args.service, |
384283adcce1
plugins XEP-0059, XEP-0060, XEP-0277, XEP-0313: better serialisation:
Goffi <goffi@goffi.org>
parents:
3241
diff
changeset
|
426 self.args.node, |
384283adcce1
plugins XEP-0059, XEP-0060, XEP-0277, XEP-0313: better serialisation:
Goffi <goffi@goffi.org>
parents:
3241
diff
changeset
|
427 self.args.max, |
384283adcce1
plugins XEP-0059, XEP-0060, XEP-0277, XEP-0313: better serialisation:
Goffi <goffi@goffi.org>
parents:
3241
diff
changeset
|
428 self.args.items, |
384283adcce1
plugins XEP-0059, XEP-0060, XEP-0277, XEP-0313: better serialisation:
Goffi <goffi@goffi.org>
parents:
3241
diff
changeset
|
429 self.getPubsubExtra(), |
3568
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
430 self.profile, |
3308
384283adcce1
plugins XEP-0059, XEP-0060, XEP-0277, XEP-0313: better serialisation:
Goffi <goffi@goffi.org>
parents:
3241
diff
changeset
|
431 ) |
3040 | 432 ) |
433 except Exception as e: | |
434 self.disp(f"can't get blog items: {e}", error=True) | |
435 self.host.quit(C.EXIT_BRIDGE_ERRBACK) | |
436 else: | |
3568
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
437 items = mb_data.pop("items") |
3308
384283adcce1
plugins XEP-0059, XEP-0060, XEP-0277, XEP-0313: better serialisation:
Goffi <goffi@goffi.org>
parents:
3241
diff
changeset
|
438 await self.output((items, mb_data)) |
3040 | 439 self.host.quit(C.EXIT_OK) |
2157
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
440 |
b4a515e36631
jp (blog): added blog/get command:
Goffi <goffi@goffi.org>
parents:
1969
diff
changeset
|
441 |
2532 | 442 class Edit(base.CommandBase, BlogPublishCommon, common.BaseEdit): |
1866
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
443 def __init__(self, host): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
444 base.CommandBase.__init__( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
445 self, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
446 host, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
447 "edit", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
448 use_pubsub=True, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
449 pubsub_flags={C.SINGLE_ITEM}, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
450 use_draft=True, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
451 use_verbose=True, |
3028 | 452 help=_("edit an existing or new blog post"), |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
453 ) |
2458
4841ad6a5db4
jp (blog): added "set" command to publish content from stdin without editing
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
454 BlogPublishCommon.__init__(self) |
2269
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
2267
diff
changeset
|
455 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
|
456 |
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
457 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
|
458 BlogPublishCommon.add_parser_options(self) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
459 self.parser.add_argument( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
460 "-P", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
461 "--preview", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
462 action="store_true", |
3028 | 463 help=_("launch a blog preview in parallel"), |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
464 ) |
3177
f4914ce9d47d
jp (blog/edit): added `--no-publish` argument.
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
465 self.parser.add_argument( |
f4914ce9d47d
jp (blog/edit): added `--no-publish` argument.
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
466 "--no-publish", |
3241
46f0b388eeea
jp (blog/edit): --no-publish is a boolean option
Goffi <goffi@goffi.org>
parents:
3177
diff
changeset
|
467 action="store_true", |
3177
f4914ce9d47d
jp (blog/edit): added `--no-publish` argument.
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
468 help=_('add "publish: False" to metadata'), |
f4914ce9d47d
jp (blog/edit): added `--no-publish` argument.
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
469 ) |
1866
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
470 |
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
|
471 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
|
472 """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
|
473 |
3040 | 474 The file is named after content_file_path, with extension replaced by |
475 _metadata.json | |
476 @param content_file_path(str): path to the temporary file which will contain the | |
477 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
|
478 @param mb_data(dict, None): microblog metadata (for existing items) |
3040 | 479 @return (tuple[dict, Path]): merged metadata put originaly in metadata file |
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
|
480 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
|
481 """ |
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 # 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
|
483 # or re-use the existing one if it exists |
3040 | 484 meta_file_path = content_file_path.with_name( |
3568
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
485 content_file_path.stem + common.METADATA_SUFF |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
486 ) |
3040 | 487 if meta_file_path.exists(): |
3028 | 488 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
|
489 try: |
3040 | 490 with meta_file_path.open("rb") as f: |
1876
1088bf7b28e7
jp (blog/edit): metadata file is reused if it already exists
Goffi <goffi@goffi.org>
parents:
1874
diff
changeset
|
491 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
|
492 except (OSError, IOError, ValueError) as e: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
493 self.disp( |
3040 | 494 f"Can't read existing metadata file at {meta_file_path}, " |
495 f"aborting: {e}", | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
496 error=True, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
497 ) |
1876
1088bf7b28e7
jp (blog/edit): metadata file is reused if it already exists
Goffi <goffi@goffi.org>
parents:
1874
diff
changeset
|
498 self.host.quit(1) |
1088bf7b28e7
jp (blog/edit): metadata file is reused if it already exists
Goffi <goffi@goffi.org>
parents:
1874
diff
changeset
|
499 else: |
1088bf7b28e7
jp (blog/edit): metadata file is reused if it already exists
Goffi <goffi@goffi.org>
parents:
1874
diff
changeset
|
500 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
|
501 |
3568
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
502 # 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
|
503 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
|
504 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
|
505 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
|
506 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
|
507 pass |
3568
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
508 # 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
|
509 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
|
510 |
3241
46f0b388eeea
jp (blog/edit): --no-publish is a boolean option
Goffi <goffi@goffi.org>
parents:
3177
diff
changeset
|
511 if self.args.no_publish: |
3177
f4914ce9d47d
jp (blog/edit): added `--no-publish` argument.
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
512 mb_data["publish"] = False |
f4914ce9d47d
jp (blog/edit): added `--no-publish` argument.
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
513 |
3568
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
514 # then we create the file and write metadata there, as JSON dict |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
515 # 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
|
516 with os.fdopen( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
517 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
|
518 ) as f: |
3040 | 519 # we need to use an intermediate unicode buffer to write to the file |
520 # unicode without escaping characters | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
521 unicode_dump = json.dumps( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
522 mb_data, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
523 ensure_ascii=False, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
524 indent=4, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
525 separators=(",", ": "), |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
526 sort_keys=True, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
527 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
528 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
|
529 |
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
|
530 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
|
531 |
3040 | 532 async 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
|
533 """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
|
534 # 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
|
535 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
|
536 |
3040 | 537 coroutines = [] |
538 | |
1887
16527dd5a81b
blog (jp/edit): added --preview option to launch a "blog preview" in parallel
Goffi <goffi@goffi.org>
parents:
1886
diff
changeset
|
539 # 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
|
540 if self.args.preview: |
3028 | 541 self.disp("Preview requested, launching it", 1) |
2998 | 542 # we redirect outputs to /dev/null to avoid console pollution in editor |
543 # if user wants to see messages, (s)he can call "blog preview" directly | |
3040 | 544 coroutines.append( |
545 asyncio.create_subprocess_exec( | |
2998 | 546 sys.argv[0], |
547 "blog", | |
548 "preview", | |
549 "--inotify", | |
550 "true", | |
551 "-p", | |
552 self.profile, | |
3040 | 553 str(content_file_path), |
554 stdout=DEVNULL, | |
555 stderr=DEVNULL, | |
556 ) | |
2998 | 557 ) |
1887
16527dd5a81b
blog (jp/edit): added --preview option to launch a "blog preview" in parallel
Goffi <goffi@goffi.org>
parents:
1886
diff
changeset
|
558 |
3568
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
559 # we launch editor |
3040 | 560 coroutines.append( |
561 self.runEditor( | |
562 "blog_editor_args", | |
563 content_file_path, | |
564 content_file_obj, | |
565 meta_file_path=meta_file_path, | |
566 meta_ori=meta_ori, | |
567 ) | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
568 ) |
2269
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
2267
diff
changeset
|
569 |
3040 | 570 await asyncio.gather(*coroutines) |
571 | |
572 async def publish(self, content, mb_data): | |
573 await self.setMbDataContent(content, mb_data) | |
1866
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
574 |
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
|
575 if self.pubsub_item: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
576 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
|
577 |
2807
0b7ce5daee9b
plugin XEP-0277: blog items data are now entirely serialised before going to bridge:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
578 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
|
579 |
3040 | 580 await self.host.bridge.mbSend( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
581 self.pubsub_service, self.pubsub_node, mb_data, self.profile |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
582 ) |
3028 | 583 self.disp("Blog item published") |
1866
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
584 |
2269
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
2267
diff
changeset
|
585 def getTmpSuff(self): |
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
2267
diff
changeset
|
586 # we get current syntax to determine file extension |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
587 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
|
588 |
3040 | 589 async 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
|
590 items = [item] if item else [] |
3040 | 591 |
3308
384283adcce1
plugins XEP-0059, XEP-0060, XEP-0277, XEP-0313: better serialisation:
Goffi <goffi@goffi.org>
parents:
3241
diff
changeset
|
592 mb_data = data_format.deserialise( |
3586
5f65f4e9f8cb
plugin XEP-0060: getItems extra is now serialised dict
Goffi <goffi@goffi.org>
parents:
3568
diff
changeset
|
593 await self.host.bridge.mbGet( |
5f65f4e9f8cb
plugin XEP-0060: getItems extra is now serialised dict
Goffi <goffi@goffi.org>
parents:
3568
diff
changeset
|
594 service, node, 1, items, data_format.serialise({}), self.profile |
5f65f4e9f8cb
plugin XEP-0060: getItems extra is now serialised dict
Goffi <goffi@goffi.org>
parents:
3568
diff
changeset
|
595 ) |
3308
384283adcce1
plugins XEP-0059, XEP-0060, XEP-0277, XEP-0313: better serialisation:
Goffi <goffi@goffi.org>
parents:
3241
diff
changeset
|
596 ) |
3568
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
597 item = mb_data["items"][0] |
3040 | 598 |
2269
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
2267
diff
changeset
|
599 try: |
3308
384283adcce1
plugins XEP-0059, XEP-0060, XEP-0277, XEP-0313: better serialisation:
Goffi <goffi@goffi.org>
parents:
3241
diff
changeset
|
600 content = item["content_xhtml"] |
2269
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
2267
diff
changeset
|
601 except KeyError: |
3308
384283adcce1
plugins XEP-0059, XEP-0060, XEP-0277, XEP-0313: better serialisation:
Goffi <goffi@goffi.org>
parents:
3241
diff
changeset
|
602 content = item["content"] |
2269
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
2267
diff
changeset
|
603 if content: |
3040 | 604 content = await self.host.bridge.syntaxConvert( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
605 content, "text", SYNTAX_XHTML, False, self.profile |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
606 ) |
3040 | 607 |
2330
74c1dbabeec8
jp (blog/edit): pretty format XHTML before editing it
Goffi <goffi@goffi.org>
parents:
2327
diff
changeset
|
608 if content and self.current_syntax != SYNTAX_XHTML: |
3040 | 609 content = await self.host.bridge.syntaxConvert( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
610 content, SYNTAX_XHTML, self.current_syntax, False, self.profile |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
611 ) |
3040 | 612 |
2330
74c1dbabeec8
jp (blog/edit): pretty format XHTML before editing it
Goffi <goffi@goffi.org>
parents:
2327
diff
changeset
|
613 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
|
614 content = content.strip() |
3568
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
615 if not content.startswith("<div>"): |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
616 content = "<div>" + content + "</div>" |
2330
74c1dbabeec8
jp (blog/edit): pretty format XHTML before editing it
Goffi <goffi@goffi.org>
parents:
2327
diff
changeset
|
617 try: |
74c1dbabeec8
jp (blog/edit): pretty format XHTML before editing it
Goffi <goffi@goffi.org>
parents:
2327
diff
changeset
|
618 from lxml import etree |
74c1dbabeec8
jp (blog/edit): pretty format XHTML before editing it
Goffi <goffi@goffi.org>
parents:
2327
diff
changeset
|
619 except ImportError: |
3028 | 620 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
|
621 else: |
74c1dbabeec8
jp (blog/edit): pretty format XHTML before editing it
Goffi <goffi@goffi.org>
parents:
2327
diff
changeset
|
622 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
|
623 root = etree.fromstring(content, parser) |
3028 | 624 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
|
625 |
3308
384283adcce1
plugins XEP-0059, XEP-0060, XEP-0277, XEP-0313: better serialisation:
Goffi <goffi@goffi.org>
parents:
3241
diff
changeset
|
626 return content, item, item["id"] |
1866
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
627 |
3040 | 628 async def start(self): |
1866
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
629 # if there are user defined extension, we use them |
3626
82e616b70a2a
CLI: fix section used for config following name change
Goffi <goffi@goffi.org>
parents:
3568
diff
changeset
|
630 SYNTAX_EXT.update( |
82e616b70a2a
CLI: fix section used for config following name change
Goffi <goffi@goffi.org>
parents:
3568
diff
changeset
|
631 config.getConfig(self.sat_conf, C.CONFIG_SECTION, CONF_SYNTAX_EXT, {}) |
82e616b70a2a
CLI: fix section used for config following name change
Goffi <goffi@goffi.org>
parents:
3568
diff
changeset
|
632 ) |
3040 | 633 self.current_syntax = await self.get_current_syntax() |
2225
301bb52c8715
jp (blog): service and node can now be specified for edit command
Goffi <goffi@goffi.org>
parents:
2164
diff
changeset
|
634 |
3568
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
635 ( |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
636 self.pubsub_service, |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
637 self.pubsub_node, |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
638 self.pubsub_item, |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
639 content_file_path, |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
640 content_file_obj, |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
641 mb_data, |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
642 ) = await 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
|
643 |
3040 | 644 await self.edit(content_file_path, content_file_obj, mb_data=mb_data) |
645 self.host.quit() | |
1827
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
646 |
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
647 |
3521
62f490eff51c
jp (pubsub, blog): `rename` commands implementation
Goffi <goffi@goffi.org>
parents:
3520
diff
changeset
|
648 class Rename(base.CommandBase): |
62f490eff51c
jp (pubsub, blog): `rename` commands implementation
Goffi <goffi@goffi.org>
parents:
3520
diff
changeset
|
649 def __init__(self, host): |
62f490eff51c
jp (pubsub, blog): `rename` commands implementation
Goffi <goffi@goffi.org>
parents:
3520
diff
changeset
|
650 base.CommandBase.__init__( |
62f490eff51c
jp (pubsub, blog): `rename` commands implementation
Goffi <goffi@goffi.org>
parents:
3520
diff
changeset
|
651 self, |
62f490eff51c
jp (pubsub, blog): `rename` commands implementation
Goffi <goffi@goffi.org>
parents:
3520
diff
changeset
|
652 host, |
62f490eff51c
jp (pubsub, blog): `rename` commands implementation
Goffi <goffi@goffi.org>
parents:
3520
diff
changeset
|
653 "rename", |
62f490eff51c
jp (pubsub, blog): `rename` commands implementation
Goffi <goffi@goffi.org>
parents:
3520
diff
changeset
|
654 use_pubsub=True, |
62f490eff51c
jp (pubsub, blog): `rename` commands implementation
Goffi <goffi@goffi.org>
parents:
3520
diff
changeset
|
655 pubsub_flags={C.SINGLE_ITEM}, |
62f490eff51c
jp (pubsub, blog): `rename` commands implementation
Goffi <goffi@goffi.org>
parents:
3520
diff
changeset
|
656 help=_("rename an blog item"), |
62f490eff51c
jp (pubsub, blog): `rename` commands implementation
Goffi <goffi@goffi.org>
parents:
3520
diff
changeset
|
657 ) |
62f490eff51c
jp (pubsub, blog): `rename` commands implementation
Goffi <goffi@goffi.org>
parents:
3520
diff
changeset
|
658 |
62f490eff51c
jp (pubsub, blog): `rename` commands implementation
Goffi <goffi@goffi.org>
parents:
3520
diff
changeset
|
659 def add_parser_options(self): |
3568
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
660 self.parser.add_argument("new_id", help=_("new item id to use")) |
3521
62f490eff51c
jp (pubsub, blog): `rename` commands implementation
Goffi <goffi@goffi.org>
parents:
3520
diff
changeset
|
661 |
62f490eff51c
jp (pubsub, blog): `rename` commands implementation
Goffi <goffi@goffi.org>
parents:
3520
diff
changeset
|
662 async def start(self): |
62f490eff51c
jp (pubsub, blog): `rename` commands implementation
Goffi <goffi@goffi.org>
parents:
3520
diff
changeset
|
663 try: |
62f490eff51c
jp (pubsub, blog): `rename` commands implementation
Goffi <goffi@goffi.org>
parents:
3520
diff
changeset
|
664 await self.host.bridge.mbRename( |
62f490eff51c
jp (pubsub, blog): `rename` commands implementation
Goffi <goffi@goffi.org>
parents:
3520
diff
changeset
|
665 self.args.service, |
62f490eff51c
jp (pubsub, blog): `rename` commands implementation
Goffi <goffi@goffi.org>
parents:
3520
diff
changeset
|
666 self.args.node, |
62f490eff51c
jp (pubsub, blog): `rename` commands implementation
Goffi <goffi@goffi.org>
parents:
3520
diff
changeset
|
667 self.args.item, |
62f490eff51c
jp (pubsub, blog): `rename` commands implementation
Goffi <goffi@goffi.org>
parents:
3520
diff
changeset
|
668 self.args.new_id, |
62f490eff51c
jp (pubsub, blog): `rename` commands implementation
Goffi <goffi@goffi.org>
parents:
3520
diff
changeset
|
669 self.profile, |
62f490eff51c
jp (pubsub, blog): `rename` commands implementation
Goffi <goffi@goffi.org>
parents:
3520
diff
changeset
|
670 ) |
62f490eff51c
jp (pubsub, blog): `rename` commands implementation
Goffi <goffi@goffi.org>
parents:
3520
diff
changeset
|
671 except Exception as e: |
3568
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
672 self.disp(f"can't rename item: {e}", error=True) |
3521
62f490eff51c
jp (pubsub, blog): `rename` commands implementation
Goffi <goffi@goffi.org>
parents:
3520
diff
changeset
|
673 self.host.quit(C.EXIT_BRIDGE_ERRBACK) |
62f490eff51c
jp (pubsub, blog): `rename` commands implementation
Goffi <goffi@goffi.org>
parents:
3520
diff
changeset
|
674 else: |
62f490eff51c
jp (pubsub, blog): `rename` commands implementation
Goffi <goffi@goffi.org>
parents:
3520
diff
changeset
|
675 self.disp("Item renamed") |
62f490eff51c
jp (pubsub, blog): `rename` commands implementation
Goffi <goffi@goffi.org>
parents:
3520
diff
changeset
|
676 self.host.quit(C.EXIT_OK) |
62f490eff51c
jp (pubsub, blog): `rename` commands implementation
Goffi <goffi@goffi.org>
parents:
3520
diff
changeset
|
677 |
3568
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
678 |
2969
eafea3bcf111
jp (blog/preview): fixed call to getCurrentFile
Goffi <goffi@goffi.org>
parents:
2945
diff
changeset
|
679 class Preview(base.CommandBase, common.BaseEdit): |
2532 | 680 # TODO: need to be rewritten with template output |
1874
658824755a0c
jp (blog): preview command, first draft
Goffi <goffi@goffi.org>
parents:
1873
diff
changeset
|
681 |
658824755a0c
jp (blog): preview command, first draft
Goffi <goffi@goffi.org>
parents:
1873
diff
changeset
|
682 def __init__(self, host): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
683 base.CommandBase.__init__( |
3028 | 684 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
|
685 ) |
2969
eafea3bcf111
jp (blog/preview): fixed call to getCurrentFile
Goffi <goffi@goffi.org>
parents:
2945
diff
changeset
|
686 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
|
687 |
658824755a0c
jp (blog): preview command, first draft
Goffi <goffi@goffi.org>
parents:
1873
diff
changeset
|
688 def add_parser_options(self): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
689 self.parser.add_argument( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
690 "--inotify", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
691 type=str, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
692 choices=("auto", "true", "false"), |
3028 | 693 default="auto", |
694 help=_("use inotify to handle preview"), | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
695 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
696 self.parser.add_argument( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
697 "file", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
698 nargs="?", |
3028 | 699 default="current", |
700 help=_("path to the content file"), | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
701 ) |
1874
658824755a0c
jp (blog): preview command, first draft
Goffi <goffi@goffi.org>
parents:
1873
diff
changeset
|
702 |
3040 | 703 async def showPreview(self): |
2271 | 704 # 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
|
705 # when preview is not used |
3040 | 706 url = "file:{}".format(self.urllib.parse.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
|
707 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
|
708 |
3040 | 709 async def _launchPreviewExt(self, cmd_line, opt_name): |
710 url = "file:{}".format(self.urllib.parse.quote(self.preview_file_path)) | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
711 args = common.parse_args( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
712 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
|
713 ) |
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
|
714 if not args: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
715 self.disp( |
3028 | 716 '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
|
717 error=True, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
718 ) |
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
|
719 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
|
720 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
|
721 |
3040 | 722 async def openPreviewExt(self): |
723 await self._launchPreviewExt(self.open_cb_cmd, "blog_preview_open_cmd") | |
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
|
724 |
3040 | 725 async def updatePreviewExt(self): |
726 await self._launchPreviewExt(self.update_cb_cmd, "blog_preview_update_cmd") | |
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
|
727 |
3040 | 728 async def updateContent(self): |
729 with self.content_file_path.open("rb") as f: | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
730 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
|
731 if content and self.syntax != SYNTAX_XHTML: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
732 # 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
|
733 # to what the people will see |
3040 | 734 content = await self.host.bridge.syntaxConvert( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
735 content, self.syntax, SYNTAX_XHTML, True, self.profile |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
736 ) |
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
|
737 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
738 xhtml = ( |
3040 | 739 f'<html xmlns="http://www.w3.org/1999/xhtml">' |
740 f'<head><meta http-equiv="Content-Type" content="text/html;charset=utf-8" />' | |
3568
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
741 f"</head>" |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
742 f"<body>{content}</body>" |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
743 f"</html>" |
3040 | 744 ) |
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
|
745 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
746 with open(self.preview_file_path, "wb") as f: |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
747 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
|
748 |
3040 | 749 async 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
|
750 import webbrowser |
3028 | 751 import urllib.request, urllib.parse, urllib.error |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
752 |
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
|
753 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
|
754 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
755 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
|
756 try: |
3040 | 757 import aionotify |
758 | |
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 except ImportError: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
760 if self.args.inotify == "auto": |
3040 | 761 aionotify = None |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
762 self.disp( |
3040 | 763 f"aionotify module not found, deactivating feature. You can " |
764 f"install it with {AIONOTIFY_INSTALL}" | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
765 ) |
1882
ed444e9aa078
jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents:
1881
diff
changeset
|
766 else: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
767 self.disp( |
3040 | 768 f"aioinotify not found, can't activate the feature! Please " |
769 f"install it with {AIONOTIFY_INSTALL}", | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
770 error=True, |
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 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
|
773 else: |
3040 | 774 aionotify = 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
|
775 |
3046 | 776 sat_conf = self.sat_conf |
3626
82e616b70a2a
CLI: fix section used for config following name change
Goffi <goffi@goffi.org>
parents:
3568
diff
changeset
|
777 SYNTAX_EXT.update( |
82e616b70a2a
CLI: fix section used for config following name change
Goffi <goffi@goffi.org>
parents:
3568
diff
changeset
|
778 config.getConfig(sat_conf, C.CONFIG_SECTION, CONF_SYNTAX_EXT, {}) |
82e616b70a2a
CLI: fix section used for config following name change
Goffi <goffi@goffi.org>
parents:
3568
diff
changeset
|
779 ) |
1874
658824755a0c
jp (blog): preview command, first draft
Goffi <goffi@goffi.org>
parents:
1873
diff
changeset
|
780 |
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
|
781 try: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
782 self.open_cb_cmd = config.getConfig( |
3626
82e616b70a2a
CLI: fix section used for config following name change
Goffi <goffi@goffi.org>
parents:
3568
diff
changeset
|
783 sat_conf, C.CONFIG_SECTION, "blog_preview_open_cmd", Exception |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
784 ) |
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
|
785 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
|
786 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
|
787 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
|
788 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
|
789 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
|
790 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
791 self.update_cb_cmd = config.getConfig( |
3626
82e616b70a2a
CLI: fix section used for config following name change
Goffi <goffi@goffi.org>
parents:
3568
diff
changeset
|
792 sat_conf, C.CONFIG_SECTION, "blog_preview_update_cmd", self.open_cb_cmd |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
793 ) |
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
|
794 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
|
795 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
|
796 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
|
797 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
|
798 |
3568
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
799 # which file do we need to edit? |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
800 if self.args.file == "current": |
2969
eafea3bcf111
jp (blog/preview): fixed call to getCurrentFile
Goffi <goffi@goffi.org>
parents:
2945
diff
changeset
|
801 self.content_file_path = self.getCurrentFile(self.profile) |
1874
658824755a0c
jp (blog): preview command, first draft
Goffi <goffi@goffi.org>
parents:
1873
diff
changeset
|
802 else: |
3040 | 803 try: |
804 self.content_file_path = Path(self.args.file).resolve(strict=True) | |
805 except FileNotFoundError: | |
3568
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
806 self.disp(_('File "{file}" doesn\'t exist!').format(file=self.args.file)) |
3040 | 807 self.host.quit(C.EXIT_NOT_FOUND) |
1874
658824755a0c
jp (blog): preview command, first draft
Goffi <goffi@goffi.org>
parents:
1873
diff
changeset
|
808 |
3040 | 809 self.syntax = await guessSyntaxFromPath( |
3568
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
810 self.host, sat_conf, self.content_file_path |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
811 ) |
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
|
812 |
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
|
813 # 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
|
814 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
|
815 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
|
816 preview_file.close() |
3040 | 817 await 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
|
818 |
3040 | 819 if aionotify is None: |
820 # XXX: we don't delete file automatically because browser needs it | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
821 # (and webbrowser.open can return before it is read) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
822 self.disp( |
3040 | 823 f"temporary file created at {self.preview_file_path}\nthis file will NOT " |
824 f"BE DELETED AUTOMATICALLY, please delete it yourself when you have " | |
825 f"finished" | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
826 ) |
3040 | 827 await open_cb() |
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
|
828 else: |
3040 | 829 await open_cb() |
830 watcher = aionotify.Watcher() | |
831 watcher_kwargs = { | |
832 # Watcher don't accept Path so we convert to string | |
833 "path": str(self.content_file_path), | |
3568
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
834 "alias": "content_file", |
3040 | 835 "flags": aionotify.Flags.CLOSE_WRITE |
3568
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
836 | aionotify.Flags.DELETE_SELF |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
837 | aionotify.Flags.MOVE_SELF, |
3040 | 838 } |
839 watcher.watch(**watcher_kwargs) | |
1886
f3db27508b31
jp (blog/preview): inotify improvments:
Goffi <goffi@goffi.org>
parents:
1885
diff
changeset
|
840 |
3040 | 841 loop = asyncio.get_event_loop() |
842 await watcher.setup(loop) | |
1886
f3db27508b31
jp (blog/preview): inotify improvments:
Goffi <goffi@goffi.org>
parents:
1885
diff
changeset
|
843 |
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
|
844 try: |
3040 | 845 while True: |
846 event = await watcher.get_event() | |
847 self.disp("Content updated", 1) | |
3568
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
848 if event.flags & ( |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
849 aionotify.Flags.DELETE_SELF | aionotify.Flags.MOVE_SELF |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
850 ): |
3040 | 851 self.disp( |
852 "DELETE/MOVE event catched, changing the watch", | |
853 2, | |
854 ) | |
855 try: | |
3568
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
856 watcher.unwatch("content_file") |
3040 | 857 except IOError as e: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
858 self.disp( |
3040 | 859 f"Can't remove the watch: {e}", |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
860 2, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
861 ) |
3040 | 862 watcher = aionotify.Watcher() |
863 watcher.watch(**watcher_kwargs) | |
864 try: | |
865 await watcher.setup(loop) | |
866 except OSError: | |
867 # if the new file is not here yet we can have an error | |
868 # as a workaround, we do a little rest and try again | |
869 await asyncio.sleep(1) | |
870 await watcher.setup(loop) | |
871 await self.updateContent() | |
872 await update_cb() | |
873 except FileNotFoundError: | |
874 self.disp("The file seems to have been deleted.", error=True) | |
875 self.host.quit(C.EXIT_NOT_FOUND) | |
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
|
876 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
|
877 os.unlink(self.preview_file_path) |
1886
f3db27508b31
jp (blog/preview): inotify improvments:
Goffi <goffi@goffi.org>
parents:
1885
diff
changeset
|
878 try: |
3568
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
879 watcher.unwatch("content_file") |
3040 | 880 except IOError as e: |
881 self.disp( | |
882 f"Can't remove the watch: {e}", | |
883 2, | |
884 ) | |
1874
658824755a0c
jp (blog): preview command, first draft
Goffi <goffi@goffi.org>
parents:
1873
diff
changeset
|
885 |
658824755a0c
jp (blog): preview command, first draft
Goffi <goffi@goffi.org>
parents:
1873
diff
changeset
|
886 |
3040 | 887 class Import(base.CommandBase): |
1827
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
888 def __init__(self, host): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
889 super(Import, self).__init__( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
890 host, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
891 "import", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
892 use_pubsub=True, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
893 use_progress=True, |
3028 | 894 help=_("import an external blog"), |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
895 ) |
1827
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
896 |
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
897 def add_parser_options(self): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
898 self.parser.add_argument( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
899 "importer", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
900 nargs="?", |
3028 | 901 help=_("importer name, nothing to display importers list"), |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
902 ) |
3568
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
903 self.parser.add_argument("--host", help=_("original blog host")) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
904 self.parser.add_argument( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
905 "--no-images-upload", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
906 action="store_true", |
3028 | 907 help=_("do *NOT* upload images (default: do upload images)"), |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
908 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
909 self.parser.add_argument( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
910 "--upload-ignore-host", |
3028 | 911 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
|
912 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
913 self.parser.add_argument( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
914 "--ignore-tls-errors", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
915 action="store_true", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
916 help=_("ignore invalide TLS certificate for uploads"), |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
917 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
918 self.parser.add_argument( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
919 "-o", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
920 "--option", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
921 action="append", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
922 nargs=2, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
923 default=[], |
3028 | 924 metavar=("NAME", "VALUE"), |
925 help=_("importer specific options (see importer description)"), | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
926 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
927 self.parser.add_argument( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
928 "location", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
929 nargs="?", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
930 help=_( |
3028 | 931 "importer data location (see importer description), nothing to show " |
932 "importer description" | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
933 ), |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
934 ) |
1827
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
935 |
3040 | 936 async def onProgressStarted(self, metadata): |
3028 | 937 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
|
938 |
3040 | 939 async def onProgressFinished(self, metadata): |
3028 | 940 self.disp(_("Blog uploaded successfully"), 2) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
941 redirections = { |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
942 k[len(URL_REDIRECT_PREFIX) :]: v |
3028 | 943 for k, v in metadata.items() |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
944 if k.startswith(URL_REDIRECT_PREFIX) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
945 } |
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
|
946 if redirections: |
3028 | 947 conf = "\n".join( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
948 [ |
3028 | 949 "url_redirections_dict = {}".format( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
950 # we need to add ' ' before each new line |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
951 # and to double each '%' for ConfigParser |
3028 | 952 "\n ".join( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
953 json.dumps(redirections, indent=1, separators=(",", ": ")) |
3028 | 954 .replace("%", "%%") |
955 .split("\n") | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
956 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
957 ), |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
958 ] |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
959 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
960 self.disp( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
961 _( |
3568
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
962 "\nTo redirect old URLs to new ones, put the following lines in your" |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
963 " sat.conf file, in [libervia] section:\n\n{conf}" |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
964 ).format(conf=conf) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
965 ) |
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
|
966 |
3040 | 967 async def onProgressError(self, error_msg): |
3568
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
968 self.disp( |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
969 _("Error while uploading blog: {error_msg}").format(error_msg=error_msg), |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
970 error=True, |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
971 ) |
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
|
972 |
3040 | 973 async def start(self): |
1827
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
974 if self.args.location is None: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
975 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
|
976 if getattr(self.args, name): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
977 self.parser.error( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
978 _( |
3568
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
979 "{name} argument can't be used without location argument" |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
980 ).format(name=name) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
981 ) |
1827
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
982 if self.args.importer is None: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
983 self.disp( |
3028 | 984 "\n".join( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
985 [ |
3040 | 986 f"{name}: {desc}" |
987 for name, desc in await self.host.bridge.blogImportList() | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
988 ] |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
989 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
990 ) |
1827
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
991 else: |
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
992 try: |
3040 | 993 short_desc, long_desc = await self.host.bridge.blogImportDesc( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
994 self.args.importer |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
995 ) |
1827
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
996 except Exception as e: |
3028 | 997 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
|
998 -1 |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
999 ] # 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
|
1000 self.disp(msg) |
1827
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1001 self.host.quit(1) |
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1002 else: |
3040 | 1003 self.disp(f"{self.args.importer}: {short_desc}\n\n{long_desc}") |
1827
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1004 self.host.quit() |
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1005 else: |
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1006 # 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
|
1007 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
|
1008 if self.args.host: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
1009 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
|
1010 if self.args.ignore_tls_errors: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
1011 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
|
1012 if self.args.no_images_upload: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
1013 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
|
1014 if self.args.upload_ignore_host: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
1015 self.parser.error( |
3028 | 1016 "upload-ignore-host option can't be used when no-images-upload " |
1017 "is set" | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
1018 ) |
1827
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1019 elif self.args.upload_ignore_host: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
1020 options["upload_ignore_host"] = self.args.upload_ignore_host |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
1021 |
3040 | 1022 try: |
1023 progress_id = await self.host.bridge.blogImport( | |
1024 self.args.importer, | |
1025 self.args.location, | |
1026 options, | |
1027 self.args.service, | |
1028 self.args.node, | |
1029 self.profile, | |
1030 ) | |
1031 except Exception as e: | |
1032 self.disp( | |
3568
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
1033 _("Error while trying to import a blog: {e}").format(e=e), |
3040 | 1034 error=True, |
1035 ) | |
1036 self.host.quit(1) | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
1037 |
3040 | 1038 await self.set_progress_id(progress_id) |
1827
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1039 |
3568
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
1040 |
1827
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1041 class Blog(base.CommandBase): |
3521
62f490eff51c
jp (pubsub, blog): `rename` commands implementation
Goffi <goffi@goffi.org>
parents:
3520
diff
changeset
|
1042 subcommands = (Set, Get, Edit, Rename, Preview, Import) |
1827
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1043 |
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1044 def __init__(self, host): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
1045 super(Blog, self).__init__( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
1046 host, "blog", use_profile=False, help=_("blog/microblog management") |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
1047 ) |