cli (blog): new --alt-link option to specify alternate links
This commit is contained in:
parent
4db2a87feb
commit
836c8bc896
1 changed files with 43 additions and 0 deletions
|
|
@ -18,6 +18,7 @@
|
|||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
|
||||
import argparse
|
||||
import asyncio
|
||||
from asyncio.subprocess import DEVNULL
|
||||
from configparser import NoOptionError, NoSectionError
|
||||
|
|
@ -110,6 +111,35 @@ async def guess_syntax_from_path(host, sat_conf, path):
|
|||
return await host.bridge.param_get_a("Syntax", "Composition", "value", host.profile)
|
||||
|
||||
|
||||
class AltLinkAction(argparse.Action):
|
||||
def __init__(self, option_strings, dest, nargs="+", **kwargs):
|
||||
if nargs != "+":
|
||||
raise ValueError('nargs must be "+"')
|
||||
|
||||
super().__init__(option_strings, dest, nargs=nargs, **kwargs)
|
||||
|
||||
def __call__(self, parser, namespace, values, option_string=None):
|
||||
assert values
|
||||
url = values[0]
|
||||
if len(values) == 1:
|
||||
media_type = None
|
||||
elif len(values) == 2:
|
||||
media_type = values[1]
|
||||
else:
|
||||
parser.error(
|
||||
f"invalid number of argument for {', '.join(self.option_strings)}, it "
|
||||
"must have at most 2 arguments."
|
||||
)
|
||||
alt_link = {'url': url}
|
||||
if media_type is not None:
|
||||
alt_link['media_type'] = media_type
|
||||
alt_links = getattr(namespace, self.dest)
|
||||
if alt_links is None:
|
||||
alt_links = []
|
||||
setattr(namespace, self.dest, alt_links)
|
||||
alt_links.append(alt_link)
|
||||
|
||||
|
||||
class BlogPublishCommon:
|
||||
"""handle common option for publising commands (Set and Edit)"""
|
||||
|
||||
|
|
@ -164,6 +194,17 @@ class BlogPublishCommon:
|
|||
)
|
||||
)
|
||||
|
||||
self.parser.add_argument(
|
||||
'--alt-link',
|
||||
action=AltLinkAction,
|
||||
dest="alt_links",
|
||||
metavar=('URL', 'MEDIA_TYPE'),
|
||||
help=(
|
||||
"add an alternative link, you can use {service}, {node} and {item} "
|
||||
"template values in URL"
|
||||
)
|
||||
)
|
||||
|
||||
comments_group = self.parser.add_mutually_exclusive_group()
|
||||
comments_group.add_argument(
|
||||
"-C",
|
||||
|
|
@ -284,6 +325,8 @@ class BlogPublishCommon:
|
|||
mb_data["language"] = self.args.language
|
||||
if self.args.no_id_suffix:
|
||||
mb_data["user_friendly_id_suffix"] = False
|
||||
if self.args.alt_links:
|
||||
mb_data.setdefault("extra", {})["alt_links"] = self.args.alt_links
|
||||
if self.args.encrypt:
|
||||
mb_data["encrypted"] = True
|
||||
if self.args.sign:
|
||||
|
|
|
|||
Loading…
Reference in a new issue