Skip to main content

Parser for DTL (Domain Transport Language) - Ultra-compact, cryptographic data transport format

Project description

๐Ÿงฌ DTL Parser

PyPI version Python License: MIT

Parser for DTL (Domain Transport Language) - Ultra-compact, schema-driven, cryptographic data transport format.

๐Ÿ“– Specification: dtlaz.org

Installation

pip install dtl-parser

Quick Start

import dtl

# ๐Ÿง  INTELLIGENT PARSING - Auto-detects input type!

# File path
doc = dtl.parse("data.dtl")

# Raw string
doc = dtl.parse("@dtlv1.0^dtHC^...")

# Bytes
doc = dtl.parse(b"@dtlv1.0^dtHC^...")

# URL
doc = dtl.parse("https://example.com/data.dtl")

# File handle
doc = dtl.parse(open("data.dtl"))

# StringIO / BytesIO
doc = dtl.parse(io.StringIO(content))

# Dict containing DTL
doc = dtl.parse({"_dtl": content})

# Base64 encoded
doc = dtl.parse(base64_encoded_string)

# pathlib.Path
doc = dtl.parse(Path("data.dtl"))

# Access tables
for row in doc.get_table("USERS"):
    print(row["name"])

# Get config values
region = doc.get_config("default_region")

๐Ÿง  Intelligent Input Detection

The parser automatically detects input type:

Input Detection Action
"data.dtl" File path Read file
"@dtlv1.0^..." DTL string Parse directly
"https://..." URL Fetch & parse
b"@dtlv1.0..." Bytes Decode & parse
open(file) File handle Read & parse
io.StringIO() Stream Read & parse
{"_dtl": ...} Dict Extract & parse
Path("file") pathlib.Path Read file
Base64 string Encoded DTL Decode & parse
None stdin Read from stdin

Helper Functions

# Check if input is valid DTL
if dtl.is_dtl(some_input):
    doc = dtl.parse(some_input)

# Detect input type
input_type = dtl.detect_input_type(some_input)
# Returns: "file", "string", "bytes", "url", "stream", "dict", "stdin"

๐Ÿ”„ Format Converters

Convert between DTL and other formats bidirectionally:

JSON โ†” DTL

import dtl

# JSON โ†’ DTL
doc = dtl.from_json({"users": [{"id": 1, "name": "Alice"}]})
doc = dtl.from_json('{"data": [1,2,3]}')  # String also works

# DTL โ†’ JSON
json_str = dtl.to_json(doc, pretty=True)

CSV โ†” DTL

# CSV โ†’ DTL
doc = dtl.from_csv("data.csv", table_name="USERS")
doc = dtl.from_csv("id,name\n1,Alice\n2,Bob")  # String content

# DTL โ†’ CSV
csv_str = dtl.to_csv(doc, table_name="USERS", delimiter=",")

Excel โ†” DTL

# Excel โ†’ DTL (requires openpyxl)
doc = dtl.from_excel("data.xlsx")
doc = dtl.from_excel("data.xlsx", sheet_name="Sheet1")

# DTL โ†’ Excel
dtl.to_excel(doc, "output.xlsx")
excel_bytes = dtl.to_excel(doc)  # Returns bytes

XML โ†” DTL

# XML โ†’ DTL
doc = dtl.from_xml("<data><user id='1'>Alice</user></data>")
doc = dtl.from_xml("data.xml")

# DTL โ†’ XML
xml_str = dtl.to_xml(doc, root_name="data")

HL7 โ†” DTL (Healthcare)

# HL7 โ†’ DTL (auto-sets domain to dtHC)
doc = dtl.from_hl7(hl7_message)
print(doc.header.domain)  # dtHC

# Access patient data
pid = doc.get_table("PID")[0]
print(pid["patient_name"])

# DTL โ†’ HL7
hl7_str = dtl.to_hl7(doc)

JWT โ†” DTL (Auth)

# JWT โ†’ DTL (auto-sets domain to dtID)
doc = dtl.from_jwt(jwt_token)
claims = doc.get_table("CLAIMS")
print(claims)

# DTL โ†’ JWT
token = dtl.to_jwt(doc, secret="optional-secret")

YAML โ†” DTL

# YAML โ†’ DTL (requires PyYAML)
doc = dtl.from_yaml("config.yaml")

# DTL โ†’ YAML
yaml_str = dtl.to_yaml(doc)

Auto-detect Format

# Auto-detects format and converts
doc = dtl.convert_to_dtl("data.json")      # Detects JSON
doc = dtl.convert_to_dtl("data.csv")       # Detects CSV
doc = dtl.convert_to_dtl("MSH|^~\\&|...")  # Detects HL7
doc = dtl.convert_to_dtl("<xml>...</xml>") # Detects XML

# Convert DTL to any format
json_str = dtl.convert_from_dtl(doc, "json")
csv_str = dtl.convert_from_dtl(doc, "csv")
xml_str = dtl.convert_from_dtl(doc, "xml")

Conversion Matrix

From โ†’ To JSON CSV Excel XML HL7 JWT YAML DTL
JSON - โœ… โœ… โœ… - - โœ… โœ…
CSV โœ… - โœ… โœ… - - โœ… โœ…
Excel โœ… โœ… - โœ… - - โœ… โœ…
XML โœ… โœ… โœ… - - - โœ… โœ…
HL7 โœ… โœ… โœ… โœ… - - โœ… โœ…
JWT โœ… - - โœ… - - โœ… โœ…
YAML โœ… โœ… โœ… โœ… - - - โœ…
DTL โœ… โœ… โœ… โœ… โœ… โœ… โœ… -

DTL Format

@dtlv1.0^dtHC^pProfile^c1^s1^w0^h0
@sec^fh0000^w0^sg0^ch0

TABLE_NAME|field:type,field:type|rowcount|S|W|C
value|value|value
value|value|value

Example

@dtlv1.0^dtHC^pMedical^c1^s1^w0^h0
@sec^fh0000^w0^sg0^chNONE

PATIENTS|id:s,name:s,dob:D|3|S1|W0|C1
P001|Ahmed Ali|1990-05-15
P002|Sara Khan|1985-03-22
P003|John Smith|1978-11-08

CONFIG|key:s,value:s|2|S0|W0|C1
region|uae
version|1.0

API Reference

Parsing

import dtl

# Parse file
doc = dtl.parse_file("data.dtl")
doc = dtl.parse_file("data.dtl", strict=True)  # Raise on validation errors

# Parse string
doc = dtl.parse(content)
doc = dtl.parse(content, strict=True)

Accessing Data

# Get table rows as list of dicts
users = doc.get_table("USERS")

# Get table object with metadata
table = doc.get_table_obj("USERS")
print(table.schema)       # {'id': 's', 'name': 's'}
print(table.row_count)    # 3
print(table.security_mode)  # S1

# Get table as dict keyed by field
users_by_id = doc.get_table_as_dict("USERS", "id")
print(users_by_id["P001"]["name"])  # Ahmed Ali

# Filter rows
active_users = table.filter(status="active")

# Find one row
admin = table.find_one(role="admin")

# Get column values
names = table.get_column("name")  # ['Ahmed', 'Sara', 'John']

Configuration

# Get config from CONFIG table
region = doc.get_config("region")
region = doc.get_config("region", default="global")

# Type-safe config
enabled = doc.get_config_bool("feature_enabled")
count = doc.get_config_int("max_items", default=100)
rate = doc.get_config_float("threshold", default=0.5)

Header Information

# Access header
print(doc.header.domain)    # dtHC
print(doc.header.version)   # 1.0
print(doc.header.profile)   # pMedical
print(doc.header.security)  # s1
print(doc.header.web3)      # w0

Writing DTL

import dtl

# Write to string
content = dtl.write(doc)

# Write to file
dtl.write_file(doc, "output.dtl")

Validation

# Validate document
errors = doc.validate()
if errors:
    for error in errors:
        print(f"Error: {error}")

# Strict parsing (raises on error)
doc = dtl.parse_file("data.dtl", strict=True)

CLI Tool

# Parse and display
dtl parse data.dtl
dtl parse data.dtl --show-data

# Validate
dtl validate data.dtl

# Convert to JSON
dtl convert data.dtl --to json
dtl convert data.dtl --to json -o output.json

# Show info
dtl info data.dtl

# List tables
dtl tables data.dtl
dtl tables data.dtl --verbose

DTL Types

Type Description Python Type Example
s String str hello
i Integer int 42
f Float float 3.14
b Boolean bool 1 or 0
D Date date 2024-01-15
T DateTime datetime 2024-01-15T10:30:00Z
a(s) Array of strings list[str] a,b,c
a(i) Array of integers list[int] 1,2,3
e(M,F) Enum str M

Domain Codes

Code Domain Use Case
dtHC Healthcare EHR, HIPAA, clinical
dtFN Finance Transactions, audit
dtLG Legal Contracts, compliance
dtID Identity Auth tokens, SSO
dtAI AI/ML Training data, models
dtIOT IoT Sensors, edge

Security Modes

Mode Description
S0 No security (inherits file defaults)
S1 Row-level hashing (BLAKE3-256)
S2 Row-level encryption

Web3 Modes

Mode Description
W0 No Web3 integration
W1 File-level digital signature
W2 Row-level digital signatures

Error Handling

from dtl.exceptions import DTLError, DTLParseError, DTLValidationError

try:
    doc = dtl.parse_file("data.dtl", strict=True)
except DTLParseError as e:
    print(f"Parse error: {e}")
except DTLValidationError as e:
    print(f"Validation error: {e}")
except DTLError as e:
    print(f"DTL error: {e}")

Optional Dependencies

# For cryptographic features (hashing, signatures)
pip install dtl-parser[crypto]

Resources

License

MIT License - see LICENSE for details.

Author

Padam Sundar Kafle โ€” Commander of Superintelligence

dtlaz.org | AlifZetta

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

dtl_parser-1.1.0.tar.gz (26.5 kB view details)

Uploaded Source

Built Distribution

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

dtl_parser-1.1.0-py3-none-any.whl (28.5 kB view details)

Uploaded Python 3

File details

Details for the file dtl_parser-1.1.0.tar.gz.

File metadata

  • Download URL: dtl_parser-1.1.0.tar.gz
  • Upload date:
  • Size: 26.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.2

File hashes

Hashes for dtl_parser-1.1.0.tar.gz
Algorithm Hash digest
SHA256 932cf88b5846a2091f3c2603205118cf6edfbe666cc485d5bebf75a63622ef68
MD5 f347f095e94d68a325eae00e2144628e
BLAKE2b-256 dad1659ce8695cd13190450d98c96eca263535aeb23278a061bb67dbf77aabb2

See more details on using hashes here.

File details

Details for the file dtl_parser-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: dtl_parser-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 28.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.2

File hashes

Hashes for dtl_parser-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9efe8a107e05ade493197e15a7f73d0020683ddd4986c76025a1ce93b30eec43
MD5 a5580fbad0833ab239b2ef63ae5a5b4d
BLAKE2b-256 5bbb83d51eb94bb75df4af8d26e9d3ee42e2f6b776c795b5147f96a9aedf012a

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