From c168f9c3bee5153a6a09e30b2462c85a166f6a90 Mon Sep 17 00:00:00 2001 From: Kelly Brazil Date: Thu, 23 Feb 2023 07:54:38 -0800 Subject: [PATCH] JC: Add plugin parser functionality to JC Filter Plugin (#6043) * Add plugin parser functionality to JC Filter Plugin The parse function was added in jc v1.18.0 which allows plugin parsers to be used. This change will try the new API if available, else fallback to the old API so there is no change in behavior. * remove whitespace from blank line * Add changelog fragment for JC plugin parser support * add .yml extension to file name * Formatting * add period at end --- changelogs/fragments/6043-jc_plugin_parser_support.yml | 2 ++ plugins/filter/jc.py | 10 ++++++++-- 2 files changed, 10 insertions(+), 2 deletions(-) create mode 100644 changelogs/fragments/6043-jc_plugin_parser_support.yml diff --git a/changelogs/fragments/6043-jc_plugin_parser_support.yml b/changelogs/fragments/6043-jc_plugin_parser_support.yml new file mode 100644 index 0000000000..3684f32fe4 --- /dev/null +++ b/changelogs/fragments/6043-jc_plugin_parser_support.yml @@ -0,0 +1,2 @@ +minor_changes: + - jc filter plugin - added the ability to use parser plugins (https://github.com/ansible-collections/community.general/pull/6043). diff --git a/plugins/filter/jc.py b/plugins/filter/jc.py index 742d4147a1..3aa8d20a5f 100644 --- a/plugins/filter/jc.py +++ b/plugins/filter/jc.py @@ -138,8 +138,14 @@ def jc_filter(data, parser, quiet=True, raw=False): raise AnsibleError('You need to install "jc" as a Python library on the Ansible controller prior to running jc filter') try: - jc_parser = importlib.import_module('jc.parsers.' + parser) - return jc_parser.parse(data, quiet=quiet, raw=raw) + # new API (jc v1.18.0 and higher) allows use of plugin parsers + if hasattr(jc, 'parse'): + return jc.parse(parser, data, quiet=quiet, raw=raw) + + # old API (jc v1.17.7 and lower) + else: + jc_parser = importlib.import_module('jc.parsers.' + parser) + return jc_parser.parse(data, quiet=quiet, raw=raw) except Exception as e: raise AnsibleFilterError('Error in jc filter plugin: %s' % e)