yaplon

Python 3-based commandline converter YAML ↔ JSON ↔ PLIST

View the Project on GitHub twardoch/yaplon

Yaplon: Yet Another Plist/JSON/YAML/XML/CSV Converter

Yaplon is a versatile command-line tool for converting between various common structured data formats: JSON, YAML, Property Lists (PLIST, both binary and XML), XML, and CSV (read-only for CSV input). It’s designed for developers, data analysts, system administrators, or anyone who needs to quickly and easily transform data from one format to another.

Why Yaplon?

In a world of diverse applications and services, data often needs to be exchanged and processed in different formats. Yaplon simplifies this by providing:

Installation

Install the latest stable release from the Python Package Index (PyPI):

pip3 install --user --upgrade yaplon

From GitHub (Development Version)

To install the latest development version directly from GitHub:

pip3 install --user --upgrade git+https://github.com/twardoch/yaplon

For Development

If you want to contribute to Yaplon or modify it:

  1. Clone the repository:
    git clone https://github.com/twardoch/yaplon.git
    cd yaplon
    
  2. Create and activate a virtual environment (recommended):
    python3 -m venv venv
    source venv/bin/activate  # On macOS/Linux
    # Or: venv\Scripts\activate  # On Windows
    
  3. Install in editable mode with development dependencies:
    pip install -e .[dev]
    

    This installs the package so that changes you make to the source code are immediately reflected. Development dependencies include tools for testing, linting, and packaging.

Usage

Yaplon can be invoked using the main yaplon command followed by a conversion subcommand (e.g., j2y for JSON to YAML), or by using dedicated shortcut scripts (e.g., json22yaml).

Main Command Structure

yaplon <command> -i <input_file> -o <output_file> [options]

Conversion Commands

The following commands define the input and output formats:

Yaplon supports JSON input with C-style comments (// ... and /* ... */) and trailing commas in objects/arrays (similar to JSON5).

General Options

These options are available for most conversion commands:

Format-Specific Options

Dedicated CLI Tools

For convenience, Yaplon also installs shortcut scripts that correspond directly to the conversion commands:

Note: These scripts use 22 (e.g., json22yaml) instead of 2 to avoid potential conflicts with other similarly named conversion tools you might have installed.

Examples

JSON to YAML (file to file using dedicated tool):

json22yaml -i input.json -o output.yaml

JSON to YAML (using pipes and main yaplon command):

cat input.json | yaplon j2y > output.yaml

PLIST to JSON (minified, preserving binary as base64 strings):

yaplon p2j -m -b -i input.plist -o output.json

CSV with header to XML (using a specific column as key and custom root tag):

yaplon c2x -H -k 1 --root "Entries" -i data.csv > data.xml

Technical Details

This section describes the internal workings of Yaplon.

Core Architecture

Yaplon’s conversion process generally follows these steps:

  1. Input: Data is read from an input file or stdin.
  2. Parsing (reader.py): The input data is parsed into a standardized Python data structure, primarily using collections.OrderedDict. This preserves key order from the source format where applicable.
    • The reader.sort_ordereddict function can be invoked via the -s/--sort CLI option to recursively sort these OrderedDicts by key.
  3. Serialization (writer.py): The Python OrderedDict object is then serialized into the target output format.
  4. Output: The resulting data is written to an output file or stdout.

The Command Line Interface (CLI) is built using the Click library.

Format-Specific Handling

Data Representation

Extensibility (Conceptual)

While not a formalized plugin system, adding support for a new format would conceptually involve:

  1. Creating new reader and writer functions in yaplon/reader.py and yaplon/writer.py respectively.
  2. The reader should parse the new format into an OrderedDict (or list of OrderedDicts, etc.).
  3. The writer should take an OrderedDict and serialize it to the new format.
  4. Adding corresponding CLI commands and options in yaplon/__main__.py.
  5. Writing comprehensive tests for the new conversions.

Contributing

Contributions to Yaplon are welcome! Whether it’s bug reports, feature suggestions, documentation improvements, or code contributions, your help is appreciated.

Development Setup

Please refer to the “Installation > For Development” section above for instructions on how to clone the repository and set up a development environment.

Workflow

  1. Fork the repository on GitHub.
  2. Create a new branch for your feature or bug fix: git checkout -b my-feature-branch.
  3. Make your changes.
  4. Write tests for your changes in the tests/ directory.
  5. Run tests: make test or python3 -m pytest. Ensure all tests pass.
  6. Lint and format your code:
    • make lint (uses Flake8 and Black to check style)
    • make format (uses Black to automatically format code)
  7. Commit your changes with a clear and descriptive commit message.
  8. Push your branch to your fork: git push origin my-feature-branch.
  9. Open a Pull Request against the main branch of the twardoch/yaplon repository.

Issue Tracking

Changelog

For significant changes, please add an entry to the CHANGELOG.md file, describing the change and referencing the relevant issue or PR if applicable.

Code Style

Yaplon uses Black for code formatting and Flake8 for linting. Please ensure your contributions adhere to these standards by running make format and make lint.

Testing

The project uses pytest for testing. New features should include corresponding tests, and bug fixes should ideally include a test that demonstrates the bug and verifies the fix.

Changelog

See CHANGELOG.md for a history of changes to the project.

License

Yaplon is distributed under the MIT License.