mirror of
https://github.com/ihabunek/toot.git
synced 2025-02-02 15:07:51 -05:00
Move code from toot.tui.base to toot.tui
This commit is contained in:
parent
2f3f686a00
commit
f72e4ba844
@ -23,8 +23,7 @@ import uuid
|
||||
from click.testing import CliRunner, Result
|
||||
from pathlib import Path
|
||||
from toot import api, App, User
|
||||
from toot.cli import Context
|
||||
from toot.cli.base import TootObj
|
||||
from toot.cli import Context, TootObj
|
||||
|
||||
|
||||
def pytest_configure(config):
|
||||
|
@ -3,7 +3,7 @@ from unittest import mock
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
from toot import User, cli
|
||||
from toot.cli.base import Run
|
||||
from toot.cli import Run
|
||||
|
||||
# TODO: figure out how to test login
|
||||
|
||||
|
@ -1,5 +1,134 @@
|
||||
# flake8: noqa
|
||||
from toot.cli.base import cli, Context
|
||||
import click
|
||||
import logging
|
||||
import os
|
||||
import sys
|
||||
import typing as t
|
||||
|
||||
from click.testing import Result
|
||||
from functools import wraps
|
||||
from toot import App, User, config, __version__
|
||||
from toot.settings import get_settings
|
||||
|
||||
if t.TYPE_CHECKING:
|
||||
import typing_extensions as te
|
||||
P = te.ParamSpec("P")
|
||||
|
||||
R = t.TypeVar("R")
|
||||
T = t.TypeVar("T")
|
||||
|
||||
|
||||
PRIVACY_CHOICES = ["public", "unlisted", "private"]
|
||||
VISIBILITY_CHOICES = ["public", "unlisted", "private", "direct"]
|
||||
|
||||
TUI_COLORS = {
|
||||
"1": 1,
|
||||
"16": 16,
|
||||
"88": 88,
|
||||
"256": 256,
|
||||
"16777216": 16777216,
|
||||
"24bit": 16777216,
|
||||
}
|
||||
TUI_COLORS_CHOICES = list(TUI_COLORS.keys())
|
||||
TUI_COLORS_VALUES = list(TUI_COLORS.values())
|
||||
|
||||
DURATION_EXAMPLES = """e.g. "1 day", "2 hours 30 minutes", "5 minutes 30
|
||||
seconds" or any combination of above. Shorthand: "1d", "2h30m", "5m30s\""""
|
||||
|
||||
|
||||
# Type alias for run commands
|
||||
Run = t.Callable[..., Result]
|
||||
|
||||
|
||||
def get_default_visibility() -> str:
|
||||
return os.getenv("TOOT_POST_VISIBILITY", "public")
|
||||
|
||||
|
||||
def get_default_map():
|
||||
settings = get_settings()
|
||||
common = settings.get("common", {})
|
||||
commands = settings.get("commands", {})
|
||||
return {**common, **commands}
|
||||
|
||||
|
||||
# Tweak the Click context
|
||||
# https://click.palletsprojects.com/en/8.1.x/api/#context
|
||||
CONTEXT = dict(
|
||||
# Enable using environment variables to set options
|
||||
auto_envvar_prefix="TOOT",
|
||||
# Add shorthand -h for invoking help
|
||||
help_option_names=["-h", "--help"],
|
||||
# Always show default values for options
|
||||
show_default=True,
|
||||
# Load command defaults from settings
|
||||
default_map=get_default_map(),
|
||||
)
|
||||
|
||||
|
||||
class Context(t.NamedTuple):
|
||||
app: t.Optional[App]
|
||||
user: t.Optional[User] = None
|
||||
color: bool = False
|
||||
debug: bool = False
|
||||
quiet: bool = False
|
||||
|
||||
|
||||
class TootObj(t.NamedTuple):
|
||||
"""Data to add to Click context"""
|
||||
color: bool = True
|
||||
debug: bool = False
|
||||
quiet: bool = False
|
||||
# Pass a context for testing purposes
|
||||
test_ctx: t.Optional[Context] = None
|
||||
|
||||
|
||||
def pass_context(f: "t.Callable[te.Concatenate[Context, P], R]") -> "t.Callable[P, R]":
|
||||
"""Pass the toot Context as first argument."""
|
||||
@wraps(f)
|
||||
def wrapped(*args: "P.args", **kwargs: "P.kwargs") -> R:
|
||||
return f(_get_context(), *args, **kwargs)
|
||||
|
||||
return wrapped
|
||||
|
||||
|
||||
def _get_context() -> Context:
|
||||
click_context = click.get_current_context()
|
||||
obj: TootObj = click_context.obj
|
||||
|
||||
# This is used to pass a context for testing, not used in normal usage
|
||||
if obj.test_ctx:
|
||||
return obj.test_ctx
|
||||
|
||||
user, app = config.get_active_user_app()
|
||||
if not user or not app:
|
||||
raise click.ClickException("This command requires you to be logged in.")
|
||||
|
||||
return Context(app, user, obj.color, obj.debug, obj.quiet)
|
||||
|
||||
|
||||
json_option = click.option(
|
||||
"--json",
|
||||
is_flag=True,
|
||||
default=False,
|
||||
help="Print data as JSON rather than human readable text"
|
||||
)
|
||||
|
||||
|
||||
@click.group(context_settings=CONTEXT)
|
||||
@click.option("-w", "--max-width", type=int, default=80, help="Maximum width for content rendered by toot")
|
||||
@click.option("--debug/--no-debug", default=False, help="Log debug info to stderr")
|
||||
@click.option("--color/--no-color", default=sys.stdout.isatty(), help="Use ANSI color in output")
|
||||
@click.option("--quiet/--no-quiet", default=False, help="Don't print anything to stdout")
|
||||
@click.version_option(__version__, message="%(prog)s v%(version)s")
|
||||
@click.pass_context
|
||||
def cli(ctx: click.Context, max_width: int, color: bool, debug: bool, quiet: bool):
|
||||
"""Toot is a Mastodon CLI"""
|
||||
ctx.obj = TootObj(color, debug, quiet)
|
||||
ctx.color = color
|
||||
ctx.max_content_width = max_width
|
||||
|
||||
if debug:
|
||||
logging.basicConfig(level=logging.DEBUG)
|
||||
|
||||
from toot.cli import accounts
|
||||
from toot.cli import auth
|
||||
|
@ -4,7 +4,7 @@ import json as pyjson
|
||||
from typing import BinaryIO, Optional
|
||||
|
||||
from toot import api
|
||||
from toot.cli.base import PRIVACY_CHOICES, cli, json_option, Context, pass_context
|
||||
from toot.cli import PRIVACY_CHOICES, cli, json_option, Context, pass_context
|
||||
from toot.cli.validators import validate_language
|
||||
from toot.output import print_acct_list
|
||||
|
||||
|
@ -8,7 +8,7 @@ from click.types import StringParamType
|
||||
|
||||
from toot import api, config, __version__
|
||||
from toot.auth import get_or_create_app, login_auth_code, login_username_password
|
||||
from toot.cli.base import cli
|
||||
from toot.cli import cli
|
||||
from toot.cli.validators import validate_instance
|
||||
|
||||
|
||||
|
130
toot/cli/base.py
130
toot/cli/base.py
@ -1,130 +0,0 @@
|
||||
import click
|
||||
import logging
|
||||
import os
|
||||
import sys
|
||||
import typing as t
|
||||
|
||||
from click.testing import Result
|
||||
from functools import wraps
|
||||
from toot import App, User, config, __version__
|
||||
from toot.settings import get_settings
|
||||
|
||||
if t.TYPE_CHECKING:
|
||||
import typing_extensions as te
|
||||
P = te.ParamSpec("P")
|
||||
|
||||
R = t.TypeVar("R")
|
||||
T = t.TypeVar("T")
|
||||
|
||||
|
||||
PRIVACY_CHOICES = ["public", "unlisted", "private"]
|
||||
VISIBILITY_CHOICES = ["public", "unlisted", "private", "direct"]
|
||||
|
||||
TUI_COLORS = {
|
||||
"1": 1,
|
||||
"16": 16,
|
||||
"88": 88,
|
||||
"256": 256,
|
||||
"16777216": 16777216,
|
||||
"24bit": 16777216,
|
||||
}
|
||||
TUI_COLORS_CHOICES = list(TUI_COLORS.keys())
|
||||
TUI_COLORS_VALUES = list(TUI_COLORS.values())
|
||||
|
||||
DURATION_EXAMPLES = """e.g. "1 day", "2 hours 30 minutes", "5 minutes 30
|
||||
seconds" or any combination of above. Shorthand: "1d", "2h30m", "5m30s\""""
|
||||
|
||||
|
||||
# Type alias for run commands
|
||||
Run = t.Callable[..., Result]
|
||||
|
||||
|
||||
def get_default_visibility() -> str:
|
||||
return os.getenv("TOOT_POST_VISIBILITY", "public")
|
||||
|
||||
|
||||
def get_default_map():
|
||||
settings = get_settings()
|
||||
common = settings.get("common", {})
|
||||
commands = settings.get("commands", {})
|
||||
return {**common, **commands}
|
||||
|
||||
|
||||
# Tweak the Click context
|
||||
# https://click.palletsprojects.com/en/8.1.x/api/#context
|
||||
CONTEXT = dict(
|
||||
# Enable using environment variables to set options
|
||||
auto_envvar_prefix="TOOT",
|
||||
# Add shorthand -h for invoking help
|
||||
help_option_names=["-h", "--help"],
|
||||
# Always show default values for options
|
||||
show_default=True,
|
||||
# Load command defaults from settings
|
||||
default_map=get_default_map(),
|
||||
)
|
||||
|
||||
|
||||
class Context(t.NamedTuple):
|
||||
app: t.Optional[App]
|
||||
user: t.Optional[User] = None
|
||||
color: bool = False
|
||||
debug: bool = False
|
||||
quiet: bool = False
|
||||
|
||||
|
||||
class TootObj(t.NamedTuple):
|
||||
"""Data to add to Click context"""
|
||||
color: bool = True
|
||||
debug: bool = False
|
||||
quiet: bool = False
|
||||
# Pass a context for testing purposes
|
||||
test_ctx: t.Optional[Context] = None
|
||||
|
||||
|
||||
def pass_context(f: "t.Callable[te.Concatenate[Context, P], R]") -> "t.Callable[P, R]":
|
||||
"""Pass the toot Context as first argument."""
|
||||
@wraps(f)
|
||||
def wrapped(*args: "P.args", **kwargs: "P.kwargs") -> R:
|
||||
return f(_get_context(), *args, **kwargs)
|
||||
|
||||
return wrapped
|
||||
|
||||
|
||||
def _get_context() -> Context:
|
||||
click_context = click.get_current_context()
|
||||
obj: TootObj = click_context.obj
|
||||
|
||||
# This is used to pass a context for testing, not used in normal usage
|
||||
if obj.test_ctx:
|
||||
return obj.test_ctx
|
||||
|
||||
user, app = config.get_active_user_app()
|
||||
if not user or not app:
|
||||
raise click.ClickException("This command requires you to be logged in.")
|
||||
|
||||
return Context(app, user, obj.color, obj.debug, obj.quiet)
|
||||
|
||||
|
||||
json_option = click.option(
|
||||
"--json",
|
||||
is_flag=True,
|
||||
default=False,
|
||||
help="Print data as JSON rather than human readable text"
|
||||
)
|
||||
|
||||
|
||||
@click.group(context_settings=CONTEXT)
|
||||
@click.option("-w", "--max-width", type=int, default=80, help="Maximum width for content rendered by toot")
|
||||
@click.option("--debug/--no-debug", default=False, help="Log debug info to stderr")
|
||||
@click.option("--color/--no-color", default=sys.stdout.isatty(), help="Use ANSI color in output")
|
||||
@click.option("--quiet/--no-quiet", default=False, help="Don't print anything to stdout")
|
||||
@click.version_option(__version__, message="%(prog)s v%(version)s")
|
||||
@click.pass_context
|
||||
def cli(ctx: click.Context, max_width: int, color: bool, debug: bool, quiet: bool):
|
||||
"""Toot is a Mastodon CLI"""
|
||||
ctx.obj = TootObj(color, debug, quiet)
|
||||
ctx.color = color
|
||||
ctx.max_content_width = max_width
|
||||
|
||||
if debug:
|
||||
logging.basicConfig(level=logging.DEBUG)
|
@ -1,7 +1,7 @@
|
||||
import click
|
||||
|
||||
from toot import api, config
|
||||
from toot.cli.base import Context, cli, pass_context
|
||||
from toot.cli import Context, cli, pass_context
|
||||
from toot.output import print_list_accounts, print_lists, print_warning
|
||||
|
||||
|
||||
|
@ -7,8 +7,8 @@ from time import sleep, time
|
||||
from typing import BinaryIO, Optional, Tuple
|
||||
|
||||
from toot import api
|
||||
from toot.cli.base import cli, json_option, pass_context, Context
|
||||
from toot.cli.base import DURATION_EXAMPLES, VISIBILITY_CHOICES
|
||||
from toot.cli import cli, json_option, pass_context, Context
|
||||
from toot.cli import DURATION_EXAMPLES, VISIBILITY_CHOICES
|
||||
from toot.cli.validators import validate_duration, validate_language
|
||||
from toot.entities import MediaAttachment, from_dict
|
||||
from toot.utils import EOF_KEY, delete_tmp_status_file, editor_input, multiline_input
|
||||
|
@ -9,7 +9,7 @@ from toot.cli.validators import validate_instance
|
||||
from toot.entities import Instance, Status, from_dict, Account
|
||||
from toot.exceptions import ApiError, ConsoleError
|
||||
from toot.output import print_account, print_instance, print_search_results, print_status, print_timeline
|
||||
from toot.cli.base import cli, json_option, pass_context, Context
|
||||
from toot.cli import cli, json_option, pass_context, Context
|
||||
|
||||
|
||||
@cli.command()
|
||||
|
@ -1,8 +1,8 @@
|
||||
import click
|
||||
|
||||
from toot import api
|
||||
from toot.cli.base import cli, json_option, Context, pass_context
|
||||
from toot.cli.base import VISIBILITY_CHOICES
|
||||
from toot.cli import cli, json_option, Context, pass_context
|
||||
from toot.cli import VISIBILITY_CHOICES
|
||||
from toot.output import print_table
|
||||
|
||||
|
||||
|
@ -2,7 +2,7 @@ import click
|
||||
import json as pyjson
|
||||
|
||||
from toot import api
|
||||
from toot.cli.base import cli, pass_context, json_option, Context
|
||||
from toot.cli import cli, pass_context, json_option, Context
|
||||
from toot.entities import Tag, from_dict
|
||||
from toot.output import print_tag_list, print_warning
|
||||
|
||||
|
@ -2,7 +2,7 @@ import sys
|
||||
import click
|
||||
|
||||
from toot import api
|
||||
from toot.cli.base import cli, pass_context, Context
|
||||
from toot.cli import cli, pass_context, Context
|
||||
from typing import Optional
|
||||
from toot.cli.validators import validate_instance
|
||||
|
||||
|
@ -1,7 +1,7 @@
|
||||
import click
|
||||
|
||||
from typing import Optional
|
||||
from toot.cli.base import TUI_COLORS, Context, cli, pass_context
|
||||
from toot.cli import TUI_COLORS, Context, cli, pass_context
|
||||
from toot.cli.validators import validate_tui_colors
|
||||
from toot.tui.app import TUI, TuiOptions
|
||||
|
||||
|
@ -4,7 +4,7 @@ import re
|
||||
from click import Context
|
||||
from typing import Optional
|
||||
|
||||
from toot.cli.base import TUI_COLORS
|
||||
from toot.cli import TUI_COLORS
|
||||
|
||||
|
||||
def validate_language(ctx: Context, param: str, value: Optional[str]):
|
||||
|
@ -7,7 +7,7 @@ from typing import NamedTuple, Optional
|
||||
|
||||
from toot import api, config, __version__, settings
|
||||
from toot import App, User
|
||||
from toot.cli.base import get_default_visibility
|
||||
from toot.cli import get_default_visibility
|
||||
from toot.exceptions import ApiError
|
||||
|
||||
from .compose import StatusComposer
|
||||
|
@ -1,7 +1,7 @@
|
||||
import urwid
|
||||
import logging
|
||||
|
||||
from toot.cli.base import get_default_visibility
|
||||
from toot.cli import get_default_visibility
|
||||
|
||||
from .constants import VISIBILITY_OPTIONS
|
||||
from .widgets import Button, EditBox
|
||||
|
Loading…
x
Reference in New Issue
Block a user