Commits vergleichen
2 Commits
768713aadc
...
8dbb67faf8
Autor | SHA1 | Datum |
---|---|---|
Sebastian Tobie | 8dbb67faf8 | |
Sebastian Tobie | e089dbc82a |
|
@ -1,7 +1,7 @@
|
|||
---
|
||||
namespace: sebastian
|
||||
name: base
|
||||
version: 0.2.0
|
||||
version: 0.3.0
|
||||
|
||||
readme: README.md
|
||||
|
||||
|
|
|
@ -19,36 +19,90 @@ SYSTEMD_SERVICE_CONFIG = SYSTEMD_CONFIG_ROOT / "system"
|
|||
AnsibleParameter = Dict[str, Any]
|
||||
|
||||
|
||||
class _Type:
|
||||
def __dir__(self) -> tuple: # pragma: nocover
|
||||
return (
|
||||
"str",
|
||||
"bool",
|
||||
"int",
|
||||
"float",
|
||||
"path",
|
||||
"raw",
|
||||
"jsonarg",
|
||||
"json",
|
||||
"bytes",
|
||||
"dict",
|
||||
"list",
|
||||
"bits",
|
||||
"__doc__",
|
||||
def wrap_func(func, **updates):
|
||||
def wrapper(*args, **kwargs):
|
||||
return func(*args, **kwargs)
|
||||
|
||||
attrs = frozenset(
|
||||
('__module__', '__name__', '__qualname__', '__doc__', '__annotations__', '__type_params__')
|
||||
) - frozenset(updates.keys())
|
||||
for attr in attrs:
|
||||
try:
|
||||
value = getattr(func, attr)
|
||||
except AttributeError:
|
||||
pass
|
||||
else:
|
||||
print(f"changing:\n {value}\n {getattr(wrapper, attr)}")
|
||||
setattr(wrapper, attr, value)
|
||||
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(
|
||||
self,
|
||||
elements: Union[Type[object], str, AnsibleParameter],
|
||||
required: bool = False,
|
||||
help: Optional[str] = None,
|
||||
) -> 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)
|
||||
if not isinstance(elements, (str, dict)):
|
||||
option["elements"] = elements.__name__
|
||||
|
@ -67,37 +121,20 @@ class _Type:
|
|||
option["description"] = help.split("\n")
|
||||
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["option"] = options
|
||||
if help is not None:
|
||||
option["description"] = help.split("\n")
|
||||
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:
|
||||
"""Converts values into things systemd can parse"""
|
||||
|
|
|
@ -1 +1 @@
|
|||
__version__ = "0.2.0"
|
||||
__version__ = "0.3.0"
|
||||
|
|
|
@ -8,6 +8,8 @@ upload(){
|
|||
printf "uploading: %s as %s\n" "$1" "$name"
|
||||
curl -u sebastian --upload-file "$1" "https://gitea.sebastian-tobie.de/api/packages/ansible/generic/${package}/${version}/$name"
|
||||
}
|
||||
for file in dist/* ; do
|
||||
for file in dist/sebastian* ; do
|
||||
upload "$file"
|
||||
done
|
||||
rm -f dist/sebastian*
|
||||
hatch publish -r ansible
|
||||
|
|
Laden…
In neuem Issue referenzieren