forked from LiveCarta/LiveCartaMeta
64 lines
1.9 KiB
Python
64 lines
1.9 KiB
Python
import json
|
|
|
|
import requests
|
|
from dynaconf import Dynaconf, Validator
|
|
from mongoengine import connect
|
|
|
|
|
|
class AppConfig:
|
|
def __init__(self):
|
|
self.config = Dynaconf(settings_files=[
|
|
"/app/configs/main.json",
|
|
"/app/configs/application_credentials.json",
|
|
"/app/configs/db.json",
|
|
"/app/configs/sources.json"
|
|
])
|
|
self.config.validators.register(
|
|
Validator('db', 'db.host', 'db.database', must_exist=True),
|
|
)
|
|
self.config.validators.validate()
|
|
|
|
creds = self.get_db_config()
|
|
connect(
|
|
db=creds.database,
|
|
host="mongodb://{host}:27017/{database}".format(host=creds.host, database=creds.database)
|
|
)
|
|
|
|
def get_bulk_insert_limit(self):
|
|
if not self.config.bulk_limit:
|
|
return 1
|
|
else:
|
|
return self.config.bulk_limit
|
|
|
|
def get_db_config(self):
|
|
return self.config.db
|
|
|
|
def get_main_app_creds(self):
|
|
return self.config.application_credentials
|
|
|
|
def get_source_by_name(self, name: str):
|
|
if name not in self.config.sources:
|
|
raise ValueError(f'"{name}" source not exists!')
|
|
return self.config.sources[name]
|
|
|
|
def get_sources_list(self):
|
|
return self.config.sources.keys()
|
|
|
|
def update_sources(self):
|
|
creds = self.get_main_app_creds()
|
|
headers = {
|
|
'Content-type': 'application/json',
|
|
'Authorization': 'Bearer {key}'.format(key=creds.api_key)
|
|
}
|
|
r = requests.get(creds.api_url + "sources", headers=headers)
|
|
if r.status_code != 200:
|
|
raise Exception('Bad app response')
|
|
|
|
new_config = {"sources": {}}
|
|
for source in r.json():
|
|
new_config["sources"][source["source_name"]] = source
|
|
|
|
with open("./configs/sources.json", "w") as outfile:
|
|
outfile.write(json.dumps(new_config))
|
|
|
|
config = AppConfig() |