Split share.py into modules by responsibility
config.py — share.config loading, constants, load_config(), client_cfg() cert.py — get_local_ip(), ensure_cert() cleanup.py — cleanup_loop() server.py — ShareServer, Handler, file_type() share.py — main() entry point only Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
"""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",
|
||||
},
|
||||
"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)
|
||||
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"),
|
||||
}
|
||||
Reference in New Issue
Block a user