Skip to content

Commit

Permalink
Allow empty variable definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
lhh committed Aug 1, 2024
1 parent e199d18 commit 6f122b7
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 15 deletions.
46 changes: 32 additions & 14 deletions jirate/template_vars.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@
import re

_sub_left = '@@'
#_sub_right = '((:([^@]+))|(\\?([^@]+)?))?@@' # for allowing null/empty
_sub_right = '(:([^@]+))?@@'
_sub_right = '((:([^@]+))|(\\?([^@]+)?))?@@' # for allowing null/empty
#_sub_right = '(:([^@]+))?@@'
_base_pattern = _sub_left + '([a-z_]+)' + _sub_right


def _apply_values(inp, values):
if isinstance(inp, str):
match = re.findall(_base_pattern, inp)
for glyph in match:
varname, _, __ = glyph
inp = re.sub(_sub_left + varname + _sub_right, values[varname], inp)
varname = glyph[0]
inp = re.sub(_sub_left + varname + _sub_right, values[varname]['value'], inp)
elif isinstance(inp, list):
ret = []
for val in inp:
Expand All @@ -37,10 +37,21 @@ def _populate_defaults(inp, values):
if isinstance(inp, str):
match = re.findall(_base_pattern, inp)
for glyph in match:
key, _, value = glyph
if key in values and values[key]:
key = glyph[0]
value = glyph[1]
if key in values and values[key]['value']:
continue
values[key] = value
values[key] = {}
if value is None:
value = ''
if value.startswith('?'):
values[key]['required'] = False
value = value[1:]
else:
values[key]['required'] = True
if value.startswith(':'):
value = value[1:]
values[key]['value'] = value
elif isinstance(inp, list):
for val in inp:
_populate_defaults(val, values)
Expand All @@ -52,12 +63,18 @@ def _populate_defaults(inp, values):
def update_values_interactive(values):
ret = {}
for key in values:
if values[key]:
ret[key] = input(f'Value for "{key}" (default: "{values[key]}"):')
ret[key] = {}
ret[key]['required'] = values[key]['required']

dflt = values[key]['value']
if dflt:
ret[key]['value'] = input(f'Value for "{key}" (default: "{dflt}"): ')
else:
ret[key] = input(f'Value for "{key}":')
if not ret[key]:
ret[key] = values[key]
ret[key]['value'] = input(f'Value for "{key}": ')

# revert to default
if not ret[key]['value']:
ret[key]['value'] = values[key]['value']

return ret

Expand All @@ -73,13 +90,14 @@ def apply_values(inp, values={}, interactive=False):
for key in values:
if key not in template_values:
extra.append(key)
template_values[key] = values[key]
else:
template_values[key]['value'] = values[key]
if extra:
raise ValueError(f'Unknown variable(s) for {extra}')

missing = []
for key in template_values:
if not template_values[key]:
if template_values[key]['required'] and not template_values[key]['value']:
missing.append(key)
if missing:
raise ValueError(f'Missing value(s) for {missing}')
Expand Down
10 changes: 9 additions & 1 deletion jirate/tests/test_template_vars.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,15 @@ def test_populate_defaults_simple():
values = {}

_populate_defaults(inp, values)
assert values == {'a': 'b'}
assert values == {'a': {'required': True, 'value': 'b'}}


def test_populate_defaults_optional():
inp = '@@a?b@@'
values = {}

_populate_defaults(inp, values)
assert values == {'a': {'required': False, 'value': 'b'}}


def test_ok_variable_no_default():
Expand Down

0 comments on commit 6f122b7

Please sign in to comment.