Introduce complete refactoring.
This commit is contained in:
77
archive/iottb/subcommands/add_device.py
Normal file
77
archive/iottb/subcommands/add_device.py
Normal file
@@ -0,0 +1,77 @@
|
||||
import logging
|
||||
import os
|
||||
import pathlib
|
||||
|
||||
from iottb import definitions
|
||||
from iottb.definitions import DEVICE_METADATA_FILE, ReturnCodes
|
||||
from iottb.models.device_metadata_model import DeviceMetadata
|
||||
|
||||
# logger.setLevel(logging.INFO) # Since module currently passes all tests
|
||||
logger = logging.getLogger('iottbLogger.add_device')
|
||||
logger.setLevel(logging.INFO)
|
||||
|
||||
def setup_init_device_root_parser(subparsers):
|
||||
#assert os.environ['IOTTB_HOME'] is not None, "IOTTB_HOME environment variable is not set"
|
||||
parser = subparsers.add_parser('add-device', aliases=['add-device-root', 'add'],
|
||||
help='Initialize a folder for a device.')
|
||||
parser.add_argument('--root_dir', type=pathlib.Path,
|
||||
default=definitions.IOTTB_HOME_ABS) # TODO: Refactor code to not use this or handle iottb here
|
||||
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):
|
||||
# TODO: This whole function should be refactored into using the fact that IOTTB_HOME is set, and the dir exists
|
||||
logger.info(f'Add device handler called with args {args}')
|
||||
|
||||
if args.guided:
|
||||
logger.debug('begin guided setup')
|
||||
metadata = guided_setup(args.root_dir) # TODO refactor to use IOTTB_HOME
|
||||
logger.debug('guided setup complete')
|
||||
else:
|
||||
logger.debug('Setup through passed args: setup')
|
||||
if not args.name:
|
||||
logger.error('No device name specified with unguided setup.')
|
||||
return ReturnCodes.ERROR
|
||||
metadata = DeviceMetadata(args.name, args.root_dir)
|
||||
|
||||
file_path = args.root_dir / DEVICE_METADATA_FILE # TODO IOTTB_HOME REFACTOR
|
||||
if file_path.exists():
|
||||
print('Directory already contains a metadata file. Aborting.')
|
||||
return ReturnCodes.ABORTED
|
||||
serialized_metadata = metadata.to_json()
|
||||
response = input(f'Confirm device metadata: {serialized_metadata} [y/N]')
|
||||
logger.debug(f'response: {response}')
|
||||
if response not in definitions.AFFIRMATIVE_USER_RESPONSE:
|
||||
print('Adding device aborted by user.')
|
||||
return ReturnCodes.ABORTED
|
||||
|
||||
logger.debug(f'Device metadata file {file_path}')
|
||||
if metadata.save_to_json(file_path) == ReturnCodes.FILE_ALREADY_EXISTS:
|
||||
logger.error('File exists after checking, which should not happen.')
|
||||
return ReturnCodes.ABORTED
|
||||
|
||||
print('Device metadata successfully created.')
|
||||
return ReturnCodes.SUCCESS
|
||||
|
||||
|
||||
def configure_metadata():
|
||||
pass
|
||||
|
||||
|
||||
def guided_setup(device_root) -> DeviceMetadata:
|
||||
logger.info('Guided setup')
|
||||
response = 'N'
|
||||
device_name = ''
|
||||
while response.upper() == 'N':
|
||||
device_name = input('Please enter name of device: ')
|
||||
response = input(f'Confirm device name: {device_name} [y/N] ')
|
||||
if device_name == '' or device_name is None:
|
||||
print('Name cannot be empty')
|
||||
logger.warning('Name cannot be empty')
|
||||
logger.debug(f'Response is {response}')
|
||||
logger.debug(f'Device name is {device_name}')
|
||||
|
||||
return DeviceMetadata(device_name, device_root)
|
||||
Reference in New Issue
Block a user