{ "info": { "author": "Santtu Pajukanta", "author_email": "santtu@pajukanta.fi", "bugtrack_url": null, "classifiers": [], "description": "# Emrichen \u2013 Template engine for YAML & JSON\n\n[![Build Status](https://travis-ci.org/japsu/emrichen.svg?branch=master)](https://travis-ci.org/japsu/emrichen) [![PyPI version](https://badge.fury.io/py/emrichen.svg)](https://badge.fury.io/py/emrichen)\n\nEmrichen takes in templates written in YAML or JSON, processes tags that do things like variable substitution, and outputs YAML or JSON.\n\nWhat makes Emrichen better for generating YAML or JSON than a text-based template system is that it works *within* YAML (or JSON).\n\nEver tried substituting a list or dict into a YAML document just to run into indentation issues? Horrible! Handling quotation marks and double backslash escapes? Nope!\n\nIn Emrichen, variables are typed in the familiar JSON types, making these a non-issue. Emrichen is a pragmatic and powerful way to generate YAML and JSON.\n\nConsider the following template that produces a minimal Kubernetes deployment:\n\n```yaml\n!Defaults\ntag: latest\nimage: !Format \"nginx:{tag}\"\nreplicas: 3\n---\napiVersion: apps/v1\nkind: Deployment\nmetadata:\n name: nginx\nspec:\n selector:\n matchLabels:\n app: nginx\n replicas: !Var replicas\n template:\n metadata:\n labels:\n app: nginx\n spec:\n containers:\n - name: nginx\n image: !Var image\n ports:\n - containerPort: 80\n```\n\nThis small template already demonstrates three of Emrichen's powerful tags: `!Defaults` that provides default values for variables; `!Var` that performs simple variable substitution; and `!Format` that performs string formatting.\n\nPut it in a file, say, `nginx.in.yaml` (we use `.in.yaml` to denote templates) and render it using this command:\n\n emrichen nginx.in.yaml\n\nPrefer JSON output?\n\n emrichen --output-format json nginx.in.yaml\n\nWanna change the tag?\n\n emrichen --define tag=1-alpine nginx.in.yaml\n\nNote how `image` is evaluated lazily \u2013 you need not override `image` just to change `tag`!\n\nSee below for a table of supported tags. There's a lot of them. If you need one that's not there yet, please shoot us an issue or PR.\n\n## Installation\n\nPython 3.5+ required. Python 2 is not and will not be supported.\n\n pip3 install emrichen\n\n## Supported tags\n\n\n\n| Tag | Arguments | Example | Description |\n|-----|-----------|---------|-------------|\n| `!All` | An iterable | `!All [true, false]` | Returns true iff all the items of the iterable argument are truthy. |\n| `!Any` | An iterable | `!Any [true, false]` | Returns true iff at least one of the items of the iterable argument is truthy. |\n| `!Base64` | The value to encode | `!Base64 foobar` | Encodes the value (or a string representation thereof) into base64. |\n| `!Compose` | `value`: The value to apply tags on
`tags`: A list of tag names to apply, latest first | `!Base64,Var foo` | Used internally to implement tag composition.
Usually not used in the spelt-out form.
See _Tag composition_ below. |\n| `!Concat` | A list of lists | `!Concat [[1, 2], [3, 4]]` | Concatenates lists. |\n| `!Debug` | Anything, really | `!Debug,Var foo` | Enriches its argument, outputs it to stderr and returns it. Useful to check the value
of some expression deep in a big template, perhaps even one that doesn't even fully render. |\n| `!Defaults` | A dict of variable definitions | See `examples/defaults/` | Defines default values for variables. These will be overridden by any other variable source.
**NOTE:** `!Defaults` must appear in a document of its own in the template file (ie. separated by `---`). The document containing `!Defaults` will be erased from the output. |\n| `!Error` | Error message | `!Error \"Must define either foo or bar, not both\"` | If the `!Error` tag is present in the template after resolving all conditionals,
it will cause the template rendering to exit with error emitting the specified error message. |\n| `!Exists` | JSONPath expression | `!Exists foo` | Returns `true` if the JSONPath expression returns one or more matches, `false` otherwise. |\n| `!Filter` | `test`, `over` | See `tests/test_cond.py` | Takes in a list and only returns elements that pass a predicate. |\n| `!Format` | Format string | `!Format \"{foo} {bar!d}\"` | Interpolate strings using [Python format strings](https://docs.python.org/3/library/string.html#formatstrings).
JSONPath supported in variable lookup (eg. `{people[0].first_name}` will do the right thing).
**NOTE:** When the format string starts with `{`, you need to quote it in order to avoid being interpreted as a YAML object. |\n| `!Group` | Accepts the same arguments as `!Loop`, except `template` is optional (default identity), plus the following:
`by`: (required) An expression used to determine the key for the current value
`result_as`: (optional, string) When evaluating `by`, the enriched `template` is available under this name. | TBD | Makes a dict out of a list. Keys are determined by `by`. Items with the same key are grouped in a list. |\n| `!If` | `test`, `then`, `else` | See `tests/test_cond.py` | Returns one of two values based on a condition. |\n| `!Include` | Path to a template to include | `!Include ../foo.yml` | Renders the requested template at this location. Both absolute and relative paths work. |\n| `!IncludeBase64` | Path to a binary file | `!IncludeBase64 ../foo.pdf` | Loads the given binary file and returns the contents encoded as Base64. |\n| `!IncludeBinary` | Path to a binary file | `!IncludeBinary ../foo.pdf` | Loads the given binary file and returns the contents as bytes. This is practically only useful for hashing. |\n| `!IncludeText` | Path to an UTF-8 text file | `!IncludeText ../foo.toml` | Loads the given UTF-8 text file and returns the contents as a string. |\n| `!Index` | Accepts the same arguments as `!Loop`, except `template` is optional (default identity), plus the following:
`by`: (required) An expression used to determine the key for the current value
`result_as`: (optional, string) When evaluating `by`, the enriched `template` is available under this name.
`duplicates`: (optional, default `error`) `error`, `warn(ing)` or `ignore` duplicate values. | TBD | Makes a dict out of a list. Keys are determined by `by`. |\n| `!IsBoolean` | Data to typecheck. | `!IsBoolean ...` | Returns True if the value enriched is of the given type, False otherwise. |\n| `!IsDict` | Data to typecheck. | `!IsDict ...` | Returns True if the value enriched is of the given type, False otherwise. |\n| `!IsInteger` | Data to typecheck. | `!IsInteger ...` | Returns True if the value enriched is of the given type, False otherwise. |\n| `!IsList` | Data to typecheck. | `!IsList ...` | Returns True if the value enriched is of the given type, False otherwise. |\n| `!IsNone` | Data to typecheck. | `!IsNone ...` | Returns True if the value enriched is None (null) or Void, False otherwise. |\n| `!IsNumber` | Data to typecheck. | `!IsNumber ...` | Returns True if the value enriched is of the given type, False otherwise. |\n| `!IsString` | Data to typecheck. | `!IsString ...` | Returns True if the value enriched is of the given type, False otherwise. |\n| `!Join` | `items`: (required) A list of items to be joined together.
`separator`: (optional, default space) The separator to place between the items.
**OR**
a list of items to be joined together with a space as the separator. | `!Join [foo, bar]`
`!Join { items: [foo, bar], separator: ', ' }` | Joins a list of items together with a separator. The result is always a string. |\n| `!Lookup` | JSONPath expression | `!Lookup people[0].first_name` | Performs a JSONPath lookup returning the first match. If there is no match, an error is raised. |\n| `!LookupAll` | JSONPath expression | `!LookupAll people[*].first_name` | Performs a JSONPath lookup returning all matches as a list. If no matches are found, the empty list `[]` is returned. |\n| `!Loop` | `over`: (required) The data to iterate over (a literal list or dict, or !Var)
`as`: (optional, default `item`) The variable name given to the current value
`index_as`: (optional) The variable name given to the loop index. If over is a list, this is a numeric index starting from `0`. If over is a dict, this is the dict key.
`index_start`: (optional, default `0`) First index, for eg. 1-based indexing.
`previous_as`: (optional) The variable name given to the previous value. On the first iteration of the loop, the previous value is `null`. _Added in 0.2.0_
`template`: (required) The template to process for each iteration of the loop.
`as_documents`: (optional) Whether to \"unfold\" the output of this loop into separate YAML documents when writing YAML. Only has an effect at the top level of a template. | See `examples/loop/`. | Loops over a list or dict and renders a template for each iteration. The output is always a list. |\n| `!MD5` | Data to hash | `!MD5 'some data to hash'` | Hashes the given data using the MD5 algorithm. If the data is not binary, it is converted to UTF-8 bytes. |\n| `!Merge` | A list of dicts | `!Merge [{a: 5}, {b: 6}]` | Merges objects. For overlapping keys the last one takes precedence. |\n| `!Not` | a value | `!Not !Var foo` | Logically negates the given value (in Python semantics). |\n| `!Op` | `a`, `op`, `b` | See `tests/test_cond.py` | Performs binary operators. Especially useful with `!If` to implement greater-than etc. |\n| `!SHA1` | Data to hash | `!SHA1 'some data to hash'` | Hashes the given data using the SHA1 algorithm. If the data is not binary, it is converted to UTF-8 bytes. |\n| `!SHA256` | Data to hash | `!SHA256 'some data to hash'` | Hashes the given data using the SHA256 algorithm. If the data is not binary, it is converted to UTF-8 bytes. |\n| `!URLEncode` | A string to encode
**OR**
`url`: The URL to combine query parameters into
`query`: An object of query string parameters to add. | `!URLEncode \"foo+bar\"`
`!URLEncode { url: \"https://example.com/\", query: { foo: bar } }` | Encodes strings for safe inclusion in a URL, or combines query string parameters into a URL. |\n| `!Var` | Variable name | `!Var image_name` | Replaced with the value of the variable. |\n| `!Void` | Anything or nothing | `foo: !Void` | The dict key, list item or YAML document that resolves to `!Void` is removed from the output. |\n| `!With` | `vars`: A dict of variable definitions.
`template`: The template to process with the variables defined. | See `examples/with/`. | Binds local variables that are only visible within `template`. Useful for giving a name for common sub-expressions. |\n\n\n### Tags in JSON\n\nJSON doesn't have a native tag construct. Instead, use an object with a single key that is the name of the tag (including the bang, eg. `!Var`). For example:\n\n```json\n{\n \"foo\": {\n \"!Var\": \"foo\"\n }\n}\n```\n\nLimitations of the JSON support:\n\n* Object keys starting with `!` are not supported.\n* A template rendered as JSON may only contain a single document.\n * JSON templates always have a single document only.\n * YAML templates may only contain a single non-`!Void`, non-`!Defaults` document.\n* As `!Defaults` must appear in a document of its own, it's not supported in JSON templates. Use a var file instead.\n\n### Tag composition\n\nDue to YAML, you can't do `!Base64 !Var foo`. We provide a convenient workaround: `!Base64,Var foo`.\n\n## CLI\n\n```\nusage: emrichen [-h] [--template-format {yaml,json}] [--var-file VAR_FILE]\n [--define VAR=VALUE] [--output-file OUTPUT_FILE]\n [--output-format {yaml,json}] [--include-env]\n [template_file]\n\nA YAML to YAML preprocessor.\n\npositional arguments:\n template_file The YAML template to process. If unspecified, the\n template is read from stdin.\n\noptional arguments:\n -h, --help show this help message and exit\n --template-format {yaml,json}\n Template format. If unspecified, attempted to be\n autodetected from the input filename (but defaults to\n YAML).\n --var-file VAR_FILE, -f VAR_FILE\n A YAML file containing an object whose top-level keys\n will be defined as variables. May be specified\n multiple times.\n --define VAR=VALUE, -D VAR=VALUE\n Defines a single variable. May be specified multiple\n times.\n --output-file OUTPUT_FILE, -o OUTPUT_FILE\n Output file. If unspecified, the template output is\n written into stdout.\n --output-format {yaml,json}\n Output format. If unspecified, attempted to be\n autodetected from the output filename (but defaults to\n YAML).\n --include-env, -e Expose process environment variables to the template.\n\nVariable precedence: -D > -e > -f > !Defaults\n```\n\n### Examples\n\n cd examples/kubernetes\n emrichen -f vars.yml -D tag=build-256 deployment.in.yml\n\n## Python API\n\nTODO\n\n### `emrichen(template, *variable_sources, **override_variables)`\n\n### `Context(*variable_sources, **override_variables)`\n\n### `Template(template_source)`\n\n## License\n\n The MIT License (MIT)\n\n Copyright \u00a9 2018\u20132019 Santtu Pajukanta\n Copyright \u00a9 2018\u20132019 Aarni Koskela\n\n Permission is hereby granted, free of charge, to any person obtaining a copy\n of this software and associated documentation files (the \"Software\"), to deal\n in the Software without restriction, including without limitation the rights\n to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n copies of the Software, and to permit persons to whom the Software is\n furnished to do so, subject to the following conditions:\n\n The above copyright notice and this permission notice shall be included in\n all copies or substantial portions of the Software.\n\n THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\n THE SOFTWARE.\n\n\n", "description_content_type": "text/markdown", "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "http://github.com/con2/emrichen", "keywords": "", "license": "MIT", "maintainer": "", "maintainer_email": "", "name": "emrichen", "package_url": "https://pypi.org/project/emrichen/", "platform": "", "project_url": "https://pypi.org/project/emrichen/", "project_urls": { "Homepage": "http://github.com/con2/emrichen" }, "release_url": "https://pypi.org/project/emrichen/0.2.2/", "requires_dist": [ "PyYAML", "pyaml", "jsonpath-rw (~=1.4.0)" ], "requires_python": "", "summary": "Template engine for YAML & JSON", "version": "0.2.2" }, "last_serial": 5485846, "releases": { "0.1.0": [ { "comment_text": "", "digests": { "md5": "a3c7a3216a964d070da879c22ad993e9", "sha256": "5cb64a3d6bad434b6be21492ccdd0c7d2fd76abf67a4003706798e56a447ea81" }, "downloads": -1, "filename": "emrichen-0.1.0-py3-none-any.whl", "has_sig": false, "md5_digest": "a3c7a3216a964d070da879c22ad993e9", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 22659, "upload_time": "2019-01-15T19:23:48", "url": "https://files.pythonhosted.org/packages/c8/d9/024829975708143904beb04e83ee350d50edb7a21f4c77784eff93e7cb0d/emrichen-0.1.0-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "ad61e415439d2245c563737f5987f950", "sha256": "73e612790dfbfa926e7c88440c66d10e48e6ba01dbdbcb4f3be60e03176a9579" }, "downloads": -1, "filename": "emrichen-0.1.0.tar.gz", "has_sig": false, "md5_digest": "ad61e415439d2245c563737f5987f950", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 16482, "upload_time": "2019-01-15T19:23:50", "url": "https://files.pythonhosted.org/packages/00/8a/820a2becbd87af31e8376e29285e97467200539dc8be1eb208a3a38149f9/emrichen-0.1.0.tar.gz" } ], "0.1.1": [ { "comment_text": "", "digests": { "md5": "bb57b4b0b2156890f43683a15d221072", "sha256": "36045b27a87af682b4db026d780895fd9424cf1077863b7bba4c021cb4db9159" }, "downloads": -1, "filename": "emrichen-0.1.1-py3-none-any.whl", "has_sig": false, "md5_digest": "bb57b4b0b2156890f43683a15d221072", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 27402, "upload_time": "2019-01-15T19:29:56", "url": "https://files.pythonhosted.org/packages/bf/e6/56cc97c82f212d229e40706e35874acf82bac963807dacd43028f569e5e6/emrichen-0.1.1-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "e90b941d10f3e9be6901043e8e95ba11", "sha256": "066027e8228f0b32a32398d6c7ccfd04bbec224973cd12ddeae8b217225e7289" }, "downloads": -1, "filename": "emrichen-0.1.1.tar.gz", "has_sig": false, "md5_digest": "e90b941d10f3e9be6901043e8e95ba11", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 21082, "upload_time": "2019-01-15T19:29:58", "url": "https://files.pythonhosted.org/packages/2c/58/0de15aef1fb628bd32a6df4cada9ee7fb57afc34beb87bb7f9dbf265606e/emrichen-0.1.1.tar.gz" } ], "0.2.0": [ { "comment_text": "", "digests": { "md5": "124f8bffb588350a5fa8e37247fbb7fa", "sha256": "357d2c3dd6b725b57d10d96bd11a53674338c9f5f988854474222bcc00678503" }, "downloads": -1, "filename": "emrichen-0.2.0-py3-none-any.whl", "has_sig": false, "md5_digest": "124f8bffb588350a5fa8e37247fbb7fa", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 28932, "upload_time": "2019-01-22T10:10:40", "url": "https://files.pythonhosted.org/packages/9a/ef/710a8feda58193970237553d59a185476c8ff70f9e0f7fecf9bcc725fc67/emrichen-0.2.0-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "b2c40d63702a6d7fb9b953054280c3ae", "sha256": "00eaf2047e64021c7cd825367461deaef5ae4aa44e7be025c275627502399a5f" }, "downloads": -1, "filename": "emrichen-0.2.0.tar.gz", "has_sig": false, "md5_digest": "b2c40d63702a6d7fb9b953054280c3ae", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 22561, "upload_time": "2019-01-22T10:10:42", "url": "https://files.pythonhosted.org/packages/7a/74/05aea2280f4eb05d6e0dee29dac6a5c44b06e99eb81344775f8112859350/emrichen-0.2.0.tar.gz" } ], "0.2.1": [ { "comment_text": "", "digests": { "md5": "ecf72fc8fde2855b91ef6ea8bc006985", "sha256": "ebd5df8db0b3bbd2e066ded38cc1185839659b3de1d25e69d30da7e9052d83d2" }, "downloads": -1, "filename": "emrichen-0.2.1-py3-none-any.whl", "has_sig": false, "md5_digest": "ecf72fc8fde2855b91ef6ea8bc006985", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 30649, "upload_time": "2019-03-22T05:17:15", "url": "https://files.pythonhosted.org/packages/aa/8d/838cb29dd548f6b40cf409583fc2a01aefe0934621ab6848c6d473a82a65/emrichen-0.2.1-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "0243aeb6a154583b69eac744e4db2be8", "sha256": "6bb8ef917cf5d1d8b08d205a62df2c4f87e249cd21dee42bdda8844c481f53c9" }, "downloads": -1, "filename": "emrichen-0.2.1.tar.gz", "has_sig": false, "md5_digest": "0243aeb6a154583b69eac744e4db2be8", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 23523, "upload_time": "2019-03-22T05:17:17", "url": "https://files.pythonhosted.org/packages/d5/88/f2f59d061ac57a5f91ed318cb847f51dbaeaeb00e2c309ef9e2076a392da/emrichen-0.2.1.tar.gz" } ], "0.2.2": [ { "comment_text": "", "digests": { "md5": "bf0e126815cd14de2e3133b1fe273c67", "sha256": "2b7d52b75f090d16c941a278819af48de0e5e673ceef0dd33a0041a6c640a37f" }, "downloads": -1, "filename": "emrichen-0.2.2-py3-none-any.whl", "has_sig": false, "md5_digest": "bf0e126815cd14de2e3133b1fe273c67", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 31269, "upload_time": "2019-07-04T09:41:43", "url": "https://files.pythonhosted.org/packages/b2/84/76709637f445a5ce0fe0b0ddb1d25d71d29380df9859d9ef2490e9169096/emrichen-0.2.2-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "4bc9bee872f0dad5cdd120e58b1894a8", "sha256": "123361a8aeee5082c7c6c03a765b0aab7406231d0694b9a11b16c4269036fe70" }, "downloads": -1, "filename": "emrichen-0.2.2.tar.gz", "has_sig": false, "md5_digest": "4bc9bee872f0dad5cdd120e58b1894a8", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 23887, "upload_time": "2019-07-04T09:41:45", "url": "https://files.pythonhosted.org/packages/0a/de/0a152ee09e1e1ac8b50c44922991887c9e62c5292750933a77f85adaaf1a/emrichen-0.2.2.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "bf0e126815cd14de2e3133b1fe273c67", "sha256": "2b7d52b75f090d16c941a278819af48de0e5e673ceef0dd33a0041a6c640a37f" }, "downloads": -1, "filename": "emrichen-0.2.2-py3-none-any.whl", "has_sig": false, "md5_digest": "bf0e126815cd14de2e3133b1fe273c67", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 31269, "upload_time": "2019-07-04T09:41:43", "url": "https://files.pythonhosted.org/packages/b2/84/76709637f445a5ce0fe0b0ddb1d25d71d29380df9859d9ef2490e9169096/emrichen-0.2.2-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "4bc9bee872f0dad5cdd120e58b1894a8", "sha256": "123361a8aeee5082c7c6c03a765b0aab7406231d0694b9a11b16c4269036fe70" }, "downloads": -1, "filename": "emrichen-0.2.2.tar.gz", "has_sig": false, "md5_digest": "4bc9bee872f0dad5cdd120e58b1894a8", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 23887, "upload_time": "2019-07-04T09:41:45", "url": "https://files.pythonhosted.org/packages/0a/de/0a152ee09e1e1ac8b50c44922991887c9e62c5292750933a77f85adaaf1a/emrichen-0.2.2.tar.gz" } ] }