fosKeyMan
Loading...
Searching...
No Matches
keyhandler.py
Go to the documentation of this file.
2r"""
3Implements the backend handler that interacts with the key on disk.
4
5\author Bertram Richter, Xiaoli Song
6\date 2025
7"""
8
9
10import json
11import os
12import shutil
13
14
16 r"""
17 Handle keyfile operations related to activation and deactivation, as well as reading keyfiles from directories.
18 The KeyHandler manages two directories: one for activated keyfiles and one for deactivated keyfiles.
19 It provides methods to check if the directories exist, read keyfiles, and move keyfiles between activated
20 and deactivated directories.
21 """
22 def __init__(self, activated_path, deactivated_path, trash_path):
23 r"""
24 Initialize the KeyHandler with the paths for activated and deactivated directories.
25 \param activated_path (str): The file path where activated keyfiles are stored.
26 \param deactivated_path (str): The file path where deactivated keyfiles are stored.
27 """
28 self.activated_path = activated_path
29 self.deactivated_path = deactivated_path
30 self.trash_path = trash_path
31
33 r"""
34 Check if both the activated and deactivated directories exist.
35 \return (bool): True if both directories exist, False if one or both do not exist.
36 """
37 if not os.path.exists(self.activated_path):
38 return False
39 if not os.path.exists(self.deactivated_path):
40 return False
41 if not os.path.exists(self.trash_path):
42 return False
43 return True
44
45 def read_keys(self, status):
46 r"""
47 Read and return a list of serial numbers from userProperties.json files within the folders in
48 the specified base directory.
49 \param status (str): 'activated' or 'deactivated'. Uses the activated_path or deactivated_path as the base directory.
50 \return (list): A list containing all sensorSerialNumber values found in the userProperties.json files.
51 """
52 if status == 'activated':
53 base_directory = self.activated_path
54 elif status == 'deactivated':
55 base_directory = self.deactivated_path
56 elif status == 'trash':
57 base_directory = self.trash_path
58 else:
59 return
60 serial_numbers = []
61 for folder_name in os.listdir(base_directory):
62 folder_path = os.path.join(base_directory, folder_name)
63 if os.path.isdir(folder_path):
64 json_file_path = os.path.join(folder_path, "userProperties.json")
65 if os.path.exists(json_file_path):
66 try:
67 with open(json_file_path, "r") as json_file:
68 data = json.load(json_file)
69 sensor_serial_number = data.get("sensorSerialNumber", None)
70 if sensor_serial_number:
71 serial_numbers.append(sensor_serial_number)
72 except json.JSONDecodeError:
73 pass
74 else:
75 pass
76 return serial_numbers
77
78 def activate_key(self, key_file):
79 r"""
80 Move a keyfile from the deactivated directory to the activated directory.
81 \param key_file (str): The name of the keyfile (without extension) to activate.
82 \return (bool): True if the keyfile was successfully moved, False if the file path does not exist.
83 """
84 current_path = os.path.join(self.deactivated_path, key_file)
85 new_path = os.path.join(self.activated_path, key_file)
86 if os.path.exists(current_path):
87 shutil.move(current_path, new_path)
88 return True
89 else:
90 return False
91
92 def deactivate_key(self, key_file):
93 r"""
94 Move a keyfile from the activated directory to the deactivated directory.
95 \param key_file (str): The name of the keyfile (without extension) to deactivate.
96 \return (bool): True if the keyfile was successfully moved, False if the file path does not exist.
97 """
98 current_path = os.path.join(self.activated_path, key_file)
99 new_path = os.path.join(self.deactivated_path, key_file)
100 if os.path.exists(current_path):
101 shutil.move(current_path, new_path)
102 return True
103 else:
104 return False
105
106 def delete_key(self, key_file):
107 r"""
108 Move a keyfile from either the activated or deactivated directory into the trash directory.
109 \param key_file (str): The folder name of the keyfile to delete.
110 \return (bool): True if deleted (moved to trash), False if not found.
111 """
112 source_path = None
113
114 if os.path.exists(os.path.join(self.activated_path, key_file)):
115 source_path = os.path.join(self.activated_path, key_file)
116 elif os.path.exists(os.path.join(self.deactivated_path, key_file)):
117 source_path = os.path.join(self.deactivated_path, key_file)
118
119 if not source_path:
120 return False
121
122 trash_target = os.path.join(self.trash_path, key_file)
123
124 if os.path.exists(trash_target):
125 shutil.rmtree(trash_target)
126
127 shutil.move(source_path, trash_target)
128 return True
129
130 def undo_delete_key(self, key_file):
131 r"""
132 Restore a keyfile from the trash directory to the deactivated directory.
133 \param key_file (str): The folder name of the keyfile to restore.
134 \return (bool): True if restored, False if not found in trash.
135 """
136 trash_path = os.path.join(self.trash_path, key_file)
137 restore_path = os.path.join(self.deactivated_path, key_file)
138
139 if not os.path.exists(trash_path):
140 return False
141
142 if os.path.exists(restore_path):
143 shutil.rmtree(restore_path)
144
145 shutil.move(trash_path, restore_path)
146 return True
147
148 def permanently_delete_key(self, key_file):
149 r"""
150 Permanently delete a keyfile folder from the trash directory.
151 \param key_file (str): The folder name of the keyfile to delete.
152 \return (bool): True if deleted, False if not found.
153 """
154 trash_path = os.path.join(self.trash_path, key_file)
155
156 if os.path.exists(trash_path) and os.path.isdir(trash_path):
157 shutil.rmtree(trash_path)
158 return True
159 return False
160
161 def key_folder_path(self, serial_number, status):
162 r"""
163 Find the folder path containing the given serial number by searching through the userProperties.json files.
164 \param serial_number (str): The serial number to locate.
165 \param status (str): 'activated' or 'deactivated' to determine the base directory.
166 \return (str): The full folder path containing the specified serial number, or None if not found.
167 """
168 if status == 'activated':
169 base_directory = self.activated_path
170 else:
171 base_directory = self.deactivated_path
172 for folder_name in os.listdir(base_directory):
173 folder_path = os.path.join(base_directory, folder_name)
174 if os.path.isdir(folder_path):
175 json_file_path = os.path.join(folder_path, "userProperties.json")
176 if os.path.exists(json_file_path):
177 try:
178 with open(json_file_path, "r") as json_file:
179 data = json.load(json_file)
180 sensor_serial_number = data.get("sensorSerialNumber", None)
181 if sensor_serial_number == serial_number:
182 return folder_path
183 except json.JSONDecodeError:
184 pass
185 return None
Handle keyfile operations related to activation and deactivation, as well as reading keyfiles from di...
Definition keyhandler.py:15
undo_delete_key(self, key_file)
Restore a keyfile from the trash directory to the deactivated directory.
read_keys(self, status)
Read and return a list of serial numbers from userProperties.json files within the folders in the spe...
Definition keyhandler.py:45
check_directories(self)
Check if both the activated and deactivated directories exist.
Definition keyhandler.py:32
key_folder_path(self, serial_number, status)
Find the folder path containing the given serial number by searching through the userProperties....
deactivate_key(self, key_file)
Move a keyfile from the activated directory to the deactivated directory.
Definition keyhandler.py:92
delete_key(self, key_file)
Move a keyfile from either the activated or deactivated directory into the trash directory.
activate_key(self, key_file)
Move a keyfile from the deactivated directory to the activated directory.
Definition keyhandler.py:78
__init__(self, activated_path, deactivated_path, trash_path)
Initialize the KeyHandler with the paths for activated and deactivated directories.
Definition keyhandler.py:22
permanently_delete_key(self, key_file)
Permanently delete a keyfile folder from the trash directory.