in heyhi/conf.py [0:0]
def _apply_scalar_override(cfg: ProtoMessage, mount: str, value: str) -> None:
assert mount, "Scalar override with empty key!"
# We want something like recursive_seattr(cfg, mount, value). But we
# need to handle the recursive parts and also cast value to correct
# type.
mount_parent, key = mount.rsplit(".", 1) if "." in mount else ("", mount)
subcfg = _get_sub_config(cfg, mount_parent)
if type(subcfg).__name__ == "ScalarMapContainer":
# Shortcut for maps.
subcfg[key] = value
return
if type(subcfg).__name__ == "RepeatedScalarContainer":
# Shortcut for arrays.
try:
key = int(key)
except ValueError:
raise ValueError(f"Got non-integer key {key} for repeated feild {mount_parent}")
if key != -1 and not 0 <= key <= len(subcfg):
raise ValueError(
f"Cannot acess element {key} in list {mount_parent} that has {len(subcfg)}"
" elements. Use '-1' to append an element"
)
if key == -1 or key == len(subcfg):
subcfg.append(value)
else:
subcfg[key] = value
return
if not hasattr(subcfg, key):
possible_typos = _find_possible_typos(cfg, mount)[:5]
raise ValueError(
"Cannot resolve path '%s' in config:\n%s\nPossible typos: %s"
% (mount, cfg, possible_typos)
)
if value == UNSET_VALUE:
subcfg.ClearField(key)
return
field = subcfg.DESCRIPTOR.fields_by_name[key]
if field.message_type is not None:
if value == SELECT_ONEOF_VALUE and field.containing_oneof is not None:
# Merge empty message of the submessage's type into the submessage.
getattr(subcfg, key).MergeFrom(type(getattr(subcfg, key))())
return
raise ValueError("Trying to set scalar '%s' for message type '%s'" % (value, mount))
attr_type = PROTO_TYPE_TO_FLOAT[field.type]
if attr_type is bool:
value = value.lower()
assert value in ("true", "false", "0", "1"), value
value = True if value in ("true", "1") else False
elif attr_type is int and not value.isdigit():
# If enum is redefined we have to search in the parrent object
# for all enums.
for maybe_enum_object in type(subcfg).__dict__.values():
if isinstance(
maybe_enum_object, google.protobuf.internal.enum_type_wrapper.EnumTypeWrapper
):
if value in maybe_enum_object.keys():
value = dict(maybe_enum_object.items())[value]
break
try:
value = attr_type(value)
except ValueError:
raise ValueError(
"Value for %s should be of type %s. Cannot cast provided value %s to this type"
% (mount, attr_type, value)
)
setattr(subcfg, key, value)