Decorators¶
- nutcli.decorators.Identity(function)¶
Identity decorator. It does nothing to the decorated function.
It can be used to simplify the code.
Example usage¶def identity_example(ignore_errors): decorator = IgnoreErrors if ignore_errors else Identity @decorator def do_something(): pass
- nutcli.decorators.IgnoreErrors(function)¶
All exceptions raised from the decorated function will be ignored.
Example usage¶@IgnoreErrors def raise_error(ignore_errors): raise Exception('This will be ignored.') raise_error()
- class nutcli.decorators.Timeout(timeout=None, message=None)¶
Bases:
objectA
nutcli.exceptions.TimeoutErroris risen if the decoration function does not finish in time.The code is based on timeout-decorator: - https://github.com/pnpnpn/timeout-decorator
Example usage¶@Timeout(2) def be_slow(): time.sleep(5) try: be_slow() except nutcli.exceptions.TimeoutError as e: print(str(e))
- Parameters
timeout (int, str, optional) – Timeout in seconds or a simple time format, defaults to None.
message (str, optional) – Error message, defaults to None
Parameter
timeoutmay contains one of the following values:None: No timeout is appliedint: Number of seconds'X seconds Y minutes Z hours': Simple natural time specificationsX,YandZcan befloatEach of
seconds,minutesandhourscan be omittedSuffix
scan be omitted
Parameter
messageis passed tonutcli.exceptions.TimeoutErrorconstructor.
- class nutcli.decorators.LogExecution(message=None, printer=None, logger=None)¶
Bases:
objectIf enabled, each call of decorated method will produce an info log message.
Example usage¶@LogExecution() def example(): pass LogExecution.enabled(True) example() # -> INFO example()
- Parameters
message (str, optional) – Message to log, defaults to None
printer (
nutcli.utils.LogExecutionPrinter, optional) – Execution printer, defaults to Nonelogger (logger, optional) – Logger, defaults to None (=
nutcli.message)
If
printeris None thennutcli.utils.LogExecutionPrinteris used. Theprinterwill log the message usinglogger.The default printer will either produce
messageif it is notNoneor exact function call.- classmethod enabled(enabled=True)¶
Enable or disable execution logging.
- Parameters
enabled (bool, optional) – The state, defaults to True
- class nutcli.decorators.SideEffect(message=None, returns=None, printer=None, logger=None)¶
Bases:
nutcli.decorators.LogExecutionIf dry run is enabled the decorated function will not be run and only its execution is printed to the info log. The dry run can be enabled with
dry_run().Note
Since this inherits from
LogExecutionit will also produce a log execution message if it is enabled.Example usage¶@SideEffect() def example(): # remove files pass SideEffect.dry_run(True) example() # -> INFO example() # Files are not deleted, example() is not executed.
- Parameters
message (str, optional) – Message to log, defaults to None
returns (any, optional) – Return value of the decorated function if dry run is enabled, defaults to None
printer (
nutcli.utils.LogExecutionPrinter, optional) – Execution printer, defaults to LogExecutionPrinter()logger (logger, optional) – Logger, defaults to None (=
nutcli.message)
If
printeris None thennutcli.utils.LogExecutionPrinteris used. Theprinterwill log the message usinglogger.The default printer will either produce
messageif it is notNoneor exact function call.Call to the decorated function will return
returnsif dry run is enabled.- classmethod dry_run(enabled=True)¶
Enable or disable dry run.
- Parameters
enabled (bool, optional) – The state, defaults to True