Utilities

Compile

The compile module converts instructions from pythonic data structures into flat lists.abs

These may require further processing before being passed to the enact module for action.

pyqubes.compile.flags_boolean(flags)[source]

Return a list of string values, corresponding to the given keys whose values evaluate to True

All keys and values will be converted to strings.

Parameters:flags (dict) – A dictionary in the form {'--flag': boolean}, where boolean is used to determine whether --flag is included in the output.
Returns:A flat list of strings
pyqubes.compile.flags_store(flags)[source]

Return a list of string values, corresponding to the given keys and values whose values evaluate to True

The output is a flat list of all strings.

All keys and values will be converted to strings.

Parameters:flags (dict) – A dictionary in the form {'--flag': value}, where value is used to determine whether the entry is included in the output.
Returns:A flat list of strings
pyqubes.compile.flags_store_iterable(flags)[source]

Calls flags_store for each value within each key in flags.

e.g. {‘–fruits’: [‘apple’, ‘pear’]} results in [‘–fruits’, ‘apple’, ‘–fruits’, ‘pear’]

Parameters:flags (dict) – A dictionary in the form {'--flag': value}, where value is an iterable.
Returns:A flat list of strings
Raises:TypeError if values are not iterable
pyqubes.compile.info(info, quote=True, style=True)[source]

Returns the given string info as a set of echo arguments.

Optionally provides quoting and terminal styling.

Parameters:
  • info (string) – Info string to add to script
  • quote (bool) – By default quote given sting in single quotes
  • style (bool) – By default add
Returns:

A flat list of strings

Constants

Enact

The enact module contains functions that act on a list of command-line arguments,

The two most important ones are: * Direct execution with call (proactive mode) * Echoing an execution-ready script with echo (reactive mode)

pyqubes.enact.call(args, **kwargs)[source]

Thin wrapper for builtin subprocess.call

pyqubes.enact.call_quiet(args)[source]

Uses the call function, but throws away stdout and stderr.

Should be used for internal unit tests wherever possible.

pyqubes.enact.echo(args, file=None)[source]

Echo a list of arguments (as given to subprocess.call) to the given stream.

This defaults to stdout, but can be changed to any stream-like object such as a file handle.

Parameters:
  • args – A string or list of strings
  • file – A file-like object to stream output to. Defaults to sys.stdout

Utils

Utility functions for pyqubes

Utilities have no dependencies.

pyqubes.utils.assert_list_items_equal_in_nested(actual_nested, expected_list)[source]

Assert that the given expected_list matches one pf the lists within actual_nested, using the comparison sorted(list)

Parameters:
  • actual_nested (list) – A list of lists (usually generated by test)
  • expected_list (list) – A list of expected values
pyqubes.utils.flatten_list(nested_list)[source]

Flatten a nested list one level.

Parameters:nested_list (list) – A nested list
Return type:list
pyqubes.utils.object_fullname(obj)[source]

Returns the full absolute name of the object provided

pyqubes.utils.object_logger(obj)[source]

Returns a correctly named logger for the given object.

Call as self.logger = object_logger(self)

Validate

Validate functions for pyqubes

These will return the original value if validation passes. Otherwise, ValueError will be raised

pyqubes.validate.firewall_policy(policy)[source]

qvm-firewall policy string should match ^(allow|deny)$

Parameters:policy (string) – Policy string to check
Returns:policy if valid, else ValueError
pyqubes.validate.label_color(color)[source]

VM label color should be one of: * red * orange * yellow * green * blue * purple * black * gray

Parameters:color (string) – Label color string to check
Returns:color if valid, else ValueError
pyqubes.validate.linux_hostname(hostname)[source]

Linux hostnames are recommended to match ^[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*$

Parameters:hostname (string) – Hostname to check
Returns:hostname if valid, else ValueError
pyqubes.validate.linux_username(username)[source]

Linux usernames are recommended to match ^[a-z_][a-z0-9_-]*\$?$

Parameters:username (string) – Username to check
Returns:username if valid, else ValueError