-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfig.py
More file actions
73 lines (60 loc) · 2.38 KB
/
Config.py
File metadata and controls
73 lines (60 loc) · 2.38 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
import json
import os
from typing import Any, Dict, List
class Config:
"""Configuration manager for ImageSorter application."""
DEFAULT_CONFIG = {
"last_folder": "",
"recursive": False,
"move_other": False,
"custom_extensions": [],
"window_geometry": "480x340"
}
def __init__(self, config_file: str = "config.json") -> None:
"""
Initialize configuration manager.
Args:
config_file: Path to configuration file
"""
self.config_file = config_file
self.config: Dict[str, Any] = self.load()
def load(self) -> Dict[str, Any]:
"""Load configuration from file or create default."""
if os.path.exists(self.config_file):
try:
with open(self.config_file, 'r', encoding='utf-8') as f:
config = json.load(f)
# Merge with defaults to ensure all keys exist
return {**self.DEFAULT_CONFIG, **config}
except (json.JSONDecodeError, IOError) as e:
print(f"Warning: Could not load config file: {e}")
return self.DEFAULT_CONFIG.copy()
return self.DEFAULT_CONFIG.copy()
def save(self) -> None:
"""Save configuration to file."""
try:
with open(self.config_file, 'w', encoding='utf-8') as f:
json.dump(self.config, f, indent=2)
except IOError as e:
print(f"Warning: Could not save config file: {e}")
def get(self, key: str, default: Any = None) -> Any:
"""Get configuration value."""
return self.config.get(key, default)
def set(self, key: str, value: Any) -> None:
"""Set configuration value and save."""
self.config[key] = value
self.save()
def get_custom_extensions(self) -> List[str]:
"""Get list of custom file extensions."""
return self.config.get("custom_extensions", [])
def add_custom_extension(self, ext: str) -> None:
"""Add a custom file extension."""
ext = ext.lower()
if not ext.startswith('.'):
ext = '.' + ext
custom_exts = self.get_custom_extensions()
if ext not in custom_exts:
custom_exts.append(ext)
self.set("custom_extensions", custom_exts)
# Global config instance
config = Config()