Skip to content

Commit

Permalink
Merge pull request #27 from EVEprosper/ignore_jinja
Browse files Browse the repository at this point in the history
adding functionality to ignore jinja templated values as None
  • Loading branch information
lockefox committed Jul 10, 2018
2 parents 4f03f4c + 067f145 commit bcada3b
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 30 deletions.
72 changes: 42 additions & 30 deletions prosper/common/prosper_config.py
Expand Up @@ -8,11 +8,12 @@
import configparser
from configparser import ExtendedInterpolation
import logging
import re

import anyconfig
import anytemplate


JINJA_PATTERN = re.compile(r'.*{{\S*}}.*')
def render_secrets(
config_path,
secret_path,
Expand Down Expand Up @@ -44,6 +45,34 @@ def render_secrets(

return p_config

def check_value(
config,
section,
option,
jinja_pattern=JINJA_PATTERN,
):
"""try to figure out if value is valid or jinja2 template value
Args:
config (:obj:`configparser.ConfigParser`): config object to read key from
section (str): name of section in configparser
option (str): name of option in configparser
jinja_pattern (:obj:`_sre.SRE_Pattern`): a `re.compile()` pattern to match on
Returns:
str: value if value, else None
Raises:
KeyError:
configparser.NoOptionError:
configparser.NoSectionError:
"""
value = config[section][option]
if re.match(jinja_pattern, value):
return None

return value

class ProsperConfig(object):
"""configuration handler for all prosper projects
Expand All @@ -57,8 +86,8 @@ class ProsperConfig(object):
5. args_default -- function default w/o global config
Args:
config_filename (str): path to config file
local_filepath_override (str): path modifier for private config file
config_filename (str): path to config
local_filepath_override (str, optional): path to alternate private config file
Attributes:
global_config (:obj:`configparser.ConfigParser`)
Expand All @@ -73,15 +102,6 @@ def __init__(
config_filename,
local_filepath_override='',
):
"""get the config filename for initializing data structures
Args:
config_filename (str): path to config
local_filepath_override (str, optional): path to alternate private config file
logger (:obj:`logging.Logger`, optional): capture messages to logger
debug_mode (bool, optional): enable debug modes for config helper
"""
self.config_filename = config_filename
self.local_config_filename = get_local_config_filepath(config_filename)
if local_filepath_override:
Expand All @@ -104,7 +124,10 @@ def get(
key_name (str): key name in config.section_name
Returns:
(str): do not check defaults, only return local value
str: do not check defaults, only return local value
Raises:
KeyError: unable to find option in either local or global config
"""
value = None
Expand Down Expand Up @@ -148,7 +171,7 @@ def get_option(
args_default (any): arg default given by a function
Returns:
(str) appropriate response as per priority order
str: appropriate response as per priority order
"""
if args_option != args_default and\
Expand All @@ -160,15 +183,15 @@ def get_option(

option = None
try:
option = self.local_config[section_name][key_name]
option = check_value(self.local_config, section_name, key_name)
self.logger.debug('-- using local config')
if option:
return option
except (KeyError, configparser.NoOptionError, configparser.NoSectionError):
self.logger.debug('`%s` not found in local config', section_info)

try:
option = self.global_config[section_name][key_name]
option = check_value(self.global_config, section_name, key_name)
self.logger.debug('-- using global config')
if option:
return option
Expand All @@ -183,9 +206,6 @@ def get_option(
self.logger.debug('-- using default argument')
return args_default #If all esle fails return the given default

def attach_logger(self, logger):
"""because load orders might be weird, add logger later"""
self.logger = logger

ENVNAME_PAD = 'PROSPER'
def get_value_from_environment(
Expand Down Expand Up @@ -252,9 +272,6 @@ def read_config(
config_filepath (str): path to config file. abspath > relpath
logger (:obj:`logging.Logger`): logger to catch error msgs
Raises:
FileNotFound: file access issues
"""
config_parser = configparser.ConfigParser(
interpolation=ExtendedInterpolation(),
Expand All @@ -263,14 +280,9 @@ def read_config(
inline_comment_prefixes=('#')
)
logger.debug('config_filepath=%s', config_filepath)
try:
with open(config_filepath, 'r') as filehandle:
config_parser.read_file(filehandle)
except Exception as error_msg:
logger.error(
'Unable to parse config file: %s', config_filepath, exc_info=True
)
raise error_msg

with open(config_filepath, 'r') as filehandle:
config_parser.read_file(filehandle)

return config_parser

Expand Down
1 change: 1 addition & 0 deletions tests/test_config.cfg.j2
@@ -1,5 +1,6 @@
[TEST]
secret = {{test.secret}}
partial_secret = secret_{{test.partial_secret}}
key1 = vals
key2 = 100
key3 =
Expand Down
17 changes: 17 additions & 0 deletions tests/test_prosper_config.py
Expand Up @@ -3,6 +3,7 @@
Pytest functions for exercising prosper.common.prosper_config
"""
import configparser
import os
from os import path
import json
Expand All @@ -29,6 +30,22 @@ def test_setup_environment():
expected_val = prosper_config.get_value_from_environment('TEST', 'dummy_val')
assert expected_val == ENV_TEST_1

def test_check_value():
"""validate check_value happypath"""
config = prosper_config.read_config(path.join(HERE, 'test_config.cfg.j2'))

assert prosper_config.check_value(config, 'TEST', 'secret') == None
assert prosper_config.check_value(config, 'TEST', 'partial_secret') == None
assert prosper_config.check_value(config, 'TEST', 'key1') == 'vals'
def test_skip_jinja():
"""validate system treats jinja formatted blocks as blanks"""
config = prosper_config.ProsperConfig(path.join(HERE, 'test_config.cfg.j2'))

print(config.get_option('TEST', 'secret'))
assert config.get_option('TEST', 'secret') == None
assert config.get_option('TEST', 'partial_secret') == None
assert config.get_option('TEST', 'key1') == 'vals'

def test_render_secrets():
"""happypath test for p_config.render_secrets"""
config_path = path.join(HERE, 'test_config.cfg.j2')
Expand Down

0 comments on commit bcada3b

Please sign in to comment.