elabforms package

Submodules

elabforms.cli module

elabforms.cli.main()[source]

Main function to handle CLI arguments and execute the template generation.

elabforms.generate_templates module

elabforms.generate_templates.check_template_parts_structure(template_parts_file_content)[source]

Validates the structure of a template parts content against Elab format rules.

Parameters:
  • template_parts_file_content (dict) – The JSON-parsed content of

  • parts. (the template)

Returns:

True if the structure is valid.

Return type:

bool

Raises:
  • ValueError – If the structure does not meet the required format.

  • Warning – If some fields do not have corresponding ‘groupfield’

  • mappings.

elabforms.generate_templates.concatenate_templates(existing_template_parts_content, new_template_part_content)[source]

Concatenates the contents of two template parts.

Parameters:
  • existing_template_parts_content (dict) – Content of the existing

  • template.

  • new_template_parts_content (dict) – Content of the new template

  • append. (to)

Returns:

The merged template content.

Return type:

dict

elabforms.generate_templates.generate_template(template_parts_list_file, template_file_path)[source]

Generates a full template by merging all template parts listed in a file.

Parameters:
  • template_parts_list_file (str) – Path to a .csv or .txt file

  • files. (listing JSON parts)

  • template_file_path (str) – Path to the output JSON file to save

  • template. (the)

Returns:

None

elabforms.generate_templates.read_template_part(template_parts_file)[source]

Reads a JSON template parts file and extracts the ‘groupfield’ and its details.

Parameters:
  • template_parts_file (str) – Path to a JSON file representing a

  • parts. (template)

Returns:

The parsed JSON content.

Return type:

content (dict)

Raises:
  • FileNotFoundError – If the JSON file does not exist.

  • json.JSONDecodeError – If the JSON structure is invalid.

elabforms.generate_templates.read_template_parts_list(template_parts_list_file)[source]

Reads a file containing a list of template parts to be concatenated.

Parameters:
  • template_parts_list_file (str) – Path to the .csv or .txt file

  • list. (containing the)

Returns:

A list of file paths as strings.

Return type:

list of str

Raises:
  • ValueError – If the file extension is not supported.

  • FileNotFoundError – If the file does not exist.

elabforms.generate_templates.read_template_parts_list_csv(template_parts_list_file)[source]

Reads file paths from a CSV file. Assumes the first column contains the paths.

Parameters:

template_parts_list_file (str) – Path to the CSV file.

Returns:

A list of strings from the first column of each row.

Return type:

list of str

Raises:

FileNotFoundError – If the file does not exist.

elabforms.generate_templates.save_template(full_template_content, template_file_path)[source]

Saves the template content to a JSON file.

Parameters:
  • full_template_content (dict) – The content to save.

  • template_file_path (str) – Path to the output JSON file.

Returns:

None

elabforms.generate_templates.set_content_id(new_id, template_parts_file_content)[source]

Updates the ID of the content in a template parts.

Parameters:
  • new_id (int) – The new ID to assign.

  • template_parts_file_content (dict) – The original template parts

  • content.

Returns:

The updated template parts content.

Return type:

dict

elabforms.template module

class elabforms.template.Template(template_file=None)[source]

Bases: object

Class to manage a template for ElabFTW.

add_template_part(new_template_part)[source]

Add a new template part to the current template :param new_template_part: A Template object :type new_template_part: Template :param containing the new content to add.:

Returns:

Merged content.

Return type:

dict

static check_structure(template_file_content)[source]

Validates the structure of a template.

Parameters:

template_file_content (dict) – JSON content of the file.

Raises:

ValueError – If the structure is invalid.

is_empty()[source]

Checks if the template content is empty.

Returns:

True if the template content is empty, False otherwise.

Return type:

bool

static read_template(template_file)[source]

Reads a JSON template file and validates its structure.

Parameters:

template_file (str) – Path to the JSON file.

Returns:

Content of the JSON file.

Return type:

dict

Raises:

ValueError – If the structure is invalid.

save_template(template_file_path)[source]

Saves the current template content to a JSON file.

Parameters:

template_file_path (str) – Path to the output JSON file.

Returns:

None

elabforms.template_builder module

class elabforms.template_builder.TemplateBuilder(template_parts_list_file, template_file_path=None)[source]

Bases: object

Class to generate a complete template from multiple parts.

static build_template(template_parts_list_file, template_file_path)[source]

Generates a complete template by merging all the listed parts.

Parameters:

template_parts_list_file (str) – Path to the CSV file

listing the parts.

template_file_path (str): Path to the output JSON file.

Returns:

None

static read_template_parts_list(template_parts_list_file)[source]

Reads a list of template part files from a CSV file.

Parameters:

template_parts_list_file (str) – Path to the CSV file.

Returns:

List of JSON file paths.

Return type:

list

Raises:
  • ValueError – If the file extension is not supported.

  • FileNotFoundError – If a file is not found.

elabforms.template_part module

class elabforms.template_part.TemplatePart(template_file)[source]

Bases: Template

Class to manage a template part for ElabFTW. Inherits from the Template class.

static check_structure(template_file_content)[source]

Validates the structure of a template part.

Parameters:

template_file_content (dict) – JSON content of the file.

Raises:

ValueError – If the structure is invalid.

set_content_id(new_id)[source]

Updates the IDs of the contents in a template part.

Parameters:

new_id (int) – New ID to assign.

Returns:

The updated template part with the new ID set.

Return type:

TemplatePart

Module contents

A set of tools to create and manage standardized forms for eLabFTW

class elabforms.Template(template_file=None)[source]

Bases: object

Class to manage a template for ElabFTW.

add_template_part(new_template_part)[source]

Add a new template part to the current template :param new_template_part: A Template object :type new_template_part: Template :param containing the new content to add.:

Returns:

Merged content.

Return type:

dict

static check_structure(template_file_content)[source]

Validates the structure of a template.

Parameters:

template_file_content (dict) – JSON content of the file.

Raises:

ValueError – If the structure is invalid.

is_empty()[source]

Checks if the template content is empty.

Returns:

True if the template content is empty, False otherwise.

Return type:

bool

static read_template(template_file)[source]

Reads a JSON template file and validates its structure.

Parameters:

template_file (str) – Path to the JSON file.

Returns:

Content of the JSON file.

Return type:

dict

Raises:

ValueError – If the structure is invalid.

save_template(template_file_path)[source]

Saves the current template content to a JSON file.

Parameters:

template_file_path (str) – Path to the output JSON file.

Returns:

None

class elabforms.TemplateBuilder(template_parts_list_file, template_file_path=None)[source]

Bases: object

Class to generate a complete template from multiple parts.

static build_template(template_parts_list_file, template_file_path)[source]

Generates a complete template by merging all the listed parts.

Parameters:

template_parts_list_file (str) – Path to the CSV file

listing the parts.

template_file_path (str): Path to the output JSON file.

Returns:

None

static read_template_parts_list(template_parts_list_file)[source]

Reads a list of template part files from a CSV file.

Parameters:

template_parts_list_file (str) – Path to the CSV file.

Returns:

List of JSON file paths.

Return type:

list

Raises:
  • ValueError – If the file extension is not supported.

  • FileNotFoundError – If a file is not found.

class elabforms.TemplatePart(template_file)[source]

Bases: Template

Class to manage a template part for ElabFTW. Inherits from the Template class.

static check_structure(template_file_content)[source]

Validates the structure of a template part.

Parameters:

template_file_content (dict) – JSON content of the file.

Raises:

ValueError – If the structure is invalid.

set_content_id(new_id)[source]

Updates the IDs of the contents in a template part.

Parameters:

new_id (int) – New ID to assign.

Returns:

The updated template part with the new ID set.

Return type:

TemplatePart