Usage

Context

Passing Dictionaries

Set common context variables with json or yaml format. The default context format is json.

fyoo --context='{"a":"A"}' \
  -- \
echo '{{ a }}'
# A

fyoo --context='a: A' --context-format=yaml \
  -- \
echo '{{ a }}'
# A

Context Priority

Context dictionaries can be passed multiple times, with latest overriding preceding contexts. In addition, --set will have higher priority than --context.

Priority:

  1. Latest --set flag (Highest priority)

  2. Earliest --set flag

  3. Latest --context flag

  4. Earliest --context flag (Lowest priority)

$ fyoo \
  --set=p=higher \
  --context='{"p":"lowest"}' \
  --set=p=highest \
  --context='{"p":"lower"}' \
  -- \
echo '{{ p }}'
highest

Templating

Jinja Controls

There are several jinja controls that allow heavy customization to how Fyoo’s jinja templating works, if desired. Many of these have corresponding environment variables, in the expectation of setting them in a Dockerfile.

For example, FYOO__JINJA_TEMPLATE_FOLDER can be really powerful in setting the location of -jtf/--jinja-template-folder at container build time.

docs/Dockerfile
FROM python:3.7
RUN pip install fyoo
WORKDIR /app
ENV \
  FYOO__JINJA_TEMPLATE_FOLDER=/app/tests/sql
COPY . .
$ docker build . -f docs/Dockerfile -t fyoo-example
$ docker run fyoo-example \
    fyoo --dry-run --set table=customer \
      -- \
    sqlite3 '{% include "count.sql.jinja" %}'
["sqlite3", "\nselect count(*) as c\nfrom customer"]

Other jinja controls such as extensions and block string settings are in Command Line Interface.

Built-in Reference

Global Attributes

The following attributes are provided to Fyoo’s jinja environment as globals, whether they are objects or callable functions.

fyoo.template.attributes.date(tz='UTC', fmt: str = '%Y-%m-%d') → str[source]

Get current time string

$ fyoo -- echo 'today is {{ date() }}'
today is 2020-01-01

$ fyoo -- echo 'the year is {{ date(fmt="%Y") }}'
the year is 2020
Parameters
  • tz (str, optional) – Timezone to use. Defaults to ‘UTC’.

  • fmt (str, optional) – Datetime format. Defaults to r’%Y-%m-%d’.

Returns

Formated datetime string

Return type

str

fyoo.template.attributes.getenv(key, default=None)[source]

Alias of os.getenv

$ fyoo -- echo 'I am a {{ getenv("USER") }}'
I am a coolcat
$ fyoo -- echo 'example {{ getenv("IDONTEXIST", "default") }}'
example default
fyoo.template.attributes.raw_datetime

alias of datetime.datetime

fyoo.template.attributes.throw(*args)[source]

Raise a FyooTemplateException

You would do this if you wanted to verify arguments at ‘compile-time’, before executing a subcommand.

$ fyoo -- echo '{% if not table %}{{ throw("table not set") }}{% endif %}table is {{ table }}'
fyoo.exception.FyooTemplateException: table not set

$ fyoo --set table=customer -- echo '{% if not table %}{{ throw("table not set") }}{% endif %}table is {{ table }}'
table is customer

Global Filters

The following are functions provided to Fyoo’s jinja runtime as jinja filters.

fyoo.template.filters.toJson(obj, *, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, default=None, sort_keys=False, **kw)

Alias of json.dumps

$ fyoo -- echo "{{ [{'a': 1}, {'b': 2}] | toJson }}"
[{"a": 1}, {"b": 2}]
fyoo.template.filters.toYaml(data, stream=None, **kwds)

Alias of yaml.safe_dump

$ fyoo -- echo "{{ [{'a': 1}, {'b': 2}] | toYaml }}"
- a: 1
- b: 2