cookbase.schema

Module contents

Package for handling Cookbase Schema builds.

Submodules

cookbase.schema.builder

Cookbase Schema Builder.

This module processes Cookbase Schema templates and compiles them into the desired directory structure, either on a local or remote (through SSH) location.

The configuration is taken by default from the build-config.yaml file, having the option to provide a custom configuration file. Any mandatory parameters lacking in the custom configuration file are directly taken from the default values.

exception cookbase.schema.builder.ConfigError(message: str)[source]

Bases: Exception

Class defining errors thrown when a configuration parsing problem arises.

class cookbase.schema.builder._SSHConnection(hostname: str = None, port: int = None, username: str = None)[source]

Bases: object

Helper class handling SFTP communication.

If provided with a hostname, automatically initiates the SFTP session.

Parameters:
  • hostname (str, optional) – The SSH host, defaults to None.
  • port (int, optional) – The port where the SSH host is listening, defaults to None.
  • username (str, optional) – The username on the target SSH host, defaults to None.
Variables:
  • client (paramiko.client.SSHClient) – The SSH session handler.
  • sftp_session (paramiko.sftp_client.SFTPClient) – The SFTP session handler.
client = None
close_session()[source]

Close the SFTP session.

is_active() → bool[source]

Check whether the SSH session is active or not.

Returns:True if the SSH session is active, False otherwise.
Return type:bool
mkdir_p(path: str)[source]

Simulate the mkdir -p <path> Unix command.

It creates the directory and all its non-existing parents.

Parameters:path (str) – The path to a directory.
put_file(local_path, remote_path)[source]

Copy the local file at local_path into remote_path.

It creates the parent directories if required.

Parameters:
  • local_path (str) – Path to the local file.
  • remote_path (str) – Path to the remote file.
set_session(hostname: str, port: int = None, username: str = None)[source]

Set up a SFTP session.

Parameters:
  • hostname (str) – The SSH host.
  • port (int, optional) – The port where the SSH host is listening, defaults to None.
  • username (str, optional) – The username on the target SSH host, defaults to None.
sftp_session = None
cookbase.schema.builder._build_abs_schema_uri(rel_path: str, cb_schemas_base_url: str) → str[source]

Form an absolute URI from a relative path.

It takes the common.cb_schemas_base_url property provided by the build configuration file.

Parameters:
  • rel_path (str) – A path relative to build_params.root_build_dir.
  • cb_schemas_base_url (str) – The base URL pointing to the directory where the Cookbase Schemas are to be located.
Returns:

An absolute URI intended to unambiguously refer to the given path.

Return type:

str

cookbase.schema.builder._build_rel_path(start_rel_path: str, target_rel_path: str, root_build_dir: str) → str[source]

Calculate the relative path from start_rel_path to target_rel_path.

Parameters:
  • start_rel_path (str) – The start-point path.
  • target_rel_path (str) – The target path.
  • root_build_dir (str) – The absolute path to the directory where the built schemas are to be located.
Returns:

A relative path from start_rel_path to target_rel_path.

Return type:

str

cookbase.schema.builder.build_caf_schema(caf_schema_path: str, common_config: Dict[str, str], build_params: Dict[str, str]) → Tuple[str, str][source]

Build the CAF Schema.

Parameters:
  • caf_schema_path (str) – The relative path to the Cookbase Appliance Function Schema.
  • common_config (Dict[str, str]) – The common configuration block.
  • build_params (Dict[str, str]) – The build_params configuration block.
Returns:

A tuple having two elements, the rendered Cookbase Appliance Function Schema as first element and the path where to be located as second element.

Return type:

Tuple[str, str]

cookbase.schema.builder.build_cb_common_definitions_schema(common_config: Dict[str, str], build_params: Dict[str, str]) → Tuple[str, str][source]

Build the CB Common Definitions Schema.

Parameters:
  • common_config (Dict[str, str]) – The common configuration block.
  • build_params (Dict[str, str]) – The build_params configuration block.
Returns:

A tuple having two elements, the rendered CB Common Definitions Schema as first element and the path where to be located as second element.

Return type:

Tuple[str, str]

cookbase.schema.builder.build_cba_schema(cba_schema_path: str, common_config: Dict[str, str], build_params: Dict[str, str]) → Tuple[str, str][source]

Build the CBA Schema.

Parameters:
  • cba_schema_path (str) – The relative path to the Cookbase Appliance Schema.
  • common_config (Dict[str, str]) – The common configuration block.
  • build_params (Dict[str, str]) – The build_params configuration block.
Returns:

A tuple having two elements, the rendered Cookbase Appliance Schema as first element and the path where to be located as second element.

Return type:

Tuple[str, str]

cookbase.schema.builder.build_cbi_schema(cbi_schema_path: str, common_config: Dict[str, str], build_params: Dict[str, str]) → Tuple[str, str][source]

Build the CBI Schema.

Parameters:
  • cbi_schema_path (str) – The relative path to the Cookbase Ingredient Schema.
  • common_config (Dict[str, str]) – The common configuration block.
  • build_params (Dict[str, str]) – The build_params configuration block.
Returns:

A tuple having two elements, the rendered Cookbase Ingredient Schema as first element and the path where to be located as second element.

Return type:

Tuple[str, str]

cookbase.schema.builder.build_cbp_schema(cbp_schema_path: str, common_config: Dict[str, str], build_params: Dict[str, str]) → Tuple[str, str][source]

Build the CBP Schema.

Parameters:
  • cbp_schema_path (str) – The relative path to the Cookbase Process Schema.
  • common_config (Dict[str, str]) – The common configuration block.
  • build_params (Dict[str, str]) – The build_params configuration block.
Returns:

A tuple having two elements, the rendered Cookbase Process Schema as first element and the path where to be located as second element.

Return type:

Tuple[str, str]

cookbase.schema.builder.build_cbr_process_definitions_schema(cbr_defs_path: str, common_config: Dict[str, str], build_params: Dict[str, str]) → Tuple[str, str][source]

Build the CBR Process Definitions Schema.

Parameters:
  • cbr_defs_path (str) – The relative path to the CBR Process Definitions Schema.
  • common_config (Dict[str, str]) – The common configuration block.
  • build_params (Dict[str, str]) – The build_params configuration block.
Returns:

A tuple having two elements, the rendered CBR Process Definitions Schema as first element and the path where to be located as second element.

Return type:

Tuple[str, str]

cookbase.schema.builder.build_cbr_process_schema(cbr_process_path: str, cbr_process_config: Dict[str, str], common_config: Dict[str, str], build_params: Dict[str, str]) → Tuple[Tuple[str, str]][source]

Build the CBR Process Schema.

Parameters:
  • cbr_process_path (str) – The relative path to the CBR Process Main Schema.
  • cbr_process_config (Dict[str, str]) – The cbr_process configuration block.
  • common_config (Dict[str, str]) – The common configuration block.
  • build_params (Dict[str, str]) – The build_params configuration block.
Returns:

A tuple of tuples that represent the CBR Process Main Schema and its subschemas. Each sub-tuple has two elements: a rendered JSON Schema representing either the CBR Process Main Schema itself or any of its subschemas as first element, and the path where the schema is to be located as second element.

Return type:

Tuple[Tuple[str, str]]

cookbase.schema.builder.build_cbr_schema(cbr_config: Dict[str, str], cbr_process_config: Dict[str, str], common_config: Dict[str, str], build_params: Dict[str, str]) → Tuple[Tuple[str, str]][source]

Build the CBR Schema.

Parameters:
  • cbr_config (Dict[str, str]) – The cbr configuration block.
  • cbr_process_config (Dict[str, str]) – The cbr_process configuration block.
  • common_config (Dict[str, str]) – The common configuration block.
  • build_params (Dict[str, str]) – The build_params configuration block.
Returns:

A tuple of tuples that represent the Cookbase Recipe Schema and all its subschemas. Each sub-tuple has two elements: a rendered JSON Schema representing either the Cookbase Recipe Schema itself or any of its subschemas as first element, and the path where the schema is to be located as second element.

Return type:

Tuple[Tuple[str, str]]

cookbase.schema.builder.build_config(custom_config_path: str = None)[source]

Generate the builder configuration.

If a custom build configuration file custom_config_path is not provided, the default build configuration is loaded; if provided, all provided parameters will override the default configuration.

Parameters:custom_config_path – Path to the custom build configuration file, defaults to None.
Raises:ConfigError – The custom configuration file has a wrong format.
cookbase.schema.builder.generate_cbr_process_collection(cbr_process_config: Dict[str, str], common_config: Dict[str, str], build_params: Dict[str, str]) → Tuple[Tuple[str, str]][source]

Build the collection of CBR Process Schemas.

Generates and writes into files the CBR Process Schemas from a collection of CBP files, which are under the directory specified by the cbr_process.cbp_dir property from the build configuration file. The routes to the JSON Schema definitions of the different types referred by the CBP documents are detailed in the configuration file under the path given by the cbr_process.types_path property.

Parameters:
  • cbr_process_config (Dict[str, str]) – The cbr_process configuration block.
  • common_config (Dict[str, str]) – The common configuration block.
  • build_params (Dict[str, str]) – The build_params configuration block.
Returns:

A tuple of tuples that represent the collection of CBR Process Schemas. Each sub-tuple has two elements: a rendered JSON Schema representing either the CBR Process Schema as first element, and the path where the schema is to be located as second element.

Return type:

Tuple[Tuple[str, str]]

cookbase.schema.builder.generate_cbr_process_collection_item(cbp: Dict[str, Any], defs: Dict[str, str], json_schema_uri: str, cbr_schema_uri: str) → str[source]

Build a CBR Process Schema from a CBP document.

If schema_filename is set to None (the default), the output file name will be assumed to be the first English name given in the CBP’s name property, lowercased, substituted spaces by underscores _, and appended the .json extension.

Parameters:
  • cbp (dict[str, Any]) – The dictionary representing the content of a CBP document.
  • defs (dict[str, str]) – A dictionary mapping the CBP. data.schema.foodstuffKeywords.*.type properties to the location of its definition
  • json_schema_uri (str) – The URI identifying the followed JSON Schema specification.
  • cbr_schema_uri (str) – The absolute URI referring to the CBR Process Schema.
Returns:

The CBR Process Schema.

Return type:

str

Raises:

ValueError – The processed CBP does not have "generic" as data.processType.

cookbase.schema.builder.generate_cbr_process_main_schema(cbr_process_path: str, cbr_process_collection_dir: str, common_config: Dict[str, str], build_params: Dict[str, str], cbr_process_collection_filenames: Tuple[str]) → Tuple[str, str][source]

Build the CBR Process Main Schema from a collection of CBR Process Schemas.

Parameters:
  • cbr_process_path (str) – The relative path to the CBR Process Main Schema.
  • cbr_process_collection_dir (str) – The relative path to the directory containing the collection of CBR Process Schemas.
  • common_config (Dict[str, str]) – The common configuration block.
  • build_params (Dict[str, str]) – The build_params configuration block.
  • cbr_process_collection_filenames (Tuple[str]) – The names of all files containing the collection of CBR Process Schemas.
Returns:

A tuple having two elements, the rendered CBR Process Main Schema as first element and the path where to be located as second element.

Return type:

Tuple[str, str]

cookbase.schema.builder.main(args: argparse.Namespace)[source]

Run the module with the provided configuration.

In case that the build_params.root_build_dir configuration property consists of a SSH or SFTP URI, a SFTP session with the target host is opened in order to store the files remotely.

Warning

This function generates output files and may overwrite stored data.

Parameters:args (argparse.Namespace) – The passed arguments including the config_path attribute which holds the path to the custom configuration file.
Raises:ValueError – The URI under the build_params.root_build_dir configuration property is neither a SSH/SFTP URI nor a filesystem path.
cookbase.schema.builder.render(template_path: str, substitutions: Dict[str, str]) → str[source]

Generate a JSON Schema by applying substitutions on a template.

Parameters:
  • template_path (str) – The path to the template file.
  • substitutions (Dict[str, str]) – A dictionary including the substitutions to be performed on the template.
Returns:

The rendered JSON Schema.

Return type:

str