Skip to content

Commit

Permalink
[PR #6043/c168f9c3 backport][stable-6] JC: Add plugin parser function…
Browse files Browse the repository at this point in the history
…ality to JC Filter Plugin (#6047)

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

(cherry picked from commit c168f9c)

Co-authored-by: Kelly Brazil <kellyjonbrazil@gmail.com>
  • Loading branch information
patchback[bot] and kellyjonbrazil authored Feb 23, 2023
1 parent ff4aff0 commit 3802d54
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 3802d54

Please sign in to comment.