Skip to main content

A robust, secure Python reference parser for the CommerceTXT protocol.

Project description

CommerceTXT Python Reference Parser (v1.0.1)

A robust, secure, and production-ready Python parser for the CommerceTXT protocol. This library handles parsing, validation, fractal inheritance, and advanced security checks for commerce.txt files.

Key Features

  • Fractal Inheritance: Support for resolving and merging nested directives across multiple files.
  • Compliance Validation: Sequential Tier-based validation (Tier 1: Core, Tier 2: Commercial, Tier 3: Rich Metadata).
  • High-Performance Caching: Built-in LRU caching to skip redundant parsing for unchanged files.
  • Async Support: Concurrent parsing for bulk file processing using AsyncCommerceTXTParser.
  • AI Readiness Bridge: Native support for generating clean, low-token prompts for Large Language Models (LLMs).
  • Enterprise-Grade Security: Blocks localhost, private IP ranges, and exotic IP notations (Octal, Hex, Integer).

Installation & Setup

Ensure you have Python 3.8+ installed.

From the root of your project

export PYTHONPATH=$PYTHONPATH:.

CLI Usage

Validate files or generate AI-ready prompts instantly.

Basic Validation:

python -m commercetxt.cli path/to/commerce.txt

Generate AI Prompt:

python -m commercetxt.cli product.txt --prompt

Advanced Usage:

python -m commercetxt.cli commerce.txt --json --metrics --log-level DEBUG

Library Usage

  1. Basic Parsing & Validation:
from commercetxt import CommerceTXTParser, CommerceTXTValidator
parser = CommerceTXTParser()
result = parser.parse(content)
validator = CommerceTXTValidator(strict=False)
validator.validate(result)
  1. High-Speed Caching:
from commercetxt.cache import parse_cached
result = parse_cached(content)
  1. Bulk Async Parsing:
import asyncio
from commercetxt.async_parser import AsyncCommerceTXTParser
async def main():
    async_parser = AsyncCommerceTXTParser()
    results = await async_parser.parse_many([file1, file2])
asyncio.run(main())

Security Limits

  • MAX_FILE_SIZE: 10 MB
  • MAX_SECTIONS: 1000
  • MAX_LINE_LENGTH: 100 KB
  • MAX_NESTING: 100
  • Blocked IPs: Any Reserved (Prevents SSRF)

Project Structure

  • parser.py: Core parsing logic with optimized Regex compilation.
  • validator.py: Business logic divided into compliance Tiers.
  • security.py: Network security utilities (IP and URL validation).
  • bridge.py: Token-efficient prompt generator for LLM integration.
  • async_parser.py: Async engine for concurrent processing.
  • cache.py: LRU caching implementation for performance.
  • resolver.py: Locale resolution and fractal inheritance logic.
  • metrics.py: Singleton for real-time performance tracking.

Running Tests

Requires pytest and pytest-asyncio.

pytest parsers/python/tests/
python -m pytest --cov=commercetxt --cov-report=html

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

commercetxt-1.0.1.tar.gz (33.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

commercetxt-1.0.1-py3-none-any.whl (20.6 kB view details)

Uploaded Python 3

File details

Details for the file commercetxt-1.0.1.tar.gz.

File metadata

  • Download URL: commercetxt-1.0.1.tar.gz
  • Upload date:
  • Size: 33.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.4

File hashes

Hashes for commercetxt-1.0.1.tar.gz
Algorithm Hash digest
SHA256 32f06b95dfe3ec9252c45179f448745943e46e8199673debf10f0ed47973cf39
MD5 463c0f0528c5e1b1d5c7720e7e9817f1
BLAKE2b-256 7002b54c4a42bc987c290f140f40e6c1a1b986ec1c04caf2e6e93b80204ed808

See more details on using hashes here.

File details

Details for the file commercetxt-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: commercetxt-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 20.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.4

File hashes

Hashes for commercetxt-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b5e5e33135a889b09cce025aa3b52c3a4538c27262bf4e1e80215b45b9adb702
MD5 0264d98ab1b751f9630861a1443704eb
BLAKE2b-256 dcdaeed6c163a230211713e47f8727af516b6531ce0db45dcc26a367bf18a2bf

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page