Permalink
Browse files

added parse_tuple

  • Loading branch information...
robnagler committed Sep 15, 2018
1 parent bd98bf2 commit 4e31e4fc46b66f19cb085c4131d93b1c917d1f97
Showing with 55 additions and 3 deletions.
  1. +35 −3 pykern/pkconfig.py
  2. +4 −0 tests/pkconfig_data/p1/m1.py
  3. +16 −0 tests/pkconfig_test.py
View
@@ -178,8 +178,11 @@ def dev():
#: Environment variable holding the load path
LOAD_PATH_ENV_NAME = 'PYKERN_PKCONFIG_LOAD_PATH'
#: Separater for load_path string
LOAD_PATH_SEP = ':'
#: parse_tuple splits strings on this
TUPLE_SEP = ':'
#: Separator for load_path string
LOAD_PATH_SEP = TUPLE_SEP
#: Root package implicit
THIS_PACKAGE = 'pykern'
@@ -434,7 +437,32 @@ def parse_bool(value):
return True
if v in ('f', 'false', 'n', 'no', '0', ''):
return False
raise AssertionError('{}: unknown boolean value'.format(value))
raise AssertionError('unknown boolean value={}'.format(value))
@parse_none
def parse_tuple(value):
"""Default parser for `tuple` types
When the parser is `tuple`, it will be replaced with this routine,
which parses strings, lists, and tuples. It splits strings on ':'.
Args:
value (object): to be parsed
Returns:
tuple: may be an empty tuple
"""
if value is None:
return tuple()
if isinstance(value, tuple):
return value
if isinstance(value, list):
return tuple(value)
assert isinstance(value, STRING_TYPES), \
'unable to convert type={} to tuple; value={}'.format(type(value), value)
return tuple(value.split(TUPLE_SEP))
def reset_state_for_testing(add_to_environ=None):
@@ -490,6 +518,10 @@ def _fixup_parser(self):
assert isinstance(self.default, int), \
'{}: default value must be a bool: '.format(self.default, self.docstring)
self.parser = parse_bool
elif self.parser == tuple:
assert isinstance(self.default, tuple), \
'{}: default value must be a tuple: '.format(self.default, self.docstring)
self.parser = parse_tuple
class _Key(str, object):
@@ -42,6 +42,10 @@ def _some_key(v):
bool2=(True, bool, 'a True boolean'),
bool3=(True, bool, 'a True boolean will be overriden'),
bool4=(False, bool, 'a False boolean will be overriden'),
tuple1=((), tuple, 'an empty tuple'),
tuple2=((1,), tuple, 'a single value tuple'),
tuple3=((2,), tuple, '(2,) will be overrriden'),
tuple4=((3,), tuple, '(3,) will be overriden'),
)
if cfg.p4 is None:
cfg.p4 = cfg.p3 * 10
View
@@ -98,6 +98,22 @@ def test_init2(monkeypatch):
from p2.m1 import cfg
def test_init3(monkeypatch):
"""Validate parse_tuple"""
home = _setup(monkeypatch, dict(P1_M1_TUPLE3='', P1_M1_TUPLE4='a:b'))
pkconfig.append_load_path('p1')
from p1.m1 import cfg
assert () == cfg.tuple1, \
'When tuple1 is none, is empty'
assert (1,) == cfg.tuple2, \
'When tuple2 is none, is (1,)'
pkconfig.reset_state_for_testing()
assert () == cfg.tuple3, \
'tuple3 should be overriden to be empty'
assert ("a", "b") == cfg.tuple4, \
'tuple4 should be overriden to be ("a", "b")'
def _setup(monkeypatch, env=None):
# Can't import anything yet
global pkconfig

0 comments on commit 4e31e4f

Please sign in to comment.