frutils package

Submodules

frutils.defaults module

Default values for both frutils and the frkl-suite of tools (if appropriate).

frutils.frutils module

Utility methods that are used across the frkl-suite (https://frkl.io) of tools.

class frutils.frutils.IgnoreUndefinedJinjaVariable(hint=None, obj=missing, name=None, exc=<class 'jinja2.exceptions.UndefinedError'>)[source]

Bases: jinja2.runtime.Undefined

class frutils.frutils.StringYAML(**kwargs)[source]

Bases: ruamel.yaml.main.YAML

Wraps :class:~YAML to be able to dump a string from a yaml object.

More details: http://yaml.readthedocs.io/en/latest/example.html#output-of-dump-as-a-string

Parameters:**kwargs (dict) – arguments for the underlying :class:~YAML class
dump(data, stream=None, **kw)[source]
frutils.frutils.add_key_to_dict(target_dict, key_path, value, split_token='.', ordered=True)[source]

Add a key into the key path of a dictionary.

Parameters:
  • target_dict (dict) – the dictionary to add to
  • key_path (str) – the path to the value (e.g. key1.child_key.test)
  • value – the value to insert
  • split_token (str) – the character to split the key_path with.
  • ordered (bool) – whether to use OrderedDicts instead of ‘normal’ ones
frutils.frutils.append_key_to_dict(target_dict, key_path, value, split_token='.')[source]

Appends a key into the key path of a dictionary.

The value either needs to be a list or tuple, or nonexistent. If this is not the case, this function will throw an exception.

Parameters:
  • target_dict (dict) – the dictionary to add to
  • key_path (str) – the path to the value (e.g. key1.child_key.test)
  • value – the value to append
  • split_token (str) – the character to split the key_path with.
frutils.frutils.calculate_cache_location_for_url(url)[source]

Utility method to get a unique path that can be used for caching a download.

frutils.frutils.can_passwordless_sudo()[source]

Checks if the user can use passwordless sudo on this host.

frutils.frutils.dict_merge(dct, merge_dct, copy_dct=True)[source]

Recursive dict merge. Inspired by :meth:dict.update(), instead of updating only top-level keys, dict_merge recurses down into dicts nested to an arbitrary depth, updating keys. The merge_dct is merged into dct.

Copied from: https://gist.github.com/angstwad/bf22d1822c38a92ec0a9

Parameters:
  • dct (dict) – dict onto which the merge is executed
  • merge_dct (dict) – dct merged into dct
  • copy_dct (bool) – whether to (deep-)copy dct before merging (and leaving it unchanged), or not (default: copy)
Returns:

the merged dict (original or copied)

Return type:

dict

frutils.frutils.ensure_parent_dir(path)[source]

Makes sure a parent directory exists.

Parameters:path – the path to a file
Returns:the parent dir
Return type:str
frutils.frutils.flatten_lists(lists)[source]

Utility method to flatten a list of lists.

This will only flatten the first sublist, any deeper list-structure will be preserved.

Parameters:lists (list) – a list of lists
Returns:the flattened list
Return type:list
frutils.frutils.get_key_path_value(source_dict, key_path, split_token='.', default_value=None)[source]

Queries the source dict tree for the register key, split up using the split_token.

Parameters:
  • source_dict (dict) – the source dictionary
  • key_path (str) – a key-path, e.g. ‘key1.child_key1.test_key’
  • split_token (str) – the character to split the register_key value with
  • default_value – the default value to return if no key matches
Returns:

the value for the matching in the tree, or None

Return type:

object

frutils.frutils.is_list_of_strings(input_obj)[source]

Helper method to determine whether an object is a list or tuple of only strings (or string_types).

Parameters:input_obj (object) – the object in question
Returns:whether or not the object is a list of strings
Return type:bool
frutils.frutils.is_templated(text, jinja_delimiter_profile={'block_end_string': '%}', 'block_start_string': '{%', 'variable_end_string': '}}', 'variable_start_string': '{{'})[source]

Utility method to determine whether a string has template markers in it.

This is pretty simplistic, it only checks whether one of the template marker strings (e.g. ‘}}’, or ‘{%’) are contained in the text. It doesn’t check for matching opening/ closed brackets etc.

Parameters:
  • text (str) – the text in question
  • jinja_delimiter_profile (dict) – a dictionary with
Returns:

whether the text is templated or not

Return type:

bool

frutils.frutils.is_url_or_abbrev(url, abbrevs={'bb': ['https://bitbucket.org', '/', -9876, '/', -9876, '/', 'src', '/', '{{ branch }}', '/'], 'gh': ['https://raw.githubusercontent.com', '/', -9876, '/', -9876, '/', '{{ branch }}', '/'], 'gl': ['https://gitlab.com', '/', -9876, '/', -9876, '/', 'raw', '/', '{{ branch }}', '/']})[source]
frutils.frutils.list_of_special_dicts_to_list_of_dicts(value)[source]
frutils.frutils.merge_list_of_dicts(dicts, starting_dict=None)[source]

Merges a list of dicts.

Parameters:
  • dicts (list) – list of dicts to be merged in order
  • starting_dict (dict) – (optional) existing dict where the others are merged into
Returns:

the merged dict (same as starting_dict)

Return type:

dict

frutils.frutils.ordered_load(text)[source]

Loads a yaml stream into an OrderedDict

frutils.frutils.readable(python_object, out='raw', safe=True, indent=0)[source]

Utility method to print out readable strings from python objects (mostly dicts).

Parameters:
  • python_object (obj) – the object to print
  • out (str) – the format of the output (available: ‘yaml’, ‘json’, ‘raw’, and ‘pformat’)
  • safe (bool) – whether to use a ‘safe’ way of converting to string (if available in the output format type)
  • indent (int) – the indentation (optional)
frutils.frutils.readable_json(python_object, indent=0)[source]

Shortcut for using the readable() method with the ‘json’ format.

frutils.frutils.readable_pformat(python_object, indent=0)[source]

Shortcut for using the readable() method with the ‘pformat’ format.

frutils.frutils.readable_raw(python_object, indent=0)[source]

Shortcut for using the readable() method with the ‘raw’ format.

frutils.frutils.readable_yaml(python_object, indent=0, safe=True)[source]

Shortcut for using the readable() method with the ‘yaml’ format.

frutils.frutils.reindent(s, numSpaces, keep_current=True)[source]

Reindents a string.

Parameters:
  • s (str) – the string
  • numSpaces (int) – the indent
  • keep_current (bool) – keep a potential current indention and add to it
Returns:

the indented string

Return type:

str

frutils.frutils.replace_string(template_string, replacement_dict=None, block_start_string='{%', block_end_string='%}', variable_start_string='{{', variable_end_string='}}', additional_jinja_extensions=None, local_env_vars_key='LOCAL_ENV', ignore_undefined=False)[source]

Replace template markers with values from a replacement dictionary within a string.

Parameters:
  • template_string (str) – the template string
  • replacement_dict (dict) – the dictionary with the replacement strings
  • block_start_string (str) – the string to indicate a template block start
  • block_end_string (str) – the string to indicate a template block end
  • variable_start_string (str) – the string to indicate a template variable start
  • variable_end_string (str) – the string to indicate a template variable end
  • additional_jinja_extensions (list) – a list of jinja extensions to use
  • local_env_vars_key (str) – the key to use under which to put local environment variables
  • ignore_undefined (bool) – whether to skip replacement when encountering undefined variables (True), or error out (False)
frutils.frutils.special_dict_to_dict(value)[source]

Converts any ‘special’ dict (like CommentedMap, OrderedDict) to a ‘normal’ one.

Parameters:value (dict) – the ‘special’ dict
Returns:the ‘normal’ dict
Return type:dict

frutils.frutils_cli module

Module contents

Top-level package for frutils.