From 16531a2f95ee601bd4aef4933ad556de48fe6a85 Mon Sep 17 00:00:00 2001
From: Karl-Hermann Wieners <karl-hermann.wieners@mpimet.mpg.de>
Date: Tue, 22 Mar 2022 16:49:54 +0100
Subject: [PATCH] mkexp: handle deprecation warnings for versions 3+ of Jinja2

The global/filter/test decorators were re-named, the old names are deprecated
---
 CHANGES.txt |  1 +
 mkexp       | 19 +++++++++++++++----
 2 files changed, 16 insertions(+), 4 deletions(-)

diff --git a/CHANGES.txt b/CHANGES.txt
index 2922c24..7ed2fcb 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -21,6 +21,7 @@ Global
 * Fixes related to python3, perl-5.26, and jinja2-3.0 compatibility
 * Added tools to manage differences/similarities between config files
   (compconfig.py, diffconfig.py, unmergeconfig)
+* Handle deprecation warnings for versions 3+ of Jinja2
 
 Release 1.1.2
 =============
diff --git a/mkexp b/mkexp
index 2923d35..1411c26 100755
--- a/mkexp
+++ b/mkexp
@@ -292,10 +292,21 @@ template_env = Environment(
     extensions = ['jinja2.ext.do']
 )
 
+# Check decorator compatibility for 3+ versions of Jinja2
+
+if 'pass_context' in dir(jinja2):
+    pass_context = jinja2.pass_context
+    pass_environment = jinja2.pass_environment
+    pass_eval_context = jinja2.pass_eval_context
+else:
+    pass_context = jinja2.contextfunction
+    pass_environment = jinja2.environmentfilter
+    pass_eval_context = jinja2.evalcontextfilter
+
 # Additional global functions
 
 # - Helper function to get context information within templates
-@jinja2.contextfunction
+@pass_context
 def get_context(ctx, key=None):
     return ctx.resolve(key) if key else ctx
 template_env.globals['context'] = get_context
@@ -310,7 +321,7 @@ template_env.globals['format_namelist'] = format_namelist
 # Additional filters
 
 # - A better wordwrap filter
-@jinja2.environmentfilter
+@pass_environment
 def wordwrap(env, s, width=79, break_long_words=True, break_on_hyphens=True,
              wrapstring=None):
     if wrapstring is None:
@@ -337,7 +348,7 @@ template_env.filters['filter'] = lambda x, f=None: list(filter(f, x))
 
 # - Replace 'list' handling simple values and strings as singleton lists
 list_original = template_env.filters['list']
-@jinja2.evalcontextfilter
+@pass_eval_context
 def list_singleton(eval_ctx, x, keep_empty=False, *args, **kwargs):
     # Workaround for 2.8 bug when applied to literals
     if isinstance(x, jinja2.nodes.EvalContext):
@@ -353,7 +364,7 @@ template_env.filters['list'] = list_singleton
 
 # - Replace 'join' handling simple values and strings as singleton lists
 join_original = template_env.filters['join']
-@jinja2.evalcontextfilter
+@pass_eval_context
 def join_singleton(eval_ctx, x, *args, **kwargs):
     # Workaround for 2.8 bug when applied to literals
     if isinstance(x, jinja2.nodes.EvalContext):
-- 
GitLab