Decorators¶
- nutcli.decorators.Identity(function)¶
Identity decorator. It does nothing to the decorated function.
It can be used to simplify the code.
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.
@IgnoreErrors def raise_error(ignore_errors): raise Exception('This will be ignored.') raise_error()
- class nutcli.decorators.Timeout(timeout=None, message=None)¶
Bases:
object
A
nutcli.exceptions.TimeoutError
is risen if the decoration function does not finish in time.The code is based on timeout-decorator: - https://github.com/pnpnpn/timeout-decorator
@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
timeout
may contains one of the following values:None
: No timeout is appliedint
: Number of seconds'X seconds Y minutes Z hours'
: Simple natural time specificationsX
,Y
andZ
can befloat
Each of
seconds
,minutes
andhours
can be omittedSuffix
s
can be omitted
Parameter
message
is passed tonutcli.exceptions.TimeoutError
constructor.
- class nutcli.decorators.LogExecution(message=None, printer=None, logger=None)¶
Bases:
object
If enabled, each call of decorated method will produce an info log message.
@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
printer
is None thennutcli.utils.LogExecutionPrinter
is used. Theprinter
will log the message usinglogger
.The default printer will either produce
message
if it is notNone
or 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.LogExecution
If 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
LogExecution
it will also produce a log execution message if it is enabled.@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
printer
is None thennutcli.utils.LogExecutionPrinter
is used. Theprinter
will log the message usinglogger
.The default printer will either produce
message
if it is notNone
or exact function call.Call to the decorated function will return
returns
if dry run is enabled.- classmethod dry_run(enabled=True)¶
Enable or disable dry run.
- Parameters
enabled (bool, optional) – The state, defaults to True