Skip to main content

Meta Yaml

Note: This is a fork of the project at https://bitbucket.org/atagunov/metayaml.

Mata Yaml is several enhancements for yaml format that allow the following:

  • include one yaml file from another

  • use python expression and other fields in the file as value

Include files syntax

To include one file or files the ‘extend’ key is used. For example:

base.yaml:

extend:
  - file1.yaml
  - file2.yaml
name: base.yaml
b: overridden_by_base
new: new

file1.yaml:

a: a
b: b
c: c
name: file1.yaml

file2.yaml:

a: aa
b: bb
d: d
name: file2.yaml

The order and sequence of file processing is shown in the following table:

Step

Action

Intermediate dict

1

Read base.yaml and extract extend key

{"extend":
    ["file1.yaml", "file2.yaml"] }

2

Read file1.yaml

{
   "a": "a",
   "b": "b",
   "c": "c",
   "name": "file1.yaml"
}

3

Read file2.yaml and merge/override values

{
   "a": "aa", # overridden
   "b": "b",  # overridden
   "c": "c",
   "d": "d",  # added
   "name": "file2.yaml" # overridden
}

4

Read rest values from base.yaml and merge/override

{
   "a": "aa",
   "b": "overridden_by_base",  # overridden
   "c": "c",
   "d": "d",
   "name": "base.yaml" # overridden
   "new": "new" # added
}

Expression syntax

Metayaml supports any python valid expression. For this expression should be enclosed in brackets ${} or $(). The first brackets is used for eager substitute and $() for laze. I.e. expressions in $() are applied after full read file and its include files but ${} during file read.

The access to other values from expression can be done by using dictionary syntax or ‘dash dictionary syntax’.

Examples:

base.yaml

extend:
  - f1.yaml

hour: ${60*60}  # just simple python expression
${2+2}: four  # expression can be in the key
delay: ${hour*2}  # delay is two hour or 7200 seconds
loggers:
  metayaml:
    name: metayaml
    level: debug
    console: false
  backend:
    name: backend
    level: ${loggers.metayaml.level}
    console: ${loggers.metayaml.console}
  ext: ${loggers.metayaml}  # copy whole dict from loggers.metayaml this key

  incorrect: ${delay} ${loggers.ext}  # In this case string representation of objects will be concatenated

f1.yaml

run_interval: $(hour*5)  # 5 hours. But 'hour' is not defined when this file is processed.
                         # Therefore only $() brackets can be used here.

Installation

Meta Yaml is in PyPI, so it can be installed directly using:

$ pip install metayaml

Or from BitBucket:

$ git clone https://bitbucket.org/atagunov/metayaml
$ cd metayaml
$ python setup.py install

Documentation

Documentation (such that it is) is available at https://bitbucket.org/atagunov/metayaml

Usage

from metayaml import read
read(["config.yaml",
      "test.yaml"],
     {'join': os.path.join, # allows get right os specific path in yaml file
      'env': os.environ}  # allows use system environments from yaml file
    )

config.yaml

extend:
  - ${join(env["HOME"], ".metayaml", "localconfig.yaml")} # added reading local config from $HOME
user_name: ${env["USER"]}
email: ${user_name + "@example.com"}
debug: false

test.yaml

debug: true

Order of substitution

The substitutions are done in order of the values in the file. I.e. the following example will be failed:

B: ${A+1}     <--- A is not defined here
AA: ${B}
A: 1

But the following The result is ok:

A: 1
B: ${A+1}
AA: ${B}

Change merge behavior

By default it is possible to add new keys in the dict and replace the list. In some cases it is necessary to remove keys from base file or add some values to list. For example

base.yaml:

main:
    iso_3166:
      China: CN
      Honduras: HN
      Madagascar: MG

    country_codes:
      - CN
      - HN
      - MG

    country_codes_3:
      - CHN
      - HND
      - MDG

last.yaml:

extend:
  - base.yaml
main:
  iso_3166:
     China: ${__del__}  #  key 'China' will be removed from the result
     Liberia: LR  # add new key

  country_codes:
     - LR         # after merge country_codes contains only one element.

  country_codes_3:
    ${__extend__}:
      - LBR       # the result list is ["CHN", "HND", "MDG", "LBR"]

The result of the code:

d = read("last.yaml")
print d

{
    "main": {
        "iso_3166": {
            "Honduras": "HN",
            "Madagascar": "MG",
            "Liberia": "LR"
        },
        "country_codes": [
            "LR"
        ],
        "country_codes_3": [
            "CHN",
            "HND",
            "MDG",
            "LBR"
        ]
    }
}

Copy method

There is method ‘cp’ which copy dict/list with extending:

cron:
  daily:
    min: 0
    hour: 0

  monthly:
    min: 0
    hour: 0
    day: 1

schedule:
  nighttask: ${cp(cron.daily, min=5)}  # min will be replaced to 5
  #  min: 5
  #  hour: 0
  daytask: ${cp(cron.daily, min=7, hour=13)} # min and hour are replaced
  #  min: 7
  #  hour: 13
  monthtask: ${cp(cron.monthly, day=2)}
  #  min: 0
  #  hour: 0
  #  day: 2

deploy:
  subnets:
    - 1.1.1.1
    - 2.2.2.2

  base_elb:
    - 4.4.4.4
    - 5.5.5.5

  elb: ${cp(deploy.subnets, "3.3.3.3", *deploy.base_elb)}
  # - 1.1.1.1
  # - 2.2.2.2
  # - 3.3.3.3
  # - 4.4.4.4
  # - 5.5.5.5

Inherit method

There are another way to copy existed dict and update some fields:

foo:
  bar:
    baz: 1
    buz: 2
    foobar: 3
  foobar: [4, 5]

bar:
  ${__inherit__}: foo.bar  # bar will be replaces by content of for.bar
  buz: 33

# the result value of 'bar' will be
#    baz: 1
#    buz: 33
#    foobar: 3

License

MetaYaml is released under a MIT license.

Metadata

Release files for metayaml-ng 1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for metayaml-ng 1.2
File Size Uploaded
metayaml-ng-1.2.tar.gz 16.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for metayaml-ng 1.2
File Interpreter ABI Platform
metayaml_ng-1.2-py3-none-any.whl Python 3 none any Details

Total release size: 25.4 kB

Release files / metayaml-ng-1.2.tar.gz

Download URL metayaml-ng-1.2.tar.gz
Size 16.1 kB
Tags Source
SHA-256 checksum
How to use checksums
c16f00995e211939a6ae6a51d33cf9835f85f3a42cf3857da1f761ebb4637fa8
BLAKE2b-256 checksum
How to use checksums
01326f96752f266c87c3a86b2f3064cb62d03f5c7c463f0e64a66100f8fd933e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.11.4

Release files / metayaml_ng-1.2-py3-none-any.whl

Download URL metayaml_ng-1.2-py3-none-any.whl
Size 9.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
54b5c99d4ca3f32b14f36ba937f90cf614cb77f1cdc427b7577246d05bed3ddc
BLAKE2b-256 checksum
How to use checksums
bb1bcd52277b5b469dd3292a129eb8b96bc0dea3883326fe924486277aa6da1d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.11.4

Release history Release notifications | RSS feed

This release

1.2 This release

2 release files

1.1

2 release files

1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page