Skip to content

Commit

Permalink
alternative layout
Browse files Browse the repository at this point in the history
  • Loading branch information
omry committed Apr 11, 2021
1 parent 7e37c82 commit 387d32f
Show file tree
Hide file tree
Showing 12 changed files with 164 additions and 180 deletions.
17 changes: 8 additions & 9 deletions omegaconf/omegaconf.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,14 @@ def SI(interpolation: str) -> Any:


def register_default_resolvers() -> None:
from .resolvers import legacy_env
from .resolvers.oc import decode, env
from .resolvers.oc.dict import dict_keys, dict_values

OmegaConf.register_new_resolver("oc.decode", decode)
OmegaConf.register_new_resolver("oc.dict.keys", dict_keys)
OmegaConf.register_new_resolver("oc.dict.values", dict_values)
OmegaConf.register_new_resolver("oc.env", env)
OmegaConf.legacy_register_resolver("env", legacy_env)
from omegaconf.resolvers import env, oc

OmegaConf.register_new_resolver("oc.decode", oc.decode)
OmegaConf.register_new_resolver("oc.env", oc.env)

OmegaConf.register_new_resolver("oc.dict.keys", oc.dict.keys)
OmegaConf.register_new_resolver("oc.dict.values", oc.dict.values)
OmegaConf.legacy_register_resolver("env", env)


class OmegaConf:
Expand Down
27 changes: 25 additions & 2 deletions omegaconf/resolvers/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,28 @@
from .env import legacy_env
import os
import warnings
from typing import Any, Optional

from omegaconf._utils import decode_primitive
from omegaconf.errors import ValidationError
from omegaconf.resolvers import oc


# DEPRECATED: remove in 2.2
def env(key: str, default: Optional[str] = None) -> Any:
warnings.warn(
"The `env` resolver is deprecated, see https://github.com/omry/omegaconf/issues/573"
)

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")


__all__ = [
"legacy_env",
"env",
"oc",
]
21 changes: 0 additions & 21 deletions omegaconf/resolvers/env.py

This file was deleted.

52 changes: 49 additions & 3 deletions omegaconf/resolvers/oc/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,53 @@
from ._decode import decode
from ._env import env
import os
from typing import Any, Optional

from omegaconf import Container
from omegaconf._utils import _DEFAULT_MARKER_, _get_value
from omegaconf.grammar_parser import parse
from omegaconf.resolvers.oc import dict


def env(key: str, default: Any = _DEFAULT_MARKER_) -> Optional[str]:
"""
:param key: Environment variable key
:param default: Optional default value to use in case the key environment variable is not set.
If default is not a string, it is converted with str(default).
None default is returned as is.
:return: The environment variable 'key'. If the environment variable is not set and a default is
provided, the default is used. If used, the default is converted to a string with str(default).
If the default is None, None is returned (without a string conversion).
"""
try:
return os.environ[key]
except KeyError:
if default is not _DEFAULT_MARKER_:
return str(default) if default is not None else None
else:
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__}"
)

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


__all__ = [
"env",
"decode",
"dict",
"env",
]
25 changes: 0 additions & 25 deletions omegaconf/resolvers/oc/_decode.py

This file was deleted.

23 changes: 0 additions & 23 deletions omegaconf/resolvers/oc/_env.py

This file was deleted.

81 changes: 81 additions & 0 deletions omegaconf/resolvers/oc/dict.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
from typing import Any, List

from omegaconf import AnyNode, Container, DictConfig, ListConfig
from omegaconf._utils import Marker
from omegaconf.basecontainer import BaseContainer
from omegaconf.errors import ConfigKeyError

_DEFAULT_SELECT_MARKER_: Any = Marker("_DEFAULT_SELECT_MARKER_")


def keys(
key: str,
_parent_: Container,
) -> ListConfig:
from omegaconf import OmegaConf

assert isinstance(_parent_, BaseContainer)

in_dict = _get_and_validate_dict_input(
key, parent=_parent_, resolver_name="oc.dict.keys"
)

ret = OmegaConf.create(list(in_dict.keys()), parent=_parent_)
assert isinstance(ret, ListConfig)
return ret


def values(key: str, _root_: BaseContainer, _parent_: Container) -> ListConfig:
assert isinstance(_parent_, BaseContainer)
in_dict = _get_and_validate_dict_input(
key, parent=_parent_, resolver_name="oc.dict.values"
)

content = in_dict._content
assert isinstance(content, dict)

ret = ListConfig([])
for k in content:
ref_node = AnyNode(f"${{{key}.{k}}}")
ret.append(ref_node)

# Finalize result by setting proper type and parent.
element_type: Any = in_dict._metadata.element_type
ret._metadata.element_type = element_type
ret._metadata.ref_type = List[element_type]
ret._set_parent(_parent_)

return ret


def _get_and_validate_dict_input(
key: str,
parent: BaseContainer,
resolver_name: str,
) -> DictConfig:
from omegaconf import OmegaConf

if not isinstance(key, str):
raise TypeError(
f"`{resolver_name}` requires a string as input, but obtained `{key}` "
f"of type: {type(key).__name__}"
)

in_dict = OmegaConf.select(
parent,
key,
throw_on_missing=True,
absolute_key=True,
default=_DEFAULT_SELECT_MARKER_,
)

if in_dict is _DEFAULT_SELECT_MARKER_:
raise ConfigKeyError(f"Key not found: '{key}'")

if not isinstance(in_dict, DictConfig):
raise TypeError(
f"`{resolver_name}` cannot be applied to objects of type: "
f"{type(in_dict).__name__}"
)

return in_dict
7 changes: 0 additions & 7 deletions omegaconf/resolvers/oc/dict/__init__.py

This file was deleted.

41 changes: 0 additions & 41 deletions omegaconf/resolvers/oc/dict/_common.py

This file was deleted.

20 changes: 0 additions & 20 deletions omegaconf/resolvers/oc/dict/_keys.py

This file was deleted.

28 changes: 0 additions & 28 deletions omegaconf/resolvers/oc/dict/_values.py

This file was deleted.

2 changes: 1 addition & 1 deletion tests/interpolation/built_in_resolvers/test_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
InterpolationResolutionError,
InterpolationToMissingValueError,
)
from omegaconf.resolvers.oc.dict._common import _get_and_validate_dict_input
from omegaconf.resolvers.oc.dict import _get_and_validate_dict_input
from tests import User, Users


Expand Down

0 comments on commit 387d32f

Please sign in to comment.