3Implements the backend handler that interacts with the key on disk.
5\author Bertram Richter, Xiaoli Song
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.
22 def __init__(self, activated_path, deactivated_path, trash_path):
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.
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.
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.
52 if status ==
'activated':
54 elif status ==
'deactivated':
56 elif status ==
'trash':
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):
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:
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.
86 if os.path.exists(current_path):
87 shutil.move(current_path, new_path)
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.
100 if os.path.exists(current_path):
101 shutil.move(current_path, new_path)
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.
122 trash_target = os.path.join(self.
trash_path, key_file)
124 if os.path.exists(trash_target):
125 shutil.rmtree(trash_target)
127 shutil.move(source_path, trash_target)
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.
136 trash_path = os.path.join(self.
trash_path, key_file)
139 if not os.path.exists(trash_path):
142 if os.path.exists(restore_path):
143 shutil.rmtree(restore_path)
145 shutil.move(trash_path, restore_path)
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.
154 trash_path = os.path.join(self.
trash_path, key_file)
156 if os.path.exists(trash_path)
and os.path.isdir(trash_path):
157 shutil.rmtree(trash_path)
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.
168 if status ==
'activated':
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):
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:
183 except json.JSONDecodeError:
Handle keyfile operations related to activation and deactivation, as well as reading keyfiles from di...
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...
check_directories(self)
Check if both the activated and deactivated directories exist.
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.
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.
__init__(self, activated_path, deactivated_path, trash_path)
Initialize the KeyHandler with the paths for activated and deactivated directories.
permanently_delete_key(self, key_file)
Permanently delete a keyfile folder from the trash directory.