gchq / gaffer-tools

gaffer-tools is deprecated. Use https://github.com/gchq/gafferpy instead
Apache License 2.0
50 stars 29 forks source link

Refactor fishbowl to use jinja #1018

Closed t92549 closed 1 year ago

t92549 commented 2 years ago

jinja is a template engine for Python. It could be used to make the fishbowl code clearer, more extensible and maintainable.

t92549 commented 2 years ago

Example usage of jinja:

from jinja2.nativetypes import NativeEnvironment
env = NativeEnvironment()

def capitalize(input):
    return input.capitalize()

function_dict = {
    "capitalize": capitalize
}

t = env.from_string("def __init__(self, {% for input in inputs %}{{ capitalize(input) }}=None, {% endfor %}options=None):")
t.globals.update(function_dict)
print(t.render(inputs=["input", "skip", "validate"]))
'def __init__(self, Input=None, Skip=None, Validate=None, options=None):'

The capitalize function is just there to show how custom functions work. In real usage, I think a different Environment would be used, and you would use a template file rather than a string.

t92549 commented 1 year ago

Closed by https://github.com/gchq/gaffer-tools/pull/1034