j2cli icon indicating copy to clipboard operation
j2cli copied to clipboard

Customization support's alter_context hook does not work when used along with data file

Open drennalls opened this issue 5 years ago • 0 comments

I'm trying to use --customize but it does not work as advertised if a data file is also used. The argument being passed to alter_context is a filter not a dict as expected.

Here are more details on my issue

  1. Created a virtualenv as follows
python3 -m venv py3venv
source ./py3venv/bin/activate
pip install j2cli[yaml]
13:35 $ python --version
Python 3.6.8

13:35 $ pip list | grep j2cli
j2cli (0.3.10)
  1. Trying to use --customize as follows
13:27 $ cat config.j2
server {
  listen 80;
  server_name {{ NGINX_HOSTNAME }};

  root {{ NGINX_WEBROOT }};
  index index.htm;
}

13:27 $ cat customization.py
def alter_context(context):
    context['NGINX_HOSTNAME'] = 'hostname_from_alter_context'
    context['NGINX_WEBROOT'] = '/from/alter_context'
    return context

13:28 $ cat data.env
NGINX_HOSTNAME=localhost
NGINX_WEBROOT=/var/www/project

..this works..


13:30 $ j2 --customize customization.py config.j2
server {
  listen 80;
  server_name hostname_from_alter_context;

  root /from/alter_context;
  index index.htm;
}

..this doesn't..

13:30 $ j2 --customize customization.py config.j2  data.env
Traceback (most recent call last):
  File "/home/drennalls/open_source/j2cli/bug_report/py3venv/bin/j2", line 11, in <module>
    sys.exit(main())
  File "/home/drennalls/open_source/j2cli/bug_report/py3venv/lib/python3.6/site-packages/j2cli/cli.py", line 206, in main
    sys.argv[1:]
  File "/home/drennalls/open_source/j2cli/bug_report/py3venv/lib/python3.6/site-packages/j2cli/cli.py", line 166, in render_command
    context = customize.alter_context(context)
  File "customization.py", line 2, in alter_context
    context['NGINX_HOSTNAME'] = 'hostname_from_alter_context'
TypeError: 'filter' object does not support item assignment

It fails differently on python2 but it still fails. The argument being passed to alter_context on python2 is a list not a dictas expected.

13:34 $ python --version
Python 2.7.16
13:38 $ pip list | grep j2cli
j2cli      0.3.10

13:32 $ j2 --customize customization.py config.j2  data.env
Traceback (most recent call last):
  File "/home/drennalls/venvs/py2venv/bin/j2", line 8, in <module>
    sys.exit(main())
  File "/home/drennalls/venvs/py2venv/local/lib/python2.7/site-packages/j2cli/cli.py", line 206, in main
    sys.argv[1:]
  File "/home/drennalls/venvs/py2venv/local/lib/python2.7/site-packages/j2cli/cli.py", line 166, in render_command
    context = customize.alter_context(context)
  File "customization.py", line 2, in alter_context
    context['NGINX_HOSTNAME'] = 'hostname_from_alter_context'
TypeError: list indices must be integers, not str

drennalls avatar Nov 19 '19 18:11 drennalls