2
0
mirror of https://github.com/frappe/frappe_docker.git synced 2024-12-23 18:48:58 +00:00
frappe_docker/images/worker/configure.py
Lev 6fa358ca4e
Build for v12 (#750)
* Build for v12

Typo!!

* Fix Python 3.7 compatibility

* Skip assets endpoint test on v12
2022-04-03 15:57:22 +05:30

57 lines
1.3 KiB
Python
Executable File

#!/usr/local/bin/python
from __future__ import annotations
import json
import os
from typing import Any, TypeVar
def update_config(**values: Any):
fname = "common_site_config.json"
if not os.path.exists(fname):
with open(fname, "a") as f:
json.dump({}, f)
with open(fname, "r+") as f:
config: dict[str, Any] = json.load(f)
config.update(values)
f.seek(0)
f.truncate()
json.dump(config, f)
_T = TypeVar("_T")
def env(name: str, type_: type[_T] = str) -> _T:
value = os.getenv(name)
if not value:
raise RuntimeError(f'Required environment variable "{name}" not set')
try:
value = type_(value)
except Exception:
raise RuntimeError(
f'Cannot convert environment variable "{name}" to type "{type_}"'
)
return value
def generate_redis_url(url: str):
return f"redis://{url}"
def main() -> int:
update_config(
db_host=env("DB_HOST"),
db_port=env("DB_PORT", int),
redis_cache=generate_redis_url(env("REDIS_CACHE")),
redis_queue=generate_redis_url(env("REDIS_QUEUE")),
redis_socketio=generate_redis_url(env("REDIS_SOCKETIO")),
socketio_port=env("SOCKETIO_PORT", int),
)
return 0
if __name__ == "__main__":
raise SystemExit(main())