-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathconfig_editor.py
More file actions
188 lines (155 loc) · 5.09 KB
/
config_editor.py
File metadata and controls
188 lines (155 loc) · 5.09 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#!/usr/bin/env python3
"""
Menu-based JSON configuration editor for Py-Sort.
Allows users to view and modify sorting categories and extensions
in the configuration JSON file. Changes can be saved back to disk
or discarded.
"""
import json
import os
import sys
CONFIG_FILE = os.path.join(os.path.dirname(__file__), "config.json")
def load_config():
"""
Load configuration from the JSON file.
Returns:
dict: Dictionary containing categories as keys and lists of
extensions as values. Returns an empty dict if the file
doesn't exist or contains invalid JSON.
"""
if not os.path.exists(CONFIG_FILE):
print(f"Config file not found: {CONFIG_FILE}")
return {}
try:
with open(CONFIG_FILE, "r", encoding="utf-8") as f:
return json.load(f)
except json.JSONDecodeError:
print("Error: Invalid JSON format. Starting with empty config.")
return {}
def save_config(config):
"""
Save the configuration dictionary back to the JSON file.
Args:
config (dict): Configuration dictionary to save.
"""
try:
with open(CONFIG_FILE, "w", encoding="utf-8") as f:
json.dump(config, f, indent=2)
print(f"✅ Configuration saved to {CONFIG_FILE}")
except Exception as e:
print(f"Error saving configuration: {e}")
def view_categories(config):
"""
Display all current categories and their associated file extensions.
Args:
config (dict): Configuration dictionary.
"""
if not config:
print("No categories defined.")
return
for category, extensions in config.items():
print(f"\n{category}:")
print(", ".join(extensions) if extensions else " (no extensions)")
def add_category(config):
"""
Prompt the user to add a new category to the configuration.
Args:
config (dict): Configuration dictionary.
"""
name = input("Enter new category name: ").strip()
if not name:
print("Category name cannot be empty.")
return
if name in config:
print("Category already exists.")
return
config[name] = []
print(f"Added category '{name}'.")
def remove_category(config):
"""
Prompt the user to remove an existing category from the configuration.
Args:
config (dict): Configuration dictionary.
"""
name = input("Enter category name to remove: ").strip()
if name not in config:
print("Category not found.")
return
del config[name]
print(f"Removed category '{name}'.")
def add_extension(config):
"""
Prompt the user to add a new extension to a specific category.
Args:
config (dict): Configuration dictionary.
"""
category = input("Enter category name: ").strip()
if category not in config:
print("Category not found.")
return
ext = input("Enter new extension (include the dot, e.g. .xyz): ").strip().lower()
if not ext.startswith("."):
print("Invalid format. Extension must start with '.'")
return
if ext in config[category]:
print("Extension already exists in this category.")
return
config[category].append(ext)
print(f"Added '{ext}' to '{category}'.")
def remove_extension(config):
"""
Prompt the user to remove an extension from a specific category.
Args:
config (dict): Configuration dictionary.
"""
category = input("Enter category name: ").strip()
if category not in config:
print("Category not found.")
return
ext = input("Enter extension to remove (include the dot): ").strip().lower()
if ext not in config[category]:
print("Extension not found in this category.")
return
config[category].remove(ext)
print(f"Removed '{ext}' from '{category}'.")
def menu():
"""
Main interactive menu loop.
Displays options to view, add, remove categories or extensions,
and allows saving or discarding changes.
"""
config = load_config()
while True:
print("\n==== CONFIG EDITOR ====")
print("1. View current categories")
print("2. Add a new category")
print("3. Remove a category")
print("4. Add extension to category")
print("5. Remove extension from category")
print("6. Save and exit")
print("7. Exit without saving")
choice = input("\nChoose an option: ").strip()
if choice == "1":
view_categories(config)
elif choice == "2":
add_category(config)
elif choice == "3":
remove_category(config)
elif choice == "4":
add_extension(config)
elif choice == "5":
remove_extension(config)
elif choice == "6":
save_config(config)
break
elif choice == "7":
print("Exiting without saving changes.")
break
else:
print("Invalid choice. Please enter a number between 1 and 7.")
if __name__ == "__main__":
try:
menu()
except KeyboardInterrupt:
print("\nExited by user.")
sys.exit(0)