mirror of
https://github.com/ihabunek/toot.git
synced 2025-02-02 15:07:51 -05:00
Add settings
This commit is contained in:
parent
bebd8e9023
commit
067f136b9d
@ -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)
|
||||
|
@ -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 <red>posting</red> <dim>dim</dim> <underline><red>unde</red><blue>rline</blue></underline> <b>bold</b> unstlyed")
|
||||
echo("<bold>Bold<italic> bold and italic </bold>italic</italic>")
|
||||
echo("<bold red underline>foo</>bar")
|
||||
echo("\\<bold red underline>foo</>bar")
|
||||
echo("plain <blue>blue <underline> blue <green>and</green> underline </underline> blue </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.")
|
||||
|
53
toot/settings.py
Normal file
53
toot/settings.py
Normal file
@ -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
|
Loading…
x
Reference in New Issue
Block a user