-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_manager.py
306 lines (259 loc) · 11.6 KB
/
data_manager.py
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
import json
import os
from datetime import datetime
from typing import List, Dict, Any, Optional
from logger import logger
class DataManager:
"""
Manages data operations for collections and items in the library system.
"""
@logger.log_execution_time
def __init__(self):
"""
Initialize the DataManager with empty collections and predefined categories.
"""
self.collections: List[Dict[str, Any]] = []
self.categories: List[str] = ["Book", "Movie", "Music", "Game"]
self.settings_file = "settings.json"
logger.info("DataManager initialized")
@logger.log_execution_time
def add_collection(self, name: str) -> bool:
"""
Add a new collection to the library.
Args:
name (str): The name of the collection to add.
Returns:
bool: True if the collection was added successfully, False otherwise.
Raises:
TypeError: If the name is not a string.
ValueError: If the name is empty or just whitespace.
"""
try:
if not isinstance(name, str):
raise TypeError("Collection name must be a string")
if not name.strip():
raise ValueError("Collection name cannot be empty or just whitespace")
if any(c['name'] == name for c in self.collections):
logger.warning(f"Collection '{name}' already exists")
return False
new_collection = {
"name": name,
"items": [],
"created_at": datetime.now().isoformat(),
"last_modified": datetime.now().isoformat()
}
self.collections.append(new_collection)
logger.info(f"Collection '{name}' added successfully")
return True
except (TypeError, ValueError) as e:
logger.error(f"Error adding collection: {str(e)}")
return False
except Exception as e:
logger.exception(f"Unexpected error adding collection: {str(e)}")
return False
@logger.log_execution_time
def add_item(self, collection_name: str, item: Dict[str, Any]) -> bool:
"""
Add a new item to a specified collection.
Args:
collection_name (str): The name of the collection to add the item to.
item (Dict[str, Any]): The item to add.
Returns:
bool: True if the item was added successfully, False otherwise.
Raises:
TypeError: If collection_name is not a string or item is not a dictionary.
ValueError: If the collection is not found or required item fields are missing.
"""
try:
if not isinstance(collection_name, str):
raise TypeError("Collection name must be a string")
if not isinstance(item, dict):
raise TypeError("Item must be a dictionary")
collection = next((c for c in self.collections if c["name"] == collection_name), None)
if not collection:
raise ValueError(f"Collection '{collection_name}' not found")
required_fields = ['name', 'category', 'price']
if not all(field in item for field in required_fields):
raise ValueError(f"Item must contain all required fields: {', '.join(required_fields)}")
if any(existing_item['name'] == item['name'] for existing_item in collection["items"]):
logger.warning(f"Item '{item['name']}' already exists in collection '{collection_name}'")
return False
collection["items"].append(item)
collection["last_modified"] = datetime.now().isoformat()
logger.info(f"Item '{item['name']}' added to collection '{collection_name}'")
return True
except (TypeError, ValueError) as e:
logger.error(f"Error adding item: {str(e)}")
return False
except Exception as e:
logger.exception(f"Unexpected error adding item: {str(e)}")
return False
@logger.log_execution_time
def get_collections(self) -> List[Dict[str, Any]]:
"""
Retrieve all collections.
Returns:
List[Dict[str, Any]]: A list of all collections.
"""
logger.info(f"Retrieved {len(self.collections)} collections")
return self.collections
@logger.log_execution_time
def get_items_in_collection(self, collection_name: str) -> List[Dict[str, Any]]:
"""
Retrieve all items in a specified collection.
Args:
collection_name (str): The name of the collection to retrieve items from.
Returns:
List[Dict[str, Any]]: A list of items in the specified collection.
Raises:
ValueError: If the collection is not found.
"""
try:
collection = next((c for c in self.collections if c["name"] == collection_name), None)
if not collection:
raise ValueError(f"Collection '{collection_name}' not found")
logger.info(f"Retrieved {len(collection['items'])} items from collection '{collection_name}'")
return collection["items"]
except ValueError as e:
logger.error(f"Error getting items from collection: {str(e)}")
return []
except Exception as e:
logger.exception(f"Unexpected error getting items from collection '{collection_name}': {str(e)}")
return []
@logger.log_execution_time
def save_to_file(self, filename: str) -> bool:
"""
Save the current library state to a file.
Args:
filename (str): The name of the file to save to.
Returns:
bool: True if the save operation was successful, False otherwise.
Raises:
IOError: If there's an error writing to the file.
"""
try:
with open(filename, 'w') as f:
json.dump(self.collections, f, indent=2)
logger.info(f"Successfully saved to {filename}")
return True
except IOError as e:
logger.error(f"IOError saving to file '{filename}': {str(e)}")
return False
except Exception as e:
logger.exception(f"Unexpected error saving to file '{filename}': {str(e)}")
return False
@logger.log_execution_time
def load_from_file(self, filename: str) -> bool:
"""
Load library state from a file.
Args:
filename (str): The name of the file to load from.
Returns:
bool: True if the load operation was successful, False otherwise.
Raises:
FileNotFoundError: If the specified file does not exist.
json.JSONDecodeError: If there's an error decoding the JSON from the file.
ValueError: If the loaded data is not in the expected format.
"""
try:
if not os.path.exists(filename):
raise FileNotFoundError(f"File '{filename}' does not exist")
with open(filename, 'r') as f:
loaded_data = json.load(f)
if not isinstance(loaded_data, list):
raise ValueError("Loaded data is not a list")
if not all(isinstance(item, dict) and self._validate_collection(item) for item in loaded_data):
raise ValueError("Not all items in loaded data are valid collections")
self.collections = loaded_data
logger.info(f"Successfully loaded {len(self.collections)} collections from {filename}")
return True
except FileNotFoundError as e:
logger.error(f"File not found: {str(e)}")
return False
except json.JSONDecodeError as e:
logger.error(f"Error decoding JSON from file '{filename}': {str(e)}")
return False
except ValueError as e:
logger.error(f"Invalid data format in file '{filename}': {str(e)}")
return False
except Exception as e:
logger.exception(f"Unexpected error loading from file '{filename}': {str(e)}")
return False
def _validate_collection(self, collection: Dict[str, Any]) -> bool:
"""
Validate the structure of a collection.
Args:
collection (Dict[str, Any]): The collection to validate.
Returns:
bool: True if the collection is valid, False otherwise.
"""
required_keys = ['name', 'items', 'created_at', 'last_modified']
return all(key in collection for key in required_keys) and isinstance(collection['items'], list)
@logger.log_execution_time
def get_categories(self) -> List[str]:
"""
Retrieve all predefined categories.
Returns:
List[str]: A list of all predefined categories.
"""
logger.info(f"Retrieved {len(self.categories)} categories")
return self.categories
@logger.log_execution_time
def search_items(self, search_term: str) -> List[Dict[str, Any]]:
"""
Search for items across all collections based on a search term.
Args:
search_term (str): The term to search for in item names and categories.
Returns:
List[Dict[str, Any]]: A list of items matching the search term.
"""
results = []
try:
search_term_lower = search_term.lower()
for collection in self.collections:
for item in collection['items']:
if search_term_lower in item.get('name', '').lower() or search_term_lower in item.get('category', '').lower():
results.append(item)
logger.info(f"Search for '{search_term}' returned {len(results)} results")
return results
except Exception as e:
logger.exception(f"Error during item search: {str(e)}")
return []
@logger.log_execution_time
def save_theme_preference(self, is_dark_mode: bool) -> bool:
"""
Save the user's theme preference to a settings file.
Args:
is_dark_mode (bool): True if dark mode is selected, False otherwise.
Returns:
bool: True if the preference was saved successfully, False otherwise.
"""
try:
settings = {"dark_mode": is_dark_mode}
with open(self.settings_file, "w") as f:
json.dump(settings, f)
logger.info(f"Theme preference saved: Dark mode = {is_dark_mode}")
return True
except Exception as e:
logger.error(f"Error saving theme preference: {str(e)}")
return False
@logger.log_execution_time
def load_theme_preference(self) -> bool:
"""
Load the user's theme preference from the settings file.
Returns:
bool: True if dark mode is preferred, False for light mode.
Defaults to True if no preference is found.
"""
try:
if os.path.exists(self.settings_file):
with open(self.settings_file, "r") as f:
settings = json.load(f)
is_dark_mode = settings.get("dark_mode", True)
else:
is_dark_mode = True # Default to dark mode if no settings file exists
logger.info(f"Theme preference loaded: Dark mode = {is_dark_mode}")
return is_dark_mode
except Exception as e:
logger.error(f"Error loading theme preference: {str(e)}")
return True # Default to dark mode in case of error