diff --git a/toot/__main__.py b/toot/__main__.py index 5ba94c6..a2f6383 100644 --- a/toot/__main__.py +++ b/toot/__main__.py @@ -1,3 +1,5 @@ from toot.asynch.commands import cli +from toot.settings import load_settings -cli() +defaults = load_settings().get("commands", {}) +cli(default_map=defaults) diff --git a/toot/asynch/commands.py b/toot/asynch/commands.py index 7c824b5..c0b37c9 100644 --- a/toot/asynch/commands.py +++ b/toot/asynch/commands.py @@ -5,14 +5,13 @@ import os import random import sys - from functools import wraps -from typing import List, NamedTuple, Optional, Tuple +from typing import NamedTuple, Optional, Tuple from toot import App, User, __version__, config from toot.asynch import api from toot.asynch.entities import Account, InstanceV2, Status, from_dict, from_response -from toot.output import echo, print_out +from toot.output import print_out from toot.utils import EOF_KEY, editor_input, multiline_input # Allow overriding options using environment variables @@ -148,12 +147,18 @@ async def timeline(ctx): help="ISO 639-2 language code of the toot, to skip automatic detection", callback=validate_language ) +@click.option( + "-v", "--visibility", + type=click.Choice(["public", "unlisted", "private", "direct"]), + default="public", +) def post( text: str, editor: str, media: Tuple[str, ...], description: Tuple[str, ...], language: Optional[str], + visibility: str, ): if editor and not sys.stdin.isatty(): raise click.UsageError("Cannot run editor if not in tty.") @@ -161,14 +166,12 @@ def post( if media and len(media) > 4: raise click.UsageError("Cannot attach more than 4 files.") - echo("unstyled posting dim underline bold unstlyed") - echo("Bold bold and italic italic") - echo("foobar") - echo("\\foobar") - echo("plain blue blue and underline blue plain") - # echo("Done") # media_ids = _upload_media(app, user, args) - # status_text = _get_status_text(text, editor) + + status_text = _get_status_text(text, editor) + + print(status_text) + print(visibility) # if not status_text and not media_ids: # raise click.UsageError("You must specify either text or media to post.") diff --git a/toot/settings.py b/toot/settings.py new file mode 100644 index 0000000..1f08afe --- /dev/null +++ b/toot/settings.py @@ -0,0 +1,53 @@ +from functools import lru_cache +from os.path import exists, join +from tomlkit import parse +from toot.config import get_config_dir +from typing import Type + + +TOOT_SETTINGS_FILE_NAME = "settings.toml" + + +def get_settings_path(): + return join(get_config_dir(), TOOT_SETTINGS_FILE_NAME) + + +SETTINGS_FILE = get_settings_path() + + +def load_settings() -> dict: + if not exists(SETTINGS_FILE): + return {} + + with open(SETTINGS_FILE) as f: + return parse(f.read()) + + +@lru_cache(maxsize=None) +def get_settings(): + return load_settings() + + +def get_setting(key: str, type: Type, default=None): + """ + Get a setting value. The key should be a dot-separated string, + e.g. "commands.post.editor" which will correspond to the "editor" setting + inside the `[commands.post]` section. + """ + settings = get_settings() + return _get_setting(settings, key.split("."), type, default) + + +def _get_setting(dct, keys, type: Type, default=None): + if len(keys) == 0: + if isinstance(dct, type): + return dct + else: + # TODO: warn? cast? both? + return default + + key = keys[0] + if isinstance(dct, dict) and key in dct: + return _get_setting(dct[key], keys[1:], type, default) + + return default