Files

67 lines
2.3 KiB
Python

"""Configuration: share.config loading, constants, CLI args."""
import argparse
import configparser
from pathlib import Path
BASE_DIR = Path(__file__).resolve().parent
_cp = configparser.ConfigParser()
_cp.read_dict({
"server": {
"port": "3001", "dir": str(Path.home() / "Downloads" / "shared"),
"ttl": "7", "san": "", "lang": "ru", "refresh": "5",
"per_page": "10,25,50,100", "token": "",
"cert": "", "key": "",
},
"internal": {
"secs_per_day": "86400", "cleanup_interval": "3600",
"ssl_handshake_timeout": "5", "sniff_size": "8192",
"chunk_size": "65536", "cert_days": "3650", "cert_key_bits": "2048",
"toast_ms": "2500", "progress_hide_ms": "1200",
},
})
_cp.read(BASE_DIR / "share.config")
SECS_PER_DAY = _cp.getint("internal", "secs_per_day")
CLEANUP_INTERVAL = _cp.getint("internal", "cleanup_interval")
SSL_HANDSHAKE_TIMEOUT = _cp.getint("internal", "ssl_handshake_timeout")
SNIFF_SIZE = _cp.getint("internal", "sniff_size")
CHUNK_SIZE = _cp.getint("internal", "chunk_size")
CERT_DAYS = _cp.getint("internal", "cert_days")
CERT_KEY_BITS = _cp.getint("internal", "cert_key_bits")
def load_config() -> dict:
"""Load config: share.config defaults <- CLI args."""
conf = dict(_cp["server"])
p = argparse.ArgumentParser(description="Share — local file sharing server")
p.add_argument("-p", "--port", type=int)
p.add_argument("-d", "--dir")
p.add_argument("--ttl", type=int)
p.add_argument("--san", nargs="*")
p.add_argument("--lang")
p.add_argument("--refresh", type=int)
p.add_argument("--token")
p.add_argument("--cert", help="Path to SSL certificate file")
p.add_argument("--key", help="Path to SSL key file")
args = p.parse_args()
for key, val in vars(args).items():
if val is not None:
conf[key] = " ".join(val) if isinstance(val, list) else str(val)
return conf
def client_cfg(conf: dict) -> dict:
"""Build config dict to inject into HTML as CFG."""
return {
"lang": conf["lang"],
"refresh": int(conf["refresh"]),
"perPage": [int(x) for x in conf["per_page"].split(",")],
"toastMs": _cp.getint("internal", "toast_ms"),
"progressHideMs": _cp.getint("internal", "progress_hide_ms"),
}