Skip to content

Commit

Permalink
Refactor: move functions around in built_in_resolvers.py
Browse files Browse the repository at this point in the history
No functional change
  • Loading branch information
odelalleau committed Apr 10, 2021
1 parent 4c06fe5 commit 6b634a3
Showing 1 changed file with 29 additions and 29 deletions.
58 changes: 29 additions & 29 deletions omegaconf/built_in_resolvers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,24 @@
from .grammar_parser import parse


# DEPRECATED: remove in 2.2
def legacy_env(key: str, default: Optional[str] = None) -> Any:
warnings.warn(
"The `env` resolver is deprecated, see https://github.com/omry/omegaconf/issues/573"
)
def decode(expr: Optional[str], _parent_: Container) -> Any:
"""
Parse and evaluate `expr` according to the `singleElement` rule of the grammar.
try:
return decode_primitive(os.environ[key])
except KeyError:
if default is not None:
return decode_primitive(default)
else:
raise ValidationError(f"Environment variable '{key}' not found")
If `expr` is `None`, then return `None`.
"""
if expr is None:
return None

if not isinstance(expr, str):
raise TypeError(
f"`oc.decode` can only take strings or None as input, "
f"but `{expr}` is of type {type(expr).__name__}"
)

parse_tree = parse(expr, parser_rule="singleElement", lexer_mode="VALUE_MODE")
val = _parent_.resolve_parse_tree(parse_tree)
return _get_value(val)


def env(key: str, default: Any = _DEFAULT_MARKER_) -> Optional[str]:
Expand All @@ -42,21 +47,16 @@ def env(key: str, default: Any = _DEFAULT_MARKER_) -> Optional[str]:
raise KeyError(f"Environment variable '{key}' not found")


def decode(expr: Optional[str], _parent_: Container) -> Any:
"""
Parse and evaluate `expr` according to the `singleElement` rule of the grammar.
If `expr` is `None`, then return `None`.
"""
if expr is None:
return None

if not isinstance(expr, str):
raise TypeError(
f"`oc.decode` can only take strings or None as input, "
f"but `{expr}` is of type {type(expr).__name__}"
)
# DEPRECATED: remove in 2.2
def legacy_env(key: str, default: Optional[str] = None) -> Any:
warnings.warn(
"The `env` resolver is deprecated, see https://github.com/omry/omegaconf/issues/573"
)

parse_tree = parse(expr, parser_rule="singleElement", lexer_mode="VALUE_MODE")
val = _parent_.resolve_parse_tree(parse_tree)
return _get_value(val)
try:
return decode_primitive(os.environ[key])
except KeyError:
if default is not None:
return decode_primitive(default)
else:
raise ValidationError(f"Environment variable '{key}' not found")

0 comments on commit 6b634a3

Please sign in to comment.