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

79 lines
1.6 KiB
Python
Raw Normal View History

2017-04-15 14:53:08 +02:00
# -*- coding: utf-8 -*-
2017-04-12 16:42:04 +02:00
import os
from . import User, App
# The dir where all toot configuration is stored
2017-04-12 16:42:04 +02:00
CONFIG_DIR = os.environ['HOME'] + '/.config/toot/'
# Subfolder where application access keys for various instances are stored
INSTANCES_DIR = CONFIG_DIR + 'instances/'
# File in which user access token is stored
2017-04-12 16:42:04 +02:00
CONFIG_USER_FILE = CONFIG_DIR + 'user.cfg'
def get_instance_config_path(instance):
return INSTANCES_DIR + instance
def get_user_config_path():
return CONFIG_USER_FILE
2017-04-12 16:42:04 +02:00
def _load(file, tuple_class):
if not os.path.exists(file):
return None
with open(file, 'r') as f:
lines = f.read().split()
try:
return tuple_class(*lines)
except TypeError:
return None
def _save(file, named_tuple):
directory = os.path.dirname(file)
if not os.path.exists(directory):
os.makedirs(directory)
with open(file, 'w') as f:
2017-04-12 17:12:47 +02:00
values = [v for v in named_tuple]
f.write("\n".join(values))
2017-04-12 16:42:04 +02:00
def load_app(instance):
path = get_instance_config_path(instance)
return _load(path, App)
2017-04-12 16:42:04 +02:00
def load_user():
path = get_user_config_path()
return _load(path, User)
2017-04-12 16:42:04 +02:00
def save_app(app):
path = get_instance_config_path(app.instance)
_save(path, app)
return path
2017-04-12 16:42:04 +02:00
def save_user(user):
path = get_user_config_path()
_save(path, user)
return path
2017-04-12 16:42:04 +02:00
def delete_app(instance):
path = get_instance_config_path(instance)
os.unlink(path)
return path
2017-04-12 16:42:04 +02:00
def delete_user():
path = get_user_config_path()
os.unlink(path)
return path