diff --git a/iottb/commands/add_device.py b/iottb/commands/add_device.py index 35624be..d5c9ff9 100644 --- a/iottb/commands/add_device.py +++ b/iottb/commands/add_device.py @@ -6,7 +6,8 @@ import logging import re from iottb import definitions -from iottb.contexts import DeviceMetadata, IottbConfig +from iottb.models.device_metadata import DeviceMetadata +from iottb.models.iottb_config import IottbConfig from iottb.definitions import CFG_FILE_PATH logger = logging.getLogger(__name__) diff --git a/iottb/commands/initialize_testbed.py b/iottb/commands/initialize_testbed.py index 56493a6..d7373ea 100644 --- a/iottb/commands/initialize_testbed.py +++ b/iottb/commands/initialize_testbed.py @@ -3,7 +3,7 @@ from pathlib import Path import logging from logging.handlers import RotatingFileHandler import sys -from iottb.contexts import IottbConfig +from iottb.models.iottb_config import IottbConfig from iottb.definitions import DB_NAME logger = logging.getLogger(__name__) diff --git a/iottb/commands/sniff.py b/iottb/commands/sniff.py new file mode 100644 index 0000000..27edbef --- /dev/null +++ b/iottb/commands/sniff.py @@ -0,0 +1,8 @@ +import click +import logging +from iottb import definitions + + + + + diff --git a/iottb/main.py b/iottb/main.py index 4c0efdc..a877752 100644 --- a/iottb/main.py +++ b/iottb/main.py @@ -8,7 +8,7 @@ import logging ################################################# from iottb.utils.logger_config import setup_logging from iottb import definitions -from iottb.contexts import IottbConfig +from iottb.models.iottb_config import IottbConfig from iottb.commands.initialize_testbed import init_db from iottb.commands.add_device import add_device diff --git a/iottb/models/__init__.py b/iottb/models/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/iottb/models/database.py b/iottb/models/database.py new file mode 100644 index 0000000..63105f2 --- /dev/null +++ b/iottb/models/database.py @@ -0,0 +1,6 @@ +class Database: + + def __init__(self, name, path): + self.name = name + self.path = path + self.device_list = [] # List of the canonical names of devices registered in this database diff --git a/iottb/models/device_metadata.py b/iottb/models/device_metadata.py new file mode 100644 index 0000000..505677a --- /dev/null +++ b/iottb/models/device_metadata.py @@ -0,0 +1,44 @@ +import logging +import uuid +from datetime import datetime +import logging +import click + +from iottb.utils.string_processing import make_canonical_name + +logger = logging.getLogger(__name__) + + +class DeviceMetadata: + def __init__(self, device_name, description="", model="", manufacturer="", firmware_version="", device_type="", + supported_interfaces="", companion_applications="", save_to_file=None): + self.device_id = str(uuid.uuid4()) + self.device_name = device_name + cn, aliases = make_canonical_name(device_name) + logger.debug(f'cn, aliases = {cn}, {str(aliases)}') + self.aliases = aliases + self.canonical_name = cn + self.date_added = datetime.now().isoformat() + self.description = description + self.model = model + self.manufacturer = manufacturer + self.current_firmware_version = firmware_version + self.device_type = device_type + self.supported_interfaces = supported_interfaces + self.companion_applications = companion_applications + self.last_metadata_update = datetime.now().isoformat() + if save_to_file is not None: + click.echo('TODO: Implement saving config to file after creation!') + + def add_alias(self, alias: str = ""): + if alias == "": + return + self.aliases.append(alias) + + def get_canonical_name(self): + return self.canonical_name + + def print_attributes(self): + print(f'Printing attribute value pairs in {__name__}') + for attr, value in self.__dict__.items(): + print(f'{attr}: {value}') diff --git a/iottb/contexts.py b/iottb/models/iottb_config.py similarity index 61% rename from iottb/contexts.py rename to iottb/models/iottb_config.py index 6c50723..1d4418d 100644 --- a/iottb/contexts.py +++ b/iottb/models/iottb_config.py @@ -1,20 +1,12 @@ import json from pathlib import Path -import logging -import json -from pathlib import Path -from datetime import datetime -import uuid -import unicodedata -import re - -import click from iottb import definitions +import logging logger = logging.getLogger(__name__) -DB_NAME = 'iottb.db' +DB_NAME = definitions.DB_NAME class IottbConfig: @@ -130,77 +122,3 @@ class IottbConfig: def get_full_default_path(self): return Path(self.default_db_location) / self.default_database - - -class Database: - - def __init__(self, name, path): - self.name = name - self.path = path - self.device_list = [] # List of the canonical names of devices registered in this database - - -class DeviceMetadata: - def __init__(self, device_name, description="", model="", manufacturer="", firmware_version="", device_type="", - supported_interfaces="", companion_applications="", save_to_file=None): - self.device_id = str(uuid.uuid4()) - self.device_name = device_name - cn, aliases = make_canonical_name(device_name) - logger.debug(f'cn, aliases = {cn}, {str(aliases)}') - self.aliases = aliases - self.canonical_name = cn - self.date_added = datetime.now().isoformat() - self.description = description - self.model = model - self.manufacturer = manufacturer - self.current_firmware_version = firmware_version - self.device_type = device_type - self.supported_interfaces = supported_interfaces - self.companion_applications = companion_applications - self.last_metadata_update = datetime.now().isoformat() - if save_to_file is not None: - click.echo('TODO: Implement saving config to file after creation!') - - def add_alias(self, alias: str = ""): - if alias == "": - return - self.aliases.append(alias) - - def get_canonical_name(self): - return self.canonical_name - - def print_attributes(self): - print(f'Printing attribute value pairs in {__name__}') - for attr, value in self.__dict__.items(): - print(f'{attr}: {value}') - - -def make_canonical_name(name): - """ - Normalize the device name to a canonical form: - - Replace the first two occurrences of spaces and transform characters with dashes. - - Remove any remaining spaces and non-ASCII characters. - - Convert to lowercase. - """ - aliases = [name] - logger.info(f'Normalizing name {name}') - - # We first normalize - chars_to_replace = definitions.REPLACEMENT_SET_CANONICAL_DEVICE_NAMES - pattern = re.compile('|'.join(re.escape(char) for char in chars_to_replace)) - norm_name = pattern.sub('-', name) - norm_name = re.sub(r'[^\x00-\x7F]+', '', norm_name) # removes non ascii chars - - aliases.append(norm_name) - # Lower case - norm_name = norm_name.lower() - aliases.append(norm_name) - - # canonical name is only first two parts of resulting string - parts = norm_name.split('-') - canonical_name = canonical_name = '-'.join(parts[:2]) - aliases.append(canonical_name) - - logger.debug(f'Canonical name: {canonical_name}') - logger.debug(f'Aliases: {aliases}') - return canonical_name, list(set(aliases)) diff --git a/iottb/utils/logger_config.py b/iottb/utils/logger_config.py index 8282b12..1ececee 100644 --- a/iottb/utils/logger_config.py +++ b/iottb/utils/logger_config.py @@ -4,7 +4,7 @@ from pathlib import Path import logging from logging.handlers import RotatingFileHandler import sys -from iottb.contexts import IottbConfig +from iottb.models.iottb_config import IottbConfig from iottb import definitions from iottb.definitions import MAX_VERBOSITY, CONSOLE_LOG_FORMATS, APP_NAME, LOGFILE_LOG_FORMAT diff --git a/iottb/utils/string_processing.py b/iottb/utils/string_processing.py index a18d095..321e842 100644 --- a/iottb/utils/string_processing.py +++ b/iottb/utils/string_processing.py @@ -1,6 +1,40 @@ import re from iottb import definitions +import logging + +logger = logging.getLogger(__name__) def normalize_string(s, chars_to_replace=None, replacement=None, allow_unicode=False): pass + + +def make_canonical_name(name): + """ + Normalize the device name to a canonical form: + - Replace the first two occurrences of spaces and transform characters with dashes. + - Remove any remaining spaces and non-ASCII characters. + - Convert to lowercase. + """ + aliases = [name] + logger.info(f'Normalizing name {name}') + + # We first normalize + chars_to_replace = definitions.REPLACEMENT_SET_CANONICAL_DEVICE_NAMES + pattern = re.compile('|'.join(re.escape(char) for char in chars_to_replace)) + norm_name = pattern.sub('-', name) + norm_name = re.sub(r'[^\x00-\x7F]+', '', norm_name) # removes non ascii chars + + aliases.append(norm_name) + # Lower case + norm_name = norm_name.lower() + aliases.append(norm_name) + + # canonical name is only first two parts of resulting string + parts = norm_name.split('-') + canonical_name = canonical_name = '-'.join(parts[:2]) + aliases.append(canonical_name) + + logger.debug(f'Canonical name: {canonical_name}') + logger.debug(f'Aliases: {aliases}') + return canonical_name, list(set(aliases)) diff --git a/tests/test_make_canonical_name.py b/tests/test_make_canonical_name.py index 5662a6a..eac541c 100644 --- a/tests/test_make_canonical_name.py +++ b/tests/test_make_canonical_name.py @@ -1,4 +1,4 @@ -from iottb.contexts import make_canonical_name +from iottb.utils.string_processing import make_canonical_name import pytest