Files
CaddyToDSM/service_functions.py
T

62 lines
1.8 KiB
Python

import json
from structs import CertInfo, DSMCredentials
from pathlib import Path
from dataclasses import asdict
import logging
class ServiceFunctions:
def save_cert_info(self, cert_info: CertInfo, path_to_save_folder: Path):
data = asdict(cert_info)
save_dir = path_to_save_folder / cert_info.dir_cert.name
save_dir.mkdir(parents=True, exist_ok=True)
data["expires_at"] = cert_info.expires_at.isoformat()
data["path_to_key"] = str(cert_info.path_to_key)
data["dir_cert"] = str(cert_info.dir_cert)
(save_dir / cert_info.dir_cert.name).write_text(
json.dumps(data, indent=4, ensure_ascii=False),
encoding="utf-8"
)
@staticmethod
def get_login_pass_from_file(path_to_file: Path):
if not path_to_file.is_file():
logging.error(f"Secret file not found: {path_to_file}")
return None
try:
lines = path_to_file.read_text(encoding="utf-8").splitlines()
except OSError as e:
logging.error(f"Cannot read secret file {path_to_file}: {e}")
return None
secret = {}
for line in lines:
line = line.strip()
if not line or "=" not in line:
continue
key, value = line.split("=", 1)
secret[key.strip()] = value.strip()
user_name = secret.get("username")
password = secret.get("password")
if not user_name:
logging.error("user_name is not specified in secret")
return None
if not password:
logging.error("password is not specified in secret")
return None
return DSMCredentials(
user_name=user_name,
password=password,
)