Skip to content

Commit

Permalink
Loosen up regex requirements in resolve single
Browse files Browse the repository at this point in the history
OmegaConf's templating language allows very few characters.
There are some use cases where you'd want to have special characters
so that your custom resolver can handle resolution there (kind of like
embedding a templating language inside of OmegaConfs). This allows
such behaviour without seemingly breaking anything too majorly
  • Loading branch information
swist committed Oct 23, 2019
1 parent ab5df0f commit a987662
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 3 deletions.
2 changes: 1 addition & 1 deletion omegaconf/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ def _resolve_value(root_node, inter_type, inter_key):
return ret

def _resolve_single(self, value):
match_list = list(re.finditer(r"\${(\w+:)?([\w\.%_-]+?)}", value))
match_list = list(re.finditer(r"\${(\w+:)?(.+?)?}", value))
if len(match_list) == 0:
return value

Expand Down
11 changes: 10 additions & 1 deletion omegaconf/omegaconf.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import copy
import io
import os
import re
import sys
import warnings
from contextlib import contextmanager
Expand Down Expand Up @@ -137,6 +138,14 @@ def merge(*others):
target.merge_with(*others[1:])
return target

@staticmethod
def _tokenize_args(string):
if string is None or string == '':
return []
escaped = re.split(r'(?<!\\),', string)
escaped = [re.sub(r'(?<!\\) ', lambda x: '', x) for x in escaped]
return [re.sub(r'(\\([ ,]))', lambda x: x.group(2), x) for x in escaped]

@staticmethod
def register_resolver(name, resolver):
assert callable(resolver), "resolver must be callable"
Expand All @@ -145,7 +154,7 @@ def register_resolver(name, resolver):

def caching(config, key):
cache = OmegaConf.get_cache(config)[name]
val = cache[key] if key in cache else resolver(key)
val = cache[key] if key in cache else resolver(*OmegaConf._tokenize_args(key))
cache[key] = val
return val

Expand Down
14 changes: 14 additions & 0 deletions tests/test_base_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,20 @@ def test_read_write_override(src, func, expectation):
func(c)


@pytest.mark.parametrize('string, tokenized', [
('dog,cat', ['dog', 'cat']),
('dog\,cat', ['dog,cat']),
('dog,\ cat', ['dog', ' cat']),
('\ ,cat', [' ', 'cat']),
('dog, cat', ['dog', 'cat']),
('dog, cat', ['dog', 'cat']),
(None, []),
('', []),
])
def test_tokenize_with_escapes(string, tokenized):
assert OmegaConf._tokenize_args(string) == tokenized


@pytest.mark.parametrize('src, func, expectation', [
({}, lambda c: c.__setattr__('foo', 1), pytest.raises(KeyError)),
])
Expand Down
25 changes: 24 additions & 1 deletion tests/test_interpolation.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ def test_resolver_cache_1():

def test_resolver_cache_2():
"""
Tests that resolver cache is not shared between different OmegaConf objects
Tests that resolver cache is not shared between different OmegaConf objects
"""
try:
OmegaConf.register_resolver("random", lambda _: random.randint(0, 10000000))
Expand All @@ -250,6 +250,29 @@ def test_resolver_cache_2():
OmegaConf.clear_resolvers()


def test_resolver_that_allows_a_list_of_arguments():
try:
OmegaConf.register_resolver('arg_list', lambda *args: args)
OmegaConf.register_resolver('zero_arg', lambda: 'zero')
os.environ["ENV_DEFAULT"] = "World"
c = OmegaConf.create(
dict(
arg_list="${arg_list:cat, dog}",
escape_comma="${arg_list:cat\, do\ g}",
escape_whitespace="${arg_list:cat,\ dog}",
zero_arg="${zero_arg:}",
)
)

assert c.arg_list == ("cat", "dog")
assert c.escape_comma == ("cat,do g",)
assert c.escape_whitespace == ("cat", " dog")
assert c.zero_arg == "zero"
finally:
del os.environ['ENV_DEFAULT']
OmegaConf.clear_resolvers


def test_copy_cache():
OmegaConf.register_resolver("random", lambda _: random.randint(0, 10000000))
c1 = OmegaConf.create(dict(
Expand Down

0 comments on commit a987662

Please sign in to comment.