rewrote the Types class
Dieser Commit ist enthalten in:
Ursprung
768713aadc
Commit
e089dbc82a
|
@ -1,7 +1,7 @@
|
||||||
---
|
---
|
||||||
namespace: sebastian
|
namespace: sebastian
|
||||||
name: base
|
name: base
|
||||||
version: 0.2.0
|
version: 0.3.0
|
||||||
|
|
||||||
readme: README.md
|
readme: README.md
|
||||||
|
|
||||||
|
|
|
@ -19,36 +19,90 @@ SYSTEMD_SERVICE_CONFIG = SYSTEMD_CONFIG_ROOT / "system"
|
||||||
AnsibleParameter = Dict[str, Any]
|
AnsibleParameter = Dict[str, Any]
|
||||||
|
|
||||||
|
|
||||||
class _Type:
|
def wrap_func(func, **updates):
|
||||||
def __dir__(self) -> tuple: # pragma: nocover
|
def wrapper(*args, **kwargs):
|
||||||
return (
|
return func(*args, **kwargs)
|
||||||
"str",
|
|
||||||
"bool",
|
attrs = frozenset(
|
||||||
"int",
|
('__module__', '__name__', '__qualname__', '__doc__', '__annotations__', '__type_params__')
|
||||||
"float",
|
) - frozenset(updates.keys())
|
||||||
"path",
|
for attr in attrs:
|
||||||
"raw",
|
try:
|
||||||
"jsonarg",
|
value = getattr(func, attr)
|
||||||
"json",
|
except AttributeError:
|
||||||
"bytes",
|
pass
|
||||||
"dict",
|
else:
|
||||||
"list",
|
print(f"changing:\n {value}\n {getattr(wrapper, attr)}")
|
||||||
"bits",
|
setattr(wrapper, attr, value)
|
||||||
"__doc__",
|
for attr, value in updates.items():
|
||||||
|
setattr(wrapper, attr, value)
|
||||||
|
wrapper.__dict__.update(func.__dict__)
|
||||||
|
setattr(wrapper, "__wrapped__", func)
|
||||||
|
return wrapper
|
||||||
|
|
||||||
|
|
||||||
|
GENERIC_DOC = """Returns an dictionary for the Ansible {type} type."""
|
||||||
|
|
||||||
|
|
||||||
|
def default(name: str):
|
||||||
|
def wrapped(
|
||||||
|
required: bool = False,
|
||||||
|
help: Optional[str] = None,
|
||||||
|
choices: Optional[Sequence] = None,
|
||||||
|
default: Optional[Any] = None,
|
||||||
|
) -> AnsibleParameter:
|
||||||
|
option: AnsibleParameter = dict(type=name, required=required)
|
||||||
|
if choices is not None:
|
||||||
|
option["choices"] = choices
|
||||||
|
if default is not None:
|
||||||
|
option["default"] = default
|
||||||
|
if help is not None:
|
||||||
|
option["description"] = help.split("\n")
|
||||||
|
return option
|
||||||
|
|
||||||
|
return wrapped
|
||||||
|
|
||||||
|
|
||||||
|
class meta(type):
|
||||||
|
def __new__(cls, clsname, bases, attrs):
|
||||||
|
types = frozenset(
|
||||||
|
(
|
||||||
|
"str",
|
||||||
|
"bool",
|
||||||
|
"int",
|
||||||
|
"float",
|
||||||
|
"path",
|
||||||
|
"raw",
|
||||||
|
"jsonarg",
|
||||||
|
"json",
|
||||||
|
"bytes",
|
||||||
|
"dict",
|
||||||
|
"list",
|
||||||
|
"bits",
|
||||||
|
)
|
||||||
)
|
)
|
||||||
|
for attr in types - set(attrs.keys()):
|
||||||
|
print(f"adding {attr} function")
|
||||||
|
attrs[attr] = wrap_func(
|
||||||
|
default(attr), __doc__=GENERIC_DOC.format(type=attr), __name__=attr, __qualname__=f"{clsname}.{attr}"
|
||||||
|
)
|
||||||
|
attrs["__slots__"] = ()
|
||||||
|
return super().__new__(cls, clsname, bases, attrs)
|
||||||
|
|
||||||
def __repr__(self) -> str: # pragma: nocover
|
|
||||||
return "Types()"
|
|
||||||
|
|
||||||
def __call__(self) -> "_Type": # pragma: nocover
|
|
||||||
return self
|
|
||||||
|
|
||||||
|
class Types(metaclass=meta):
|
||||||
def list(
|
def list(
|
||||||
self,
|
|
||||||
elements: Union[Type[object], str, AnsibleParameter],
|
elements: Union[Type[object], str, AnsibleParameter],
|
||||||
required: bool = False,
|
required: bool = False,
|
||||||
help: Optional[str] = None,
|
help: Optional[str] = None,
|
||||||
) -> AnsibleParameter:
|
) -> AnsibleParameter:
|
||||||
|
"""Wrapper for the Ansible list type
|
||||||
|
|
||||||
|
Args:
|
||||||
|
elements: The type of the elements
|
||||||
|
required: if the item is absolutly required
|
||||||
|
help: an helptext for the ansible-doc
|
||||||
|
"""
|
||||||
option: AnsibleParameter = dict(type="list", required=required)
|
option: AnsibleParameter = dict(type="list", required=required)
|
||||||
if not isinstance(elements, (str, dict)):
|
if not isinstance(elements, (str, dict)):
|
||||||
option["elements"] = elements.__name__
|
option["elements"] = elements.__name__
|
||||||
|
@ -67,37 +121,20 @@ class _Type:
|
||||||
option["description"] = help.split("\n")
|
option["description"] = help.split("\n")
|
||||||
return option
|
return option
|
||||||
|
|
||||||
def dict(self, required: bool = False, help: Optional[str] = None, **options: dict) -> AnsibleParameter:
|
def dict(required: bool = False, help: Optional[str] = None, **options: AnsibleParameter) -> AnsibleParameter:
|
||||||
|
"""Wrapper for the Ansible dict type
|
||||||
|
|
||||||
|
Args:
|
||||||
|
required: if the item is absolutly required
|
||||||
|
help: an helptext for the ansible-doc
|
||||||
|
options: The individual options that this parameter has
|
||||||
|
"""
|
||||||
option: AnsibleParameter = dict(type="dict", required=required)
|
option: AnsibleParameter = dict(type="dict", required=required)
|
||||||
option["option"] = options
|
option["option"] = options
|
||||||
if help is not None:
|
if help is not None:
|
||||||
option["description"] = help.split("\n")
|
option["description"] = help.split("\n")
|
||||||
return option
|
return option
|
||||||
|
|
||||||
def __getattr__(self, name: str):
|
|
||||||
def argument(
|
|
||||||
required: bool = False,
|
|
||||||
help: Optional[str] = None,
|
|
||||||
choices: Optional[Sequence] = None,
|
|
||||||
default: Optional[Any] = None,
|
|
||||||
) -> AnsibleParameter:
|
|
||||||
option: AnsibleParameter = dict(type=name, required=required)
|
|
||||||
if choices is not None:
|
|
||||||
option["choices"] = choices
|
|
||||||
if default is not None:
|
|
||||||
option["default"] = default
|
|
||||||
if help is not None:
|
|
||||||
option["description"] = help.split("\n")
|
|
||||||
return option
|
|
||||||
|
|
||||||
argument.__name__ = name
|
|
||||||
argument.__qualname__ = f"Types.{name}"
|
|
||||||
argument.__doc__ = f"Simple wrapper for Ansible {name} argument dict"
|
|
||||||
return argument
|
|
||||||
|
|
||||||
|
|
||||||
Types = _Type()
|
|
||||||
|
|
||||||
|
|
||||||
def systemdbool(b: Union[bool, str]) -> str:
|
def systemdbool(b: Union[bool, str]) -> str:
|
||||||
"""Converts values into things systemd can parse"""
|
"""Converts values into things systemd can parse"""
|
||||||
|
|
|
@ -1 +1 @@
|
||||||
__version__ = "0.2.0"
|
__version__ = "0.3.0"
|
||||||
|
|
Laden…
In neuem Issue referenzieren