Use predefined config locations
This commit is contained in:
29
ankisyncd/config.py
Normal file
29
ankisyncd/config.py
Normal 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']
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user