Implement custom to_json function for DeviceMetadata
This commit is contained in:
parent
cb1ad33cae
commit
27ae736f11
@ -49,7 +49,7 @@ class DeviceMetadata:
|
||||
|
||||
with device_meta_filename.open('r') as file:
|
||||
metadata_json = json.load(file)
|
||||
metadata_model_obj = cls.model_validate_json(metadata_json)
|
||||
metadata_model_obj = cls.from_json(metadata_json)
|
||||
return metadata_model_obj
|
||||
|
||||
def save_to_json(self, file_path: Path):
|
||||
@ -57,18 +57,42 @@ class DeviceMetadata:
|
||||
if file_path.is_file():
|
||||
print(f"File {file_path} already exists, update instead.")
|
||||
return ReturnCodes.FILE_ALREADY_EXISTS
|
||||
metadata = self.model_dump_json(indent=2)
|
||||
metadata = self.to_json(indent=2)
|
||||
with file_path.open('w') as file:
|
||||
json.dump(metadata, file)
|
||||
return ReturnCodes.SUCCESS
|
||||
|
||||
|
||||
@classmethod
|
||||
def model_validate_json(cls, metadata_json):
|
||||
def from_json(cls, metadata_json):
|
||||
pass
|
||||
|
||||
def model_dump_json(self, indent):
|
||||
pass
|
||||
def to_json(self, indent=2):
|
||||
# TODO: atm almost exact copy as in CaptureMetadata
|
||||
data = {}
|
||||
|
||||
fields = {
|
||||
"device_name": True,
|
||||
"device_short_name": True,
|
||||
"device_id": True,
|
||||
"date_created": True,
|
||||
"device_root_path": True,
|
||||
"aliases": False,
|
||||
"device_type": False,
|
||||
"device_serial_number": False,
|
||||
"device_firmware_version": False,
|
||||
"date_updated": True,
|
||||
"capture_files": False,
|
||||
}
|
||||
|
||||
for field, is_mandatory in fields.items():
|
||||
value = getattr(self, field, None)
|
||||
if value not in [None, ""] or is_mandatory:
|
||||
if value in [None, ""] and is_mandatory:
|
||||
raise ValueError(f"Field {field} is required and cannot be empty.")
|
||||
data[field] = str(value) if not isinstance(value, Path) else value
|
||||
logger.debug(f"Device metadata: {data}")
|
||||
return json.dumps(data, indent=indent)
|
||||
|
||||
|
||||
def dir_contains_device_metadata(dir_path: Path):
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user