Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

675 extended env variable interpolation #676

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion pywps/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,36 @@
wps_strict = True


class EnvInterpolation(configparser.BasicInterpolation):
"""
Configuration parser class to allow env variable interpolation.

With this interpolator it is possible to use env variables as
part of the values in an cfg file.

Example (given an env variable HOSTNAME=localhost):

[server]
url = http://${HOSTNAME}/wps


==> http://localhost/wps


This code is an adaption from the configuation parsing in the pycsw
project.
See also
- https://github.com/geopython/pycsw/blob/3.0.0-alpha3/pycsw/core/util.py
- https://stackoverflow.com/a/49529659

"""

def before_get(self, parser, section, option, value, defaults):
"""Expand env variables when returning values."""
value = super().before_get(parser, section, option, value, defaults)
return os.path.expandvars(value)


def get_config_value(section, option, default_value=''):
"""Get desired value from configuration files

Expand Down Expand Up @@ -67,7 +97,7 @@ def load_configuration(cfgfiles=None):
global CONFIG

LOGGER.info('loading configuration')
CONFIG = configparser.ConfigParser(os.environ)
CONFIG = configparser.ConfigParser(os.environ, interpolation=EnvInterpolation())

LOGGER.debug('setting default values')
CONFIG.add_section('server')
Expand Down
2 changes: 2 additions & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import pywps.configuration as config

from tests import test_capabilities
from tests import test_configuration
from tests import test_describe
from tests import test_execute
from tests import test_exceptions
Expand Down Expand Up @@ -76,6 +77,7 @@ def load_tests(loader=None, tests=None, pattern=None):

return unittest.TestSuite([
test_capabilities.load_tests(),
test_configuration.load_tests(),
test_execute.load_tests(),
test_describe.load_tests(),
test_inout.load_tests(),
Expand Down
49 changes: 49 additions & 0 deletions tests/test_configuration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
##################################################################
# Copyright 2018 Open Source Geospatial Foundation and others #
# licensed under MIT, Please consult LICENSE.txt for details #
##################################################################

"""Tests for the configuration."""

import os
import unittest

from pywps import configuration


class TestEnvInterpolation(unittest.TestCase):
"""Test cases for env variable interpolation within the configuration."""

def test_expand_user(self):
"""Ensure we can parse a value with the $USER entry."""
user = os.environ.get("USER")
configuration.CONFIG.read_string("[envinterpolationsection]\nuser=$USER")
assert user == configuration.CONFIG["envinterpolationsection"]["user"]

def test_expand_user_with_some_text(self):
"""Ensure we can parse a value with the $USER entry and some more text."""
user = os.environ.get("USER")
new_user = "new_" + user
configuration.CONFIG.read_string("[envinterpolationsection]\nuser=new_${USER}")
assert new_user == configuration.CONFIG["envinterpolationsection"]["user"]

def test_dont_expand_value_without_env_variable(self):
"""
Ensure we don't expand values that are no env variables.

Could be an important case for existing config keys that need to
contain the $symbol.
"""
key = "$example_key_that_hopefully_will_never_be_a_real_env_variable"
configuration.CONFIG.read_string("[envinterpolationsection]\nuser=" + key)
assert key == configuration.CONFIG["envinterpolationsection"]["user"]


def load_tests(loader=None, tests=None, pattern=None):
"""Load the tests and return the test suite for this file."""
if not loader:
loader = unittest.TestLoader()
suite_list = [
loader.loadTestsFromTestCase(TestEnvInterpolation),
]
return unittest.TestSuite(suite_list)