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