Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 35 additions & 18 deletions cortex/config_manager.py
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import os
import re
import subprocess
import threading
from datetime import datetime
from pathlib import Path
from typing import Any, ClassVar
Expand Down Expand Up @@ -43,17 +44,24 @@

def __init__(self, sandbox_executor=None):
"""
Initialize ConfigManager.

Args:
sandbox_executor: Optional SandboxExecutor instance for safe command execution

Create a ConfigManager and prepare its runtime environment.

Check failure on line 48 in cortex/config_manager.py

View workflow job for this annotation

GitHub Actions / Lint

Ruff (W293)

cortex/config_manager.py:48:1: W293 Blank line contains whitespace
Parameters:
sandbox_executor (optional): Executor used to run system commands inside a sandbox; if None, commands run directly.

Check failure on line 51 in cortex/config_manager.py

View workflow job for this annotation

GitHub Actions / Lint

Ruff (W293)

cortex/config_manager.py:51:1: W293 Blank line contains whitespace
Notes:
- Initializes paths `~/.cortex` and `~/.cortex/preferences.yaml`.
- Creates the `~/.cortex` directory with mode 0700 if it does not exist.
- Enforces ownership and 0700 permissions on the directory.
- Creates an instance-level lock to serialize preferences file I/O.

Check failure on line 57 in cortex/config_manager.py

View workflow job for this annotation

GitHub Actions / Lint

Ruff (W293)

cortex/config_manager.py:57:1: W293 Blank line contains whitespace
Raises:
PermissionError: If directory ownership or permissions cannot be secured
PermissionError: If ownership or permissions for the cortex directory cannot be secured.
"""
self.sandbox_executor = sandbox_executor
self.cortex_dir = Path.home() / ".cortex"
self.preferences_file = self.cortex_dir / "preferences.yaml"
self._file_lock = threading.Lock() # Protect file I/O operations

# Ensure .cortex directory exists with secure permissions
self.cortex_dir.mkdir(mode=0o700, exist_ok=True)
Expand Down Expand Up @@ -273,30 +281,39 @@

def _load_preferences(self) -> dict[str, Any]:
"""
Load user preferences from ~/.cortex/preferences.yaml.

Load preferences from the user's preferences YAML file and return them as a dictionary.

Check failure on line 285 in cortex/config_manager.py

View workflow job for this annotation

GitHub Actions / Lint

Ruff (W293)

cortex/config_manager.py:285:1: W293 Blank line contains whitespace
Reads the configured preferences file while holding the instance file lock. If the file does not exist, is empty, malformed, or cannot be read, this returns an empty dict.

Check failure on line 287 in cortex/config_manager.py

View workflow job for this annotation

GitHub Actions / Lint

Ruff (W293)

cortex/config_manager.py:287:1: W293 Blank line contains whitespace
Returns:
Dictionary of preferences
dict: Preferences mapping loaded from YAML, or an empty dict if no valid preferences could be loaded.
"""
if self.preferences_file.exists():
try:
with open(self.preferences_file) as f:
return yaml.safe_load(f) or {}
with self._file_lock:
with open(self.preferences_file) as f:
return yaml.safe_load(f) or {}
except Exception:
pass

return {}

def _save_preferences(self, preferences: dict[str, Any]) -> None:
"""
Save user preferences to ~/.cortex/preferences.yaml.

Args:
preferences: Dictionary of preferences to save
Save the provided preferences dictionary to the user's preferences file (~/.cortex/preferences.yaml).

Check failure on line 304 in cortex/config_manager.py

View workflow job for this annotation

GitHub Actions / Lint

Ruff (W293)

cortex/config_manager.py:304:1: W293 Blank line contains whitespace
Writes the preferences as YAML while acquiring the instance file lock to serialize concurrent access.

Check failure on line 306 in cortex/config_manager.py

View workflow job for this annotation

GitHub Actions / Lint

Ruff (W293)

cortex/config_manager.py:306:1: W293 Blank line contains whitespace
Parameters:
preferences (dict[str, Any]): Preferences to persist.

Check failure on line 309 in cortex/config_manager.py

View workflow job for this annotation

GitHub Actions / Lint

Ruff (W293)

cortex/config_manager.py:309:1: W293 Blank line contains whitespace
Raises:
RuntimeError: If writing the preferences file fails.
"""
try:
with open(self.preferences_file, "w") as f:
yaml.safe_dump(preferences, f, default_flow_style=False)
with self._file_lock:
with open(self.preferences_file, "w") as f:
yaml.safe_dump(preferences, f, default_flow_style=False)
except Exception as e:
raise RuntimeError(f"Failed to save preferences: {e}")

Expand Down Expand Up @@ -1064,4 +1081,4 @@


if __name__ == "__main__":
main()
main()

Check failure on line 1084 in cortex/config_manager.py

View workflow job for this annotation

GitHub Actions / Lint

Ruff (W292)

cortex/config_manager.py:1084:11: W292 No newline at end of file
Loading
Loading