config: DataClass.transform(): add allow_extra=False parameter
This commit is contained in:
parent
d7f61f6475
commit
76b5b26157
1 changed files with 12 additions and 4 deletions
|
@ -30,7 +30,7 @@ class DataClass(Munch):
|
||||||
self.update(d | kwargs, validate=validate)
|
self.update(d | kwargs, validate=validate)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def transform(cls, values: Mapping[str, Any], validate: bool = True) -> Any:
|
def transform(cls, values: Mapping[str, Any], validate: bool = True, allow_extra: bool = False) -> Any:
|
||||||
results = {}
|
results = {}
|
||||||
values = dict(values)
|
values = dict(values)
|
||||||
for key in list(values.keys()):
|
for key in list(values.keys()):
|
||||||
|
@ -48,7 +48,7 @@ class DataClass(Munch):
|
||||||
if validate:
|
if validate:
|
||||||
if not isinstance(value, _classes):
|
if not isinstance(value, _classes):
|
||||||
raise Exception(f'key "{key}" has value of wrong type {_classes}: {value}')
|
raise Exception(f'key "{key}" has value of wrong type {_classes}: {value}')
|
||||||
elif validate:
|
elif validate and not allow_extra:
|
||||||
raise Exception(f'Unknown key "{key}"')
|
raise Exception(f'Unknown key "{key}"')
|
||||||
else:
|
else:
|
||||||
if isinstance(value, dict) and not isinstance(value, Munch):
|
if isinstance(value, dict) and not isinstance(value, Munch):
|
||||||
|
@ -150,12 +150,14 @@ class ProfilesSection(DataClass):
|
||||||
default: SparseProfile
|
default: SparseProfile
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def transform(cls, values: Mapping[str, Any], validate: bool = True):
|
def transform(cls, values: Mapping[str, Any], validate: bool = True, allow_extra: bool = True):
|
||||||
results = {}
|
results = {}
|
||||||
for k, v in values.items():
|
for k, v in values.items():
|
||||||
if k == 'current':
|
if k == 'current':
|
||||||
results[k] = v
|
results[k] = v
|
||||||
continue
|
continue
|
||||||
|
if not allow_extra and k != 'default':
|
||||||
|
raise Exception(f'Unknown key {k} in profiles section (Hint: extra_keys not allowed for some reason)')
|
||||||
if not isinstance(v, dict):
|
if not isinstance(v, dict):
|
||||||
raise Exception(f'profile {v} is not a dict!')
|
raise Exception(f'profile {v} is not a dict!')
|
||||||
results[k] = SparseProfile.fromDict(v, validate=True)
|
results[k] = SparseProfile.fromDict(v, validate=True)
|
||||||
|
@ -178,7 +180,13 @@ class Config(DataClass):
|
||||||
profiles: ProfilesSection
|
profiles: ProfilesSection
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def fromDict(cls, values: Mapping[str, Any], validate: bool = True, allow_incomplete: bool = False):
|
def fromDict(
|
||||||
|
cls,
|
||||||
|
values: Mapping[str, Any],
|
||||||
|
validate: bool = True,
|
||||||
|
allow_extra: bool = False,
|
||||||
|
allow_incomplete: bool = False,
|
||||||
|
):
|
||||||
values = dict(values) # copy for later modification
|
values = dict(values) # copy for later modification
|
||||||
_vals = {}
|
_vals = {}
|
||||||
for name, _class in cls._type_hints.items():
|
for name, _class in cls._type_hints.items():
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue