Skip to content

kvendingoldo/ordnung

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

50 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Ordnung

Python 3.8+ License: Apache 2.0 Code style: ruff


Logo

Ordnung is a Python utility for sorting YAML and JSON files alphabetically by keys. It supports batch processing, directory traversal, pattern matching, and is designed for both development and CI/CD workflows.

✨ Features

  • πŸ”§ Automatic file type detection - Supports .json, .yaml, and .yml files (including both single-document and multi-document YAML files with --- separators)
  • πŸ“ Batch processing - Process multiple files, directories, or use glob patterns
  • πŸ”„ Recursive sorting - Sorts nested dictionaries, lists, and complex data structures
  • 🎯 Pattern matching - Filter files with glob patterns and regex
  • 🚫 File exclusion - Exclude files matching patterns (supports glob and regex)
  • βœ… Check mode - Verify formatting without rewriting files (CI-friendly)
  • πŸ” Data validation - Validate that all data structures are preserved after sorting
  • πŸ“ Custom indentation - Configurable indentation for both JSON and YAML
  • πŸ”’ Array sorting - Optionally sort arrays of objects by first key value
  • 🌍 Unicode support - Full support for international characters
  • ⚑ Fast and efficient - Optimized for large files and batch operations

πŸš€ Quick Start

Installation

# Install from PyPI
pip install ordnung

# Or install from source
git clone <repository-url>
cd ordnung
pip install -e .

Basic Usage

# Sort a single file (overwrites original)
ordnung config.json
ordnung settings.yaml

# Sort and save to new file
ordnung input.json -o sorted.json

# Sort multiple files
ordnung file1.json file2.yaml file3.yml

πŸ“– Usage Examples

File Processing

Ordnung supports both single-document YAML and multi-document YAML files (with --- separators). Each document in a multi-document YAML file will be sorted individually, and document order can be preserved or sorted using the appropriate flag.

# Sort single files
ordnung config.json
ordnung settings.yaml

# Sort multi-document YAML (all docs in file will be sorted)
ordnung multi-docs.yaml

# Sort multiple files at once
ordnung file1.json file2.yaml file3.yml

# Save to new file (only for single file input)
ordnung input.json -o sorted.json

Directory Processing

# Sort all JSON/YAML files in a directory
ordnung ./configs

# Recursively process subdirectories
ordnung ./configs --recursive

# Use glob patterns
ordnung './data/**/*.json' --pattern
ordnung './configs/*.yaml' --pattern

Advanced Filtering

# Filter with regex patterns
ordnung ./mydir --regex '.*\.ya?ml$'
ordnung ./data --regex '.*_prod\.json$'
ordnung ./configs --regex '.*_config\.ya?ml$'

# Combine recursive search with regex
ordnung ./data --recursive --regex '.*\.json$'

File Exclusion

# Exclude files matching patterns
ordnung ./data --exclude '*.tmp' --exclude 'backup_*'
ordnung ./configs --exclude '.*\.bak$' --recursive

# Exclude with multiple patterns
ordnung ./data --exclude '*.tmp' --exclude '*.bak' --exclude 'backup_*'

# Combine exclusion with other filters
ordnung ./data --exclude '*.tmp' --regex '.*\.json$' --recursive

CI/CD Integration

# Check mode: verify formatting without modifying files
ordnung ./data --check

# Use in CI pipeline (exits with error if files need formatting)
ordnung ./configs --recursive --check

Data Validation

# Validate data preservation during sorting
ordnung config.json --validate
ordnung ./data --validate --recursive

# Combine validation with other options
ordnung ./configs --validate --exclude '*.tmp' --recursive

# Use validation in CI/CD pipelines
ordnung ./data --validate --check --recursive

Custom Formatting

# Custom indentation
ordnung config.json --json-indent 4
ordnung settings.yaml --yaml-indent 4

# Sort arrays of objects by first key value
ordnung data.json --sort-arrays-by-first-key

Debugging

# Verbose logging
ordnung config.json --log-level DEBUG

# Quiet mode
ordnung config.json --log-level ERROR

πŸ”§ Command Line Options

Option Description Example
inputs Input file(s), directory(ies), or glob pattern(s) config.json
-o, --output Output file path (single file only) -o sorted.json
--json-indent JSON indentation spaces (default: 2) --json-indent 4
--yaml-indent YAML indentation spaces (default: 2) --yaml-indent 4
--recursive Recursively search directories --recursive
--pattern Treat inputs as glob patterns --pattern
--regex Filter files with regex --regex '.*\.json$'
--exclude Exclude files matching pattern (can be used multiple times) --exclude '*.tmp'
--validate Validate that all data structures are preserved after sorting --validate
--check Check formatting without modifying --check
--sort-arrays-by-first-key Sort arrays by first key value --sort-arrays-by-first-key
--sort-docs-by-first-key For YAML files with multiple documents (--- separated), sort documents by the type and string value of the first key's value in each document, for robust and deterministic ordering. Documents with string values come before int, then float, then dict, then list, etc. For example: all docs whose first key is a string value are first, then int, then dict, then list. --sort-docs-by-first-key
--log-level Set logging level --log-level DEBUG

πŸ“‹ Examples

Before and After

Input JSON:

{
  "zebra": "striped animal",
  "apple": "red fruit",
  "settings": {
    "theme": "dark",
    "language": "en"
  }
}

Output JSON:

{
  "apple": "red fruit",
  "settings": {
    "language": "en",
    "theme": "dark"
  },
  "zebra": "striped animal"
}

Array Sorting

Input (with --sort-arrays-by-first-key):

{
  "users": [
    {"name": "Charlie", "id": 3},
    {"name": "Alice", "id": 1},
    {"name": "Bob", "id": 2}
  ]
}

Output:

{
  "users": [
    {"name": "Alice", "id": 1},
    {"name": "Bob", "id": 2},
    {"name": "Charlie", "id": 3}
  ]
}

Data Validation

The --validate option ensures that all data structures, keys, and values are preserved during sorting. This is particularly useful for critical configuration files where data integrity is paramount.

What gets validated:

  • βœ… All dictionary keys are preserved
  • βœ… All array elements are preserved (order may change)
  • βœ… All nested structures are intact
  • βœ… Data types remain consistent
  • βœ… Values are unchanged

Example validation output:

$ ordnung config.json --validate
INFO: Detected file type: JSON
INFO: Loaded data from: config.json
INFO: Data sorted successfully
INFO: Validating data preservation...
INFO: Data validation passed - all structures preserved

If validation fails:

$ ordnung config.json --validate
ERROR: Data validation failed! The following issues were found:
ERROR:   Missing keys at root: ['important_key']
ERROR:   Value mismatch at settings.debug: true vs false
ERROR: Data validation failed - data structures were not preserved during sorting

πŸ§ͺ Testing

# Run all tests
pytest

# Run with coverage
pytest --cov=src/ordnung

# Run specific test categories
pytest tests/test_dedicated.py
pytest tests/test_file_sorter.py

πŸ—οΈ Development

Project Structure

ordnung/
β”œβ”€β”€ src/
β”‚   └── ordnung/
β”‚       β”œβ”€β”€ __init__.py
β”‚       └── file_sorter.py
β”œβ”€β”€ tests/
β”‚   β”œβ”€β”€ conftest.py          # Shared test helpers
β”‚   β”œβ”€β”€ test_file_sorter.py  # Auto-generated file-based tests
β”‚   β”œβ”€β”€ test_dedicated.py    # Dedicated scenario tests
β”‚   └── data/
β”‚       β”œβ”€β”€ pass/            # Valid test files
β”‚       └── fail/            # Invalid test files
β”œβ”€β”€ pyproject.toml
└── README.md

Setup Development Environment

# Clone repository
git clone <repository-url>
cd ordnung

# Install in development mode
pip install -e .

# Install development dependencies
pip install -e ".[dev]"

# Run linting
ruff check .

# Run tests
pytest

🀝 Contributing

Contributions are welcome! Please:

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Make your changes
  4. Add or update tests
  5. Run the test suite (pytest)
  6. Ensure code quality (ruff check .)
  7. Commit your changes (git commit -m 'Add amazing feature')
  8. Push to the branch (git push origin feature/amazing-feature)
  9. Open a Pull Request

Code Style

  • Follow PEP 8 style guidelines
  • Use ruff for linting and formatting
  • Write comprehensive tests for new features
  • Update documentation as needed

πŸ“„ License

This project is licensed under the Apache License, Version 2.0 - see the LICENSE file for details.

πŸ™ Acknowledgments

  • Built with PyYAML for YAML processing
  • Uses ruff for code quality
  • Inspired by the need for consistent configuration file formatting

Ordnung - Bringing order to your configuration files! 🎯

Sponsor this project

 

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •