annotate sat_frontends/jp/cmd_blog.py @ 3989:f5ba7594cced

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