1
0
mirror of https://github.com/spl0k/supysonic.git synced 2024-11-10 04:02:17 +00:00
supysonic/supysonic/config.py

100 lines
2.6 KiB
Python
Raw Normal View History

# This file is part of Supysonic.
2014-03-02 17:31:32 +00:00
# Supysonic is a Python implementation of the Subsonic server API.
#
# Copyright (C) 2013-2020 Alban 'spl0k' Féron
# 2017 Óscar García Amor
2014-03-02 17:31:32 +00:00
#
# Distributed under terms of the GNU AGPLv3 license.
2014-03-02 17:31:32 +00:00
import os
import sys
import tempfile
2012-10-13 09:29:48 +00:00
2019-12-23 15:23:57 +00:00
from configparser import RawConfigParser
current_config = None
2019-06-29 15:25:44 +00:00
def get_current_config():
return current_config or DefaultConfig()
2019-06-29 15:25:44 +00:00
2020-11-22 15:12:14 +00:00
class DefaultConfig:
2017-11-27 21:30:13 +00:00
DEBUG = False
2019-06-29 15:25:44 +00:00
tempdir = os.path.join(tempfile.gettempdir(), "supysonic")
2017-11-27 21:30:13 +00:00
BASE = {
2019-06-29 15:25:44 +00:00
"database_uri": "sqlite:///" + os.path.join(tempdir, "supysonic.db"),
"scanner_extensions": None,
"follow_symlinks": False,
2017-11-27 21:30:13 +00:00
}
WEBAPP = {
2019-06-29 15:25:44 +00:00
"cache_dir": tempdir,
"cache_size": 1024,
"transcode_cache_size": 512,
"log_file": None,
"log_level": "WARNING",
"mount_webui": True,
"mount_api": True,
"index_ignored_prefixes": "El La Le Las Les Los The",
2017-11-27 21:30:13 +00:00
}
DAEMON = {
"socket": r"\\.\pipe\supysonic"
if sys.platform == "win32"
else os.path.join(tempdir, "supysonic.sock"),
2019-06-29 15:25:44 +00:00
"run_watcher": True,
"wait_delay": 5,
"jukebox_command": None,
2019-06-29 15:25:44 +00:00
"log_file": None,
"log_level": "WARNING",
2017-11-27 21:30:13 +00:00
}
2019-06-29 15:25:44 +00:00
LASTFM = {"api_key": None, "secret": None}
2017-11-27 21:30:13 +00:00
TRANSCODING = {}
MIMETYPES = {}
def __init__(self):
current_config = self
2019-06-29 15:25:44 +00:00
2017-11-27 21:30:13 +00:00
class IniConfig(DefaultConfig):
common_paths = [
2019-06-29 15:25:44 +00:00
"/etc/supysonic",
os.path.expanduser("~/.supysonic"),
os.path.expanduser("~/.config/supysonic/supysonic.conf"),
"supysonic.conf",
2017-11-27 21:30:13 +00:00
]
2017-11-27 21:30:13 +00:00
def __init__(self, paths):
2020-11-22 15:12:14 +00:00
super().__init__()
parser = RawConfigParser()
2017-11-27 21:30:13 +00:00
parser.read(paths)
2017-11-27 21:30:13 +00:00
for section in parser.sections():
2019-06-29 15:25:44 +00:00
options = {k: self.__try_parse(v) for k, v in parser.items(section)}
2017-11-27 21:30:13 +00:00
section = section.upper()
2017-11-29 19:09:48 +00:00
2017-11-27 21:30:13 +00:00
if hasattr(self, section):
getattr(self, section).update(options)
else:
setattr(self, section, options)
2017-11-29 19:09:48 +00:00
@staticmethod
def __try_parse(value):
try:
return int(value)
except ValueError:
try:
return float(value)
except ValueError:
lv = value.lower()
2019-06-29 15:25:44 +00:00
if lv in ("yes", "true", "on"):
return True
2019-09-14 10:39:05 +00:00
if lv in ("no", "false", "off"):
return False
return value
2017-11-29 19:09:48 +00:00
2017-11-27 21:30:13 +00:00
@classmethod
def from_common_locations(cls):
return IniConfig(cls.common_paths)