1. API Reference#

1.1. Rickle#

1.1.1. BaseRickle#

The BaseRickle forms the basis for extended versions. This basis includes important methods for manipulating data, and formulating the internal representation.

  • dict()

  • items()

  • get()

  • set()

  • remove()

  • values()

  • keys()

  • has()

  • search_path()

class rickle.__init__.BaseRickle(base: dict | str | TextIOWrapper | list | None = None, deep: bool = False, strict: bool = True, **init_args)#

A base class that creates internal structures from embedded structures.

Parameters:
  • base (str,dict,TextIOWrapper, list) – String (YAML or JSON, file path to YAML/JSON file, URL), text IO stream, dict (default = None).

  • deep (bool) – Internalize dictionary structures in lists (default = False).

  • strict (bool) – Check keywords, if YAML/JSON key is Rickle keyword (or member of object) raise ValueError (default = True).

  • **init_args (kw_args) – Additional arguments for string replacement

Raises:

ValueError – If the given base object can not be handled. Also raises if YAML key is already member of Rickle.

add(name, value)#

Add a new key (attribute) and value member to Rick.

Parameters:
  • name (str) – Key / property name.

  • value (any) – Value of new key.

dict(serialised: bool = False)#

Deconstructs the whole object into a Python dictionary.

Parameters:

serialised (bool) – Give a Python dictionary in serialised (True) form or deserialised (default = False).

Notes

Functions and lambdas are always given in serialised form.

Returns:

of object.

Return type:

dict

static flatten_dict(dictionary, path_sep: str | None = None, list_brackets: tuple = ('(', ')'))#

Flattens a deepl structure python dictionary into a shallow (or ‘thin’) dictionary of depth 1.

Notes

Dictionary can only contain types str, bool, int, float, dict, list. Any other types won’t be expanded upon.

Parameters:
  • dictionary (dict) – Input dictionary.

  • path_sep (str) – Path separator.

  • list_brackets (tuple) – Tuple of strings for list index values (default = (‘(’, ‘)’)).

Returns:

Flattened to depth 1.

Return type:

dict

get(key: str, default=None, do_recursive: bool = False)#

Acts as a regular get from a dictionary but can employ a recursive search of structure and returns the first found key-value pair.

Note

Document paths like ‘/root/to/path’ can also be used. If the path can not be traversed, the default value is returned.

Parameters:
  • key (str) – key string being searched.

  • default (any) – Return value if nothing is found.

  • do_recursive (bool) – Search recursively until first match is found (default = False).

Returns:

value found, or None for nothing found.

Return type:

obj

has(key: str, deep=False) bool#

Checks whether the key exists in the object.

Parameters:
  • key (str) – key string being searched.

  • deep (bool) – whether to search deeply (default = False).

Returns:

if found.

Return type:

bool

static inflate_dict(flat_dict: dict, path_sep: str | None = None, list_brackets: tuple = ('(', ')'))#

Does reverse operation of flatten_dict and inflates a shallow dictionary.

Parameters:
  • flat_dict (dict) – Input dictionary, can be any dict (won’t have an effect).

  • path_sep (str) – Path separator.

  • list_brackets (tuple) – Tuple of strings for list index values (default = (‘(’, ‘)’)).

Returns:

Inflated dictionary.

Return type:

dict

items()#

Iterate through all key value pairs. Rickle is destructed into dict.

Yields:

tuple – str, object.

keys()#

Gets the higher level keys of the current Rick object.

Returns:

of keys.

Return type:

list

meta(name: str | None = None)#

Get the metadata for a property.

Parameters:

name (str) – The name of the property (default = None).

Returns:

The metadata as a dict.

Return type:

dict

remove(key: str)#

Removes using path.

Parameters:

key (str) – Path to key-value to be removed.

search_path(key: str) list#

Search the current Rickle for all paths that match the search key. Returns empty list if nothing is found.

Parameters:

key (str) – The key to search.

Returns:

all paths found.

Return type:

list

set(key: str, value)#

As with the get method, this method can be used to update the inherent dictionary with new values.

Note

Document paths like ‘/root/to/path’ can also be used. If the path can not be traversed, an error is raised.

Parameters:
  • key (str) – key string to set.

  • value – Any Python like value that can be deserialised.

to_ini(output: str | TextIOWrapper | None = None, serialised: bool = False, encoding: str = 'utf-8', path_sep: str | None = None, list_brackets: tuple | None = None)#

Flattens self into a thin dict and does a dump to INI string or file.

Parameters:
  • output (str, TextIOWrapper) – File path or stream (default = None).

  • serialised (bool) – Give a Python dictionary in serialised (True) form or deserialised (default = False).

  • encoding (str) – Output stream encoding (default = ‘utf-8’).

  • path_sep (str) – For flattened dictionary, use path separator or default (default = None).

  • list_brackets (tuple) – Tuple of strings, defining opening and closing brackets of list indexes (default = None).

Notes

Functions and lambdas are always given in serialised form. IO stream “output” needs to be BytesIO object

to_json(output: str | TextIOWrapper | None = None, serialised: bool = False, encoding: str = 'utf-8')#

Does a self dump to a JSON file or returns as string.

Parameters:
  • output (str, TextIOWrapper) – File path or stream (default = None).

  • serialised (bool) – Give a Python dictionary in serialised (True) form or deserialised (default = False).

  • encoding (str) – Output stream encoding (default = ‘utf-8’).

Notes

Functions and lambdas are always given in serialised form.

to_toml(output: str | BytesIO | None = None, serialised: bool = False, encoding: str = 'utf-8')#

Does a self dump to a TOML file or returns as string.

Parameters:
  • output (str, BytesIO) – File path or stream (default = None).

  • serialised (bool) – Give a Python dictionary in serialised (True) form or deserialised (default = False).

  • encoding (str) – Output stream encoding (default = ‘utf-8’).

Notes

Functions and lambdas are always given in serialised form. IO stream “output” needs to be BytesIO object

to_xml(output: str | BytesIO | None = None, serialised: bool = False, encoding: str = 'utf-8')#

Does a self dump to a XML file or returns as string.

Parameters:
  • output (str, BytesIO) – File path or stream (default = None).

  • serialised (bool) – Give a Python dictionary in serialised (True) form or deserialised (default = False).

  • encoding (str) – Output stream encoding (default = ‘utf-8’).

Notes

Functions and lambdas are always given in serialised form. IO stream “output” needs to be BytesIO object

to_yaml(output: str | TextIOWrapper | None = None, serialised: bool = False, encoding: str = 'utf-8')#

Does a self dump to a YAML file or returns as string.

Parameters:
  • output (str, TextIOWrapper) – File path or stream (default = None).

  • serialised (bool) – Give a Python dictionary in serialised (True) form or deserialised (default = False).

  • encoding (str) – Output stream encoding (default = ‘utf-8’).

Notes

Functions and lambdas are always given in serialised form.

values()#

Gets the higher level values of the current Rick object.

Returns:

of objects.

Return type:

list

1.1.2. Rickle#

The extended version of BaseRickle that allows for easy loading of external data such as OS environmental variables, files, JSON responses from APIs. Contains all the same methods in BasicRick such as search_path etc.

class rickle.__init__.Rickle(base: dict | str | TextIOWrapper | list | None = None, deep: bool = False, load_lambda: bool = False, strict: bool = True, **init_args)#

An extended version of the BasicRick that can load OS environ variables and Python functions.

Parameters:
  • base (str, list) – String (YAML or JSON, file path to YAML/JSON file) or list of file paths, text IO stream, dict.

  • deep (bool) – Internalize dictionary structures in lists.

  • load_lambda (bool) – Load lambda as code or strings.

  • strict (bool) – Check keywords, if YAML/JSON key is Rickle keyword (or member of object) raise ValueError (default = True).

  • **init_args (kw_args) – Additional arguments for string replacement

Raises:

ValueError – If the given base object can not be handled. Also raises if YAML key is already member of Rickle.

add_api_json(name, url: str, http_verb: str = 'GET', headers: dict | None = None, params: dict | None = None, body: dict | None = None, load_as_rick: bool = False, deep: bool = False, load_lambda: bool = False, expected_http_status: int = 200, hot_load: bool = False)#

Load a JSON response from a URL and create a Rick from it. This opens up dynamic possibility, but with that it also opens up extreme security vulnerabilities. Only ever load JSON objects from trusted sources. Important note: Even with ``deep`` and ``load_lambda`` set to False, further API calls could be found within the source that loads lambda functions. Important note: Be careful to never self-reference an API call, i.e. don’t load the same API from within itself to avoid infinte looping.

Parameters:
  • name (str) – Property name.

  • url (str) – URL to load from.

  • http_verb (str) – Either ‘POST’ or ‘GET’ allowed (default = ‘GET’).

  • headers (dict) – Key-value pair for headers (default = None).

  • params (dict) – Key-value pair for parameters (default = None).

  • body (dict) – Key-value pair for data (default = None).

  • load_as_rick (bool) – If true, loads and creates Rick from source, else loads the contents as dictionary (default = False).

  • deep (bool) – Internalize dictionary structures in lists (default = False).

  • load_lambda (bool) – Load lambda as code or strings (default = False).

  • expected_http_status (int) – Should a none 200 code be expected (default = 200).

  • hot_load (bool) – Load the data on calling or load it only once on start (cold) (default = False).

add_base64(name, load)#

Add Base 64 encoded byte string data.

Parameters:
  • name (str) – Property name.

  • load (str) – Base 64 encoded data.

add_csv(name, file_path_or_str: str, fieldnames: list | None = None, load_as_rick: bool = False, encoding: str = 'utf-8')#

Adds the ability to load CSV data as lists or even a list of Ricks where the column names are the properties.

Parameters:
  • name (str) – Property name.

  • file_path_or_str (str) – File path to load from, or CSV string.

  • fieldnames (list) – Column headers (default = None).

  • load_as_rick (bool) – If true, loads and creates Rick from source, else loads the contents as text (default = False).

  • encoding (str) – If text, encoding can be specified (default = ‘utf-8’).

add_env(name, load, default=None)#

Add a new OS ENVIRONMENT VARIABLE to Rick.

Parameters:
  • name (str) – Property name.

  • load (str) – ENV var name.

  • default (any) – Default to value (default = None).

add_file(name, file_path: str, load_as_rick: bool = False, deep: bool = False, load_lambda: bool = False, is_binary: bool = False, encoding: str = 'utf-8', hot_load: bool = False)#

Adds the ability to further load Ricks from other YAML or JSON files, or alternatively load a text file. This opens up dynamic possibility, but with that it also opens up extreme security vulnerabilities. Only ever load files from trusted sources. Important note: Even with ``deep`` and ``load_lambda`` set to False, further file or API calls could be found within the source that loads lambda functions. Important note: Be careful to never self-reference a file, i.e. don’t load the same file from within itself to avoid infinte looping.

Parameters:
  • name (str) – Property name.

  • file_path (str) – File path to load from.

  • load_as_rick (bool) – If true, loads and creates Rick from source, else loads the contents as text (default = False).

  • deep (bool) – Internalize dictionary structures in lists (default = False).

  • load_lambda (bool) – Load lambda as code or strings (default = False).

  • is_binary (bool) – If the file is a binary file (default = False).

  • encoding (str) – If text, encoding can be specified (default = ‘utf-8’).

  • hot_load (bool) – Load the data on calling or load it only once on start (cold) (default = False).

add_html_page(name, url: str, headers: dict | None = None, params: dict | None = None, expected_http_status: int = 200, hot_load: bool = False)#

Loads HTML page as property.

Parameters:
  • name (str) – Property name.

  • url (str) – URL to load from.

  • headers (dict) – Key-value pair for headers (default = None).

  • params (dict) – Key-value pair for parameters (default = None).

  • expected_http_status (int) – Should a none 200 code be expected (default = 200).

  • hot_load (bool) – Load the data on calling or load it only once on start (cold) (default = False).

add_random_value(name, value_type: str, value_properties: dict | None = None, hot_load: bool = False)#

Adds a completely random value, useful for generating mock data.

Notes

integer: Properties include min and max. Defaults to 0 and 256. number: Properties include min and max. Defaults to 0 and 256. string: Properties include chars and length. Defaults to ASCII chars and 10. enum: Properties include values. Defaults to ASCII uppercase chars. array: Properties include values and length. Defaults to ‘integer’ and 10.

values can be a string of value_type.

object: Properties include keys, values, min, and max.

Defaults to random ASCII uppercase and 10 random integers, min and max of 1 and 5. values can be a string of value_type.

Parameters:
  • name (str) – Key / property name.

  • value_type (str) – Either ‘string’, ‘integer’, ‘number’, ‘enum’, ‘array’, ‘object’, or ‘any’.

  • value_properties (dict) – Extra properties defining what the randomly generated value should look like.

  • hot_load (bool) – Load the data on calling or load it only once on start (cold) (default = False).

add_secret(name, secret_id: str, provider: str, provider_access_key: str | dict, secret_version: str | None = None, load_as_rick: bool = False, deep: bool = False, load_lambda: bool = False, hot_load: bool = False)#

Adds a secret from a cloud provider. Providers include ASW, Google, Azure, and Hashicorp.

Note

The provider_access_key can either be a string or dict. A string can either be a JSON (or YAML) string or a file path to an access key file.

Parameters:
  • name (str) – Property name.

  • secret_id (str) – The ID or name of the secret in the secret manager / key vault.

  • provider (str) – Either ‘aws’, ‘google’, ‘azure’.

  • provider_access_key (dict, str) – Key/secrets or other access information. Dependent on provider.

  • secret_version (str) – Version ID of the secret (default = None).

  • load_as_rick (bool) – If true, loads and creates Rick from source, else loads the contents as dictionary (default = False).

  • deep (bool) – Internalize dictionary structures in lists (default = False).

  • load_lambda (bool) – Load lambda as code or strings (default = False).

  • hot_load (bool) – Load the data on calling or load it only once on start (cold) (default = False).

dict(serialised: bool = False)#

Deconstructs the whole object into a Python dictionary.

Parameters:

serialised (bool) – Give a Python dictionary in serialised (True) form or deserialised (default = False).

Notes

Functions and lambdas are always given in serialised form.

Returns:

of object.

Return type:

dict

1.1.3. UnsafeRickle#

class rickle.__init__.UnsafeRickle(base: dict | str | TextIOWrapper | list | None = None, deep: bool = False, load_lambda: bool = False, strict: bool = True, **init_args)#
add_class_definition(name, attributes, imports: list | None = None)#

Adds a class definition, with attributes such as functions and lambdas.

Parameters:
  • name (str) – Property name.

  • attributes (dict) – Standard items or Rickle function definitions.

  • imports (list) – Python modules to import (default = None).

add_function(name, load, args: dict | None = None, imports: list | None = None, return_function: bool = False, is_method: bool = False)#

Add a new function to Rick.

Parameters:
  • name (str) – Property name.

  • load (str) – Python code containing the function.

  • args (dict) – Key-value pairs of arguments with default values (default = None).

  • imports (list) – Python modules to import (default = None).

  • return_function (bool) – Add to rickle or return the function (default = False).

  • is_method (bool) – Indicates whether class method source includes self (default = False).

Examples

Basic example for adding to a PickleRick:

>> test_rick = PickleRick()

>> load = ‘’’
def tester(x, c):

y = x * 2 + c return math.cos(y)

‘’’

>> args = { ‘x’ : 0.42, ‘c’ : 1.7 }

>> imports = [‘math’]

>> test_rick.add_function(‘tester’,load, args, imports)

>> y = test_rick.tester(x=0.66, c=1.6)

add_module_import(name, imports: list)#

Add global Python module imports.

Parameters:
  • name – Name of import list.

  • imports (list) – List of strings of Python module names.

dict(serialised: bool = False)#

Deconstructs the whole object into a Python dictionary.

Parameters:

serialised (bool) – Give a Python dictionary in serialised (True) form or deserialised (default = False).

Notes

Functions and lambdas are always given in serialised form.

Returns:

of object.

Return type:

dict

1.2. Object Rickler#

The ObjectRickler contains static methods for converting Python objects to Rickle objects for YAML/JSON export, and then reconstructing the YAML/JSON files to the Python objects. These are not guaranteed to work for all Python objects.

class rickle.__init__.ObjectRickler#

A class of static methods to help convert Python objects to UnsafeRickle objects, deconstruct objects, create objects from UnsafeRickle objects.

Notes

  • tuple types are deconstructed as lists

  • UnsafeRickle come with security risks

static deconstruct(obj, include_imports: bool = False, include_class_source: bool = False, include_methods: bool = False)#

Takes (almost) any Python object and deconstructs it into a dict.

Parameters:
  • obj – Any object.

  • include_imports (bool) – Add a list of modules to import as is imported in current env (default = False).

  • include_class_source (bool) – Add the source of the object’s class (default = False).

  • include_methods (bool) – Include object methods (default = False).

Returns:

Deconstructed object in typical Rickle dictionary format.

Return type:

dict

static from_rickle(rickle: UnsafeRickle, cls: T, **args) T#

Takes an UnsafeRickle and initialises the class and updates attributes with the ones from the Rickle.

Parameters:
  • rickle (UnsafeRickle) – Rickle to create from.

  • cls (type) – The class to initialise from.

Returns:

Initiliased cls.

Return type:

object

static to_rickle(obj, deep: bool = False, load_lambda: bool = False) UnsafeRickle#

Transforms a Python object into a Rickle.

Parameters:
  • obj – Any initialised Python object.

  • deep (bool) – Internalize dictionary structures in lists (default = False).

  • load_lambda (bool) – Load python code as code or strings (default = False).

Returns:

A constructed UnsafeRickle object.

Return type:

UnsafeRickle

1.3. Helper tools - Functions#

1.3.1. Flatten dictionaries#

The flatten_dict function takes a Python dictionary and makes a thin (shallow) dictionary suitable for lower dimension structures like INI.

input_dict = {
   'settings': {
      'credentials': {
         'password': 'ken sent me',
         'user': 'larry'
      },
      'nodes': [
         'us-west',
         'us-central',
         'us-east',
      ]
   }
}

When using the flatten function:

flatten_dict(input_dict, path_sep='.', list_brackets=('(', ')'))

This will result in the following dictionary:

{'settings.credentials.password': 'ken sent me',
 'settings.credentials.user': 'larry',
 'settings.nodes.(0)': 'us-west',
 'settings.nodes.(1)': 'us-central',
 'settings.nodes.(2)': 'us-east'}
rickle.tools.flatten_dict(dictionary, path_sep: str | None = None, list_brackets: tuple = ('(', ')'))#

Flattens a deepl structure python dictionary into a shallow (or ‘thin’) dictionary of depth 1.

Notes

Dictionary can only contain types str, bool, int, float, dict, list. Any other types won’t be expanded upon.

Parameters:
  • dictionary (dict) – Input dictionary.

  • path_sep (str) – Path separator.

  • list_brackets (tuple) – Tuple of strings for list index values (default = (‘(’, ‘)’)).

Returns:

Flattened to depth 1.

Return type:

dict

1.3.2. Inflate dictionaries#

Flattened dictionaries in turn can be undone using the inflate_dict function. This will recursively built deeper structures as encoded in the keys.

 1flat_dict = {
 2   'settings.credentials.password': 'ken sent me',
 3   'settings.credentials.user': 'larry',
 4   'settings.nodes.(0).name': 'US West',
 5   'settings.nodes.(0).key': 'us-west',
 6   'settings.nodes.(1).name': 'US Central',
 7   'settings.nodes.(1).key': 'us-central',
 8   'settings.nodes.(2).name': 'US East',
 9   'settings.nodes.(2).key': 'us-east'
10 }

When using the inflate function:

flatten_dict(input_dict, path_sep='.', list_brackets=('(', ')'))

Will result in the following dictionary:

{
   'settings': {
      'credentials': {
         'password': 'ken sent me', 'user': 'larry'
       },
       'nodes': [
         {'name': 'US West', 'key': 'us-west'},
         {'name': 'US Central', 'key': 'us-central'},
         {'name': 'US East', 'key': 'us-east'}
       ]
   }
}
rickle.tools.inflate_dict(flat_dict: dict, path_sep: str | None = None, list_brackets: tuple = ('(', ')'))#

Does reverse operation of flatten_dict and inflates a shallow dictionary.

Parameters:
  • flat_dict (dict) – Input dictionary, can be any dict (won’t have an effect).

  • path_sep (str) – Path separator.

  • list_brackets (tuple) – Tuple of strings for list index values (default = (‘(’, ‘)’)).

Returns:

Inflated dictionary.

Return type:

dict

1.3.3. INI parsing helpers#

The INI helpers transform ConfigParser objects into Python dictionaries or vice versa and use among other the inflate and flatten functions.

rickle.tools.parse_ini(config: ConfigParser, path_sep: str | None = None, list_brackets: tuple | None = None)#

Func to create a dictionary from an initialised config parser and then returns inflated dictionary.

Parameters:
  • config (ConfigParser) – Initialised ConfigParser.

  • path_sep (str) – For inflating sections from deeply nested structures (default = None).

  • list_brackets (tuple) – For list indexes, type of bracket (default = None).

Return type:

dict

rickle.tools.unparse_ini(dictionary: dict, path_sep: str | None = None, list_brackets: tuple | None = None) ConfigParser#

Function to flatten a dictionary and create ConfigParser from the result.

Parameters:
  • dictionary (dict) – Any dictionary.

  • path_sep (str) – For creating sections from deeply nested structures (default = None).

  • list_brackets (tuple) – For list indexes, type of bracket (default = None).

Returns:

Config parser with flattened dictionary set.

Return type:

ConfigParser

1.3.4. Other#

rickle.tools.toml_null_stripper(input: dict | list)#

Remove null valued key-value pairs or list items.

Parameters:

dictionary (dict,list) – Input dictionary or list.

Returns:

Output dictionary (or list).

Return type:

dict

rickle.tools.classify_string(input_string: str)#

Try to classify the type from a string. This is done by attempting to load the string as each type. In the cases where the base decoder is not installed a simple Regex match is attempted.

Parameters:

input_string (str) – String to classify.

Returns:

The classified type (“json”, “yaml”, “toml”, “xml”, “ini”, “env”, “unknown”)

Return type:

str

rickle.tools.supported_encodings() list#

A very rudimentary way to figure out the different supported file encodings supported.

Returns:

List (str) of encoding names.

Return type:

list

rickle.tools.get_native_type_name(python_type_name: str, format_type: str, default: str | None = None)#

Helper mapping from Python type names to format names.

Parameters:
  • python_type_name (str) – Python type name.

  • format_type (str) – Format type, either yaml, json, toml, xml, ini, env, or python.

  • default (str) – If unmatched, return this default (default = None).

Returns:

Native name for the given format.

Return type:

str

rickle.tools.generate_random_value(value_type, value_properties)#

Helper function to generate a random value.

Notes

integer: Properties include min and max. Defaults to 0 and 256. number: Properties include min and max. Defaults to 0 and 256. string: Properties include chars and length. Defaults to ASCII chars and 10. enum: Properties include values. Defaults to ASCII uppercase chars. array: Properties include values and length. Defaults to ‘integer’ and 10.

values can be a string of value_type.

object: Properties include keys, values, min, and max.

Defaults to random ASCII uppercase and 10 random integers, min and max of 1 and 5. values can be a string of value_type.

Parameters:
  • value_type (str) – Either ‘string’, ‘integer’, ‘number’, ‘enum’, ‘array’, ‘object’, or ‘any’.

  • value_properties (dict) – Properties about the randomly generated value. See notes.

Returns:

Randomly generated value.

Return type:

value

class rickle.tools.CLIError(message, cli_tool: CLITool)#

1.4. Converter#

The converter is a tool to essentially load a file into a Python dictionary and then dump that dictionary into the target format. Consider the following YAML file for conversion:

1settings:
2   credentials:
3      password: ken sent me
4      user: larry
5   nodes:
6      - us-west
7      - us-central
8      - us-east

When using the convert_string method, the input YAML string can be converted to, for example, XML.

Converter.convert_string(input_string=input, input_type='yaml', output_type='xml')
 1<?xml version="1.0" encoding="utf-8"?>
 2<settings>
 3        <credentials>
 4                <password>ken sent me</password>
 5                <user>larry</user>
 6        </credentials>
 7        <nodes>us-west</nodes>
 8        <nodes>us-central</nodes>
 9        <nodes>us-east</nodes>
10</settings>
class rickle.tools.Converter(input_files: List[str] | None = None, input_directory: str | None = None, output_files: List[str] | None = None, default_output_type: str = 'yaml', verbose: bool = False)#

Tool for converting between YAML and JSON (and TOML, XML, INI, .ENV), mainly to be used in CLI.

Parameters:
  • input_files (list) – List of input file paths (default = None).

  • input_directories (list) – List of directories (default = None).

  • output_files (list) – List of output file paths (default = None).

  • default_output_type (str) – Default type to convert to if output files are not provided (default = ‘yaml’).

  • silent (bool) – Suppress verbose output (default = None).

static convert_string(input_string: str, output_type: str, input_type: str | None = None)#

Convert a string from one format to another. Supported input types:

Parameters:
  • input_string (str) – Data in input_type format.

  • output_type (str) – Output type, either [‘yaml’, ‘json’, ‘toml’, ‘xml’].

  • input_type (str) – Input type, either [‘yaml’, ‘json’, ‘toml’, ‘xml’, ‘env’] (default = None).

Notes

Output can not be of type .ENV . If no input type is given, the type is inferred.

Returns:

Converted string

Return type:

str

do_convert()#

Converts all input files to output type(s).

static infer_read_file_type(file_path: str)#

Infer the file type and return loaded contents. By default, the type is inferred from the suffix of the file path. Thus, file.yaml will be read as a YAML file. If the file extension is not known, the file will be read and tried to be loaded as both types.

Raises:

ValueError – If the type could not be inferred.

Parameters:

file_path (str) – Input file path to read.

Returns:

Loaded YAML (or JSON).

Return type:

dict

static infer_read_string_type(string: str)#

Brute force try every possible loading.

Parameters:

string (str) – Input.

Returns:

Loaded.

Return type:

dict

1.5. Schema tools#

class rickle.tools.Schema(input_files: List[str] | None = None, input_directories: List[str] | None = None, schema: str | dict | None = None, output_files: List[str] | None = None, output_dir: str | None = None, verbose: bool = False, silent: bool = False, default_output_type: str = 'yaml', use_json_schema: bool = False, include_extended_properties: bool = True)#

Tool for inferring schemas from Python dicts and validating schemas.

Parameters:
  • input_files (list) – List of input file paths (default = None).

  • input_directories (list) – List of directories (default = None).

  • schema (str, dict) – The dict definition of schema or path to schema file (default = None).

  • output_dir (str) – Directory to move output files to (default = None).

  • silent (bool) – Suppress verbose output (default = None).

  • use_json_schema (bool) – If installed and is true, the schema will be validated as a JSON schem (default = False).

  • include_extended_properties (bool) – Whether to include “required”, “nullable”, etc. (default = True).

do_generation()#

Generates schema files for the given input files.

do_validation()#

Validates input files against schema definition.

Returns:

List of files that did not pass validation.

Return type:

list

static generate_schema_from_obj(obj, include_extended_properties: bool = True)#

Generate a schema definition from a Python object.

Parameters:
  • obj – Dict like object.

  • include_extended_properties (bool) – Whether to include “required”, “nullable”, etc. (default = True).

Returns:

Schema detected from obj.

Return type:

dict

static schema_validation(obj, schema: dict, path: str = '', no_print: bool = False, use_json_schema: bool = False) bool#

Validates if obj conforms to schema.

Parameters:
  • obj – The object to check.

  • schema (dict) – The schema in dict form.

  • path (str) – The current path of the object tree (default = ‘root’).

  • no_print (bool) – If failures should not be printed (default = False).

  • use_json_schema (bool) – If true and jsonschema is installed, performs JSON schema instead (default = False).

Returns:

True if the object conforms.

Return type:

bool