Skip to main content

Shell config file parser (json, yaml)

Project description

niet

Build Status PyPI PyPI - Python Version PyPI - Status

Get data from yaml file directly in your shell

Install

$ pip install niet

Requirements

  • Python 2.7+

Usage

With YAML file

Consider the yaml file with the following content:

# /path/to/your/file.yaml
project:
    meta:
        name: my-project
    foo: bar
    list-items:
        - item1
        - item2
        - item3

You can retrieve data from this file by using niet like this:

$ niet /path/to/your/file.yaml "project.meta.name"
my-project
$ niet /path/to/your/file.yaml "project.foo"
bar
$ niet /path/to/your/file.yaml "project.list-items"
item1 item2 item3
$ # assign return value to shell variable
$ NAME=$(niet /path/to/your/file.yaml "project.meta.name")
$ echo $NAME
my-project

With JSON file

Consider the json file with the following content:

{
    "project": {
        "meta": {
            "name": "my-project"
        }
    },
    "foo": "bar",
    "list-items": [
        "item1",
        "item2",
        "item3"
    ]
}

You can retrieve data from this file by using niet like this:

$ niet /path/to/your/file.json "project.meta.name"
my-project
$ niet /path/to/your/file.json "project.foo"
bar
$ niet /path/to/your/file.json "project.list-items"
item1 item2 item3
$ # assign return value to shell variable
$ NAME=$(niet /path/to/your/file.json "project.meta.name")
$ echo $NAME
my-project

Deal with errors

When your JSON file content are not valid niet display an error and exit with return code 1

You can easily protect your script like this:

PROJECT_NAME=$(niet your-file.yaml project.meta.name)
if [ "$?" = "1" ]; then
    echo "Error occur ${PROJECT_NAME}"
else
    echo "Project name: ${PROJECT_NAME}"
fi

Examples

You can try niet by using the samples provided with the project sources code.

Sample example:

# tests/samples/sample.yaml
project:
    meta:
        name: project-sample
        tags:
          - example
          - sample
          - for
          - testing
          - purpose

Retrieve the project name:

$ niet tests/samples/sample.yaml project.meta.name
project-sample

Deal with list of items

$ for el in $(niet tests/samples/sample.yaml project.meta.tags); do echo ${el}; done
example
sample
for
testing
purpose

Tips

You can pass your search with or without quotes like this:

$ niet your-file.yaml project.meta.name
$ niet your-file.yaml "project.meta.name"

Contribute

If you want to contribute to niet please first read the contribution guidelines

Licence

This project is under the MIT License.

See the license file for more details

Hervé Beraud herveberaud.pro@gmail.com

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

niet-1.0.0.tar.gz (11.5 kB view details)

Uploaded Source

Built Distribution

niet-1.0.0-py2.py3-none-any.whl (3.7 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file niet-1.0.0.tar.gz.

File metadata

  • Download URL: niet-1.0.0.tar.gz
  • Upload date:
  • Size: 11.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for niet-1.0.0.tar.gz
Algorithm Hash digest
SHA256 fc2938005f3cab950724958b842fb0222ffccd13465afeedffd4785bb795e664
MD5 5ca7051fd205e579f3edfc669d9b809d
BLAKE2b-256 c312dcff044e69d0ee90fd96acbe1ce7305047713a87ab16a93d369e179079dc

See more details on using hashes here.

File details

Details for the file niet-1.0.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for niet-1.0.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 24b389126ef867f96d2f826607a669dd4f742761e01a1a16e76054360dfe03c1
MD5 31766fb6b117747b597969f911b1c041
BLAKE2b-256 847805469667ff89c7a3d25c76f13c75ad8dfc0c0986b8dd18b191729b15e18a

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page