Refactor various things to be more seperate
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
@@ -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))
|
||||
|
||||
Reference in New Issue
Block a user