# Conflicts: # .idea/workspace.xml # code/iottb/logger.py # code/iottb/models/capture_metadata_model.py # code/iottb/models/device_metadata_model.py # code/iottb/subcommands/add_device.py
53 lines
1.9 KiB
Python
53 lines
1.9 KiB
Python
import pathlib
|
|
|
|
from iottb import definitions
|
|
from iottb.definitions import DEVICE_METADATA_FILE, ReturnCodes
|
|
from iottb.models.device_metadata_model import DeviceMetadata
|
|
from iottb.utils.device_metadata_utils import *
|
|
|
|
|
|
def setup_init_device_root_parser(subparsers):
|
|
parser = subparsers.add_parser("add-device", aliases=["add-device-root", "add"])
|
|
parser.add_argument("--root_dir", type=pathlib.Path, default=pathlib.Path.cwd())
|
|
group = parser.add_mutually_exclusive_group()
|
|
group.add_argument("--guided", action="store_true", help="Guided setup", default=False)
|
|
group.add_argument("--name", action="store", type=str, help="name of device")
|
|
parser.set_defaults(func=handle_add)
|
|
|
|
|
|
def handle_add(args):
|
|
|
|
if args.guided:
|
|
metadata = guided_setup(args.root_dir)
|
|
else:
|
|
device_name = args.name
|
|
args.root_dir.mkdir(parents=True, exist_ok=True)
|
|
metadata = DeviceMetadata(device_name, args.root_dir)
|
|
|
|
file_path = args.root_dir / DEVICE_METADATA_FILE
|
|
response = input(f"Confirm device metadata: {metadata.model_dump()} [y/N]")
|
|
if response.lower() not in definitions.AFFIRMATIVE_USER_RESPONSE.add(""):
|
|
configure_metadata()
|
|
assert False, "TODO implement dynamic setup"
|
|
if metadata.save_to_json(file_path) == ReturnCodes.FILE_ALREADY_EXISTS:
|
|
print("Directory already contains a device metadata file. Aborting operation.")
|
|
return ReturnCodes.ABORTED
|
|
assert Path(file_path).exists(), f"{file_path} does not exist"
|
|
return ReturnCodes.SUCCESS
|
|
|
|
|
|
def configure_metadata():
|
|
pass
|
|
|
|
|
|
def guided_setup(device_root) -> DeviceMetadata:
|
|
response = "N"
|
|
device_name = ""
|
|
while response.upper() == "N":
|
|
device_name = input("Please enter name of device: ")
|
|
if device_name == "" or device_name is None:
|
|
print("Name cannot be empty")
|
|
|
|
return DeviceMetadata(device_name, device_root)
|
|
|