Resolve circular import by moving get_config_dir

This commit is contained in:
Ivan Habunek 2023-06-28 14:30:22 +02:00
parent 953cad5023
commit d4f8acb3ce
No known key found for this signature in database
GPG Key ID: CDBD63C43A30BB95
4 changed files with 39 additions and 37 deletions

View File

@ -1,3 +1,7 @@
import os
import sys
from os.path import join, expanduser
from collections import namedtuple
__version__ = '0.37.0'
@ -9,3 +13,22 @@ DEFAULT_INSTANCE = 'https://mastodon.social'
CLIENT_NAME = 'toot - a Mastodon CLI client'
CLIENT_WEBSITE = 'https://github.com/ihabunek/toot'
TOOT_CONFIG_DIR_NAME = "toot"
def get_config_dir():
"""Returns the path to toot config directory"""
# On Windows, store the config in roaming appdata
if sys.platform == "win32" and "APPDATA" in os.environ:
return join(os.getenv("APPDATA"), TOOT_CONFIG_DIR_NAME)
# Respect XDG_CONFIG_HOME env variable if set
# https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
if "XDG_CONFIG_HOME" in os.environ:
config_home = expanduser(os.environ["XDG_CONFIG_HOME"])
return join(config_home, TOOT_CONFIG_DIR_NAME)
# Default to ~/.config/toot/
return join(expanduser("~"), ".config", TOOT_CONFIG_DIR_NAME)

View File

@ -1,44 +1,22 @@
import json
import os
import sys
from functools import wraps
from os.path import dirname, join, expanduser
from os.path import dirname, join
from toot import User, App
from toot import User, App, get_config_dir
from toot.exceptions import ConsoleError
from toot.output import print_out
TOOT_CONFIG_DIR_NAME = "toot"
TOOT_CONFIG_FILE_NAME = "config.json"
def get_config_dir():
"""Returns the path to toot config directory"""
# On Windows, store the config in roaming appdata
if sys.platform == "win32" and "APPDATA" in os.environ:
return join(os.getenv("APPDATA"), TOOT_CONFIG_DIR_NAME)
# Respect XDG_CONFIG_HOME env variable if set
# https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
if "XDG_CONFIG_HOME" in os.environ:
config_home = expanduser(os.environ["XDG_CONFIG_HOME"])
return join(config_home, TOOT_CONFIG_DIR_NAME)
# Default to ~/.config/toot/
return join(expanduser("~"), ".config", TOOT_CONFIG_DIR_NAME)
def get_config_file_path():
"""Returns the path to toot config file."""
return join(get_config_dir(), TOOT_CONFIG_FILE_NAME)
CONFIG_FILE = get_config_file_path()
def user_id(user):
return "{}@{}".format(user.username, user.instance)
@ -63,15 +41,18 @@ def make_config(path):
def load_config():
if not os.path.exists(CONFIG_FILE):
make_config(CONFIG_FILE)
path = get_config_file_path()
with open(CONFIG_FILE) as f:
if not os.path.exists(path):
make_config(path)
with open(path) as f:
return json.load(f)
def save_config(config):
with open(CONFIG_FILE, 'w') as f:
path = get_config_file_path()
with open(path, "w") as f:
return json.dump(config, f, indent=True, sort_keys=True)

View File

@ -4,6 +4,7 @@ import sys
import textwrap
from functools import lru_cache
from toot import settings
from toot.entities import Instance, Notification, Poll, Status
from toot.utils import get_text, parse_html
from toot.wcstring import wc_wrap
@ -118,8 +119,7 @@ def use_ansi_color():
return False
# Check in settings
from toot.settings import get_setting
color = get_setting("common.color", bool)
color = settings.get_setting("common.color", bool)
if color is not None:
return color
@ -128,7 +128,6 @@ def use_ansi_color():
def print_out(*args, **kwargs):
from toot import settings
if not settings.get_quiet():
args = [colorize(a) if use_ansi_color() else strip_tags(a) for a in args]
print(*args, **kwargs)

View File

@ -4,7 +4,7 @@ import sys
from functools import lru_cache
from os.path import exists, join
from tomlkit import parse
from toot.config import get_config_dir
from toot import get_config_dir
from typing import Optional, Type
@ -15,14 +15,13 @@ 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):
path = get_settings_path()
if not exists(path):
return {}
with open(SETTINGS_FILE) as f:
with open(path) as f:
return parse(f.read())