Separate the handling of envvars by prefix

This commit is contained in:
Bálint Bokros 2018-02-09 14:48:37 +01:00
parent 2499c4759e
commit 4f181b8f09
6 changed files with 19 additions and 12 deletions

9
lib/envvars.py Normal file
View File

@ -0,0 +1,9 @@
from collections import namedtuple
from os import environ
def generate_namedtuple_from_prefixed_envvars(prefix: str, tuple_name: str):
envvars = {envvar.replace(prefix, '', 1): environ.get(envvar)
for envvar in environ.keys()
if envvar.startswith(prefix)}
return namedtuple(tuple_name, envvars)(**envvars)

0
lib/tao/__init__.py Normal file
View File

View File

@ -0,0 +1 @@
from .envvars import *

View File

@ -0,0 +1,3 @@
from envvars import generate_namedtuple_from_prefixed_envvars
taoenv = generate_namedtuple_from_prefixed_envvars('AVATAO_', 'taoenvtuple')

View File

@ -1,10 +1,3 @@
from os import environ, getenv from envvars import generate_namedtuple_from_prefixed_envvars
from collections import namedtuple
TFW_PREFIX = 'TFW_' tfwenv = generate_namedtuple_from_prefixed_envvars('TFW_', 'tfwenvtuple')
tfwenvvars = {envvar.replace(TFW_PREFIX, '', 1): environ.get(envvar)
for envvar in environ.keys()
if envvar.startswith(TFW_PREFIX)}
tfwenv = namedtuple('tfwenvtuple', tfwenvvars)(**tfwenvvars)
AVATAO_SECRET = getenv('AVATAO_SECRET', 'secret')

View File

@ -1,7 +1,8 @@
from tornado.ioloop import IOLoop from tornado.ioloop import IOLoop
from tornado.web import Application from tornado.web import Application
from tfw.config import tfwenv, AVATAO_SECRET from tao.config import taoenv
from tfw.config import tfwenv
from handlers import SolutionCheckHandler, TestHandler from handlers import SolutionCheckHandler, TestHandler
from tfw.networking.solvable_connector import SolvableConnector from tfw.networking.solvable_connector import SolvableConnector
@ -11,8 +12,8 @@ log = logging.getLogger(__name__)
if __name__ == '__main__': if __name__ == '__main__':
solvable_connector = SolvableConnector() solvable_connector = SolvableConnector()
routes = [ routes = [
(r'/{secret}/?'.format(secret=AVATAO_SECRET), SolutionCheckHandler, {'solvable_connector': solvable_connector}), (r'/{secret}/?'.format(secret=taoenv.SECRET), SolutionCheckHandler, {'solvable_connector': solvable_connector}),
(r'/{secret}/test/?'.format(secret=AVATAO_SECRET), TestHandler, {'solvable_connector': solvable_connector}) (r'/{secret}/test/?'.format(secret=taoenv.SECRET), TestHandler, {'solvable_connector': solvable_connector})
] ]
app = Application( app = Application(
routes routes