comparison libervia/cli/cmd_blog.py @ 4176:121925996ffb

cli (blog): new `--alt-link` option to specify alternate links
author Goffi <goffi@goffi.org>
date Tue, 05 Dec 2023 13:14:33 +0100
parents 7df6ba11bdae
children 0d7bb4df2343
comparison
equal deleted inserted replaced
4175:30f7513e5590 4176:121925996ffb
16 16
17 # You should have received a copy of the GNU Affero General Public License 17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. 18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 19
20 20
21 import argparse
21 import asyncio 22 import asyncio
22 from asyncio.subprocess import DEVNULL 23 from asyncio.subprocess import DEVNULL
23 from configparser import NoOptionError, NoSectionError 24 from configparser import NoOptionError, NoSectionError
24 import json 25 import json
25 import os 26 import os
108 109
109 # if not found, we use current syntax 110 # if not found, we use current syntax
110 return await host.bridge.param_get_a("Syntax", "Composition", "value", host.profile) 111 return await host.bridge.param_get_a("Syntax", "Composition", "value", host.profile)
111 112
112 113
114 class AltLinkAction(argparse.Action):
115 def __init__(self, option_strings, dest, nargs="+", **kwargs):
116 if nargs != "+":
117 raise ValueError('nargs must be "+"')
118
119 super().__init__(option_strings, dest, nargs=nargs, **kwargs)
120
121 def __call__(self, parser, namespace, values, option_string=None):
122 assert values
123 url = values[0]
124 if len(values) == 1:
125 media_type = None
126 elif len(values) == 2:
127 media_type = values[1]
128 else:
129 parser.error(
130 f"invalid number of argument for {', '.join(self.option_strings)}, it "
131 "must have at most 2 arguments."
132 )
133 alt_link = {'url': url}
134 if media_type is not None:
135 alt_link['media_type'] = media_type
136 alt_links = getattr(namespace, self.dest)
137 if alt_links is None:
138 alt_links = []
139 setattr(namespace, self.dest, alt_links)
140 alt_links.append(alt_link)
141
142
113 class BlogPublishCommon: 143 class BlogPublishCommon:
114 """handle common option for publising commands (Set and Edit)""" 144 """handle common option for publising commands (Set and Edit)"""
115 145
116 async def get_current_syntax(self): 146 async def get_current_syntax(self):
117 """Retrieve current_syntax 147 """Retrieve current_syntax
159 "--attachment", 189 "--attachment",
160 dest="attachments", 190 dest="attachments",
161 nargs="+", 191 nargs="+",
162 help=_( 192 help=_(
163 "attachment in the form URL [metadata_name=value]" 193 "attachment in the form URL [metadata_name=value]"
194 )
195 )
196
197 self.parser.add_argument(
198 '--alt-link',
199 action=AltLinkAction,
200 dest="alt_links",
201 metavar=('URL', 'MEDIA_TYPE'),
202 help=(
203 "add an alternative link, you can use {service}, {node} and {item} "
204 "template values in URL"
164 ) 205 )
165 ) 206 )
166 207
167 comments_group = self.parser.add_mutually_exclusive_group() 208 comments_group = self.parser.add_mutually_exclusive_group()
168 comments_group.add_argument( 209 comments_group.add_argument(
282 mb_data["title"] = self.args.title 323 mb_data["title"] = self.args.title
283 if self.args.language is not None: 324 if self.args.language is not None:
284 mb_data["language"] = self.args.language 325 mb_data["language"] = self.args.language
285 if self.args.no_id_suffix: 326 if self.args.no_id_suffix:
286 mb_data["user_friendly_id_suffix"] = False 327 mb_data["user_friendly_id_suffix"] = False
328 if self.args.alt_links:
329 mb_data.setdefault("extra", {})["alt_links"] = self.args.alt_links
287 if self.args.encrypt: 330 if self.args.encrypt:
288 mb_data["encrypted"] = True 331 mb_data["encrypted"] = True
289 if self.args.sign: 332 if self.args.sign:
290 mb_data["signed"] = True 333 mb_data["signed"] = True
291 if self.args.encrypt_for: 334 if self.args.encrypt_for: