actions
Actions module.
- class wmflib.actions.Actions(name: Hashable)[source]
Bases:
object
Class to keep track and log a set of actions performed and their result with a nice string representation.
The instance gets initialized with the given name, that can represent a host or any other identifier.
It allows to save actions performed on the given name with their status (success, warning, failure). It exposes the following public properties:
name
: the name passed to the instance at instantiation time.has_warnings
: abool
that isTrue
when at least one warning action was registered,False
otherwise.has_failures
: abool
that isTrue
when at least one failed action was registered,False
otherwise.
When converted to a string it returns a nicely formatted representation of the instance and all its actions that can be used as-is in Phabricator:
<name> (**<status>**) - <action1> - <action2> - ...
The status is enclosed in double asterisks to be rendered as bold in Phabricator.
Examples
actions = Actions("host1001") actions.success("Downtimed on Icinga") actions.success("Restarted ntp") print(actions)
The above code will print:
host1001 (**PASS**) - Downtimed on Icinga - Restarted ntp
- Parameters:
name (Hashable) – the name of the set of actions to be registered.
- __init__(name: Hashable)[source]
The instance gets initialized with the given name, that can represent a host or any other identifier.
It allows to save actions performed on the given name with their status (success, warning, failure). It exposes the following public properties:
name
: the name passed to the instance at instantiation time.has_warnings
: abool
that isTrue
when at least one warning action was registered,False
otherwise.has_failures
: abool
that isTrue
when at least one failed action was registered,False
otherwise.
When converted to a string it returns a nicely formatted representation of the instance and all its actions that can be used as-is in Phabricator:
<name> (**<status>**) - <action1> - <action2> - ...
The status is enclosed in double asterisks to be rendered as bold in Phabricator.
Examples
actions = Actions("host1001") actions.success("Downtimed on Icinga") actions.success("Restarted ntp") print(actions)
The above code will print:
host1001 (**PASS**) - Downtimed on Icinga - Restarted ntp
- Parameters:
name (Hashable) – the name of the set of actions to be registered.
- __str__() str [source]
Custom string representation of the actions performed.
- Returns:
the string representation.
- Return type:
- property status: str
Return the current status of the actions based on the worst result recorded.
- Returns:
FAIL
: if there was at least one failure action registeredWARN
if there was at least one warning action and noFAIL
actions registeredPASS
if only success actions were registered
- Return type:
- success(message: str) None [source]
Register a successful action, it gets also logged with info level.
- Parameters:
message (str) – the action description.
- failure(message: str) None [source]
Register a failed action, it gets also logged with error level.
- Parameters:
message (str) – the action description.
- warning(message: str) None [source]
Register an action that require some attention, it gets also logged with warning level.
- Parameters:
message (str) – the action description.
- __dict__ = mappingproxy({'__module__': 'wmflib.actions', '__doc__': 'Class to keep track and log a set of actions performed and their result with a nice string representation.', '__init__': <function Actions.__init__>, '__str__': <function Actions.__str__>, 'status': <property object>, 'success': <function Actions.success>, 'failure': <function Actions.failure>, 'warning': <function Actions.warning>, '_action': <function Actions._action>, '__dict__': <attribute '__dict__' of 'Actions' objects>, '__weakref__': <attribute '__weakref__' of 'Actions' objects>, '__annotations__': {'actions': 'List[str]'}})
- __doc__ = 'Class to keep track and log a set of actions performed and their result with a nice string representation.'
- __module__ = 'wmflib.actions'
- __weakref__
list of weak references to the object (if defined)
- class wmflib.actions.ActionsDict[source]
Bases:
dict
Custom dictionary with defaultdict capabilities for the
wmflib.actions.Action
class.Automatically instantiate and returns a new instance of the
wmflib.actions.Actions
class for every missing key like acollections.defaultdict
.When accessing a missing key, the key itself is passed to the new
wmflib.actions.Actions
instance asname
.When converted to string returns a nicely formatted representation of the instance and all its items, that can be used as-is in Phabricator.
Examples
actions = ActionsDict() actions["host1001"].success("Downtimed on Icinga") actions["host1001"].failure("**Failed to restart ntp**") # Will be rendered in bold in Phabricator actions["host2001"].warning("//Host with alerts on Icinga//") # Will be rendered in italic in Phabricator print(actions)
The above code will print:
- host1001 (**FAIL**) - Downtimed on Icinga - **Failed to restart ntp** - host2001 (**WARN**) - //Host not optimal on Icinga//
It will also include a final newline at the end of the block that doesn’t get rendered in this documentation.
- __missing__(key: Hashable) Actions [source]
Instantiate a new Actions instance for the missing key like a defaultdict.
Parameters as required by Python’s data model, see
object.__missing__()
.
- __str__() str [source]
Custom string representation of the instance.
- Returns:
the string representation of each item in the dictionary, newline-separated.
- Return type:
- __dict__ = mappingproxy({'__module__': 'wmflib.actions', '__doc__': 'Custom dictionary with defaultdict capabilities for the :py:class:`wmflib.actions.Action` class.\n\n Automatically instantiate and returns a new instance of the :py:class:`wmflib.actions.Actions` class for every\n missing key like a :py:class:`collections.defaultdict`.\n\n When accessing a missing key, the key itself is passed to the new :py:class:`wmflib.actions.Actions` instance\n as ``name``.\n\n When converted to string returns a nicely formatted representation of the instance and all its items, that can be\n used as-is in Phabricator.\n\n Examples:\n ::\n\n actions = ActionsDict()\n actions["host1001"].success("Downtimed on Icinga")\n actions["host1001"].failure("**Failed to restart ntp**") # Will be rendered in bold in Phabricator\n actions["host2001"].warning("//Host with alerts on Icinga//") # Will be rendered in italic in Phabricator\n print(actions)\n\n The above code will print::\n\n - host1001 (**FAIL**)\n - Downtimed on Icinga\n - **Failed to restart ntp**\n\n - host2001 (**WARN**)\n - //Host not optimal on Icinga//\n\n It will also include a final newline at the end of the block that doesn\'t get rendered in this documentation.\n\n ', '__missing__': <function ActionsDict.__missing__>, '__str__': <function ActionsDict.__str__>, '__dict__': <attribute '__dict__' of 'ActionsDict' objects>, '__weakref__': <attribute '__weakref__' of 'ActionsDict' objects>, '__annotations__': {}})
- __doc__ = 'Custom dictionary with defaultdict capabilities for the :py:class:`wmflib.actions.Action` class.\n\n Automatically instantiate and returns a new instance of the :py:class:`wmflib.actions.Actions` class for every\n missing key like a :py:class:`collections.defaultdict`.\n\n When accessing a missing key, the key itself is passed to the new :py:class:`wmflib.actions.Actions` instance\n as ``name``.\n\n When converted to string returns a nicely formatted representation of the instance and all its items, that can be\n used as-is in Phabricator.\n\n Examples:\n ::\n\n actions = ActionsDict()\n actions["host1001"].success("Downtimed on Icinga")\n actions["host1001"].failure("**Failed to restart ntp**") # Will be rendered in bold in Phabricator\n actions["host2001"].warning("//Host with alerts on Icinga//") # Will be rendered in italic in Phabricator\n print(actions)\n\n The above code will print::\n\n - host1001 (**FAIL**)\n - Downtimed on Icinga\n - **Failed to restart ntp**\n\n - host2001 (**WARN**)\n - //Host not optimal on Icinga//\n\n It will also include a final newline at the end of the block that doesn\'t get rendered in this documentation.\n\n '
- __module__ = 'wmflib.actions'
- __weakref__
list of weak references to the object (if defined)