anki-sync-server/ankisyncd/config.py

30 lines
919 B
Python
Raw Normal View History

2018-08-28 23:15:40 +08:00
import configparser
import logging
2018-08-29 00:04:35 +08:00
import os
from os.path import dirname, realpath
paths = [
"/etc/ankisyncd/ankisyncd.conf",
os.environ.get("XDG_CONFIG_HOME") and
(os.path.join(os.environ['XDG_CONFIG_HOME'], "ankisyncd", "ankisyncd.conf")) or
os.path.join(os.path.expanduser("~"), ".config", "ankisyncd", "ankisyncd.conf"),
os.path.join(dirname(dirname(realpath(__file__))), "ankisyncd.conf"),
]
2018-08-28 23:15:40 +08:00
def load(path=None):
2018-08-29 00:04:35 +08:00
choices = paths
parser = configparser.ConfigParser()
if path:
choices = [path]
2018-08-28 23:15:40 +08:00
for path in choices:
logging.debug("config.location: trying", path)
try:
parser.read(path)
conf = parser['sync_app']
logging.info("Loaded config from {}".format(path))
return conf
except KeyError:
pass
raise Exception("No config found, looked for {}".format(", ".join(choices)))