feat: generate and validate config.ini
All checks were successful
Build / Upload to production (push) Successful in 1m38s
All checks were successful
Build / Upload to production (push) Successful in 1m38s
Signed-off-by: mctaylors <cantsendmails@mctaylors.ru>
This commit is contained in:
parent
c64075f84d
commit
e448cbc45f
7 changed files with 113 additions and 77 deletions
75
config.py
75
config.py
|
@ -1,10 +1,70 @@
|
|||
import configparser
|
||||
from os import path
|
||||
from typing import Optional
|
||||
|
||||
import requests
|
||||
|
||||
config_file = "config.ini"
|
||||
|
||||
_c = configparser.ConfigParser()
|
||||
_c.read("config.ini", "utf-8")
|
||||
_c.optionxform = str
|
||||
|
||||
|
||||
def init_config() -> None:
|
||||
_validate_config()
|
||||
global general, app
|
||||
general = _General()
|
||||
app = _App()
|
||||
|
||||
|
||||
def _validate_config() -> None:
|
||||
default_config = {
|
||||
"General": {"Token": "", "SourceUrl": ""},
|
||||
"App": {
|
||||
"Name": "Danbooru",
|
||||
"Host": "stashboo.ru",
|
||||
"HostName": "",
|
||||
"PreferSecureProtocol": True,
|
||||
},
|
||||
}
|
||||
|
||||
if not path.exists(config_file):
|
||||
for section, options in default_config.items():
|
||||
_c[section] = options
|
||||
with open(config_file, "w") as cf:
|
||||
_c.write(cf)
|
||||
print(f"{config_file} generated successfully.")
|
||||
return
|
||||
else:
|
||||
_c.read(config_file, "utf-8")
|
||||
|
||||
is_valid = True
|
||||
for section, options in default_config.items():
|
||||
if section != "DEFAULT" and not _c.has_section(section):
|
||||
print(f"Missing section: [{section}]")
|
||||
is_valid = False
|
||||
for option in options:
|
||||
if not _c.has_option(section, option):
|
||||
print(f"Missing option: '{option}' in section [{section}]")
|
||||
is_valid = False
|
||||
if not is_valid:
|
||||
print("Config is invalid.")
|
||||
exit(1)
|
||||
|
||||
try:
|
||||
response = requests.get(
|
||||
f"http://{_c.get("App", "Host")}/status.json", timeout=10
|
||||
)
|
||||
if response.status_code != 200:
|
||||
raise requests.exceptions.InvalidURL
|
||||
except requests.exceptions.RequestException:
|
||||
print(f"Unable to validate: 'Host' in section [App]")
|
||||
is_valid = False
|
||||
if not is_valid:
|
||||
print("Config is invalid.")
|
||||
exit(1)
|
||||
|
||||
return
|
||||
|
||||
|
||||
class _General:
|
||||
|
@ -12,6 +72,7 @@ class _General:
|
|||
sourceurl: Optional[str]
|
||||
|
||||
def __init__(self):
|
||||
_c.read(config_file, "utf-8")
|
||||
self.token = _c.get("General", "Token")
|
||||
self.sourceurl = _c.get("General", "SourceUrl")
|
||||
if not self.sourceurl.startswith("http://") and not self.sourceurl.startswith(
|
||||
|
@ -26,15 +87,9 @@ class _App:
|
|||
hostname: str
|
||||
|
||||
def __init__(self):
|
||||
_c.read(config_file, "utf-8")
|
||||
self.name = _c.get("App", "Name")
|
||||
self.host = _c.get("App", "Host")
|
||||
try:
|
||||
response = requests.get(f"http://{self.host}/status.json", timeout=10)
|
||||
if response.status_code != 200:
|
||||
raise requests.exceptions.InvalidURL
|
||||
except requests.exceptions.RequestException:
|
||||
print("Unable validate App.Host in config.ini.")
|
||||
exit(1)
|
||||
self.hostname = _c.get("App", "HostName")
|
||||
if not self.hostname:
|
||||
self.hostname = self.host
|
||||
|
@ -43,5 +98,5 @@ class _App:
|
|||
self.protocol = "https"
|
||||
|
||||
|
||||
general = _General()
|
||||
app = _App()
|
||||
general = None
|
||||
app = None
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue