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