jellyfin/app/src/config.py

54 lines
1.4 KiB
Python
Raw Normal View History

2023-04-05 16:31:14 +00:00
"""handle config file"""
import json
import os
2023-07-26 04:19:10 +00:00
from typing import Literal
2023-04-05 16:31:14 +00:00
2023-04-06 04:58:04 +00:00
from src.static_types import ConfigType
2023-04-05 16:31:14 +00:00
2023-04-06 04:58:04 +00:00
def get_config() -> ConfigType:
2023-04-05 16:31:14 +00:00
"""get connection config"""
2023-07-26 04:19:10 +00:00
config_content: ConfigType | Literal[False] = (
get_config_file() or get_config_env()
)
if not config_content:
raise ValueError("No config.json or environment variable found")
return config_content
def get_config_file() -> ConfigType | Literal[False]:
"""read config file if available"""
2023-07-27 15:38:49 +00:00
if os.path.exists("./config.json"):
with open("./config.json", "r", encoding="utf-8") as f:
config_content: ConfigType = json.loads(f.read())
2023-07-26 04:19:10 +00:00
return config_content
2023-07-26 04:19:10 +00:00
return False
2023-12-21 05:29:43 +00:00
def get_variable_or_default(variable: str, default: str) -> str:
if not variable or len(variable) < 1:
return default
return variable
2023-07-26 04:19:10 +00:00
def get_config_env() -> ConfigType | Literal[False]:
"""read config from environment"""
if "TA_URL" in os.environ:
config_content: ConfigType = {
"ta_video_path": "/youtube",
"ta_url": os.environ["TA_URL"],
"ta_token": os.environ["TA_TOKEN"],
"jf_url": os.environ["JF_URL"],
"jf_token": os.environ["JF_TOKEN"],
2023-12-21 05:29:43 +00:00
"jf_folder": get_variable_or_default(
os.environ["JF_FOLDER"], "youtube"
),
2023-07-26 04:19:10 +00:00
}
return config_content
2023-07-26 04:19:10 +00:00
return False