Skip to content

Commit

Permalink
JC: Add plugin parser functionality to JC Filter Plugin (#6043)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
kellyjonbrazil authored Feb 23, 2023
1 parent a7e8e95 commit c168f9c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/6043-jc_plugin_parser_support.yml
Original file line number Diff line number Diff line change
@@ -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).
10 changes: 8 additions & 2 deletions plugins/filter/jc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit c168f9c

Please sign in to comment.