Corrections
This commit is contained in:
@@ -2,12 +2,11 @@ import json
|
||||
import uuid
|
||||
from datetime import datetime
|
||||
from pathlib import Path
|
||||
from typing import Optional, List, Any
|
||||
from typing import Optional, List
|
||||
|
||||
# iottb modules
|
||||
from iottb.definitions import ReturnCodes, DEVICE_METADATA_FILE
|
||||
from iottb.logger import logger
|
||||
|
||||
# 3rd party libs
|
||||
|
||||
IMMUTABLE_FIELDS = {"device_name", "device_short_name", "device_id", "date_created"}
|
||||
@@ -17,8 +16,8 @@ class DeviceMetadata:
|
||||
# Required fields
|
||||
device_name: str
|
||||
device_short_name: str
|
||||
device_id: str = lambda: str(uuid.uuid4())
|
||||
date_created: str = lambda: datetime.now().strftime('%d-%m-%YT%H:%M:%S').lower()
|
||||
device_id: str
|
||||
date_created: str
|
||||
|
||||
device_root_path: Path
|
||||
# Optional Fields
|
||||
@@ -30,15 +29,18 @@ class DeviceMetadata:
|
||||
|
||||
capture_files: Optional[List[str]] = []
|
||||
|
||||
def __init__(self, device_name: str, device_root_dir: Path):
|
||||
def __init__(self, device_name: str, device_root_path: Path):
|
||||
self.device_name = device_name
|
||||
self.device_short_name = device_name.lower().replace(" ", "_")
|
||||
# assert dir_contains_device_metadata(device_root_dir), \
|
||||
# f"Directory {device_root_dir} is missing a {DEVICE_METADATA_FILE} file"
|
||||
self.device_root_dir = device_root_dir
|
||||
self.device_id = str(uuid.uuid4())
|
||||
self.date_created = datetime.now().strftime('%d-%m-%YT%H:%M:%S').lower()
|
||||
self.device_root_path = device_root_path
|
||||
if not self.device_root_path or not self.device_root_path.is_dir():
|
||||
logger.error(f"Invalid device root path: {device_root_path}")
|
||||
raise ValueError(f"Invalid device root path: {device_root_path}")
|
||||
logger.debug(f"Device name: {device_name}")
|
||||
logger.debug(f"Device short_name: {self.device_short_name}")
|
||||
logger.debug(f"Device root dir: {device_root_dir}")
|
||||
logger.debug(f"Device root dir: {device_root_path}")
|
||||
logger.info(f"Initialized DeviceMetadata model: {device_name}")
|
||||
|
||||
@classmethod
|
||||
@@ -82,7 +84,7 @@ class DeviceMetadata:
|
||||
"device_type": False,
|
||||
"device_serial_number": False,
|
||||
"device_firmware_version": False,
|
||||
"date_updated": True,
|
||||
"date_updated": False,
|
||||
"capture_files": False,
|
||||
}
|
||||
|
||||
@@ -90,8 +92,9 @@ class DeviceMetadata:
|
||||
value = getattr(self, field, None)
|
||||
if value not in [None, ""] or is_mandatory:
|
||||
if value in [None, ""] and is_mandatory:
|
||||
logger.debug(f"Mandatory field {field}: {value}")
|
||||
raise ValueError(f"Field {field} is required and cannot be empty.")
|
||||
data[field] = str(value) if not isinstance(value, Path) else value
|
||||
data[field] = str(value) if not isinstance(value, str) else value
|
||||
logger.debug(f"Device metadata: {data}")
|
||||
return json.dumps(data, indent=indent)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user