web_utils.py


Overview

The web_utils.py file is a utility module providing various helper functions primarily related to web operations, including:

This file is designed to support web applications by offering reusable utilities to handle common web-related tasks, improving code modularity and maintainability.


Detailed Explanation of Functions

1. html2pdf(source: str, timeout: int = 2, install_driver: bool = True, print_options: dict = {}) -> bytes

Purpose:
Converts an HTML source (either a URL or a local file path) into a PDF document.

Parameters:

Returns:
bytes - The binary PDF data generated from the HTML content.

Usage Example:

pdf_bytes = html2pdf("https://example.com", timeout=5)
with open("output.pdf", "wb") as f:
    f.write(pdf_bytes)

Implementation Details:


2. __send_devtools(driver, cmd, params={}) -> dict

Purpose:
Internal helper function to send commands directly to Chrome DevTools Protocol via the Selenium WebDriver.

Parameters:

Returns:
dict - The result from the DevTools command.

Notes:


3. __get_pdf_from_html(path: str, timeout: int, install_driver: bool, print_options: dict) -> bytes | None

Purpose:
Internal function that implements the detailed logic for rendering an HTML page to PDF.

Parameters:

Returns:
bytes - PDF binary data, or None if generation failed.

Implementation Details:


4. is_private_ip(ip: str) -> bool

Purpose:
Checks whether a given IP address is within a private network range.

Parameters:

Returns:
bool - True if the IP is private, False otherwise or if invalid.

Usage Example:

is_private = is_private_ip("192.168.1.1")  # True

Implementation Details:


5. is_valid_url(url: str) -> bool

Purpose:
Validates a URL string ensuring it matches HTTP/HTTPS scheme and does not point to a private IP address.

Parameters:

Returns:
bool - True if URL is valid and not private, False otherwise.

Usage Example:

valid = is_valid_url("https://www.google.com")  # True

Implementation Details:


6. safe_json_parse(data: str | dict) -> dict

Purpose:
Safely parses a JSON string into a Python dictionary, or returns the dictionary if already provided.

Parameters:

Returns:
dict - Parsed dictionary or empty dict on failure.

Usage Example:

parsed = safe_json_parse('{"key": "value"}')  # {'key': 'value'}

Implementation Details:


7. get_float(req: dict, key: str, default: float | int = 10.0) -> float

Purpose:
Extracts a float value from a dictionary for a given key, with a fallback default and validation for positive numbers.

Parameters:

Returns:
float - Parsed float value or default.

Usage Example:

value = get_float({"timeout": "3.5"}, "timeout")  # 3.5
value = get_float({"timeout": "-1"}, "timeout")  # 10.0 (default)

8. send_invite_email(to_email, invite_url, tenant_id, inviter)

Purpose:
Sends an invitation email to a user to join a team, using a predefined HTML email template.

Parameters:

Returns:
None

Usage Example:

send_invite_email("[email protected]", "https://app/invite/abc123", "team123", "Alice")

Implementation Details:


Important Implementation Details and Algorithms


Interaction with Other System Components


Content Type Map

The module contains a dictionary CONTENT_TYPE_MAP mapping common file extensions to their MIME types to facilitate content handling in other parts of the application.


Diagram: Utility Functions Flowchart

flowchart TD
    A[html2pdf(source)] --> B[__get_pdf_from_html(path, timeout, install_driver, print_options)]
    B --> C[__send_devtools(driver, cmd, params)]

    D[is_valid_url(url)] --> E[is_private_ip(ip)]

    F[safe_json_parse(data)] --> G[return dict or {}]

    H[get_float(req, key, default)] --> I[parse float, fallback default]

    J[send_invite_email(...)] --> K[render_template_string(INVITE_EMAIL_TMPL)]
    K --> L[smtp_mail_server.send(msg)]

Summary

The web_utils.py file is a utility-focused module designed to provide robust, reusable tools for:

It integrates tightly with Flask and Selenium-based components and is critical in enabling secure and feature-rich web backend operations.


End of Documentation for web_utils.py