Repository navigation
Expand file tree
/
Copy pathconfig.py
More file actions
165 lines (137 loc) · 6.11 KB
/
Copy pathconfig.py
File metadata and controls
165 lines (137 loc) · 6.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# file: config.py
"""Configuration management for persistent user settings."""
from __future__ import annotations
import json
from pathlib import Path
from typing import Dict, Any, Optional
from dataclasses import dataclass, asdict, field
from report import OutputFormat
@dataclass
class AppConfig:
"""Application configuration settings"""
# Last used paths
last_source_folder: Optional[str] = None
last_save_folder: Optional[str] = None
last_save_file: Optional[str] = None # NEW: last selected output file
# Export options
output_format: str = OutputFormat.MARKDOWN.value
include_contents: bool = True
# Window settings
window_width: int = 720
window_height: int = 500
window_x: Optional[int] = None
window_y: Optional[int] = None
# Per-project content selection exclusions (paths stored relative to project root)
content_exclusions: Dict[str, list[str]] = field(default_factory=dict)
deep_options: Dict[str, bool] = field(default_factory=dict)
def to_dict(self) -> Dict[str, Any]:
"""Convert config to dictionary"""
return asdict(self)
@classmethod
def from_dict(cls, data: Dict[str, Any]) -> "AppConfig":
"""Create config from dictionary"""
# Filter out any unknown keys to handle version changes
known_fields = {f.name for f in cls.__dataclass_fields__.values()}
filtered_data = {k: v for k, v in data.items() if k in known_fields}
return cls(**filtered_data)
class ConfigManager:
"""Manages loading and saving application configuration"""
def __init__(self, config_name: str = "project_export_helper.json"):
self.config_path = self._get_config_dir() / config_name
self._config: Optional[AppConfig] = None
def _get_config_dir(self) -> Path:
"""Get the appropriate config directory for the platform"""
import os
import sys
if sys.platform == "win32":
# Windows: %APPDATA%
config_dir = Path(os.environ.get("APPDATA", "~")).expanduser()
elif sys.platform == "darwin":
# macOS: ~/Library/Application Support
config_dir = Path("~/Library/Application Support").expanduser()
else:
# Linux/Unix: ~/.config
config_dir = Path("~/.config").expanduser()
app_config_dir = config_dir / "ProjectExportHelper"
app_config_dir.mkdir(parents=True, exist_ok=True)
return app_config_dir
def load_config(self) -> AppConfig:
"""Load configuration from disk, or create default if not found"""
if self._config is not None:
return self._config
try:
if self.config_path.exists():
with open(self.config_path, 'r', encoding='utf-8') as f:
data = json.load(f)
self._config = AppConfig.from_dict(data)
else:
self._config = AppConfig()
except (json.JSONDecodeError, KeyError, TypeError, OSError):
# If config is corrupted or unreadable, use defaults
self._config = AppConfig()
return self._config
def save_config(self, config: AppConfig) -> None:
"""Save configuration to disk"""
self._config = config
try:
# Ensure directory exists
self.config_path.parent.mkdir(parents=True, exist_ok=True)
with open(self.config_path, 'w', encoding='utf-8') as f:
json.dump(config.to_dict(), f, indent=2, ensure_ascii=False)
except OSError:
# Silently fail if we can't save - don't crash the app
pass
def update_source_folder(self, path: str) -> None:
"""Update the last used source folder"""
config = self.load_config()
config.last_source_folder = path
self.save_config(config)
def update_save_folder(self, path: str) -> None:
"""Update the last used save folder and file"""
config = self.load_config()
config.last_save_folder = str(Path(path).parent)
config.last_save_file = str(path)
self.save_config(config)
def update_export_options(self, format_value: str, include_contents: bool) -> None:
"""Update export format and content inclusion setting"""
config = self.load_config()
config.output_format = format_value
config.include_contents = include_contents
self.save_config(config)
def get_content_exclusions(self, root_path: str) -> list[str]:
"""Get stored excluded file paths for a given project root."""
config = self.load_config()
return list(config.content_exclusions.get(root_path, []))
def update_content_exclusions(self, root_path: str, exclusions: list[str]) -> None:
"""Persist excluded file paths for a project root."""
config = self.load_config()
if exclusions:
config.content_exclusions[root_path] = exclusions
else:
config.content_exclusions.pop(root_path, None)
self.save_config(config)
def get_deep_options(self) -> Dict[str, bool]:
"""Return stored deep analysis option overrides."""
config = self.load_config()
return dict(config.deep_options)
def update_deep_options(self, options: Dict[str, bool]) -> None:
"""Persist deep analysis option overrides."""
config = self.load_config()
config.deep_options.update(options)
self.save_config(config)
def update_window_geometry(self, width: int, height: int, x: int, y: int) -> None:
"""Update window size and position"""
config = self.load_config()
config.window_width = width
config.window_height = height
config.window_x = x
config.window_y = y
self.save_config(config)
# Global config manager instance
_config_manager: Optional[ConfigManager] = None
def get_config_manager() -> ConfigManager:
"""Get the global config manager instance"""
global _config_manager
if _config_manager is None:
_config_manager = ConfigManager()
return _config_manager