示例#1
0
文件: style.py 项目: bgrundmann/iommi
def reinvoke_new_defaults(obj: Any, additional_kwargs: Dict[str, Any]) -> Any:
    assert is_reinvokable(obj), f'reinvoke_new_defaults() called on object with ' \
                                f'missing @reinvokable constructor decorator: {obj!r}'
    additional_kwargs_namespace = Namespace(additional_kwargs)

    kwargs = Namespace(additional_kwargs_namespace)
    for name, saved_param in items(obj._iommi_saved_params):
        try:
            new_param = getattr_path(additional_kwargs_namespace, name)
        except AttributeError:
            kwargs[name] = saved_param
        else:
            if is_reinvokable(saved_param):
                assert isinstance(new_param, dict)
                kwargs[name] = reinvoke_new_defaults(saved_param, new_param)
            else:
                if isinstance(saved_param, Namespace):
                    kwargs[name] = Namespace(new_param, saved_param)
                else:
                    kwargs[name] = saved_param

    additional_kwargs_namespace.pop('call_target', None)

    try:
        result = type(obj)(**kwargs)
    except TypeError as e:
        raise InvalidStyleConfigurationException(
            f'Object {obj!r} could not be updated with style configuration {flatten(additional_kwargs_namespace)}'
        ) from e

    retain_special_cases(obj, result)
    return result
示例#2
0
def apply_style_data(style_data: Namespace, obj: Any) -> Any:
    if not style_data:
        return obj
    if not is_reinvokable(obj):
        print(f'Missing out of {style_data} for {type(obj)}')
        return obj
    return reinvoke_new_defaults(obj, style_data)