Use predefined config locations

This commit is contained in:
flan 2018-08-28 17:15:40 +02:00
parent ec63149d5c
commit 5bcb01bd9e
4 changed files with 40 additions and 14 deletions

View File

@ -47,7 +47,7 @@ Installing
4. Run ankisyncd:
$ python -m ankisyncd ankisyncd.conf
$ python -m ankisyncd
Setting up Anki
---------------

View File

@ -3,13 +3,12 @@ import os
import sys
import getpass
import ankisyncd.config
from ankisyncd.users import SqliteUserManager
DATAPREFIX = os.path.join(os.path.expanduser("~"), ".local", "share")
#DATADIR = os.path.join(DATAPREFIX, "ankisyncd")
DATADIR = "."
AUTHDBPATH = os.path.join(DATADIR, "auth.db")
COLLECTIONPATH = os.path.join(DATADIR, "collections")
config = ankisyncd.config.load()
AUTHDBPATH = config['auth_db_path']
COLLECTIONPATH = config['data_root']
def usage():
print("usage: "+sys.argv[0]+" <command> [<args>]")

29
ankisyncd/config.py Normal file
View File

@ -0,0 +1,29 @@
import configparser
import logging
import os.path
def location():
dirname = os.path.dirname
realpath = os.path.realpath
choices = [
"/etc/ankisyncd/ankisyncd.conf",
os.environ.get("XDG_CONFIG_DIR") and
(os.path.join(os.environ['XDG_CONFIG_DIR'], "ankisyncd", "ankisyncd.conf")) or
os.path.join(os.path.expanduser("~"), ".config", "ankisyncd", "ankisyncd.conf"),
os.path.join(dirname(dirname(realpath(__file__))), "ankisyncd.conf"),
]
for path in choices:
logging.debug("config.location: trying", path)
if os.path.isfile(path):
logging.debug("config.location: choosing", path)
return path
logging.error("No config found, looked in", ", ".join(choices))
def load(path=location()):
logging.info("Loading config from {}".format(path))
parser = configparser.ConfigParser()
parser.read(path)
return parser['sync_app']

View File

@ -42,7 +42,6 @@ from anki.consts import REM_CARD, REM_NOTE
from ankisyncd.users import SimpleUserManager, SqliteUserManager
class SyncCollectionHandler(anki.sync.Syncer):
operations = ['meta', 'applyChanges', 'start', 'applyGraves', 'chunk', 'applyChunk', 'sanityCheck2', 'finish']
@ -755,15 +754,14 @@ def make_app(global_conf, **local_conf):
def main():
from wsgiref.simple_server import make_server
from ankisyncd.thread import shutdown
import ankisyncd.config
logging.basicConfig(level=logging.INFO)
if len(sys.argv) < 2:
print("usage: {} configfile".format(os.path.basename(sys.argv[0])), file=sys.stderr)
exit(1)
parser = ConfigParser()
parser.read(sys.argv[1])
config = parser['sync_app']
if len(sys.argv) > 1:
# backwards compat
config = ankisyncd.config.load(sys.argv[1])
else:
config = ankisyncd.config.load()
ankiserver = SyncApp(config)
httpd = make_server(config['host'], int(config['port']), ankiserver)