Quickstart & Examples
Relevant source files
The following files were used as context for generating this wiki page:
The flext-target-ldif module is a Singer-compatible target designed to output data in LDAP Data Interchange Format (LDIF). It provides a structured way to transform JSON-based Singer records into standardized LDIF entries, supporting complex Distinguished Name (DN) generation, attribute mapping, and RFC 2849 compliant line wrapping.
Sources: README.md:10-12, src/flext_target_ldif/target.py:18-21
Getting Started
Installation and CLI Usage
The target can be invoked via the command line after installation. It provides two primary entry points: flext-target-ldif and target-ldif.
# Example usage via pipeline
tap-some-source | target-ldif --config config.json
Sources: pyproject.toml:96-98, src/flext_target_ldif/target.py:44-46
Basic Configuration
A typical configuration requires defining the output path and the DN template. The DN template uses placeholder syntax to map record fields into the LDAP hierarchy.
| Field | Type | Default | Description |
|---|---|---|---|
output_path | string | ./output | Directory where LDIF files will be saved. |
dn_template | string | uid={uid},ou=users,dc=example,dc=com | Template for generating the Distinguished Name. |
file_naming_pattern | string | {stream_name}_{timestamp}.ldif | Pattern for generated filenames. |
Sources: src/flext_target_ldif/target.py:32-36, tests/unit/test_target.py:220-226
Architecture and Data Flow
The system processes data through a hierarchy starting from the FlextTargetLdif class, which manages multiple Sink instances (one per stream). Each sink utilizes a FlextTargetLdifWriter to handle the actual file operations and formatting.
Component Relationship
The following diagram illustrates the relationship between the main target components and the data writing layer.
The FlextTargetLdif class acts as the orchestrator, delegating stream-specific logic to Sink objects which encapsulate the FlextTargetLdifWriter.
Sources: src/flext_target_ldif/target.py:21-65, src/flext_target_ldif/models.py:182-243, src/flext_target_ldif/writer.py:32-84
Data Processing Flow
When a record enters the target, it undergoes transformation and validation before being written to the buffer.
The flow ensures that every record is correctly mapped to its LDAP attributes and that sensitive or non-ASCII characters are handled via Base64 encoding.
Sources: src/flext_target_ldif/writer.py:107-130, src/flext_target_ldif/utilities.py:84-110
Implementation Examples
Record Transformation
The RecordTransformer utility allows for standardizing data before it reaches the LDIF file. It includes built-in handlers for common LDAP attributes.
# Example: Using built-in transformers
from flext_target_ldif.utilities import FlextTargetLdifUtilities as u
# Normalizes names to title case
clean_name = u.TargetLdif.RecordTransformer.transform_name("john doe")
# Output: "John Doe"
# Standardizes booleans to LDAP format
ldap_bool = u.TargetLdif.RecordTransformer.transform_boolean(True)
# Output: "TRUE"
Sources: src/flext_target_ldif/utilities.py:205-236
Configuration with Attribute Mapping
Attribute mapping allows you to translate Singer record keys (e.g., email_address) to LDAP-specific attribute names (e.g., mail).
{
"output_path": "./export",
"dn_template": "cn={full_name},ou=people,dc=company,dc=com",
"attribute_mapping": {
"email_address": "mail",
"username": "uid",
"full_name": "cn"
},
"ldif_options": {
"line_length": 78,
"base64_encode": false,
"include_timestamps": true
}
}
Sources: tests/unit/test_target.py:47-54, src/flext_target_ldif/writer.py:39-43
Handling Special Characters (Base64)
The writer automatically detects values that require Base64 encoding according to LDIF specifications, such as values starting with a space, a colon, or containing non-ASCII characters.
# Internal logic for Base64 detection
def _needs_base64_encoding(self, value: str) -> bool:
if value and value[0] in {" ", ":"}:
return True
try:
value.encode("ascii")
except UnicodeEncodeError:
return True
return "\n" in value or "\r" in value
Sources: src/flext_target_ldif/writer.py:155-164
Summary
flext-target-ldif provides a robust, standardized bridge between Singer-based data pipelines and LDAP-compatible systems. By utilizing Pydantic-based settings for validation and a dedicated writer layer that handles RFC-compliant formatting, it ensures that output LDIF files are both accurate and ready for directory server import.
Sources: src/flext_target_ldif/target.py:21-30, src/flext_target_ldif/models.py:53-70