Commit fa346ef8 authored by Michal Čihař's avatar Michal Čihař

Factor out environment parsing to module

Move the code to openshift helper module and add testcases for it.

Issue #785
Signed-off-by: default avatarMichal Čihař <michal@cihar.com>
parent 53fdec47
......@@ -22,6 +22,9 @@
import os
import hashlib
import sys
import re
import ast
from string import Template
def get_openshift_secret_key():
......@@ -43,3 +46,23 @@ def get_openshift_secret_key():
"please set OPENSHIFT_SECRET_TOKEN!"
)
raise ValueError('No key available')
def import_env_vars(environ, target):
"""Imports WEBLATE_* variables into given object.
This is used for importing settings from environment into settings module.
"""
weblate_var = re.compile('^WEBLATE_[A-Za-z0-9_]+$')
for name, value in environ.items():
if weblate_var.match(name):
try:
setattr(target, name[8:],
ast.literal_eval(Template(value).substitute(environ)))
except ValueError as e:
if not e.args:
e.args = ('',)
e.args = (
"Error parsing %s = '%s': %s" % (name, value, e.args[0]),
) + e.args[1:]
raise
......@@ -20,10 +20,7 @@
import os
import sys
import re
import ast
from string import Template
from weblate.opneshiftlib import get_openshift_secret_key
from weblate.openshiftlib import get_openshift_secret_key, import_env_vars
# Import example settings file to get default values for Weblate settings.
from weblate.settings_example import *
......@@ -126,18 +123,4 @@ os.environ['HOME'] = os.environ['OPENSHIFT_DATA_DIR']
# Import environment variables prefixed with WEBLATE_ as weblate settings
_this_module = sys.modules[__name__]
weblate_var = re.compile('^WEBLATE_[A-Za-z0-9_]+$')
for name, value in os.environ.items():
if weblate_var.match(name):
try:
setattr(_this_module, name[8:],
ast.literal_eval(Template(value).substitute(os.environ)))
except ValueError as e:
if not e.args:
e.args = ('',)
e.args = (
"Error parsing %s = '%s': %s" % (name, value, e.args[0]),
) + e.args[1:]
raise
import_env_vars(os.environ, sys.modules[__name__])
......@@ -21,7 +21,11 @@
from unittest import TestCase
import os
from weblate.openshiftlib import get_openshift_secret_key
from weblate.openshiftlib import get_openshift_secret_key, import_env_vars
class FakeStorage(object):
pass
class OpenShiftTest(TestCase):
......@@ -50,3 +54,28 @@ class OpenShiftTest(TestCase):
)
del os.environ['OPENSHIFT_APP_NAME']
del os.environ['OPENSHIFT_APP_UUID']
def test_import_env_string(self):
storage = FakeStorage()
import_env_vars({'WEBLATE_FOO': '"bar"'}, storage)
self.assertEquals(storage.FOO, 'bar')
def test_import_env_int(self):
storage = FakeStorage()
import_env_vars({'WEBLATE_FOO': '1234'}, storage)
self.assertEquals(storage.FOO, 1234)
def test_import_env_tuple(self):
storage = FakeStorage()
import_env_vars({'WEBLATE_FOO': '(1, 2)'}, storage)
self.assertEquals(storage.FOO, (1, 2))
def test_import_env_env(self):
storage = FakeStorage()
import_env_vars({'WEBLATE_FOO': '"$BAR"', 'BAR': 'baz'}, storage)
self.assertEquals(storage.FOO, 'baz')
def test_import_env_raw(self):
storage = FakeStorage()
import_env_vars({'WEBLATE_FOO': '(r"/project/(.*)$$",)'}, storage)
self.assertEquals(storage.FOO, ('/project/(.*)$',))
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment