SAMLoader Class

SAMLoader is a lightweight utility class for loading and parsing Smarter API Manifest (SAM) files into Pydantic models. It provides a simple interface for reading manifest files, validating their contents, and converting them into lightweight structured data models that can be used for initializing SAM Pydantic models.

Usage:
from smarter.apps.secret.manifest.models.secret.model import (
    SAMSecret,
    SAMSecretMetadata,
    SAMSecretSpec,
)
from smarter.lib.manifest.loader import SAMLoader

# initialize the loader from a yaml file.
loader = SAMLoader(manifest_path="path/to/manifest.yaml")

# initialize a SAM Pydantic model from the loader.
self._manifest = SAMSecret(
    apiVersion=self.loader.manifest_api_version,
    kind=self.loader.manifest_kind,
    metadata=SAMSecretMetadata(**self.loader.manifest_metadata),
    spec=SAMSecretSpec(**self.loader.manifest_spec),
)

Smarter API Manifest Loader base class.

class smarter.lib.manifest.loader.SAMLoader(*args, api_version='smarter.sh/v1', kind=None, manifest=None, file_path=None, url=None, **kwargs)[source]

Smarter API Manifest Loader base class.

This class provides the foundational logic for loading, parsing, and validating Smarter API Manifest files, prior to attempting to use the manifest data for initializing a Pydantic model. It is designed to handle manifests provided as JSON or YAML, and supports loading from a string, dictionary, file path, or URL. The loader checks the manifest’s format and its top-level keys, so that a manifest with invalid syntax or structure gets a more human readable error message than Pydantic’s.

Usage Overview

The SAMLoader is intended to be subclassed for specific manifest types. It performs the following core functions:

  • Manifest Acquisition: Accepts manifest data in various forms, including a raw string, a Python dictionary, a file path, or a URL. Only one source should be provided at a time. The loader reads and stores the manifest data for further processing.

  • Specification: Keeps its own copy of a specification dictionary that describes the manifest’s top-level keys and metadata, with the api version and kind filled in. validate_manifest() does not read it; subclasses may.

  • Format Detection: Automatically detects whether the manifest data is in JSON or YAML format, and parses it accordingly. If the format is invalid or unsupported, an error is raised.

  • Validation: Checks that the manifest is a dict with the required top-level keys, apiVersion, kind, metadata and spec. The values themselves are validated later, by the kind’s Pydantic model. Validation errors are raised as exceptions.

  • Extensibility: Designed for subclassing, allowing child classes to override the specification and validation logic to accommodate custom manifest structures.

Manifest Structure

The expected manifest structure includes the following top-level keys:

  • apiVersion: The version of the Smarter API. Must be supported by the loader.

  • kind: The type of manifest. Can be specified directly or inferred from the manifest data.

  • metadata: A dictionary containing descriptive information such as name, description, version, tags, and annotations. Certain fields are required, while others are optional.

  • spec: The specification section, which is required and typically defined by subclasses.

  • status: An optional, read-only section for status information.

Validation Process

Upon initialization, the loader validates the manifest by:

  1. Ensuring the manifest data is present and in a supported format.

  2. Checking that the parsed manifest is a dict.

  3. Raising an error if any required top-level key is missing.

Subclassing

To support custom manifest types, create a subclass of SAMLoader and override the _specification attribute and the validate_manifest method as needed. This allows for the enforcement of additional keys, custom validation logic, and specialized handling of manifest data.

Error Handling

All validation and loading errors are raised as SAMLoaderError exceptions, providing clear feedback on the nature of the issue encountered.

Logging

The loader logs its state once it is initialized: at DEBUG when it is ready, and as a warning when it is not.

__init__(*args, api_version='smarter.sh/v1', kind=None, manifest=None, file_path=None, url=None, **kwargs)[source]

Initialize a new instance of the SAMLoader.

This constructor is responsible for acquiring, parsing, and validating a Smarter API Manifest. It supports loading manifest data from several sources, but only one source should be provided at a time.

Parameters:
  • api_version (str) – The API version of the manifest. Must be one of the supported API versions.

  • kind (str | None) – The kind of manifest. If not provided, it will be inferred from the manifest data.

  • manifest (str | dict[str, Any] | None) – The manifest data, provided as a JSON/YAML string or a Python dictionary.

  • file_path (str | None) – Path to a file containing the manifest data.

  • url (str | None) – URL pointing to the manifest data.

Raises:

SAMLoaderError – If the API version is not supported, if no manifest source is provided, if multiple sources are provided, or if the manifest format is invalid.

Acquisition and Validation Process

  1. The constructor checks that the provided API version is supported.

  2. It ensures that exactly one manifest source is specified (manifest, file_path, or url).

  3. The manifest data is loaded from the specified source.

  4. The loader sets the specification keys for API version and kind.

  5. The manifest is validated by validate_manifest(), which checks its format and its required top-level keys.

Child classes may override the specification and validation logic to support custom manifest structures.

property amnesty_urls: list[str]

Returns a list of URLs that are exempt from certain checks.

Returns:

List of URL path strings that are exempt.

Return type:

list[str]

property api_version: str

The manifest’s apiVersion, or the api version this loader was created with.

bool_environment_variable(var_name, default=False)

Retrieves a boolean value from an environment variable.

This method checks the specified environment variable and returns its value as a boolean. It recognizes common truthy values such as “true”, “1”, “yes”, and “on”. If the variable is not set or cannot be interpreted as a boolean, it returns the provided default value.

Parameters:
  • var_name (str) – The name of the environment variable to check.

  • default (bool) – The default boolean value to return if the environment variable is not set or invalid.

Returns:

The boolean value of the environment variable or the default.

Return type:

bool

property data_format: SAMDataFormats

Detect, and cache, whether the manifest data is JSON or YAML.

A JSON string is parsed, and replaces the raw data.

Returns:

The data format, or SAMDataFormats.UNKNOWN if there is no data.

Raises:

SAMLoaderError – If the data is neither JSON nor YAML.

data_to_dict(data)

Converts data to a dictionary, handling different types of input.

This method accepts either a dictionary or a string. If a string is provided, it will attempt to parse it as JSON first, and if that fails, as YAML. If parsing fails or the data type is unsupported, a SmarterValueError is raised.

Parameters:

data (dict[str, Any] | str) – The data to convert, either a dict or a JSON/YAML string.

Returns:

The data as a dictionary.

Return type:

dict[str, Any]

Raises:

SmarterValueError – If the data cannot be converted to a dictionary.

deserves_amnesty(slug)

Determines if a given URL deserves amnesty based on the amnesty URLs list.

This excuses certain endpoints (like health checks) from select middleware checks.

Parameters:

slug (str) – The URL path to check.

Returns:

True if the URL deserves amnesty, False otherwise.

Return type:

bool

dict_is_contained_in(dict1, dict2)

Checks if one dictionary is contained within another.

This method determines if all key-value pairs in dict1 are present in dict2.

Parameters:
  • dict1 (dict[str, Any]) – The dictionary to check for containment.

  • dict2 (dict[str, Any]) – The dictionary to check against for containment.

Returns:

True if dict1 is contained in dict2, False otherwise.

Return type:

bool

dict_is_subset(small, big)

Checks if one dictionary is a subset of another.

This method determines if all key-value pairs in the small dictionary are present in the big dictionary. It returns True if the small dictionary is a subset of the big dictionary, and False otherwise.

Parameters:
  • small (dict[str, Any]) – The dictionary to check as a subset.

  • big (dict[str, Any]) – The dictionary to check against as a superset.

Returns:

True if the small dictionary is a subset of the big dictionary, False otherwise.

Return type:

bool

property file_path: str | None

The path of the manifest file this loader was created with, if any.

property formatted_class_name: str

Returns the class name formatted for logging.

Returns:

The formatted class name as a string.

property formatted_data: str

The manifest data as a JSON string.

formatted_json(json_obj)

Formats a JSON object as a pretty-printed string with ANSI color codes for logging.

Parameters:

json_obj (dict[str, Any] | list) – The JSON object (dict or list) to format.

Returns:

A string representation of the JSON object with ANSI color codes.

Return type:

str

property formatted_state_not_ready: str

Returns the not-ready state formatted for logging.

Returns:

The formatted not-ready state as a string.

Return type:

str

property formatted_state_ready: str

Returns the readiness state formatted for logging.

Returns:

The formatted readiness state as a string.

Return type:

str

formatted_text(text, color_code='\\x1b[1;31m')

Formats text with ANSI color codes for logging.

Parameters:
  • text (str) – The text to format.

  • color_code (str) – The ANSI color code to apply.

Returns:

The formatted text with ANSI color codes.

Return type:

str

formatted_text_blue(text)

Formats text in bold dark blue for logging.

Parameters:

text (str) – The text to format.

Returns:

The formatted text in bold dark blue.

Return type:

str

formatted_text_green(text)

Formats text in bright green for logging.

Parameters:

text (str) – The text to format.

Returns:

The formatted text in bright green.

Return type:

str

formatted_text_red(text)

Formats text in dark red for logging.

Parameters:

text (str) – The text to format.

Returns:

The formatted text in dark red.

Return type:

str

generate_fernet_encryption_key()

Generates a Fernet encryption key.

This method creates a new Fernet encryption key, which can be used for secure encryption and decryption of data. The generated key is returned as a URL-safe base64-encoded string.

Returns:

A new Fernet encryption key.

Return type:

str

get_key(key)[source]

Get a key from the manifest’s JSON data.

Parameters:

key (str) – The key to retrieve.

Return type:

Any

Returns:

The value of the key, or None if the key does not exist, or the manifest data is not a dict.

get_readonly_csv_file(file_path)

Reads a CSV file and returns its contents as a list of dictionaries, one per row.

The file is opened read-only, so reading it never modifies it.

Parameters:

file_path (str) – The path to the CSV file to read.

Returns:

The rows of the CSV file, one dictionary per row, keyed by the column headers.

Return type:

list[dict]

get_readonly_yaml_file(file_path)

Reads a YAML file and returns its contents as a dictionary.

The file is opened read-only, so reading it never modifies it.

Parameters:

file_path (str) – The path to the YAML file to read.

Returns:

The contents of the YAML file, parsed into a dictionary.

Return type:

dict

property health_check_urls: list[str]

Returns a list of URL paths that are considered health check endpoints.

Returns:

List of health check URL path strings.

Return type:

list[str]

property json_data: dict[str, Any] | None

The manifest data parsed into a dict, or None if its format is unknown.

A YAML string is parsed once, by data_format, and the dict is kept: every key lookup reads this property.

property kind: str

The manifest’s kind, or the kind this loader was created with, or unknown-kind.

loader_ready_state()[source]

Returns a string representation of the loader’s readiness state.

Return type:

str

Returns:

“ready” if the loader is ready, otherwise “not ready”.

log_loader_state()[source]

Log the current state of the SAMLoader instance: at DEBUG if it is ready, otherwise as a warning.

Return type:

None

property manifest: str | dict[str, Any] | None

The manifest this loader was created with, or None if it is not a string or a dict.

An invalid type is logged as an error.

property manifest_api_version: str | None

Returns the API version of the manifest.

Returns:

The API version string, or None if the manifest has none.

property manifest_kind: str | None

Returns the kind of the manifest.

Returns:

The kind string, or None if the manifest has none.

property manifest_metadata: dict[str, Any] | None

Returns the metadata section of the manifest.

Returns:

The metadata dictionary, or None if the manifest has none.

property manifest_metadata_keys: list[str]

Returns a list of all metadata keys defined in the SAMMetadataKeys enumeration.

Returns:

A list of metadata key strings.

property manifest_sections: dict[str, Any]

Returns the manifest’s apiVersion, kind, metadata and spec, to validate a broker’s SAM model with.

A broker builds its manifest with SAMGuardrail.model_validate(self.loader.manifest_sections). A section that the manifest doesn’t have is None, which the model’s validation rejects.

Returns:

A dictionary with the keys apiVersion, kind, metadata and spec.

property manifest_spec: dict[str, Any] | None

Returns the spec section of the manifest.

Returns:

The spec dictionary, or None if the manifest has none.

property manifest_spec_keys: list[str]

none, here.

This should be overridden by child classes to provide the specific spec keys relevant to their manifest type.

Returns:

A list of spec key strings, empty here.

Type:

Returns the keys of the manifest’s spec

property manifest_status: dict[str, Any] | None

Returns the status section of the manifest.

Returns:

The status dictionary, or None if the manifest has none.

property manifest_status_keys: list[str]

none, here.

This should be overridden by child classes to provide the specific status keys relevant to their manifest type.

Returns:

A list of status key strings, empty here.

Type:

Returns the keys of the manifest’s status

mask_string(string='', mask_char='*', mask_length=4, string_length=8)

Masks a string for secure logging.

This utility function masks all but the last unmasked_chars characters of the input string, replacing them with asterisks. It is useful for logging sensitive information like API keys or passwords.

Parameters:
  • string (str | None) – The string to be masked.

  • mask_char (str) – The character used for masking.

  • mask_length (int) – The number of characters to mask.

  • string_length (int) – The length of the string to consider for masking.

Returns:

The masked string.

Return type:

str

property name: str

The manifest’s metadata.name, or unknown-name.

pydantic_model_dump()[source]

Return a dictionary representation of the manifest data suitable for use with Pydantic models.

This method produces a dictionary that can be directly passed as keyword arguments to any descendant of the AbstractSAMBase class. This enables seamless integration with Pydantic-based data validation and serialization workflows.

The returned dictionary includes the following keys:

  • apiVersion: The API version of the manifest.

  • kind: The kind of manifest.

  • metadata: The manifest metadata section.

  • spec: The manifest specification section.

  • status: The manifest status section.

Example usage:

SAMObject(**loader.pydantic_model_dump())
Return type:

dict[str, Any]

Returns:

Dictionary representation of the manifest data, suitable for Pydantic model instantiation.

property raw_data: str | dict[str, Any] | None

a JSON or YAML string, or a dict.

Type:

The manifest data as loaded

property ready: bool

Returns whether the manifest is ready for use.

This property returns True if the manifest has been successfully validated and is in a valid state. A manifest is considered ready if all required sections—API version, kind, metadata, and spec—are present and non-empty. This property is useful for checking the readiness of a manifest before attempting to use it in downstream processes or integrations.

Returns:

True if the manifest is valid and ready to be used, otherwise False.

recursive_sort_dict(data)

Recursively sorts a dictionary by its keys.

This method takes a dictionary and returns a new dictionary with all keys sorted in ascending order. If any values are also dictionaries, they will be sorted recursively as well.

Parameters:

data (dict[str, Any]) – The dictionary to sort.

Returns:

A new dictionary with all keys sorted.

Return type:

dict[str, Any]

rfc1034_compliant_str(name)

Converts a string to an RFC 1034 compliant format.

This method takes a string and converts it to a format that complies with RFC 1034, which is commonly used for domain names. It replaces invalid characters with hyphens and ensures the resulting string is lowercase.

Parameters:

name (str) – The string to convert to RFC 1034 compliant format.

Returns:

The converted string in RFC 1034 compliant format.

Return type:

str

rfc1034_compliant_to_snake(name)

Converts an RFC 1034 compliant string to snake_case.

This method takes a string in RFC 1034 compliant format and converts it to snake_case. It replaces hyphens with underscores and ensures the resulting string is lowercase.

Parameters:

name (str) – The RFC 1034 compliant string to convert.

Returns:

The converted string in snake_case.

Return type:

str

smarter_build_absolute_uri(request)

Attempts to get the absolute URI from a request object.

This utility function tries to retrieve the request URL from any valid child class of django.http.HttpRequest. It is especially useful in unit tests or scenarios where the request object may not implement build_absolute_uri().

Parameters:

request (HttpRequest) – The request object.

Returns:

The absolute request URL.

Return type:

str | None

Raises:

SmarterValueError – If the URI cannot be built from the request.

sorted_dict(data)

Returns a new dictionary with keys sorted.

Parameters:

data (dict[str, Any]) – The dictionary to sort.

Returns:

A new dictionary with sorted keys.

Return type:

dict[str, Any]

property source: str

Describe where the manifest came from (manifest, file path or url), for log messages.

property specification: dict[SAMKeys, Any]

The specification of required manifest keys that the manifest is validated against.

to_camel_case(data, convert_values=False)

Converts a snake_case string to camelCase.

This method takes a string in snake_case format and converts it to camelCase. It is useful for standardizing naming conventions across different formats.

Parameters:
  • data (TypeVar(ConvertibleCaseT, str, dict[str, Any], list[Any])) – The snake_case string to convert.

  • convert_values (bool) – Whether to convert the values of dictionaries and lists recursively.

Returns:

The converted string in camelCase.

Return type:

TypeVar(ConvertibleCaseT, str, dict[str, Any], list[Any])

to_json()[source]

Return the manifest data as a dict, with its keys sorted, for logging.

Return type:

dict[str, Any]

Returns:

The manifest data, as a sorted dict.

to_snake_case(data, convert_values=False)

Converts a camelCase or PascalCase string to snake_case.

This method takes a string in camelCase or PascalCase format and converts it to snake_case. It is useful for standardizing naming conventions across different formats.

Parameters:
  • data (TypeVar(ConvertibleCaseT, str, dict[str, Any], list[Any])) – The camelCase or PascalCase string to convert.

  • convert_values (bool) – Whether to convert the values of dictionaries and lists recursively.

Returns:

The converted string in snake_case.

Return type:

TypeVar(ConvertibleCaseT, str, dict[str, Any], list[Any])

property unformatted_class_name: str

Returns the raw class name without formatting.

Returns:

The unformatted class name as a string.

Return type:

str

This is useful for logging or serialization where the plain class name is needed.

property url: str | None

The url of the manifest this loader was created with, if any.

validate_manifest()[source]

Validate the manifest data.

It checks the manifest’s structure only. Its values are validated later, by the kind’s Pydantic model, so a wrong data type or value is not reported here.

The validation process includes the following steps:

  1. If the manifest data is empty, it logs a warning and returns, without raising.

  2. Ensures that the manifest data is in a supported format (JSON or YAML).

  3. Ensures that the parsed manifest is a dict.

  4. Ensures that it has the required top-level keys: apiVersion, kind, metadata and spec.

  5. Raises a SAMLoaderError with a descriptive message if any of these checks fails.

This method is intended to be called automatically during loader initialization, but can also be invoked manually to re-validate the manifest after modifications.

Subclasses may override this method to implement additional or custom validation logic as needed.

Raises:

SAMLoaderError – If the manifest data is in an unsupported format, is not a dict, or is missing a required top-level key.

Return type:

None

property yaml_data: str | None

The manifest data as a YAML string, or None if its format is unknown.

exception smarter.lib.manifest.loader.SAMLoaderError(message='')[source]

The error that SAMLoader and validate_key() raise.

It is raised for a manifest that cannot be loaded or validated: an unsupported api version, no manifest source or more than one, a format that is neither JSON nor YAML, or a missing top-level key.

Parameters:

message (str) – What went wrong, e.g. Missing required key: apiVersion.

try:
    loader = SAMLoader(manifest=my_manifest)
except SAMLoaderError as err:
    print(f"Manifest validation failed: {err}")

Note

The get_formatted_err_message property is a fixed label, Smarter API Manifest Loader Error, for logging and display.

__init__(message='')
add_note(note, /)

Add a note to the exception

args
property get_formatted_err_message: str

Return the static formatted error message for SAMLoader errors.

with_traceback(tb, /)

Set self.__traceback__ to tb and return self.

smarter.lib.manifest.loader.validate_key(key, key_value, spec)[source]

Validate a manifest key and its value against a specification.

This function enforces schema rules for manifest keys and values, supporting multiple validation strategies based on the type of the specification provided. It is a foundational utility for manifest validation and is used throughout the Smarter API Manifest Loader system.

Parameters

Parameters:
  • key (str | Enum) – The manifest key to validate. Must be a string or an Enum with a string value.

  • key_value (Any) – The value associated with the manifest key. The expected type and constraints depend on the specification.

  • spec (Any) – The specification against which the key and value are validated. This can be: - A list: The value must be one of the items in the list. - A tuple: The first element is the expected data type; the second is a list of key options (e.g., required, optional, readonly). - Any other type: The value must match the type and value of the spec.

Return type:

None

Validation Logic

  • If the specification is a list, the value must be present in the list.

  • If the specification is a tuple, the value’s type and presence are validated according to the tuple’s contents:
    • The first element is the expected type (e.g., str, dict).

    • The second element is a list of options, such as REQUIRED, OPTIONAL, or READONLY.

    • If the key is required and the value is missing or empty, an error is raised.

    • If the value’s type does not match the expected type, an error is raised.

  • If the specification is any other type, the value must match both the type and the value of the spec.

Examples

# Example 1: List validation
validate_key("color", "red", ["red", "green", "blue"])
# Passes if "red" is in the list

# Example 2: Tuple validation
validate_key("name", "Widget", (str, [SAMSpecificationKeyOptions.REQUIRED]))
# Passes if "Widget" is a string and the key is required

# Example 3: Exact value validation
validate_key("apiVersion", "v1", "v1")
# Passes if the value matches the spec exactly

Note

  • All manifest keys must be strings. If an Enum is provided, its value is used.

  • If the value is missing or of the wrong type, a SAMLoaderError is raised with a descriptive message.

  • This function is intended for internal use in manifest validation routines and is not typically called directly.

Attention

  • Ensure that the specification accurately reflects the schema requirements for your manifest type.

  • Improper use of this function may result in manifest validation failures or runtime errors.

Raises:

SAMLoaderError – If the key or value does not conform to the specification.