weaver.transform.handlers

Module Contents

weaver.transform.handlers.LOGGER[source]
weaver.transform.handlers.HTML_CONTENT = Multiline-String[source]
Show Value
"""<html>
    <head></head>
    <body><p>%CONTENT%</p></body>
    </html>"""
weaver.transform.handlers.exception_handler(func)[source]

Decorator to handle exceptions in functions and log them.

Parameters:

func – Function to wrap with exception handling.

Returns:

The wrapped function.

weaver.transform.handlers.image_to_any(image: str, out: str) None[source]

Converts image files to a specified output format. If no conversion is needed, it copies the file.

Parameters:
  • image – Input image file path.

  • out – Output file path.

weaver.transform.handlers.images_to_any(images: List[PIL.Image.Image], out: str) None[source]

Processes a list of images and converts them to the desired format, saving them in the specified output path.

Parameters:
  • images – List of Image objects to process.

  • out – Output file path.

weaver.transform.handlers.any_to_html(i: str, out: str) None[source]

Converts any content type (text or image) to HTML format.

Parameters:
  • i – Input file path.

  • out – Output file path.

weaver.transform.handlers.any_to_pdf(i: str, out: str) None[source]

Converts a file to PDF format. If the file is an image, it is embedded in the PDF, otherwise, it is treated as text.

Parameters:
  • i – Input file path.

  • out – Output PDF file path.

weaver.transform.handlers.csv_to_json(i: str, out: str) None[source]

Converts a CSV file to a JSON file with a ‘datas’ key containing the rows.

Parameters:
  • i – Path to the input CSV file.

  • out – Path to the output JSON file.

weaver.transform.handlers.csv_to_xml(i: str, out: str) None[source]

Converts a CSV file to an XML file by first converting it to JSON.

Parameters:
  • i – Path to the input CSV file.

  • out – Path to the output XML file.

weaver.transform.handlers.json_to_xml(i: str, out: str) None[source]

Converts a JSON file to an XML file.

Parameters:
  • i – Path to the input JSON file.

  • out – Path to the output XML file.

weaver.transform.handlers.json_to_txt(i: str, out: str) None[source]

Converts a JSON file to a text file.

Parameters:
  • i – Path to the input JSON file.

  • out – Path to the output text file.

weaver.transform.handlers.json_to_yaml(i: str, out: str) None[source]

Converts a JSON file to a YAML file.

Parameters:
  • i – Path to the input JSON file.

  • out – Path to the output YAML file.

weaver.transform.handlers.yaml_to_json(i: str, out: str) None[source]

Converts a YAML file to a JSON file.

Parameters:
  • i – Path to the input YAML file.

  • out – Path to the output JSON file.

weaver.transform.handlers.json_to_csv(i: str, out: str) None[source]

Converts a JSON file to a CSV file.

Parameters:
  • i – Path to the input JSON file.

  • out – Path to the output CSV file.

weaver.transform.handlers.xml_to_json(i: str, out: str) None[source]

Converts an XML file to a JSON file.

Parameters:
  • i – Path to the input XML file.

  • out – Path to the output JSON file.

weaver.transform.handlers.html_to_txt(i: str, out: str) None[source]

Converts an HTML file to a text file.

Parameters:
  • i – Path to the input HTML file.

  • out – Path to the output text file.

weaver.transform.handlers.yaml_to_csv(i: str, out: str) None[source]

Converts a YAML file to a CSV file by first converting it to JSON.

Parameters:
  • i – Path to the input YAML file.

  • out – Path to the output CSV file.

weaver.transform.handlers.yaml_to_xml(i: str, out: str) None[source]

Converts a YAML file to an XML file.

Parameters:
  • i – Path to the input YAML file.

  • out – Path to the output XML file.

weaver.transform.handlers.xml_to_yaml(i: str, out: str) None[source]

Converts an XML file to a YAML file.

Parameters:
  • i – Path to the input XML file.

  • out – Path to the output YAML file.

weaver.transform.handlers.csv_to_yaml(i: str, out: str) None[source]

Converts a CSV file to a YAML file by first converting it to JSON.

Parameters:
  • i – Path to the input CSV file.

  • out – Path to the output YAML file.

class weaver.transform.handlers.Transform(file_path: str, current_media_type: str, wanted_media_type: str)[source]

Class for handling the transformation of files between different media types (e.g., text, image, application).

Parameters:
  • file_path – The path to the input file to be transformed.

  • current_media_type – The media type of the input file.

  • wanted_media_type – The desired media type after transformation.

Attributes:

file_path (str): The path to the input file to be transformed. current_media_type (str): The media type of the input file. wanted_media_type (str): The desired media type after transformation. output_path (str): The path where the transformed file will be saved. ext (str): The extension of the output file based on the wanted media type.

Methods:
process():

Initiates the file transformation process based on the input and output media types.

get():

Returns a FileResponse with the transformed file for download.

Initializes the Transform object with file paths and media types.

Parameters:
  • file_path – Path to the file to be transformed.

  • current_media_type – The media type of the input file.

  • wanted_media_type – The desired media type for the output file.

file_path[source]
cmt[source]
wmt[source]
output_path[source]
ext = None[source]
process() None[source]

Processes the file based on the current and wanted media types and performs the transformation.

Raises:

RuntimeError – If an error occurs during the file transformation process.

process_text() None[source]

Handles the transformation of text-based files (e.g., plain text, HTML, CSV).

Raises:

RuntimeError – If a conversion type is unsupported.

process_csv() None[source]

Handles the conversion of CSV files to other formats like JSON, XML, and YAML.

Raises:

RuntimeError – If a conversion type is unsupported.

process_application() None[source]

Handles the conversion of application files (e.g., JSON, XML, YAML).

Raises:

RuntimeError – If a conversion type is unsupported.

process_json() None[source]

Handles the transformation of JSON files to other formats like CSV, XML, YAML, and plain text.

Raises:

RuntimeError – If a conversion type is unsupported.

process_yaml() None[source]

Handles the conversion of YAML files to other formats like CSV, JSON, and XML.

Raises:

RuntimeError – If a conversion type is unsupported.

process_xml() None[source]

Handles the conversion of XML files to JSON or YAML.

Raises:

RuntimeError – If a conversion type is unsupported.

process_image() None[source]

Handles the conversion of image files to other formats (e.g., image to image or image to PDF).

Raises:

RuntimeError – If a conversion type is unsupported.

get() pyramid.response.FileResponse[source]

Returns the transformed file as a response for download.

Returns:

The response containing the transformed file.

Raises:

HTTPUnprocessableEntity – If an error occurs during file transformation.