diff --git a/README.md b/README.md new file mode 100644 index 0000000..d7a21e2 --- /dev/null +++ b/README.md @@ -0,0 +1,9 @@ +# Iottb +## Basic Invocation + +## Configuration +### Env Vars +- IOTTB_CONF_HOME + +By setting this variable you control where the basic iottb application +configuration should be looked for \ No newline at end of file diff --git a/iottb/__init__.py b/iottb/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/iottb/main.py b/iottb/main.py new file mode 100644 index 0000000..7fd8e14 --- /dev/null +++ b/iottb/main.py @@ -0,0 +1,80 @@ +import subprocess + +import click +from pathlib import PurePath +import configparser +import logging +from logging.handlers import RotatingFileHandler +import sys + +APP_NAME = 'iottb' +MAX_VERBOSITY = 3 + + +def setup_logging(verbosity, debug): + # Formats + console_log_formats = { + 0: '%(levelname)s - %(message)s', + 1: '%(levelname)s - %(module)s - %(message)s', + 2: '%(levelname)s - %(asctime)s - %(module)s - %(funcName)s - %(lineno)d - %(message)s' + } + + file_log_formats = { + 0: '%(levelname)s - %(asctime)s - %(module)s - %(message)s', + 1: '%(levelname)s - %(asctime)s - %(module)s - %(funcName)s - %(message)s', + 2: '%(levelname)s - %(asctime)s - %(module)s - %(funcName)s - %(lineno)d - %(message)s' + } + + # set default log level, change later based on parameters + log_level = logging.ERROR + logger = logging.getLogger() + date_format = '%Y-%m-%d %H:%M:%S' + if verbosity > 0: + log_level = logging.WARNING + if verbosity > MAX_VERBOSITY: + verbosity = MAX_VERBOSITY + log_level = logging.INFO + assert verbosity <= MAX_VERBOSITY, f'Verbosity must be <= {MAX_VERBOSITY}' + console_handler = logging.StreamHandler(sys.stdout) + console_handler.setFormatter(logging.Formatter(console_log_formats[verbosity], datefmt=date_format)) + console_handler.setLevel(logging.DEBUG) # can keep at debug since it depends on global level? + logger.addHandler(console_handler) + + if debug: + log_level = logging.DEBUG + + # Logfile logs INFO+, no debugs though + file_handler = RotatingFileHandler(f'{APP_NAME}.log', maxBytes=10240, backupCount=5) + file_handler.setFormatter(logging.Formatter(file_log_formats[verbosity], datefmt=date_format)) + file_handler.setLevel(logging.INFO) + + # finnish setting up logging + logger.addHandler(file_handler) + logger.setLevel(log_level) + return logger + + +class Environment: + def __init__(self, cfg=None, db=None, device=None): + self.cfg = cfg + self.db = db + self.device = device + pass + + +def load_config(cfg_file_path): + cfg_file = PurePath(click.get_app_dir(APP_NAME)).joinpath('iottb.cfg') + + +@click.group() +@click.option('-v', '--verbosity', type=click.IntRange(0, MAX_VERBOSITY), default=0, + help='Verbosity level') +@click.option('-d', '--debug', is_flag=True, default=False, help='Debug mode') +@click.option('--cfg-file', type=click.Path(), default=PurePath(click.get_app_dir(APP_NAME)).joinpath('iottb.cfg'), + envvar='IOTTB_CONF_HOME',) +def main(debug, verbose): + setup_logging(verbose, debug) + + +if __name__ == '__main__': + main(auto_envvar_prefix='IOTTB') diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..5192efb --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,14 @@ +[tool.poetry] +name = "iottb" +version = "0.1.0" +description = "IoT Testbed" +authors = ["Sebastian Lenzlinger "] +readme = "README.md" + +[tool.poetry.dependencies] +python = "^3.12" + + +[build-system] +requires = ["poetry-core"] +build-backend = "poetry.core.masonry.api"