Mercurial > libervia-backend
annotate libervia/cli/cmd_blog.py @ 4113:3f59a2b141cc
doc (installation): update `pipx` instruction and remove `requirements.txt` mention
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 09 Aug 2023 00:48:11 +0200 |
parents | 47401850dec6 |
children | 7df6ba11bdae |
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 |
4075
47401850dec6
refactoring: rename `libervia.frontends.jp` to `libervia.cli`
Goffi <goffi@goffi.org>
parents:
4074
diff
changeset
|
4 # Libervia CLI |
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 |
4071
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
34 from libervia.backend.core.i18n import _ |
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
35 from libervia.backend.tools import config |
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
36 from libervia.backend.tools.common import uri |
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
37 from libervia.backend.tools.common import data_format |
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
38 from libervia.backend.tools.common.ansi import ANSI as A |
4075
47401850dec6
refactoring: rename `libervia.frontends.jp` to `libervia.cli`
Goffi <goffi@goffi.org>
parents:
4074
diff
changeset
|
39 from libervia.cli import common |
47401850dec6
refactoring: rename `libervia.frontends.jp` to `libervia.cli`
Goffi <goffi@goffi.org>
parents:
4074
diff
changeset
|
40 from libervia.cli.constants import Const as C |
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
|
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 |
4075
47401850dec6
refactoring: rename `libervia.frontends.jp` to `libervia.cli`
Goffi <goffi@goffi.org>
parents:
4074
diff
changeset
|
603 # XXX: if we port libervia-cli one day on Windows, O_BINARY may need to be |
47401850dec6
refactoring: rename `libervia.frontends.jp` to `libervia.cli`
Goffi <goffi@goffi.org>
parents:
4074
diff
changeset
|
604 # added here |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
605 with os.fdopen( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
606 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
|
607 ) as f: |
3040 | 608 # we need to use an intermediate unicode buffer to write to the file |
609 # unicode without escaping characters | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
610 unicode_dump = json.dumps( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
611 mb_data, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
612 ensure_ascii=False, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
613 indent=4, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
614 separators=(",", ": "), |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
615 sort_keys=True, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
616 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
617 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
|
618 |
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 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
|
620 |
3040 | 621 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
|
622 """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
|
623 # we first create metadata file |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4024
diff
changeset
|
624 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
|
625 |
3040 | 626 coroutines = [] |
627 | |
1887
16527dd5a81b
blog (jp/edit): added --preview option to launch a "blog preview" in parallel
Goffi <goffi@goffi.org>
parents:
1886
diff
changeset
|
628 # 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
|
629 if self.args.preview: |
3028 | 630 self.disp("Preview requested, launching it", 1) |
2998 | 631 # we redirect outputs to /dev/null to avoid console pollution in editor |
632 # if user wants to see messages, (s)he can call "blog preview" directly | |
3040 | 633 coroutines.append( |
634 asyncio.create_subprocess_exec( | |
2998 | 635 sys.argv[0], |
636 "blog", | |
637 "preview", | |
638 "--inotify", | |
639 "true", | |
640 "-p", | |
641 self.profile, | |
3040 | 642 str(content_file_path), |
643 stdout=DEVNULL, | |
644 stderr=DEVNULL, | |
645 ) | |
2998 | 646 ) |
1887
16527dd5a81b
blog (jp/edit): added --preview option to launch a "blog preview" in parallel
Goffi <goffi@goffi.org>
parents:
1886
diff
changeset
|
647 |
3568
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
648 # we launch editor |
3040 | 649 coroutines.append( |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4024
diff
changeset
|
650 self.run_editor( |
3040 | 651 "blog_editor_args", |
652 content_file_path, | |
653 content_file_obj, | |
654 meta_file_path=meta_file_path, | |
655 meta_ori=meta_ori, | |
656 ) | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
657 ) |
2269
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
2267
diff
changeset
|
658 |
3040 | 659 await asyncio.gather(*coroutines) |
660 | |
661 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
|
662 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
|
663 |
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
|
664 if self.pubsub_item: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
665 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
|
666 |
2807
0b7ce5daee9b
plugin XEP-0277: blog items data are now entirely serialised before going to bridge:
Goffi <goffi@goffi.org>
parents:
2771
diff
changeset
|
667 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
|
668 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4024
diff
changeset
|
669 await self.host.bridge.mb_send( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
670 self.pubsub_service, self.pubsub_node, mb_data, self.profile |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
671 ) |
3028 | 672 self.disp("Blog item published") |
1866
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
673 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4024
diff
changeset
|
674 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
|
675 # we get current syntax to determine file extension |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
676 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
|
677 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4024
diff
changeset
|
678 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
|
679 items = [item] if item else [] |
3040 | 680 |
3308
384283adcce1
plugins XEP-0059, XEP-0060, XEP-0277, XEP-0313: better serialisation:
Goffi <goffi@goffi.org>
parents:
3241
diff
changeset
|
681 mb_data = data_format.deserialise( |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4024
diff
changeset
|
682 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
|
683 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
|
684 ) |
3308
384283adcce1
plugins XEP-0059, XEP-0060, XEP-0277, XEP-0313: better serialisation:
Goffi <goffi@goffi.org>
parents:
3241
diff
changeset
|
685 ) |
3568
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
686 item = mb_data["items"][0] |
3040 | 687 |
2269
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
2267
diff
changeset
|
688 try: |
3308
384283adcce1
plugins XEP-0059, XEP-0060, XEP-0277, XEP-0313: better serialisation:
Goffi <goffi@goffi.org>
parents:
3241
diff
changeset
|
689 content = item["content_xhtml"] |
2269
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
2267
diff
changeset
|
690 except KeyError: |
3308
384283adcce1
plugins XEP-0059, XEP-0060, XEP-0277, XEP-0313: better serialisation:
Goffi <goffi@goffi.org>
parents:
3241
diff
changeset
|
691 content = item["content"] |
2269
606ff34d30f2
jp (blog, common): moved and improved edit code from blog:
Goffi <goffi@goffi.org>
parents:
2267
diff
changeset
|
692 if content: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4024
diff
changeset
|
693 content = await self.host.bridge.syntax_convert( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
694 content, "text", SYNTAX_XHTML, False, self.profile |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
695 ) |
3040 | 696 |
2330
74c1dbabeec8
jp (blog/edit): pretty format XHTML before editing it
Goffi <goffi@goffi.org>
parents:
2327
diff
changeset
|
697 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
|
698 content = await self.host.bridge.syntax_convert( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
699 content, SYNTAX_XHTML, self.current_syntax, False, self.profile |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
700 ) |
3040 | 701 |
2330
74c1dbabeec8
jp (blog/edit): pretty format XHTML before editing it
Goffi <goffi@goffi.org>
parents:
2327
diff
changeset
|
702 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
|
703 content = content.strip() |
3568
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
704 if not content.startswith("<div>"): |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
705 content = "<div>" + content + "</div>" |
2330
74c1dbabeec8
jp (blog/edit): pretty format XHTML before editing it
Goffi <goffi@goffi.org>
parents:
2327
diff
changeset
|
706 try: |
74c1dbabeec8
jp (blog/edit): pretty format XHTML before editing it
Goffi <goffi@goffi.org>
parents:
2327
diff
changeset
|
707 from lxml import etree |
74c1dbabeec8
jp (blog/edit): pretty format XHTML before editing it
Goffi <goffi@goffi.org>
parents:
2327
diff
changeset
|
708 except ImportError: |
3028 | 709 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
|
710 else: |
74c1dbabeec8
jp (blog/edit): pretty format XHTML before editing it
Goffi <goffi@goffi.org>
parents:
2327
diff
changeset
|
711 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
|
712 root = etree.fromstring(content, parser) |
3028 | 713 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
|
714 |
3308
384283adcce1
plugins XEP-0059, XEP-0060, XEP-0277, XEP-0313: better serialisation:
Goffi <goffi@goffi.org>
parents:
3241
diff
changeset
|
715 return content, item, item["id"] |
1866
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
716 |
3040 | 717 async def start(self): |
1866
397ef87958b9
jp (blog): edit command, first draft:
Goffi <goffi@goffi.org>
parents:
1864
diff
changeset
|
718 # 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
|
719 SYNTAX_EXT.update( |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4024
diff
changeset
|
720 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
|
721 ) |
3040 | 722 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
|
723 |
3568
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
724 ( |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
725 self.pubsub_service, |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
726 self.pubsub_node, |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
727 self.pubsub_item, |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
728 content_file_path, |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
729 content_file_obj, |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
730 mb_data, |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4024
diff
changeset
|
731 ) = 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
|
732 |
3040 | 733 await self.edit(content_file_path, content_file_obj, mb_data=mb_data) |
734 self.host.quit() | |
1827
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
735 |
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
736 |
3521
62f490eff51c
jp (pubsub, blog): `rename` commands implementation
Goffi <goffi@goffi.org>
parents:
3520
diff
changeset
|
737 class Rename(base.CommandBase): |
62f490eff51c
jp (pubsub, blog): `rename` commands implementation
Goffi <goffi@goffi.org>
parents:
3520
diff
changeset
|
738 def __init__(self, host): |
62f490eff51c
jp (pubsub, blog): `rename` commands implementation
Goffi <goffi@goffi.org>
parents:
3520
diff
changeset
|
739 base.CommandBase.__init__( |
62f490eff51c
jp (pubsub, blog): `rename` commands implementation
Goffi <goffi@goffi.org>
parents:
3520
diff
changeset
|
740 self, |
62f490eff51c
jp (pubsub, blog): `rename` commands implementation
Goffi <goffi@goffi.org>
parents:
3520
diff
changeset
|
741 host, |
62f490eff51c
jp (pubsub, blog): `rename` commands implementation
Goffi <goffi@goffi.org>
parents:
3520
diff
changeset
|
742 "rename", |
62f490eff51c
jp (pubsub, blog): `rename` commands implementation
Goffi <goffi@goffi.org>
parents:
3520
diff
changeset
|
743 use_pubsub=True, |
62f490eff51c
jp (pubsub, blog): `rename` commands implementation
Goffi <goffi@goffi.org>
parents:
3520
diff
changeset
|
744 pubsub_flags={C.SINGLE_ITEM}, |
62f490eff51c
jp (pubsub, blog): `rename` commands implementation
Goffi <goffi@goffi.org>
parents:
3520
diff
changeset
|
745 help=_("rename an blog item"), |
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 |
62f490eff51c
jp (pubsub, blog): `rename` commands implementation
Goffi <goffi@goffi.org>
parents:
3520
diff
changeset
|
748 def add_parser_options(self): |
3568
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
749 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
|
750 |
62f490eff51c
jp (pubsub, blog): `rename` commands implementation
Goffi <goffi@goffi.org>
parents:
3520
diff
changeset
|
751 async def start(self): |
62f490eff51c
jp (pubsub, blog): `rename` commands implementation
Goffi <goffi@goffi.org>
parents:
3520
diff
changeset
|
752 try: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4024
diff
changeset
|
753 await self.host.bridge.mb_rename( |
3521
62f490eff51c
jp (pubsub, blog): `rename` commands implementation
Goffi <goffi@goffi.org>
parents:
3520
diff
changeset
|
754 self.args.service, |
62f490eff51c
jp (pubsub, blog): `rename` commands implementation
Goffi <goffi@goffi.org>
parents:
3520
diff
changeset
|
755 self.args.node, |
62f490eff51c
jp (pubsub, blog): `rename` commands implementation
Goffi <goffi@goffi.org>
parents:
3520
diff
changeset
|
756 self.args.item, |
62f490eff51c
jp (pubsub, blog): `rename` commands implementation
Goffi <goffi@goffi.org>
parents:
3520
diff
changeset
|
757 self.args.new_id, |
62f490eff51c
jp (pubsub, blog): `rename` commands implementation
Goffi <goffi@goffi.org>
parents:
3520
diff
changeset
|
758 self.profile, |
62f490eff51c
jp (pubsub, blog): `rename` commands implementation
Goffi <goffi@goffi.org>
parents:
3520
diff
changeset
|
759 ) |
62f490eff51c
jp (pubsub, blog): `rename` commands implementation
Goffi <goffi@goffi.org>
parents:
3520
diff
changeset
|
760 except Exception as e: |
3568
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
761 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
|
762 self.host.quit(C.EXIT_BRIDGE_ERRBACK) |
62f490eff51c
jp (pubsub, blog): `rename` commands implementation
Goffi <goffi@goffi.org>
parents:
3520
diff
changeset
|
763 else: |
62f490eff51c
jp (pubsub, blog): `rename` commands implementation
Goffi <goffi@goffi.org>
parents:
3520
diff
changeset
|
764 self.disp("Item renamed") |
62f490eff51c
jp (pubsub, blog): `rename` commands implementation
Goffi <goffi@goffi.org>
parents:
3520
diff
changeset
|
765 self.host.quit(C.EXIT_OK) |
62f490eff51c
jp (pubsub, blog): `rename` commands implementation
Goffi <goffi@goffi.org>
parents:
3520
diff
changeset
|
766 |
3568
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
767 |
3841
b337d7da72e5
li (blog): new `repeat` subcommand, to re-publish a blog post:
Goffi <goffi@goffi.org>
parents:
3727
diff
changeset
|
768 class Repeat(base.CommandBase): |
b337d7da72e5
li (blog): new `repeat` subcommand, to re-publish a blog post:
Goffi <goffi@goffi.org>
parents:
3727
diff
changeset
|
769 def __init__(self, host): |
b337d7da72e5
li (blog): new `repeat` subcommand, to re-publish a blog post:
Goffi <goffi@goffi.org>
parents:
3727
diff
changeset
|
770 super().__init__( |
b337d7da72e5
li (blog): new `repeat` subcommand, to re-publish a blog post:
Goffi <goffi@goffi.org>
parents:
3727
diff
changeset
|
771 host, |
b337d7da72e5
li (blog): new `repeat` subcommand, to re-publish a blog post:
Goffi <goffi@goffi.org>
parents:
3727
diff
changeset
|
772 "repeat", |
b337d7da72e5
li (blog): new `repeat` subcommand, to re-publish a blog post:
Goffi <goffi@goffi.org>
parents:
3727
diff
changeset
|
773 use_pubsub=True, |
b337d7da72e5
li (blog): new `repeat` subcommand, to re-publish a blog post:
Goffi <goffi@goffi.org>
parents:
3727
diff
changeset
|
774 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
|
775 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
|
776 ) |
b337d7da72e5
li (blog): new `repeat` subcommand, to re-publish a blog post:
Goffi <goffi@goffi.org>
parents:
3727
diff
changeset
|
777 |
b337d7da72e5
li (blog): new `repeat` subcommand, to re-publish a blog post:
Goffi <goffi@goffi.org>
parents:
3727
diff
changeset
|
778 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
|
779 pass |
b337d7da72e5
li (blog): new `repeat` subcommand, to re-publish a blog post:
Goffi <goffi@goffi.org>
parents:
3727
diff
changeset
|
780 |
b337d7da72e5
li (blog): new `repeat` subcommand, to re-publish a blog post:
Goffi <goffi@goffi.org>
parents:
3727
diff
changeset
|
781 async def start(self): |
b337d7da72e5
li (blog): new `repeat` subcommand, to re-publish a blog post:
Goffi <goffi@goffi.org>
parents:
3727
diff
changeset
|
782 try: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4024
diff
changeset
|
783 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
|
784 self.args.service, |
b337d7da72e5
li (blog): new `repeat` subcommand, to re-publish a blog post:
Goffi <goffi@goffi.org>
parents:
3727
diff
changeset
|
785 self.args.node, |
b337d7da72e5
li (blog): new `repeat` subcommand, to re-publish a blog post:
Goffi <goffi@goffi.org>
parents:
3727
diff
changeset
|
786 self.args.item, |
b337d7da72e5
li (blog): new `repeat` subcommand, to re-publish a blog post:
Goffi <goffi@goffi.org>
parents:
3727
diff
changeset
|
787 "", |
b337d7da72e5
li (blog): new `repeat` subcommand, to re-publish a blog post:
Goffi <goffi@goffi.org>
parents:
3727
diff
changeset
|
788 self.profile, |
b337d7da72e5
li (blog): new `repeat` subcommand, to re-publish a blog post:
Goffi <goffi@goffi.org>
parents:
3727
diff
changeset
|
789 ) |
b337d7da72e5
li (blog): new `repeat` subcommand, to re-publish a blog post:
Goffi <goffi@goffi.org>
parents:
3727
diff
changeset
|
790 except Exception as e: |
b337d7da72e5
li (blog): new `repeat` subcommand, to re-publish a blog post:
Goffi <goffi@goffi.org>
parents:
3727
diff
changeset
|
791 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
|
792 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
|
793 else: |
b337d7da72e5
li (blog): new `repeat` subcommand, to re-publish a blog post:
Goffi <goffi@goffi.org>
parents:
3727
diff
changeset
|
794 if repeat_id: |
b337d7da72e5
li (blog): new `repeat` subcommand, to re-publish a blog post:
Goffi <goffi@goffi.org>
parents:
3727
diff
changeset
|
795 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
|
796 else: |
b337d7da72e5
li (blog): new `repeat` subcommand, to re-publish a blog post:
Goffi <goffi@goffi.org>
parents:
3727
diff
changeset
|
797 self.disp("Item repeated") |
b337d7da72e5
li (blog): new `repeat` subcommand, to re-publish a blog post:
Goffi <goffi@goffi.org>
parents:
3727
diff
changeset
|
798 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
|
799 |
b337d7da72e5
li (blog): new `repeat` subcommand, to re-publish a blog post:
Goffi <goffi@goffi.org>
parents:
3727
diff
changeset
|
800 |
2969
eafea3bcf111
jp (blog/preview): fixed call to getCurrentFile
Goffi <goffi@goffi.org>
parents:
2945
diff
changeset
|
801 class Preview(base.CommandBase, common.BaseEdit): |
2532 | 802 # TODO: need to be rewritten with template output |
1874
658824755a0c
jp (blog): preview command, first draft
Goffi <goffi@goffi.org>
parents:
1873
diff
changeset
|
803 |
658824755a0c
jp (blog): preview command, first draft
Goffi <goffi@goffi.org>
parents:
1873
diff
changeset
|
804 def __init__(self, host): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
805 base.CommandBase.__init__( |
3028 | 806 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
|
807 ) |
2969
eafea3bcf111
jp (blog/preview): fixed call to getCurrentFile
Goffi <goffi@goffi.org>
parents:
2945
diff
changeset
|
808 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
|
809 |
658824755a0c
jp (blog): preview command, first draft
Goffi <goffi@goffi.org>
parents:
1873
diff
changeset
|
810 def add_parser_options(self): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
811 self.parser.add_argument( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
812 "--inotify", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
813 type=str, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
814 choices=("auto", "true", "false"), |
3028 | 815 default="auto", |
816 help=_("use inotify to handle preview"), | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
817 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
818 self.parser.add_argument( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
819 "file", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
820 nargs="?", |
3028 | 821 default="current", |
822 help=_("path to the content file"), | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
823 ) |
1874
658824755a0c
jp (blog): preview command, first draft
Goffi <goffi@goffi.org>
parents:
1873
diff
changeset
|
824 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4024
diff
changeset
|
825 async def show_preview(self): |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4024
diff
changeset
|
826 # 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
|
827 # when preview is not used |
3040 | 828 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
|
829 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
|
830 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4024
diff
changeset
|
831 async def _launch_preview_ext(self, cmd_line, opt_name): |
3040 | 832 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
|
833 args = common.parse_args( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
834 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
|
835 ) |
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
|
836 if not args: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
837 self.disp( |
3028 | 838 '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
|
839 error=True, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
840 ) |
1885
edd8dc8df1b9
jp (blog/preview): open and preview commands can be changed in sat.conf with "blog_preview_open_cmd" and "blog_preview_update_cmd" in [jp] section + don't convert syntax when content is empty
Goffi <goffi@goffi.org>
parents:
1883
diff
changeset
|
841 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
|
842 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
|
843 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4024
diff
changeset
|
844 async def open_preview_ext(self): |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4024
diff
changeset
|
845 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
|
846 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4024
diff
changeset
|
847 async def update_preview_ext(self): |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4024
diff
changeset
|
848 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
|
849 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4024
diff
changeset
|
850 async def update_content(self): |
3040 | 851 with self.content_file_path.open("rb") as f: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
852 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
|
853 if content and self.syntax != SYNTAX_XHTML: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
854 # 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
|
855 # 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
|
856 content = await self.host.bridge.syntax_convert( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
857 content, self.syntax, SYNTAX_XHTML, True, self.profile |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
858 ) |
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
|
859 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
860 xhtml = ( |
3040 | 861 f'<html xmlns="http://www.w3.org/1999/xhtml">' |
862 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
|
863 f"</head>" |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
864 f"<body>{content}</body>" |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
865 f"</html>" |
3040 | 866 ) |
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
|
867 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
868 with open(self.preview_file_path, "wb") as f: |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
869 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
|
870 |
3040 | 871 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
|
872 import webbrowser |
3028 | 873 import urllib.request, urllib.parse, urllib.error |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
874 |
1882
ed444e9aa078
jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents:
1881
diff
changeset
|
875 self.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
|
876 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
877 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
|
878 try: |
3040 | 879 import aionotify |
880 | |
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
|
881 except ImportError: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
882 if self.args.inotify == "auto": |
3040 | 883 aionotify = None |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
884 self.disp( |
3040 | 885 f"aionotify module not found, deactivating feature. You can " |
886 f"install it with {AIONOTIFY_INSTALL}" | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
887 ) |
1882
ed444e9aa078
jp (blog/preview): added inotify support, so preview can be updated each time the file is modified (actually IN_CLOSE_WRITE is used, but this may change in the future)
Goffi <goffi@goffi.org>
parents:
1881
diff
changeset
|
888 else: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
889 self.disp( |
3040 | 890 f"aioinotify not found, can't activate the feature! Please " |
891 f"install it with {AIONOTIFY_INSTALL}", | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
892 error=True, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
893 ) |
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
|
894 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
|
895 else: |
3040 | 896 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
|
897 |
3046 | 898 sat_conf = self.sat_conf |
3626
82e616b70a2a
CLI: fix section used for config following name change
Goffi <goffi@goffi.org>
parents:
3568
diff
changeset
|
899 SYNTAX_EXT.update( |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4024
diff
changeset
|
900 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
|
901 ) |
1874
658824755a0c
jp (blog): preview command, first draft
Goffi <goffi@goffi.org>
parents:
1873
diff
changeset
|
902 |
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
|
903 try: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4024
diff
changeset
|
904 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
|
905 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
|
906 ) |
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
|
907 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
|
908 self.open_cb_cmd = None |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4024
diff
changeset
|
909 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
|
910 else: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4024
diff
changeset
|
911 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
|
912 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4024
diff
changeset
|
913 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
|
914 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
|
915 ) |
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
|
916 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
|
917 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
|
918 else: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4024
diff
changeset
|
919 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
|
920 |
3568
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
921 # which file do we need to edit? |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
922 if self.args.file == "current": |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4024
diff
changeset
|
923 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
|
924 else: |
3040 | 925 try: |
926 self.content_file_path = Path(self.args.file).resolve(strict=True) | |
927 except FileNotFoundError: | |
3568
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
928 self.disp(_('File "{file}" doesn\'t exist!').format(file=self.args.file)) |
3040 | 929 self.host.quit(C.EXIT_NOT_FOUND) |
1874
658824755a0c
jp (blog): preview command, first draft
Goffi <goffi@goffi.org>
parents:
1873
diff
changeset
|
930 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4024
diff
changeset
|
931 self.syntax = await guess_syntax_from_path( |
3568
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
932 self.host, sat_conf, self.content_file_path |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
933 ) |
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
|
934 |
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
|
935 # 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
|
936 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
|
937 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
|
938 preview_file.close() |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4024
diff
changeset
|
939 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
|
940 |
3040 | 941 if aionotify is None: |
942 # 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
|
943 # (and webbrowser.open can return before it is read) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
944 self.disp( |
3040 | 945 f"temporary file created at {self.preview_file_path}\nthis file will NOT " |
946 f"BE DELETED AUTOMATICALLY, please delete it yourself when you have " | |
947 f"finished" | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
948 ) |
3040 | 949 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
|
950 else: |
3040 | 951 await open_cb() |
952 watcher = aionotify.Watcher() | |
953 watcher_kwargs = { | |
954 # Watcher don't accept Path so we convert to string | |
955 "path": str(self.content_file_path), | |
3568
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
956 "alias": "content_file", |
3040 | 957 "flags": aionotify.Flags.CLOSE_WRITE |
3568
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
958 | aionotify.Flags.DELETE_SELF |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
959 | aionotify.Flags.MOVE_SELF, |
3040 | 960 } |
961 watcher.watch(**watcher_kwargs) | |
1886
f3db27508b31
jp (blog/preview): inotify improvments:
Goffi <goffi@goffi.org>
parents:
1885
diff
changeset
|
962 |
3040 | 963 loop = asyncio.get_event_loop() |
964 await watcher.setup(loop) | |
1886
f3db27508b31
jp (blog/preview): inotify improvments:
Goffi <goffi@goffi.org>
parents:
1885
diff
changeset
|
965 |
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
|
966 try: |
3040 | 967 while True: |
968 event = await watcher.get_event() | |
969 self.disp("Content updated", 1) | |
3568
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
970 if event.flags & ( |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
971 aionotify.Flags.DELETE_SELF | aionotify.Flags.MOVE_SELF |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
972 ): |
3040 | 973 self.disp( |
974 "DELETE/MOVE event catched, changing the watch", | |
975 2, | |
976 ) | |
977 try: | |
3568
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
978 watcher.unwatch("content_file") |
3040 | 979 except IOError as e: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
980 self.disp( |
3040 | 981 f"Can't remove the watch: {e}", |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
982 2, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
983 ) |
3040 | 984 watcher = aionotify.Watcher() |
985 watcher.watch(**watcher_kwargs) | |
986 try: | |
987 await watcher.setup(loop) | |
988 except OSError: | |
989 # if the new file is not here yet we can have an error | |
990 # as a workaround, we do a little rest and try again | |
991 await asyncio.sleep(1) | |
992 await watcher.setup(loop) | |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4024
diff
changeset
|
993 await self.update_content() |
3040 | 994 await update_cb() |
995 except FileNotFoundError: | |
996 self.disp("The file seems to have been deleted.", error=True) | |
997 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
|
998 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
|
999 os.unlink(self.preview_file_path) |
1886
f3db27508b31
jp (blog/preview): inotify improvments:
Goffi <goffi@goffi.org>
parents:
1885
diff
changeset
|
1000 try: |
3568
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
1001 watcher.unwatch("content_file") |
3040 | 1002 except IOError as e: |
1003 self.disp( | |
1004 f"Can't remove the watch: {e}", | |
1005 2, | |
1006 ) | |
1874
658824755a0c
jp (blog): preview command, first draft
Goffi <goffi@goffi.org>
parents:
1873
diff
changeset
|
1007 |
658824755a0c
jp (blog): preview command, first draft
Goffi <goffi@goffi.org>
parents:
1873
diff
changeset
|
1008 |
3040 | 1009 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
|
1010 def __init__(self, host): |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4024
diff
changeset
|
1011 super().__init__( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
1012 host, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
1013 "import", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
1014 use_pubsub=True, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
1015 use_progress=True, |
3028 | 1016 help=_("import an external blog"), |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
1017 ) |
1827
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1018 |
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1019 def add_parser_options(self): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
1020 self.parser.add_argument( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
1021 "importer", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
1022 nargs="?", |
3028 | 1023 help=_("importer name, nothing to display importers list"), |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
1024 ) |
3568
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
1025 self.parser.add_argument("--host", help=_("original blog host")) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
1026 self.parser.add_argument( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
1027 "--no-images-upload", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
1028 action="store_true", |
3028 | 1029 help=_("do *NOT* upload images (default: do upload images)"), |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
1030 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
1031 self.parser.add_argument( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
1032 "--upload-ignore-host", |
3028 | 1033 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
|
1034 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
1035 self.parser.add_argument( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
1036 "--ignore-tls-errors", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
1037 action="store_true", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
1038 help=_("ignore invalide TLS certificate for uploads"), |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
1039 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
1040 self.parser.add_argument( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
1041 "-o", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
1042 "--option", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
1043 action="append", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
1044 nargs=2, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
1045 default=[], |
3028 | 1046 metavar=("NAME", "VALUE"), |
1047 help=_("importer specific options (see importer description)"), | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
1048 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
1049 self.parser.add_argument( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
1050 "location", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
1051 nargs="?", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
1052 help=_( |
3028 | 1053 "importer data location (see importer description), nothing to show " |
1054 "importer description" | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
1055 ), |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
1056 ) |
1827
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1057 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4024
diff
changeset
|
1058 async def on_progress_started(self, metadata): |
3028 | 1059 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
|
1060 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4024
diff
changeset
|
1061 async def on_progress_finished(self, metadata): |
3028 | 1062 self.disp(_("Blog uploaded successfully"), 2) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
1063 redirections = { |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
1064 k[len(URL_REDIRECT_PREFIX) :]: v |
3028 | 1065 for k, v in metadata.items() |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
1066 if k.startswith(URL_REDIRECT_PREFIX) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
1067 } |
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
|
1068 if redirections: |
3028 | 1069 conf = "\n".join( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
1070 [ |
3028 | 1071 "url_redirections_dict = {}".format( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
1072 # we need to add ' ' before each new line |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
1073 # and to double each '%' for ConfigParser |
3028 | 1074 "\n ".join( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
1075 json.dumps(redirections, indent=1, separators=(",", ": ")) |
3028 | 1076 .replace("%", "%%") |
1077 .split("\n") | |
2624
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 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
1082 self.disp( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
1083 _( |
3568
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
1084 "\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
|
1085 " sat.conf file, in [libervia] section:\n\n{conf}" |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
1086 ).format(conf=conf) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
1087 ) |
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
|
1088 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4024
diff
changeset
|
1089 async def on_progress_error(self, error_msg): |
3568
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
1090 self.disp( |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
1091 _("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
|
1092 error=True, |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
1093 ) |
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
|
1094 |
3040 | 1095 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
|
1096 if self.args.location is None: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
1097 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
|
1098 if getattr(self.args, name): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
1099 self.parser.error( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
1100 _( |
3568
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
1101 "{name} argument can't be used without location argument" |
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
1102 ).format(name=name) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
1103 ) |
1827
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1104 if self.args.importer is None: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
1105 self.disp( |
3028 | 1106 "\n".join( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
1107 [ |
3040 | 1108 f"{name}: {desc}" |
1109 for name, desc in await self.host.bridge.blogImportList() | |
2624
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 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
1112 ) |
1827
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1113 else: |
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1114 try: |
3040 | 1115 short_desc, long_desc = await self.host.bridge.blogImportDesc( |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
1116 self.args.importer |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
1117 ) |
1827
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1118 except Exception as e: |
3028 | 1119 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
|
1120 -1 |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
1121 ] # 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
|
1122 self.disp(msg) |
1827
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1123 self.host.quit(1) |
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1124 else: |
3040 | 1125 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
|
1126 self.host.quit() |
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1127 else: |
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1128 # 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
|
1129 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
|
1130 if self.args.host: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
1131 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
|
1132 if self.args.ignore_tls_errors: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
1133 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
|
1134 if self.args.no_images_upload: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
1135 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
|
1136 if self.args.upload_ignore_host: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
1137 self.parser.error( |
3028 | 1138 "upload-ignore-host option can't be used when no-images-upload " |
1139 "is set" | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
1140 ) |
1827
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1141 elif self.args.upload_ignore_host: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
1142 options["upload_ignore_host"] = self.args.upload_ignore_host |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
1143 |
3040 | 1144 try: |
1145 progress_id = await self.host.bridge.blogImport( | |
1146 self.args.importer, | |
1147 self.args.location, | |
1148 options, | |
1149 self.args.service, | |
1150 self.args.node, | |
1151 self.profile, | |
1152 ) | |
1153 except Exception as e: | |
1154 self.disp( | |
3568
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
1155 _("Error while trying to import a blog: {e}").format(e=e), |
3040 | 1156 error=True, |
1157 ) | |
1158 self.host.quit(1) | |
3866
915fb230cb28
cli (blog, pubsub): new `attachments` subcommands:
Goffi <goffi@goffi.org>
parents:
3858
diff
changeset
|
1159 else: |
915fb230cb28
cli (blog, pubsub): new `attachments` subcommands:
Goffi <goffi@goffi.org>
parents:
3858
diff
changeset
|
1160 await self.set_progress_id(progress_id) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
1161 |
3866
915fb230cb28
cli (blog, pubsub): new `attachments` subcommands:
Goffi <goffi@goffi.org>
parents:
3858
diff
changeset
|
1162 |
915fb230cb28
cli (blog, pubsub): new `attachments` subcommands:
Goffi <goffi@goffi.org>
parents:
3858
diff
changeset
|
1163 class AttachmentGet(cmd_pubsub.AttachmentGet): |
915fb230cb28
cli (blog, pubsub): new `attachments` subcommands:
Goffi <goffi@goffi.org>
parents:
3858
diff
changeset
|
1164 |
915fb230cb28
cli (blog, pubsub): new `attachments` subcommands:
Goffi <goffi@goffi.org>
parents:
3858
diff
changeset
|
1165 def __init__(self, host): |
915fb230cb28
cli (blog, pubsub): new `attachments` subcommands:
Goffi <goffi@goffi.org>
parents:
3858
diff
changeset
|
1166 super().__init__(host) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4024
diff
changeset
|
1167 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
|
1168 |
915fb230cb28
cli (blog, pubsub): new `attachments` subcommands:
Goffi <goffi@goffi.org>
parents:
3858
diff
changeset
|
1169 |
915fb230cb28
cli (blog, pubsub): new `attachments` subcommands:
Goffi <goffi@goffi.org>
parents:
3858
diff
changeset
|
1170 async def start(self): |
915fb230cb28
cli (blog, pubsub): new `attachments` subcommands:
Goffi <goffi@goffi.org>
parents:
3858
diff
changeset
|
1171 if not self.args.node: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4024
diff
changeset
|
1172 namespaces = await self.host.bridge.namespaces_get() |
3866
915fb230cb28
cli (blog, pubsub): new `attachments` subcommands:
Goffi <goffi@goffi.org>
parents:
3858
diff
changeset
|
1173 try: |
915fb230cb28
cli (blog, pubsub): new `attachments` subcommands:
Goffi <goffi@goffi.org>
parents:
3858
diff
changeset
|
1174 ns_microblog = namespaces["microblog"] |
915fb230cb28
cli (blog, pubsub): new `attachments` subcommands:
Goffi <goffi@goffi.org>
parents:
3858
diff
changeset
|
1175 except KeyError: |
915fb230cb28
cli (blog, pubsub): new `attachments` subcommands:
Goffi <goffi@goffi.org>
parents:
3858
diff
changeset
|
1176 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
|
1177 self.host.quit(C.EXIT_MISSING_FEATURE) |
915fb230cb28
cli (blog, pubsub): new `attachments` subcommands:
Goffi <goffi@goffi.org>
parents:
3858
diff
changeset
|
1178 else: |
915fb230cb28
cli (blog, pubsub): new `attachments` subcommands:
Goffi <goffi@goffi.org>
parents:
3858
diff
changeset
|
1179 self.args.node = ns_microblog |
915fb230cb28
cli (blog, pubsub): new `attachments` subcommands:
Goffi <goffi@goffi.org>
parents:
3858
diff
changeset
|
1180 return await super().start() |
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 |
915fb230cb28
cli (blog, pubsub): new `attachments` subcommands:
Goffi <goffi@goffi.org>
parents:
3858
diff
changeset
|
1183 class AttachmentSet(cmd_pubsub.AttachmentSet): |
915fb230cb28
cli (blog, pubsub): new `attachments` subcommands:
Goffi <goffi@goffi.org>
parents:
3858
diff
changeset
|
1184 |
915fb230cb28
cli (blog, pubsub): new `attachments` subcommands:
Goffi <goffi@goffi.org>
parents:
3858
diff
changeset
|
1185 def __init__(self, host): |
915fb230cb28
cli (blog, pubsub): new `attachments` subcommands:
Goffi <goffi@goffi.org>
parents:
3858
diff
changeset
|
1186 super().__init__(host) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4024
diff
changeset
|
1187 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
|
1188 |
915fb230cb28
cli (blog, pubsub): new `attachments` subcommands:
Goffi <goffi@goffi.org>
parents:
3858
diff
changeset
|
1189 async def start(self): |
915fb230cb28
cli (blog, pubsub): new `attachments` subcommands:
Goffi <goffi@goffi.org>
parents:
3858
diff
changeset
|
1190 if not self.args.node: |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
4024
diff
changeset
|
1191 namespaces = await self.host.bridge.namespaces_get() |
3866
915fb230cb28
cli (blog, pubsub): new `attachments` subcommands:
Goffi <goffi@goffi.org>
parents:
3858
diff
changeset
|
1192 try: |
915fb230cb28
cli (blog, pubsub): new `attachments` subcommands:
Goffi <goffi@goffi.org>
parents:
3858
diff
changeset
|
1193 ns_microblog = namespaces["microblog"] |
915fb230cb28
cli (blog, pubsub): new `attachments` subcommands:
Goffi <goffi@goffi.org>
parents:
3858
diff
changeset
|
1194 except KeyError: |
915fb230cb28
cli (blog, pubsub): new `attachments` subcommands:
Goffi <goffi@goffi.org>
parents:
3858
diff
changeset
|
1195 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
|
1196 self.host.quit(C.EXIT_MISSING_FEATURE) |
915fb230cb28
cli (blog, pubsub): new `attachments` subcommands:
Goffi <goffi@goffi.org>
parents:
3858
diff
changeset
|
1197 else: |
915fb230cb28
cli (blog, pubsub): new `attachments` subcommands:
Goffi <goffi@goffi.org>
parents:
3858
diff
changeset
|
1198 self.args.node = ns_microblog |
915fb230cb28
cli (blog, pubsub): new `attachments` subcommands:
Goffi <goffi@goffi.org>
parents:
3858
diff
changeset
|
1199 return await super().start() |
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 |
915fb230cb28
cli (blog, pubsub): new `attachments` subcommands:
Goffi <goffi@goffi.org>
parents:
3858
diff
changeset
|
1202 class Attachments(base.CommandBase): |
915fb230cb28
cli (blog, pubsub): new `attachments` subcommands:
Goffi <goffi@goffi.org>
parents:
3858
diff
changeset
|
1203 subcommands = (AttachmentGet, AttachmentSet) |
915fb230cb28
cli (blog, pubsub): new `attachments` subcommands:
Goffi <goffi@goffi.org>
parents:
3858
diff
changeset
|
1204 |
915fb230cb28
cli (blog, pubsub): new `attachments` subcommands:
Goffi <goffi@goffi.org>
parents:
3858
diff
changeset
|
1205 def __init__(self, host): |
915fb230cb28
cli (blog, pubsub): new `attachments` subcommands:
Goffi <goffi@goffi.org>
parents:
3858
diff
changeset
|
1206 super().__init__( |
915fb230cb28
cli (blog, pubsub): new `attachments` subcommands:
Goffi <goffi@goffi.org>
parents:
3858
diff
changeset
|
1207 host, |
915fb230cb28
cli (blog, pubsub): new `attachments` subcommands:
Goffi <goffi@goffi.org>
parents:
3858
diff
changeset
|
1208 "attachments", |
915fb230cb28
cli (blog, pubsub): new `attachments` subcommands:
Goffi <goffi@goffi.org>
parents:
3858
diff
changeset
|
1209 use_profile=False, |
915fb230cb28
cli (blog, pubsub): new `attachments` subcommands:
Goffi <goffi@goffi.org>
parents:
3858
diff
changeset
|
1210 help=_("set or retrieve blog attachments"), |
915fb230cb28
cli (blog, pubsub): new `attachments` subcommands:
Goffi <goffi@goffi.org>
parents:
3858
diff
changeset
|
1211 ) |
1827
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1212 |
3568
04283582966f
core, frontends: fix invalid translatable strings.
Goffi <goffi@goffi.org>
parents:
3521
diff
changeset
|
1213 |
1827
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1214 class Blog(base.CommandBase): |
3866
915fb230cb28
cli (blog, pubsub): new `attachments` subcommands:
Goffi <goffi@goffi.org>
parents:
3858
diff
changeset
|
1215 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
|
1216 |
74014a9cc203
jp: added a blog/import command to handle new blog import plugin
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1217 def __init__(self, host): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
1218 super(Blog, self).__init__( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
1219 host, "blog", use_profile=False, help=_("blog/microblog management") |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2598
diff
changeset
|
1220 ) |