-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from nkongenelly/add_config_class
Add config class
- Loading branch information
Showing
10 changed files
with
629 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
runfolder_schema = { | ||
"type": "object", | ||
"properties": { | ||
"monitored_directories": { | ||
"type": "array", | ||
"items": { | ||
"type": "string", | ||
"minLength": 1, | ||
}, | ||
"minItems": 1, | ||
}, | ||
"completed_marker_grace_minutes": { | ||
"type": "number", | ||
}, | ||
"port": { | ||
"type": "number", | ||
}, | ||
"logger_config_file": { | ||
"type": "string", | ||
"minLength": 1, | ||
} | ||
}, | ||
"required": [ | ||
"monitored_directories", | ||
"completed_marker_grace_minutes", | ||
"port", | ||
"logger_config_file", | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import copy | ||
|
||
from jsonschema import validate | ||
|
||
|
||
class Config: | ||
""" | ||
Class to make the app's config easily available throughout the program. | ||
A Config instance possesses a global config dictionary available to all | ||
instances, as well as a local one that can be used to provide default | ||
values, when they are not defined in the global dictionary. | ||
Variables stored in the config are meant to be constant throughout the | ||
program and updating them after initialization them should be avoided. | ||
""" | ||
|
||
def __new__(cls, *args, **kwargs): | ||
if hasattr(cls, "_instance"): | ||
return cls._instance | ||
|
||
config = super(Config, cls).__new__(cls) | ||
config._global_config_dict = {} | ||
return config | ||
|
||
def __init__(self, local_config_dict=None): | ||
config_dict = copy.deepcopy(local_config_dict) or {} | ||
config_dict.update(self._global_config_dict) | ||
self._config_dict = config_dict | ||
|
||
@classmethod | ||
def new(cls, global_config_dict, exist_ok=False, schema=None): | ||
""" | ||
Initialize a new global config. | ||
Raises AssertionError if a Config already exists, unles `exist_ok` is | ||
set to True. | ||
Validates the global_config_dict with jsonschema | ||
""" | ||
assert not hasattr(cls, "_instance") or exist_ok, "Config has already been initialized" | ||
if schema: | ||
validate(instance=global_config_dict, schema=schema) | ||
|
||
if not hasattr(cls, "_instance"): | ||
cls._instance = super(Config, cls).__new__(cls) | ||
cls._instance._global_config_dict = copy.deepcopy(global_config_dict) | ||
|
||
return cls() | ||
|
||
@classmethod | ||
def clear(cls): | ||
""" | ||
Clear existing config. | ||
""" | ||
if hasattr(cls, "_instance"): | ||
del cls._instance | ||
|
||
def __getitem__(self, item): | ||
return self._config_dict[item] | ||
|
||
def get(self, item, default=None): | ||
return self._config_dict.get(item, default) | ||
|
||
def to_dict(self): | ||
""" | ||
Returns config as dict | ||
""" | ||
# Copy is returned to avoid accidentaly modifying the original config. | ||
return copy.deepcopy(self._config_dict) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,39 @@ | ||
import yaml | ||
import argparse | ||
import logging.config | ||
|
||
from aiohttp import web | ||
|
||
from arteria.models.config import Config | ||
from arteria.handlers.base import base_routes as routes | ||
from arteria.config_schemas.schema_arteria_runfolder import runfolder_schema | ||
|
||
|
||
def get_app(config_dict): | ||
""" | ||
Creates an Application instance | ||
Sets up logger from configuration file specified in the config | ||
Registers the request handler | ||
""" | ||
config = Config.new(config_dict, schema=runfolder_schema) | ||
|
||
with open(config["logger_config_file"], "r", encoding="utf-8") as logger_config_file: | ||
logger_config = yaml.safe_load(logger_config_file.read()) | ||
logging.config.dictConfig(logger_config) | ||
|
||
def get_app(config): | ||
app = web.Application() | ||
app.router.add_routes(routes) | ||
|
||
return app | ||
|
||
|
||
def main(): | ||
app = get_app({}) | ||
web.run_app(app) | ||
parser = argparse.ArgumentParser(description="arteria-runfolder server") | ||
parser.add_argument('--config_file') | ||
|
||
args = parser.parse_args() | ||
with open(args.config_file, "r", encoding="utf-8") as config_file: | ||
config_dict = yaml.safe_load(config_file.read()) | ||
|
||
app = get_app(config_dict) | ||
web.run_app(app, port=config.get("port")) |
Oops, something went wrong.