mirror of
https://github.com/ihabunek/toot.git
synced 2024-12-04 14:46:33 -05:00
Remove legacy configuration file handling
This commit is contained in:
parent
7fc39379c9
commit
130ef9a608
@ -7,7 +7,6 @@ from functools import wraps
|
||||
from os.path import dirname
|
||||
|
||||
from toot import User, App
|
||||
from toot.config_legacy import load_legacy_config
|
||||
from toot.exceptions import ConsoleError
|
||||
from toot.output import print_out
|
||||
|
||||
@ -31,20 +30,11 @@ def user_id(user):
|
||||
|
||||
|
||||
def make_config(path):
|
||||
"""Creates a config file.
|
||||
|
||||
Attempts to load data from legacy config files if they exist.
|
||||
"""
|
||||
apps, user = load_legacy_config()
|
||||
|
||||
apps = {a.instance: a._asdict() for a in apps}
|
||||
users = {user_id(user): user._asdict()} if user else {}
|
||||
active_user = user_id(user) if user else None
|
||||
|
||||
"""Creates an empty toot configuration file."""
|
||||
config = {
|
||||
"apps": apps,
|
||||
"users": users,
|
||||
"active_user": active_user,
|
||||
"apps": {},
|
||||
"users": {},
|
||||
"active_user": None,
|
||||
}
|
||||
|
||||
print_out("Creating config file at <blue>{}</blue>".format(path))
|
||||
|
@ -1,63 +0,0 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import os
|
||||
|
||||
from . import User, App
|
||||
|
||||
# The dir where all toot configuration is stored
|
||||
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
|
||||
CONFIG_USER_FILE = CONFIG_DIR + 'user.cfg'
|
||||
|
||||
|
||||
def load_user(path):
|
||||
if not os.path.exists(path):
|
||||
return None
|
||||
|
||||
with open(path, 'r') as f:
|
||||
lines = f.read().split()
|
||||
try:
|
||||
return User(*lines)
|
||||
except TypeError:
|
||||
return None
|
||||
|
||||
|
||||
def load_apps(path):
|
||||
if not os.path.exists(path):
|
||||
return []
|
||||
|
||||
for name in os.listdir(path):
|
||||
with open(path + name) as f:
|
||||
values = f.read().split()
|
||||
try:
|
||||
yield App(*values)
|
||||
except TypeError:
|
||||
pass
|
||||
|
||||
|
||||
def add_username(user, apps):
|
||||
"""When using broser login, username was not stored so look it up"""
|
||||
if not user:
|
||||
return None
|
||||
|
||||
apps = [a for a in apps if a.instance == user.instance]
|
||||
|
||||
if not apps:
|
||||
return None
|
||||
|
||||
from toot.api import verify_credentials
|
||||
creds = verify_credentials(apps.pop(), user)
|
||||
|
||||
return User(user.instance, creds['username'], user.access_token)
|
||||
|
||||
|
||||
def load_legacy_config():
|
||||
apps = list(load_apps(INSTANCES_DIR))
|
||||
user = load_user(CONFIG_USER_FILE)
|
||||
user = add_username(user, apps)
|
||||
|
||||
return apps, user
|
Loading…
Reference in New Issue
Block a user