summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorDominik Holland <dominik.holland@qt.io>2022-10-13 11:32:07 +0200
committerDominik Holland <dominik.holland@qt.io>2022-10-18 09:34:51 +0200
commitf79cdbbaa6969fb3dd92748c7cda71ed0fc5746e (patch)
treee0c549b14638683b39b09744be8215d4fe3eac47 /src
parentbfdc561cc3ef050b312d40ed430801f0d5efe73a (diff)
ifcodegen: Add support to load extra jinja filters
In addition to allow a filters.py which can be part of a custom template folder, there is now also a way to load filters from other locations. The extra_filters are configured in the generation yaml file and can load files from other templates or common folders within the template search path. Fixes: QTBUG-107005 Change-Id: I518ff96659a3792fbb2a1d64ffc1d5c40eb69767 Reviewed-by: Robert Griebl <robert.griebl@qt.io>
Diffstat (limited to 'src')
-rw-r--r--src/interfaceframework/doc/snippets/filters.py19
-rw-r--r--src/interfaceframework/doc/src/ifcodegen/generator-usage.qdoc23
-rwxr-xr-xsrc/tools/ifcodegen/generate.py5
-rw-r--r--src/tools/ifcodegen/generator/rule_generator.py11
4 files changed, 58 insertions, 0 deletions
diff --git a/src/interfaceframework/doc/snippets/filters.py b/src/interfaceframework/doc/snippets/filters.py
new file mode 100644
index 00000000..31784d61
--- /dev/null
+++ b/src/interfaceframework/doc/snippets/filters.py
@@ -0,0 +1,19 @@
+#!/usr/bin/env python3
+# Copyright (C) 2021 The Qt Company Ltd.
+# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
+
+import json
+import inspect
+
+from qface.idl.domain import Module, Interface, Property, Parameter, Field, Struct
+from qface.helper.generic import lower_first, upper_first
+from qface.helper.qtcpp import Filters
+
+from generator.global_functions import jinja_error, jinja_warning
+from generator.filters import deprecated_filter
+
+def custom_filter(s):
+ jinja_warning("Test calling a function from the ifcodegen within our own filters")
+ return
+
+filters['custom_filter'] = custom_filter
diff --git a/src/interfaceframework/doc/src/ifcodegen/generator-usage.qdoc b/src/interfaceframework/doc/src/ifcodegen/generator-usage.qdoc
index 1131e54d..ffe5621b 100644
--- a/src/interfaceframework/doc/src/ifcodegen/generator-usage.qdoc
+++ b/src/interfaceframework/doc/src/ifcodegen/generator-usage.qdoc
@@ -168,6 +168,29 @@
structures. See the \l{QFace - Rule Base Generation}{QFace Rule Base Generation Documentation}
for more information.
+ \section3 Extra Jinja Filters
+
+ Sometimes the Jinja filters provided by ifcodegen are not enough, but it is possible to write your
+ own filters using Python.
+ If you add a python script called \c{filters.py} within your template it will be loaded automatically.
+ You can use the following boiler-plate code as a template:
+
+ \quotefile filters.py
+
+ To share filters also between templates, you can also specify other python scripts which should
+ be loaded in addition. This needs to be done in the Generation YAML file.
+
+ The following snippet generates a plugin.h and loads additional filters from a folder called
+ extra-filter:
+
+ \code
+ frontend:
+ extra_filters: [ "extra-filter/filters.py" ]
+ module:
+ documents:
+ - "{{module.module_name|lower}}plugin.h": "plugin.h.tpl"
+ \endcode
+
\section2 Annotations YAML
Currently, not all aspects of the interface description can be expressed using the IDL itself.
diff --git a/src/tools/ifcodegen/generate.py b/src/tools/ifcodegen/generate.py
index 7e85b065..ad032438 100755
--- a/src/tools/ifcodegen/generate.py
+++ b/src/tools/ifcodegen/generate.py
@@ -88,6 +88,11 @@ def generate(template_search_paths, tplconfig, moduleConfig, annotations, import
'srcBase': srcBase
}
search_path = [tplconfig]
+ # In case tplconfig is a path, we also want to add the containing folder to the search-path
+ # This makes sure that also templates referenced by path can use common templates or commmon filters
+ if os.path.exists(tplconfig):
+ search_path += [os.path.dirname(tplconfig)]
+
search_path += template_search_paths
generator = CustomRuleGenerator(search_path=search_path, destination=dst,
context=ctx, modules=module_names)
diff --git a/src/tools/ifcodegen/generator/rule_generator.py b/src/tools/ifcodegen/generator/rule_generator.py
index 39b49df0..f611056d 100644
--- a/src/tools/ifcodegen/generator/rule_generator.py
+++ b/src/tools/ifcodegen/generator/rule_generator.py
@@ -2,12 +2,14 @@
# Copyright (C) 2021 The Qt Company Ltd.
# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
+import os
import click
import logging.config
import sys
import yaml
from path import Path
+from qface.utils import load_filters
from qface.generator import RuleGenerator
from qface.idl.domain import Module, Interface, Property, Parameter, Field, Struct
@@ -36,7 +38,16 @@ class CustomRuleGenerator(RuleGenerator):
self.context.update(rules.get('context', {}))
self.path = rules.get('path', '')
self.source = rules.get('source', None)
+ self.extra_filters = rules.get('extra_filters', [])
+
+ for filter in self.extra_filters:
+ filter_path = self.get_template(filter).filename
+ if os.path.exists(filter_path):
+ extra_filters = load_filters(Path(filter_path))
+ self.filters = extra_filters
+
self._process_rule(rules.get('system', None), {'system': system})
+
for module in system.modules:
# Only generate files for the modules detected from the first parse run
if module.name not in self.module_names: