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