added new /getConfig

This commit is contained in:
Harvey Tindall 2020-06-29 23:23:43 +01:00
parent 55d26b541a
commit 52f9b5c963
3 changed files with 44 additions and 18 deletions

View File

@ -44,6 +44,7 @@ else:
data_dir = Path.home() / ".jf-accounts" data_dir = Path.home() / ".jf-accounts"
local_dir = (Path(__file__).parent / "data").resolve() local_dir = (Path(__file__).parent / "data").resolve()
config_base_path = local_dir / "config-base.json"
first_run = False first_run = False
if data_dir.exists() is False or (data_dir / "config.ini").exists() is False: if data_dir.exists() is False or (data_dir / "config.ini").exists() is False:
@ -53,8 +54,9 @@ if data_dir.exists() is False or (data_dir / "config.ini").exists() is False:
if args.config is None: if args.config is None:
config_path = data_dir / "config.ini" config_path = data_dir / "config.ini"
from jellyfin_accounts.generate_ini import generate_ini from jellyfin_accounts.generate_ini import generate_ini
default_path = local_dir / "config-default.ini" default_path = local_dir / "config-default.ini"
generate_ini(local_dir / "config-base.json", default_path) generate_ini(config_base_path, default_path)
shutil.copy(str(default_path), str(config_path)) shutil.copy(str(default_path), str(config_path))
print("Setup through the web UI, or quit and edit the configuration manually.") print("Setup through the web UI, or quit and edit the configuration manually.")
first_run = True first_run = True
@ -213,6 +215,7 @@ def resp(success=True, code=500):
r.status_code = code r.status_code = code
return r return r
def main(): def main():
if args.get_defaults: if args.get_defaults:
import json import json
@ -306,6 +309,7 @@ def main():
app = Flask(__name__, root_path=str(local_dir)) app = Flask(__name__, root_path=str(local_dir))
app.config["DEBUG"] = config.getboolean("ui", "debug") app.config["DEBUG"] = config.getboolean("ui", "debug")
app.config["SECRET_KEY"] = secrets.token_urlsafe(16) app.config["SECRET_KEY"] = secrets.token_urlsafe(16)
app.config["JSON_SORT_KEYS"] = False
from waitress import serve from waitress import serve

View File

@ -2,31 +2,29 @@ import configparser
import json import json
from pathlib import Path from pathlib import Path
def generate_ini(base_file, ini_file): def generate_ini(base_file, ini_file):
""" """
Generates .ini file from config-base file. Generates .ini file from config-base file.
""" """
with open(Path(base_file), 'r') as f: with open(Path(base_file), "r") as f:
config_base = json.load(f) config_base = json.load(f)
ini = configparser.RawConfigParser(allow_no_value=True) ini = configparser.RawConfigParser(allow_no_value=True)
for section in config_base: for section in config_base:
ini.add_section(section) ini.add_section(section)
for entry in config_base[section]: for entry in config_base[section]:
if 'description' in config_base[section][entry]: if "description" in config_base[section][entry]:
ini.set(section, ini.set(section, "; " + config_base[section][entry]["description"])
'; ' + config_base[section][entry]['description']) if entry != "meta":
if entry != 'meta': value = config_base[section][entry]["value"]
value = config_base[section][entry]['value']
if isinstance(value, bool): if isinstance(value, bool):
value = str(value).lower() value = str(value).lower()
else: else:
value = str(value) value = str(value)
ini.set(section, ini.set(section, entry, value)
entry,
value)
with open(Path(ini_file), 'w') as config_file: with open(Path(ini_file), "w") as config_file:
ini.write(config_file) ini.write(config_file)
return True return True

View File

@ -4,7 +4,16 @@ import json
import datetime import datetime
import secrets import secrets
import time import time
from jellyfin_accounts import config, config_path, app, g, data_store, resp, configparser from jellyfin_accounts import (
config,
config_path,
app,
g,
data_store,
resp,
configparser,
config_base_path,
)
from jellyfin_accounts import web_log as log from jellyfin_accounts import web_log as log
from jellyfin_accounts.validate_password import PasswordValidator from jellyfin_accounts.validate_password import PasswordValidator
@ -314,13 +323,14 @@ def setDefaults():
return resp() return resp()
@app.route("/modifyConfig", methods=["POST"]) @app.route("/modifyConfig", methods=["POST"])
@auth.login_required @auth.login_required
def modifyConfig(): def modifyConfig():
log.info("Config modification requested") log.info("Config modification requested")
data = request.get_json() data = request.get_json()
temp_config = configparser.RawConfigParser(comment_prefixes="/", allow_no_value=True) temp_config = configparser.RawConfigParser(
comment_prefixes="/", allow_no_value=True
)
temp_config.read(config_path) temp_config.read(config_path)
for section in data: for section in data:
if section in temp_config: if section in temp_config:
@ -340,8 +350,22 @@ def modifyConfig():
return resp() return resp()
@app.route('/getConfig', methods=["GET"]) # @app.route('/getConfig', methods=["GET"])
@auth.login_required # @auth.login_required
# def getConfig():
# log.debug('Config requested')
# return jsonify(config._sections), 200
@app.route("/getConfig", methods=["GET"])
# @auth.login_required
def getConfig(): def getConfig():
log.debug('Config requested') log.debug('Config requested')
return jsonify(config._sections), 200 with open(config_base_path, "r") as f:
config_base = json.load(f)
response_config = config_base
for section in config_base:
for entry in config_base[section]:
if entry in config[section]:
response_config[section][entry]["value"] = config[section][entry]
return jsonify(response_config), 200