17 changed files with 137 additions and 81 deletions
@ -0,0 +1,4 @@ |
|||||
|
**/__pycache__/ |
||||
|
build/ |
||||
|
dist/ |
||||
|
**/*.egg-info |
||||
@ -0,0 +1 @@ |
|||||
|
include monsun_backend/defaults/config.yml |
||||
@ -1,58 +0,0 @@ |
|||||
from pathlib import Path |
|
||||
from typing import Dict |
|
||||
from typing import Optional |
|
||||
|
|
||||
from dependency_injector import containers |
|
||||
from dependency_injector import providers |
|
||||
from flask_api import FlaskAPI |
|
||||
from serial import Serial |
|
||||
|
|
||||
DEFAULTS_DIR = Path(__file__).parent / "defaults" |
|
||||
CONFIG_FILE = DEFAULTS_DIR / "config.yml" |
|
||||
|
|
||||
|
|
||||
class Container(containers.DeclarativeContainer): |
|
||||
config = providers.Configuration("config") |
|
||||
|
|
||||
serial = providers.Factory( |
|
||||
Serial, |
|
||||
port=config.device_id.required(), |
|
||||
baudrate=config.baudrate.required(), |
|
||||
) |
|
||||
|
|
||||
|
|
||||
__container: Optional[Container] = None |
|
||||
|
|
||||
|
|
||||
def initialize_container( |
|
||||
config: Optional[Dict] = None, |
|
||||
config_file: Optional[Path] = None, |
|
||||
) -> Container: |
|
||||
global __container |
|
||||
if __container is not None: |
|
||||
raise RuntimeError("Container already initialized") |
|
||||
|
|
||||
__container = Container() |
|
||||
__container.config.from_yaml(CONFIG_FILE) |
|
||||
|
|
||||
if config is not None: |
|
||||
__container.config.from_dict(config) |
|
||||
|
|
||||
if config_file is not None: |
|
||||
__container.config.from_yaml(config_file) |
|
||||
|
|
||||
return __container |
|
||||
|
|
||||
|
|
||||
def init_app(app: FlaskAPI): |
|
||||
initialize_container( |
|
||||
config=app.config.get("DI_CONFIG"), |
|
||||
config_file=app.config.get("DI_CONFIG_FILE"), |
|
||||
) |
|
||||
|
|
||||
|
|
||||
def get_container() -> Container: |
|
||||
global __container |
|
||||
if __container is None: |
|
||||
raise RuntimeError("Container not initialized") |
|
||||
return __container |
|
||||
@ -0,0 +1,56 @@ |
|||||
|
import logging |
||||
|
from pathlib import Path |
||||
|
from pprint import pformat |
||||
|
from typing import Dict |
||||
|
from typing import Optional |
||||
|
|
||||
|
from dependency_injector import containers |
||||
|
from dependency_injector import providers |
||||
|
from serial import Serial |
||||
|
|
||||
|
from .util import log_function_call |
||||
|
|
||||
|
DEFAULTS_DIR = Path(__file__).parent / "defaults" |
||||
|
CONFIG_FILE = DEFAULTS_DIR / "config.yml" |
||||
|
|
||||
|
_logger = logging.getLogger(__name__) |
||||
|
|
||||
|
|
||||
|
class Container(containers.DeclarativeContainer): |
||||
|
config = providers.Configuration("config") |
||||
|
|
||||
|
serial = providers.Factory( |
||||
|
Serial, |
||||
|
port=config.device_id.required(), |
||||
|
baudrate=config.baudrate.required(), |
||||
|
) |
||||
|
|
||||
|
|
||||
|
@log_function_call |
||||
|
def get_initialize_container( |
||||
|
config: Optional[Dict] = None, |
||||
|
config_file: Optional[Path] = None, |
||||
|
) -> Container: |
||||
|
logger = _logger.getChild("initialize_container") |
||||
|
|
||||
|
logger.debug("initialize container...") |
||||
|
container = Container() |
||||
|
|
||||
|
logger.debug(f"initialize container from config file: {CONFIG_FILE}") |
||||
|
container.config.from_yaml(CONFIG_FILE, required=True) |
||||
|
|
||||
|
user_config = Path.cwd() / "config.yml" |
||||
|
if user_config.is_file(): |
||||
|
logger.debug(f"initialize container from user config file: {user_config}") |
||||
|
container.config.from_yaml(user_config, required=True) |
||||
|
|
||||
|
if config is not None: |
||||
|
logger.debug(f"initialize container with config: {config}") |
||||
|
container.config.from_dict(config) |
||||
|
|
||||
|
if config_file is not None: |
||||
|
logger.debug(f"initialize container from config file: {config_file}") |
||||
|
container.config.from_yaml(config_file) |
||||
|
|
||||
|
logger.debug(f"container config:\n{pformat(container.config())}") |
||||
|
return container |
||||
@ -1,5 +1,3 @@ |
|||||
device_id: /dev/tty.usbmodem2067368F32521 |
|
||||
# device_id: /dev/tty.usbmodem207E3283544E1 |
|
||||
baudrate: 115200 |
baudrate: 115200 |
||||
header_size: 4 |
header_size: 4 |
||||
heartbeat_interval: 1 |
heartbeat_interval: 1 |
||||
@ -0,0 +1,27 @@ |
|||||
|
import functools |
||||
|
import logging |
||||
|
|
||||
|
function_call_logger = logging.getLogger("call") |
||||
|
|
||||
|
|
||||
|
# decorator |
||||
|
def log_function_call(func): |
||||
|
if logging.getLogger().level != logging.DEBUG: |
||||
|
return func |
||||
|
|
||||
|
@functools.wraps(func) |
||||
|
def wrapper(*args, **kwargs): |
||||
|
function_call_logger.debug( |
||||
|
"calling {func}({arguments})".format( |
||||
|
func=func.__name__, |
||||
|
arguments=", ".join( |
||||
|
[str(value) for value in args] |
||||
|
+ [f"{key}={value}" for key, value in kwargs.items()], |
||||
|
), |
||||
|
), |
||||
|
) |
||||
|
return_val = func(*args, **kwargs) |
||||
|
function_call_logger.debug(f"{func.__name__}() returned {return_val}") |
||||
|
return return_val |
||||
|
|
||||
|
return wrapper |
||||
@ -0,0 +1,4 @@ |
|||||
|
dependency-injector[yaml]>=4.34.0,<5 |
||||
|
flask-api>=3.0.post1,<4 |
||||
|
pyserial>=3.5,<4 |
||||
|
uwsgi |
||||
@ -1,3 +0,0 @@ |
|||||
from . import create_app |
|
||||
|
|
||||
application = create_app() |
|
||||
@ -0,0 +1,19 @@ |
|||||
|
import setuptools |
||||
|
from monsun_backend import __author__ |
||||
|
from monsun_backend import __email__ |
||||
|
from monsun_backend import __version__ |
||||
|
|
||||
|
setuptools.setup( |
||||
|
name="monsun-backend", |
||||
|
version=__version__, |
||||
|
author=__author__, |
||||
|
author_email=__email__, |
||||
|
description="Monsun backend", |
||||
|
packages=setuptools.find_packages(), |
||||
|
classifiers=[ |
||||
|
"Programming Language :: Python :: 3", |
||||
|
"Operating System :: OS Independent", |
||||
|
], |
||||
|
python_requires=">=3.7", |
||||
|
include_package_data=True, |
||||
|
) |
||||
@ -0,0 +1,3 @@ |
|||||
|
from monsun_backend import create_app |
||||
|
|
||||
|
application = create_app() |
||||
@ -0,0 +1 @@ |
|||||
|
device_id: /dev/tty.usbmodem207E3283544E1 |
||||
Loading…
Reference in new issue