Skip to content

Detector

detector

Format detection via parse-based heuristics.

Format

Bases: Enum

Supported input formats.

Source code in packages/axm-smelt/src/axm_smelt/core/models.py
Python
class Format(enum.Enum):
    """Supported input formats."""

    JSON = "json"
    YAML = "yaml"
    XML = "xml"
    TOML = "toml"
    CSV = "csv"
    MARKDOWN = "markdown"
    TEXT = "text"

detect_format(text)

Detect the format of text using parse-based heuristics.

Source code in packages/axm-smelt/src/axm_smelt/core/detector.py
Python
def detect_format(text: str) -> Format:
    """Detect the format of *text* using parse-based heuristics."""
    fmt, _ = detect_format_parsed(text)
    return fmt

detect_format_parsed(text)

Detect format and return (format, parsed_data).

parsed_data is non-None only when the format is JSON, giving the caller the already-parsed object so it can be injected into a :class:SmeltContext without a redundant json.loads call.

Source code in packages/axm-smelt/src/axm_smelt/core/detector.py
Python
def detect_format_parsed(text: str) -> tuple[Format, object | None]:
    """Detect format and return ``(format, parsed_data)``.

    *parsed_data* is non-None only when the format is JSON, giving the
    caller the already-parsed object so it can be injected into a
    :class:`SmeltContext` without a redundant ``json.loads`` call.
    """
    stripped = text.strip()
    if not stripped:
        return Format.TEXT, None

    # JSON probe — capture the parsed object
    if stripped[0] in ("{", "["):
        try:
            data = json.loads(stripped)
            return Format.JSON, data
        except (json.JSONDecodeError, ValueError):
            pass

    # Remaining probes (XML, YAML, Markdown) — no parsed data
    for probe in (_try_xml, _try_yaml, _try_markdown):
        result = probe(stripped)
        if result is not None:
            return result, None

    return Format.TEXT, None