Pushing to save temp changes while i fix the bug found in RC

This commit is contained in:
2025-10-20 10:12:09 -04:00
parent 6f2355fea9
commit d608b3aa69
9 changed files with 61 additions and 35 deletions
+49 -23
View File
@@ -22,29 +22,46 @@ def get_base_directory() -> Path:
else:
return home / '.local' / 'share' / "AirlockTools"
def get_system_config_path() -> Path:
base_path = Path(getattr(sys, '_MEIPASS', os.path.dirname(os.path.abspath(__file__))))
return base_path.parent / "system_config.json"
def configure_logging(log_dir: Path, log_level: str = "DEBUG"):
log_file = log_dir / "airlocktools.log"
logger = logging.getLogger()
logger.setLevel(getattr(logging, log_level.upper(), logging.DEBUG))
# Always allow all messages to propagate to handlers
logger.setLevel(logging.DEBUG)
# Set httpx logger to DEBUG as well
httpx_logger = logging.getLogger("httpx")
httpx_logger.setLevel(getattr(logging, log_level.upper(), logging.DEBUG))
httpx_logger.setLevel(logging.DEBUG)
# Remove existing handlers
for handler in logger.handlers[:]:
logger.removeHandler(handler)
# File handler always logs DEBUG and above
file_handler = logging.handlers.RotatingFileHandler(
log_file, maxBytes=5_000_000, backupCount=5, encoding='utf-8'
)
file_handler.setFormatter(logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s'))
file_handler.setLevel(logging.DEBUG)
file_handler.setFormatter(logging.Formatter(
'%(asctime)s - %(name)s - %(levelname)s - %(message)s'
))
logger.addHandler(file_handler)
# Console handler respects the configured log level
console_handler = logging.StreamHandler()
console_handler.setLevel(getattr(logging, log_level.upper(), logging.INFO))
console_handler.setFormatter(logging.Formatter('%(levelname)s - %(message)s'))
logger.addHandler(console_handler)
# Optional Windows Event Log handler
if platform.system() == "Windows":
try:
event_handler = logging.handlers.NTEventLogHandler("AirlockTools")
@@ -56,40 +73,50 @@ def configure_logging(log_dir: Path, log_level: str = "DEBUG"):
logger.debug("✅ Logging configured.")
def get_system_config_path() -> Path:
base_path = Path(getattr(sys, '_MEIPASS', os.path.dirname(os.path.abspath(__file__))))
return base_path.parent / "system_config.json"
def load_system_config() -> dict:
default_config = {
"APPNAME": "AirlockTools",
"LOG_LEVEL": "DEBUG",
"PATH_EXCLUSION_CONST": 4,
"MIN_FILES_FOR_PATH": 4,
"VT_THREAT_TOLERANCE": 4,
"EXTRAS": "NOTTODAY",
"POLICY_MAP_ENF_AUD": {
"enforced_id": "audit_id"
}
}
try:
config_path = get_system_config_path()
with open(config_path, "r") as f:
return json.load(f)
except FileNotFoundError:
logging.warning("⚠️ system_config.json not found. Using built-in defaults.")
return {
"APPNAME": "AirlockTools",
"LOG_LEVEL": "DEBUG",
"PATH_EXCLUSION_CONST": 4,
"MIN_FILES_FOR_PATH": 4,
"VT_THREAT_TOLERANCE": 4,
"POLICY_MAP_ENF_AUD": {
"enforced_id": "audit_id"
}
}
except json.JSONDecodeError as e:
logging.error(f"❌ Failed to parse system_config.json: {e}")
return default_config
def load_user_config(config_dir: Path) -> dict:
user_config_path = config_dir / "user_config.json"
if not user_config_path.exists():
default_user_config = {
"URL": "",
"LOG_LEVEL": "INFO"
"LOG_LEVEL": "INFO",
"EXTRAS" : "NOTTODAY"
}
with open(user_config_path, "w") as f:
json.dump(default_user_config, f, indent=4)
logging.debug(f"Created user config at {user_config_path}")
with open(user_config_path, "r") as f:
return json.load(f)
try:
with open(user_config_path, "r") as f:
return json.load(f)
except json.JSONDecodeError as e:
logging.error(f"❌ Failed to parse user_config.json: {e}")
return {}
def write_config_to_env(config: dict, env_path: Path):
for key, value in config.items():
@@ -101,7 +128,7 @@ def write_config_to_env(config: dict, env_path: Path):
except Exception as e:
logging.warning(f"Failed to write {key} to .env: {e}")
async def setup() -> Path:
async def setup():
base_dir = get_base_directory()
dirs = {
'config': base_dir / 'config',
@@ -158,4 +185,3 @@ async def setup() -> Path:
logging.debug(f"Service URL set to: {url}")
write_config_to_env(merged_config, env_path)
return working_dir