You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
34 lines
831 B
Python
34 lines
831 B
Python
|
|
# Copyright (C) 2020 Intel Corporation
|
|
#
|
|
# SPDX-License-Identifier: MIT
|
|
|
|
import attr
|
|
|
|
_NOTSET = object()
|
|
|
|
def not_empty(inst, attribute, x):
|
|
assert len(x) != 0, x
|
|
|
|
def default_if_none(conv):
|
|
def validator(inst, attribute, value):
|
|
default = attribute.default
|
|
if value is None:
|
|
if callable(default):
|
|
value = default()
|
|
elif isinstance(default, attr.Factory):
|
|
value = default.factory()
|
|
else:
|
|
value = default
|
|
elif not isinstance(value, attribute.type or conv):
|
|
value = conv(value)
|
|
setattr(inst, attribute.name, value)
|
|
return validator
|
|
|
|
def ensure_cls(c):
|
|
def converter(arg):
|
|
if isinstance(arg, c):
|
|
return arg
|
|
else:
|
|
return c(**arg)
|
|
return converter |