as3ninja.jinja2 package

Submodules

as3ninja.jinja2.filterfunctions module

This module holds Jinja2 functions which also work as filters.

as3ninja.jinja2.filterfunctions.b64decode(data, urlsafe=False)[source]

Accepts a string and returns the Base64 decoded representation of this string. urlsafe=True decodes urlsafe base64

Return type:

Union[str, bytes]

as3ninja.jinja2.filterfunctions.b64encode(data, urlsafe=False)[source]

Accepts a string and returns the Base64 encoded representation of this string. urlsafe=True encodes string as urlsafe base64

Return type:

str

as3ninja.jinja2.filterfunctions.env(env_var, default=None)[source]

Reads an environment variable and returns its value.

Use default to specify a default value in case the environment variable does not exist, Empty environment variables will return an empty string.

Examples:

{# using env as a filter #}
"HOME_DIR": "{{ 'HOME' | env }}"
{# using env as a function #}
{% set home_dir = env("HOME") %}
{% set temp_dir = env("TEMPDIR", default="/tmp") %}
Return type:

str

as3ninja.jinja2.filterfunctions.hashfunction(data, hash_algo, digest_format='hex')[source]

Returns the digest of data for hash algorithm hash_algo. The digest is returned as hex by default, but can be returned as binary as well (digest_format)

Check the hashlib documentation of your python version for supported hash functions.

Parameters:
  • hash_algo (str) – hash algorithm

  • digest_format (str) – Digest format to return. Either hex (default) or binary.

Return type:

Union[str, bytes]

For the variable length shake digest, 256 bits are returned.

{% set whirlpool_hexdigest = hashfunction("fun with hashes", "whirlpool") %}
{# value of whirlpool_hexdigest is "ce38f0a536e71b5b0758932c1d5f32d2ab6cc5bff9f02fb7c97a70291d45efa4516d4e3d99000956587c7c9f691f64b3444a91661d45f526552a9e2d42428b09"
 # note that whirlpool is not a guaranteed hash function, hence might not be available on all platforms
 #}
as3ninja.jinja2.filterfunctions.jsonify(data, quote=True)[source]

serializes data to JSON format.

quote=False avoids surrounding quotes, For example:

"key": "{{ ninja.somevariable | jsonify(quote=False) }}"

Instead of:

"key": {{ ninja.somevariable | jsonify }}
Return type:

str

as3ninja.jinja2.filterfunctions.md5sum(data)[source]

Returns the hash as a hexdigest of data. data is automatically converted to bytes, using backslashreplace for utf8 characters.

Return type:

str

as3ninja.jinja2.filterfunctions.readfile(ctx, filepath, missing_ok=False)[source]

Reads a file and returns its content as ASCII. Expects the file to be a ASCII (not utf8!) encoded file.

missing_ok=True prevents raising an exception when the file is missing and will return an empty string (default: missing_ok=False).

Return type:

str

as3ninja.jinja2.filterfunctions.sha1sum(data)[source]

Returns the hash as a hexdigest of data. data is automatically converted to bytes, using backslashreplace for utf8 characters.

Return type:

str

as3ninja.jinja2.filterfunctions.sha256sum(data)[source]

Returns the hash as a hexdigest of data. data is automatically converted to bytes, using backslashreplace for utf8 characters.

Return type:

str

as3ninja.jinja2.filterfunctions.sha512sum(data)[source]

Returns the hash as a hexdigest of data. data is automatically converted to bytes, using backslashreplace for utf8 characters.

Return type:

str

as3ninja.jinja2.filterfunctions.to_list(data)[source]

Converts data to a list. Unlike list it will not convert str to a list of each character but wrap the whole str in a list. Does not convert existing lists.

For example:

"virtualAddresses": {{ ninja.virtual_addresses | to_list | jsonify }},

If ninja.virtual_addresses is a list already it will not be nested, if it is a string, the string will be placed in a list.

Another example using the python REPL:

( to_list("foo bar") == ['foo bar'] ) == True  # strings

( to_list(["foo", "bar"]) == ['foo', 'bar'] ) == True  # existing lists

( to_list(245) == [245] ) == True  # integers
Return type:

list

as3ninja.jinja2.filterfunctions.uuid(_=None)[source]

Returns a UUID4

Return type:

str

as3ninja.jinja2.filters module

This module holds Jinja2 filters for AS3 Ninja.

as3ninja.jinja2.filters.ninjutsu(ctx, value, **kwargs)[source]

ninjutsu passes its input to jinja2 rendereing using the existing jinja2 environment and context. You can specify arbitary keyword arguments to pass additional variables to the renderer. This is important if you define variables within control structures, for example loops. These variables are not exported in the context and can therefore not be accessed within the jinja2 template passed to ninjutsu.

Example:

...
{% for thisone in allofthem %}
...
{# making thisone available to ninjutsu by passing
 # it as a keyword argument with the same name
 #}
{{ somesource.content.with.jinja2 | ninjutsu(thisone=thisone) }}
...
{% endfor %}

If somesource.content.with.jinja2 uses myvar it would fail if we don’t specify the keyword argument myvar=myvar, as it is not automatically exposed to the existing jinja2 context. An alternative are namespaces, just make sure the namespace is defined outside any of the control structures. Also note that the variable name will be an attribute of the namespace.

...
{% clipboard = namespace() %}
{% for thisone in allofthem %}
...
{% set clipboard.thisone = thisone %}
...
{# thisone is now available to ninjutsu as clipboard.thisone #}
{{ somesource.content.with.jinja2 | ninjutsu }}
...
{% endfor %}
Return type:

str

as3ninja.jinja2.functions module

This module holds Jinja2 functions for AS3 Ninja.

class as3ninja.jinja2.functions.iterfiles(pattern, missing_ok=False)[source]

Bases: object

iterates files, returns a tuple of all globbing matches and the file content as dict. Assumes the file content is either JSON or YAML.

iterfiles will ignore missing files if missing_ok=True is specified (default: False), otherwise will raise a FileNotFoundError exception.

as3ninja.jinja2.j2ninja module

J2Ninja collects jinja2 filters, functions an tests in a single class.

class as3ninja.jinja2.j2ninja.J2Ninja[source]

Bases: object

J2Ninja provides decorator methods to register jinja2 filters, functions and tests, which are available as class attributes (dict).

filters: dict = {'b64decode': <function b64decode>, 'b64encode': <function b64encode>, 'env': <function env>, 'hashfunction': <function hashfunction>, 'jsonify': <function jsonify>, 'md5sum': <function md5sum>, 'ninjutsu': <function ninjutsu>, 'readfile': <function readfile>, 'sha1sum': <function sha1sum>, 'sha256sum': <function sha256sum>, 'sha512sum': <function sha512sum>, 'to_list': <function to_list>, 'uuid': <function uuid>, 'vault': <function vault>}
functions: dict = {'VaultClient': <class 'as3ninja.vault.VaultClient'>, 'b64decode': <function b64decode>, 'b64encode': <function b64encode>, 'env': <function env>, 'hashfunction': <function hashfunction>, 'iterfiles': <class 'as3ninja.jinja2.functions.iterfiles'>, 'jsonify': <function jsonify>, 'md5sum': <function md5sum>, 'readfile': <function readfile>, 'sha1sum': <function sha1sum>, 'sha256sum': <function sha256sum>, 'sha512sum': <function sha512sum>, 'to_list': <function to_list>, 'uuid': <function uuid>, 'vault': <function vault>}
classmethod registerfilter(function)[source]

Decorator to register a jinja2 filter

classmethod registerfunction(function)[source]

Decorator to register a jinja2 function

classmethod registertest(function)[source]

Decorator to register a jinja2 test

tests: dict = {}

as3ninja.jinja2.tests module

This module holds Jinja2 tests for AS3 Ninja.

Module contents

Jinja2 filters, functions and tests module for AS3 Ninja.