Utils

nutcli.utils.check_instance(obj, allowed_classes)

Check object instance and raise a ValueError if it is not instance of one of allowed_classes.

Parameters
  • obj (any) – Object to check.

  • allowed_classes (class or tuple of class) – Tuple of allowed classes.

Raises

ValueError – Object is not instance of any allowed class.

nutcli.utils.get_as_list(arg)

Return value as list.

  • list is returned as is

  • tuple is converted to list

  • anything else is returned as [arg]

Parameters

arg (any) – Value that will be returned as list.

Returns

Value as list.

Return type

list

nutcli.utils.dict_to_namespace(d)

Recursively convert dictionary into a namespace.

Parameters

d (dict) – The dictionary.

Returns

Namespace.

Return type

types.SimpleNamespace

class nutcli.utils.Colorize

Bases: object

Utilities to provide colorized output, using terminal colors.

You can use colorama package to obtain the color definitions.

Colors are enabled by default, you can disable the functionality with enabled(). All functions will return unchanged input if the colorization is disabled.

classmethod enabled(enabled)

Enable or disable colors.

Parameters

enabled (bool) – state

classmethod all(fmt, *args)

Surround fmt with provided colors.

Example usage
str = Colorize.all(
    'hello world', colorama.Fore.RED, colorama.Style.BRIGHT
)
print(str)
# -> Red and bold 'hello world'
Parameters
  • fmt (str) – String to format.

  • *args (str) – Color specifications.

Returns

Colorized string.

Return type

str

classmethod bold(fmt)

Make fmt bold.

Example usage
str = Colorize.bold('hello world')
print(str)
# -> Bold 'hello world'
Parameters

fmt (str) – String to format.

Returns

Colorized string.

Return type

str

classmethod re(fmt, pattern, *args)

Colorize all matches found in fmt using regular expression.

Example usage
str = Colorize.re('hello', r'(.*)', colorama.Style.RED)
print(str) # -> Red 'hello'

str = Colorize.re(
    'hello', r'h(e)ll(o)',
    colorama.Fore.RED,
    [colorama.Fore.BLUE, colorama.Style.BRIGHT]
)
print(str) # -> 'hello' with red 'e' and blue and bold 'o'
Parameters
  • fmt (str) – String to format.

  • pattern (str or type(re.compile(r''))) – Plain or compiled regular expression.

  • *args (str or list of str) – Color specifications. i-th match is colored with i-th element

Returns

Colorized string.

Return type

str

class nutcli.utils.LogExecutionPrinter(skip_self=True, logger=None)

Bases: object

Print exact function call to the information logs.

You can use this in conjunction with nutcli.decorators.LogExecution and nutcli.decorators.SideEffect.

Parameters
  • skip_self (bool, optional) – If True the first parameter named ‘self’ will be skipped, defaults to True

  • logger (logger, optional) – Logger, defaults to None (= nutcli.message)

__call__(message, function, args, kwargs)

Produce the log message.

Parameters
  • message (str) – The message.

  • function (callable) – Function that was called.

  • args (list of any) – Function positional arguments.

  • kwargs (dict of str:any) – Function keyword arguments.

If message is not None then only the message is printed to the log. Otherwise the function execution is produced.