Skip to main content

json compatible library supporting templating

Project description

tpljson

PyPI License: MIT PyPI - Python Version PyPI - Downloads
tests Coverage Status Docs

A Templating Json package that implements the official Python json interface.

tpljson works by referencing placeholder template strings into your json file... like {$.mykey}
These template values will be filled with other values that appear in the document.

Features

  • template json, referencing other fields in the current document via {$.key}
  • comments
  • colorized and context on errors
  • built-in json module compatible, all json errors raise json.JSONDecodeError and TypeError - identical to the built-in json module.

Installation

pip3 install tpljson

Usage Example 1

Define contact.json as

// this is another comment, multi-line comments are not supported.
{
  "first_name": "Peter", // first_name - comments can appear at the end of a line
  "last_name": "Henderson",
  "phone": "3338675309",
  "email": "{$.first_name}.{$.last_name}@example.org", // construct email address from 
  "contact": "{$.first_name} {$.last_name}\nCell: {$.phone}\nEmail: {$.email}"
}

Load contact.json using tpljson

import tpljson as json
d = json.load(open('contact.json'))
print(json.dumps(d, indent=4))

Output

{
    "first_name": "Peter",
    "last_name": "Henderson",
    "phone": "3338675309",
    "email": "Peter.Henderson@example.org",
    "contact": "Peter Henderson\nCell: 3338675309\nEmail: Peter.Henderson@example.org"
}

Usage Example 2

A more

Define permissions.json as:

{
  "user_id": "u381815",
  "cluster": "hive-cluster",
  "groups": ["finance", "accounting"],
  "description": "Permissions for user Nicholas",
  "create_role": [
    {"role": "{$.user_id}"}
  ],
  "grant_role": [
    {"group": "{$.user_id}", "role": "{$.user_id}"}
  ],
  "grant_privilege": [
    {"privilege": "select",  "object": "database",  "patterns": "{$.groups}", "role": "{$.user_id}"},
    {"privilege": "all",  "object": "database",  "name": "{$.user_id}", "role": "{$.user_id}"},
    {"privilege": "all",  "object": "uri",  "name": "'hdfs://{$.cluster}/user/{$.user_id}'", "role": "{$.user_id}"}
  ],
  "create_database": [
    {"name": "{$.user_id}", "owner": "{$.user_id}"}
  ],
  "sql": [
    "SHOW GRANT ROLE {$.user_id}",
    "SHOW ROLE GRANT GROUP {$.user_id}"
  ]
}

Load permissions.json using tpljson

import tpljson as json
d = json.load(open('permissions.json'))
print(json.dumps(d, indent=4))

Output:

{
    "user_id": "u381815",
    "cluster": "hive-cluster",
    "groups": [
        "finance",
        "accounting"
    ],
    "description": "Permissions for user Nicholas",
    "create_role": [
        {
            "role": "u381815"
        }
    ],
    "grant_role": [
        {
            "group": "u381815",
            "role": "u381815"
        }
    ],
    "grant_privilege": [
        {
            "privilege": "select",
            "object": "database",
            "patterns": [
                "finance",
                "accounting"
            ],
            "role": "u381815"
        },
        {
            "privilege": "all",
            "object": "database",
            "name": "u381815",
            "role": "u381815"
        },
        {
            "privilege": "all",
            "object": "uri",
            "name": "'hdfs://hive-cluster/user/u381815'",
            "role": "u381815"
        }
    ],
    "create_database": [
        {
            "name": "u381815",
            "owner": "u381815"
        }
    ],
    "sql": [
        "SHOW GRANT ROLE u381815",
        "SHOW ROLE GRANT GROUP u381815"
    ]
}

Implementation

  • Note that the order of values matters in the json, if one value depends on another.
  • commentjson is used internally to render comments, can be disabled via comments=false in load and loads.
  • errors are colorized by default via colorama

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

tpljson-0.1.5.tar.gz (15.5 kB view details)

Uploaded Source

Built Distribution

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

tpljson-0.1.5-py3-none-any.whl (10.6 kB view details)

Uploaded Python 3

File details

Details for the file tpljson-0.1.5.tar.gz.

File metadata

  • Download URL: tpljson-0.1.5.tar.gz
  • Upload date:
  • Size: 15.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.8.6

File hashes

Hashes for tpljson-0.1.5.tar.gz
Algorithm Hash digest
SHA256 ad2353f79508c0aa5a2757a99d24b130fdfe4997cdff615dc4f91b4bb14b5a03
MD5 035f5d629fe63e5552bc2c9f122359c0
BLAKE2b-256 72ab1b53284befc1fc6f3c27e268e12b18db0855fbf77b9d1a0d65f9fd78049d

See more details on using hashes here.

File details

Details for the file tpljson-0.1.5-py3-none-any.whl.

File metadata

  • Download URL: tpljson-0.1.5-py3-none-any.whl
  • Upload date:
  • Size: 10.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.8.6

File hashes

Hashes for tpljson-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 442b32d8131ddbb23256e08c75f14ed7bd95181b571a411f2f5698138df2445d
MD5 032d28f6a0a5cf794ef5e419663ca286
BLAKE2b-256 54be43e725dc4f3d65221a9c55e536e68d4b67dc93241ef6bb821267ce2516cd

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