Toot-Mastodon-CLI-TUI-clien.../toot/console.py

551 lines
14 KiB
Python
Raw Normal View History

2017-04-15 14:53:08 +02:00
# -*- coding: utf-8 -*-
import logging
2018-01-14 15:34:41 +01:00
import os
import shutil
2017-04-12 16:42:04 +02:00
import sys
from argparse import ArgumentParser, FileType, ArgumentTypeError
2017-04-19 14:47:30 +02:00
from collections import namedtuple
2018-06-12 12:22:16 +02:00
from toot import config, commands, CLIENT_NAME, CLIENT_WEBSITE, __version__
2017-12-30 13:32:52 +01:00
from toot.exceptions import ApiError, ConsoleError
from toot.output import print_out, print_err
2017-04-19 14:47:30 +02:00
VISIBILITY_CHOICES = ['public', 'unlisted', 'private', 'direct']
def language(value):
"""Validates the language parameter"""
if len(value) != 3:
raise ArgumentTypeError(
"Invalid language specified: '{}'. Expected a 3 letter "
"abbreviation according to ISO 639-2 standard.".format(value)
)
return value
2017-04-19 14:47:30 +02:00
def visibility(value):
"""Validates the visibility parameter"""
2017-04-19 14:47:30 +02:00
if value not in VISIBILITY_CHOICES:
raise ValueError("Invalid visibility value")
2017-04-19 14:47:30 +02:00
return value
def timeline_count(value):
n = int(value)
if not 0 < n <= 20:
raise ArgumentTypeError("Number of toots should be between 1 and 20.")
return n
def editor(value):
if not value:
raise ArgumentTypeError(
"Editor not specified in --editor option and $EDITOR environment "
"variable not set."
)
# Check editor executable exists
exe = shutil.which(value)
if not exe:
2019-08-29 12:46:00 +02:00
raise ArgumentTypeError("Editor `{}` not found".format(value))
return exe
2017-04-19 14:47:30 +02:00
Command = namedtuple("Command", ["name", "description", "require_auth", "arguments"])
2020-01-02 10:34:00 +01:00
# Arguments added to every command
common_args = [
(["--no-color"], {
"help": "don't use ANSI colors in output",
"action": 'store_true',
"default": False,
}),
(["--quiet"], {
"help": "don't write to stdout on success",
"action": 'store_true',
"default": False,
}),
(["--debug"], {
"help": "show debug log in console",
"action": 'store_true',
"default": False,
}),
]
# Arguments added to commands which require authentication
common_auth_args = [
(["-u", "--using"], {
"help": "the account to use, overrides active account",
}),
]
2017-04-26 11:49:21 +02:00
account_arg = (["account"], {
"help": "account name, e.g. 'Gargron@mastodon.social'",
2017-04-26 11:49:21 +02:00
})
instance_arg = (["-i", "--instance"], {
"type": str,
"help": 'mastodon instance to log into e.g. "mastodon.social"',
})
email_arg = (["-e", "--email"], {
"type": str,
"help": 'email address to log in with',
})
2018-12-25 02:20:30 +01:00
scheme_arg = (["--disable-https"], {
"help": "disable HTTPS and use insecure HTTP",
"dest": "scheme",
"default": "https",
"action": "store_const",
"const": "http",
})
status_id_arg = (["status_id"], {
"help": "ID of the status",
"type": int,
})
2017-04-26 11:49:21 +02:00
# Arguments for selecting a timeline (see `toot.commands.get_timeline_generator`)
common_timeline_args = [
(["-p", "--public"], {
"action": "store_true",
"default": False,
"help": "show public timeline (does not require auth)",
}),
(["-t", "--tag"], {
"type": str,
"help": "show hashtag timeline (does not require auth)",
}),
(["-l", "--local"], {
"action": "store_true",
"default": False,
"help": "show only statuses from local instance (public and tag timelines only)",
}),
(["-i", "--instance"], {
"type": str,
"help": "mastodon instance from which to read (public and tag timelines only)",
}),
(["--list"], {
"type": int,
"help": "show timeline for given list.",
}),
]
timeline_args = common_timeline_args + [
(["-c", "--count"], {
"type": timeline_count,
"help": "number of toots to show per page (1-20, default 10).",
"default": 10,
}),
(["-r", "--reverse"], {
"action": "store_true",
"default": False,
"help": "Reverse the order of the shown timeline (to new posts at the bottom)",
}),
(["-1", "--once"], {
"action": "store_true",
"default": False,
"help": "Only show the first <count> toots, do not prompt to continue.",
}),
]
curses_args = common_timeline_args + [
(["-c", "--count"], {
"type": timeline_count,
"help": "number of toots to show per page (1-20, default 20).",
"default": 20,
}),
]
AUTH_COMMANDS = [
2017-04-19 14:47:30 +02:00
Command(
name="login",
2018-06-15 09:39:28 +02:00
description="Log into a mastodon instance using your browser (recommended)",
2018-12-25 02:20:30 +01:00
arguments=[instance_arg, scheme_arg],
require_auth=False,
),
Command(
2018-06-15 09:39:28 +02:00
name="login_cli",
description="Log in from the console, does NOT support two factor authentication",
2018-12-25 02:20:30 +01:00
arguments=[instance_arg, email_arg, scheme_arg],
require_auth=False,
),
Command(
name="activate",
description="Switch between logged in accounts.",
arguments=[account_arg],
2017-04-19 14:47:30 +02:00
require_auth=False,
),
Command(
name="logout",
description="Log out, delete stored access keys",
arguments=[account_arg],
2017-04-19 14:47:30 +02:00
require_auth=False,
),
Command(
name="auth",
description="Show logged in accounts and instances",
2017-04-19 14:47:30 +02:00
arguments=[],
require_auth=False,
),
]
2019-08-29 13:44:06 +02:00
TUI_COMMANDS = [
Command(
name="tui",
description="Launches the toot terminal user interface",
arguments=[],
2020-01-02 10:21:58 +01:00
require_auth=True,
2019-08-29 13:44:06 +02:00
),
Command(
name="curses",
description="An experimental timeline app (DEPRECATED, use 'toot tui' instead)",
arguments=curses_args,
require_auth=False,
),
]
READ_COMMANDS = [
2017-04-19 14:47:30 +02:00
Command(
name="whoami",
description="Display logged in user details",
arguments=[],
require_auth=True,
),
2017-04-19 15:29:40 +02:00
Command(
name="whois",
description="Display account details",
2017-04-19 15:29:40 +02:00
arguments=[
(["account"], {
"help": "account name or numeric ID"
}),
],
require_auth=True,
),
Command(
name="notifications",
description="Notifications for logged in user",
arguments=[
(["--clear"], {
"help": "delete all notifications from the server",
"action": 'store_true',
"default": False,
}),
],
require_auth=True,
),
2017-12-29 14:26:40 +01:00
Command(
name="instance",
description="Display instance details",
arguments=[
(["instance"], {
"help": "instance domain (e.g. 'mastodon.social') or blank to use current",
"nargs": "?",
}),
scheme_arg,
2017-12-29 14:26:40 +01:00
],
require_auth=False,
),
Command(
name="search",
description="Search for users or hashtags",
arguments=[
(["query"], {
"help": "the search query",
}),
(["-r", "--resolve"], {
"action": 'store_true',
"default": False,
"help": "Resolve non-local accounts",
}),
],
require_auth=True,
),
Command(
name="thread",
description="Show toot thread items",
arguments=[
(["status_id"], {
"help": "Show thread for toot.",
}),
],
require_auth=True,
),
Command(
name="timeline",
2018-06-12 10:40:36 +02:00
description="Show recent items in a timeline (home by default)",
arguments=timeline_args,
require_auth=True,
),
]
POST_COMMANDS = [
2017-04-19 14:47:30 +02:00
Command(
name="post",
description="Post a status text to your timeline",
arguments=[
(["text"], {
"help": "The status text to post.",
"nargs": "?",
2017-04-19 14:47:30 +02:00
}),
(["-m", "--media"], {
"action": "append",
"type": FileType("rb"),
"help": "path to the media file to attach (specify multiple "
"times to attach up to 4 files)"
2017-04-19 14:47:30 +02:00
}),
(["-v", "--visibility"], {
"type": visibility,
"default": "public",
"help": 'post visibility, one of: %s' % ", ".join(VISIBILITY_CHOICES),
}),
(["-s", "--sensitive"], {
"action": 'store_true',
"default": False,
"help": "mark the media as NSFW",
}),
(["-p", "--spoiler-text"], {
"type": str,
"help": "text to be shown as a warning before the actual content",
2018-06-13 12:43:31 +02:00
}),
(["-r", "--reply-to"], {
"type": int,
"help": "local ID of the status you want to reply to",
}),
(["-l", "--language"], {
"type": language,
"help": "ISO 639-2 language code of the toot, to skip automatic detection",
2018-06-13 12:43:31 +02:00
}),
(["-e", "--editor"], {
"type": editor,
"nargs": "?",
"const": os.getenv("EDITOR", ""), # option given without value
"help": "Specify an editor to compose your toot, "
"defaults to editor defined in $EDITOR env variable.",
}),
2017-04-19 14:47:30 +02:00
],
require_auth=True,
),
Command(
name="upload",
description="Upload an image or video file",
arguments=[
(["file"], {
"help": "Path to the file to upload",
"type": FileType('rb')
})
],
require_auth=True,
),
]
STATUS_COMMANDS = [
2018-06-14 10:40:16 +02:00
Command(
name="delete",
description="Delete a status",
arguments=[status_id_arg],
require_auth=True,
),
Command(
name="favourite",
description="Favourite a status",
arguments=[status_id_arg],
require_auth=True,
),
Command(
name="unfavourite",
description="Unfavourite a status",
arguments=[status_id_arg],
require_auth=True,
),
Command(
name="reblog",
description="Reblog a status",
arguments=[status_id_arg],
require_auth=True,
),
Command(
name="unreblog",
description="Unreblog a status",
arguments=[status_id_arg],
require_auth=True,
),
2019-01-24 09:36:25 +01:00
Command(
name="reblogged_by",
description="Show accounts that reblogged the status",
arguments=[status_id_arg],
require_auth=False,
),
Command(
name="pin",
description="Pin a status",
arguments=[status_id_arg],
require_auth=True,
),
Command(
name="unpin",
description="Unpin a status",
arguments=[status_id_arg],
2018-06-14 10:40:16 +02:00
require_auth=True,
),
]
ACCOUNTS_COMMANDS = [
2017-04-19 14:47:30 +02:00
Command(
name="follow",
description="Follow an account",
arguments=[
2017-04-26 11:49:21 +02:00
account_arg,
2017-04-19 14:47:30 +02:00
],
require_auth=True,
),
Command(
name="unfollow",
description="Unfollow an account",
arguments=[
2017-04-26 11:49:21 +02:00
account_arg,
],
require_auth=True,
),
Command(
name="mute",
description="Mute an account",
arguments=[
account_arg,
],
require_auth=True,
),
Command(
name="unmute",
description="Unmute an account",
arguments=[
account_arg,
],
require_auth=True,
),
Command(
name="block",
description="Block an account",
arguments=[
account_arg,
],
require_auth=True,
),
Command(
name="unblock",
description="Unblock an account",
arguments=[
account_arg,
2017-04-19 14:47:30 +02:00
],
require_auth=True,
),
]
2019-08-29 13:44:06 +02:00
COMMANDS = AUTH_COMMANDS + READ_COMMANDS + TUI_COMMANDS + POST_COMMANDS + STATUS_COMMANDS + ACCOUNTS_COMMANDS
2017-04-12 16:42:04 +02:00
def print_usage():
max_name_len = max(len(command.name) for command in COMMANDS)
groups = [
("Authentication", AUTH_COMMANDS),
2019-08-29 13:44:06 +02:00
("TUI", TUI_COMMANDS),
("Read", READ_COMMANDS),
("Post", POST_COMMANDS),
("Status", STATUS_COMMANDS),
("Accounts", ACCOUNTS_COMMANDS),
]
print_out("<green>{}</green>".format(CLIENT_NAME))
2018-06-12 12:22:16 +02:00
print_out("<blue>v{}</blue>".format(__version__))
2017-04-19 14:47:30 +02:00
for name, cmds in groups:
print_out("")
print_out(name + ":")
2017-04-19 14:47:30 +02:00
for cmd in cmds:
cmd_name = cmd.name.ljust(max_name_len + 2)
print_out(" <yellow>toot {}</yellow> {}".format(cmd_name, cmd.description))
2017-04-19 14:47:30 +02:00
print_out("")
print_out("To get help for each command run:")
print_out(" <yellow>toot <command> --help</yellow>")
print_out("")
print_out("<green>{}</green>".format(CLIENT_WEBSITE))
2017-04-12 16:42:04 +02:00
2017-04-19 14:47:30 +02:00
def get_argument_parser(name, command):
parser = ArgumentParser(
prog='toot %s' % name,
description=command.description,
epilog=CLIENT_WEBSITE)
2017-04-12 16:42:04 +02:00
combined_args = command.arguments + common_args
if command.require_auth:
combined_args += common_auth_args
for args, kwargs in combined_args:
parser.add_argument(*args, **kwargs)
2017-04-19 14:47:30 +02:00
return parser
2017-04-12 16:42:04 +02:00
2017-04-15 12:12:33 +02:00
2017-04-19 14:47:30 +02:00
def run_command(app, user, name, args):
command = next((c for c in COMMANDS if c.name == name), None)
2017-04-12 16:42:04 +02:00
2017-04-19 14:47:30 +02:00
if not command:
print_err("Unknown command '{}'\n".format(name))
2017-04-19 14:47:30 +02:00
print_usage()
2017-04-16 17:15:05 +02:00
return
2017-04-19 14:47:30 +02:00
parser = get_argument_parser(name, command)
parsed_args = parser.parse_args(args)
2017-04-13 13:52:28 +02:00
# Override the active account if 'using' option is given
if command.require_auth and parsed_args.using:
user, app = config.get_user_app(parsed_args.using)
if not user or not app:
raise ConsoleError("User '{}' not found".format(parsed_args.using))
2017-04-19 14:47:30 +02:00
if command.require_auth and (not user or not app):
print_err("This command requires that you are logged in.")
print_err("Please run `toot login` first.")
2017-04-13 13:52:28 +02:00
return
2017-04-19 14:47:30 +02:00
fn = commands.__dict__.get(name)
2017-04-13 13:52:28 +02:00
2017-04-19 15:29:40 +02:00
if not fn:
raise NotImplementedError("Command '{}' does not have an implementation.".format(name))
2017-04-19 14:47:30 +02:00
return fn(app, user, parsed_args)
2017-04-13 13:52:28 +02:00
def main():
2018-01-14 15:34:41 +01:00
# Enable debug logging if --debug is in args
if "--debug" in sys.argv:
2018-01-14 15:34:41 +01:00
filename = os.getenv("TOOT_LOG_FILE")
logging.basicConfig(level=logging.DEBUG, filename=filename)
2017-04-12 16:42:04 +02:00
2017-04-19 14:47:30 +02:00
command_name = sys.argv[1] if len(sys.argv) > 1 else None
args = sys.argv[2:]
2017-04-12 16:42:04 +02:00
2017-04-19 14:47:30 +02:00
if not command_name:
2017-04-13 13:52:28 +02:00
return print_usage()
user, app = config.get_active_user_app()
2017-04-19 14:47:30 +02:00
2017-04-13 13:52:28 +02:00
try:
2017-04-19 14:47:30 +02:00
run_command(app, user, command_name, args)
except (ConsoleError, ApiError) as e:
print_err(str(e))
sys.exit(1)
except KeyboardInterrupt:
pass