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: 4. Run ankisyncd:
$ python -m ankisyncd ankisyncd.conf $ python -m ankisyncd
Setting up Anki Setting up Anki
--------------- ---------------

View File

@ -3,13 +3,12 @@ import os
import sys import sys
import getpass import getpass
import ankisyncd.config
from ankisyncd.users import SqliteUserManager from ankisyncd.users import SqliteUserManager
DATAPREFIX = os.path.join(os.path.expanduser("~"), ".local", "share") config = ankisyncd.config.load()
#DATADIR = os.path.join(DATAPREFIX, "ankisyncd") AUTHDBPATH = config['auth_db_path']
DATADIR = "." COLLECTIONPATH = config['data_root']
AUTHDBPATH = os.path.join(DATADIR, "auth.db")
COLLECTIONPATH = os.path.join(DATADIR, "collections")
def usage(): def usage():
print("usage: "+sys.argv[0]+" <command> [<args>]") 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 from ankisyncd.users import SimpleUserManager, SqliteUserManager
class SyncCollectionHandler(anki.sync.Syncer): class SyncCollectionHandler(anki.sync.Syncer):
operations = ['meta', 'applyChanges', 'start', 'applyGraves', 'chunk', 'applyChunk', 'sanityCheck2', 'finish'] operations = ['meta', 'applyChanges', 'start', 'applyGraves', 'chunk', 'applyChunk', 'sanityCheck2', 'finish']
@ -755,15 +754,14 @@ def make_app(global_conf, **local_conf):
def main(): def main():
from wsgiref.simple_server import make_server from wsgiref.simple_server import make_server
from ankisyncd.thread import shutdown from ankisyncd.thread import shutdown
import ankisyncd.config
logging.basicConfig(level=logging.INFO) logging.basicConfig(level=logging.INFO)
if len(sys.argv) < 2: if len(sys.argv) > 1:
print("usage: {} configfile".format(os.path.basename(sys.argv[0])), file=sys.stderr) # backwards compat
exit(1) config = ankisyncd.config.load(sys.argv[1])
else:
parser = ConfigParser() config = ankisyncd.config.load()
parser.read(sys.argv[1])
config = parser['sync_app']
ankiserver = SyncApp(config) ankiserver = SyncApp(config)
httpd = make_server(config['host'], int(config['port']), ankiserver) httpd = make_server(config['host'], int(config['port']), ankiserver)