"""Ansible action plugin to decode ignition payloads"""

import base64
import os
import six
from six.moves import urllib
from ansible.plugins.action import ActionBase


# pylint: disable=too-many-function-args
def get_files(files_dict, systemd_dict, dir_list, data):
    """parse data to populate file_dict"""
    files = data.get('storage', []).get('files', [])
    for item in files:
        path = item["path"]
        dir_list.add(os.path.dirname(path))
        # remove prefix "data:,"
        encoding, contents = item['contents']['source'].split(',', 1)
        if 'base64' in encoding:
            contents = base64.b64decode(contents).decode('utf-8')
        else:
            contents = urllib.parse.unquote(contents)
        # convert from int to octal, padding at least to 4 places.
        # eg, 420 becomes '0644'
        mode = str(format(int(item["mode"]), '04o'))
        inode = {"contents": contents, "mode": mode}
        files_dict[path] = inode
    # get the systemd units files we're here
    systemd_units = data.get('systemd', []).get('units', [])
    for item in systemd_units:
        contents = item['contents']
        if six.PY2:
            # pylint: disable=redefined-variable-type
            contents = contents.decode('unicode-escape')
        mode = "0644"
        inode = {"contents": contents, "mode": mode}
        name = item['name']
        path = '/etc/systemd/system/' + name
        dir_list.add(os.path.dirname(path))
        files_dict[path] = inode
        enabled = item.get('enabled') or True
        systemd_dict[name] = enabled


# pylint: disable=too-few-public-methods
class ActionModule(ActionBase):
    """ActionModule for parse_ignition.py"""

    def run(self, tmp=None, task_vars=None):
        """Run parse_ignition action plugin"""
        result = super(ActionModule, self).run(tmp, task_vars)
        result["changed"] = False
        result["failed"] = False
        result["msg"] = "Parsed successfully"
        files_dict = {}
        systemd_dict = {}
        dir_list = set()
        result["files_dict"] = files_dict
        result["systemd_dict"] = systemd_dict

        # self.task_vars holds all in-scope variables.
        # Ignore settting self.task_vars outside of init.
        # pylint: disable=W0201
        self.task_vars = task_vars or {}
        ign_file_contents = self._task.args.get('ign_file_contents')
        get_files(files_dict, systemd_dict, dir_list, ign_file_contents)
        result["dir_list"] = list(dir_list)
        return result

@@ -162,16 +162,726 @@
 nBase%0A%0A%0A
+# pylint: disable=too-many-function-args%0Adef get_file_data(encoded_contents):%0A    %22%22%22Decode data URLs as specified in RFC 2397%22%22%22%0A    # The following source is adapted from Python3 source%0A    # License: https://github.com/python/cpython/blob/3.7/LICENSE%0A    # retrieved from: https://github.com/python/cpython/blob/3.7/Lib/urllib/request.py%0A    _, data = encoded_contents.split(%22:%22, 1)%0A    mediatype, data = data.split(%22,%22, 1)%0A%0A    # even base64 encoded data URLs might be quoted so unquote in any case:%0A    data = urllib.parse.unquote(data)%0A    if mediatype.endswith(%22;base64%22):%0A        data = base64.b64decode(data).decode('utf-8')%0A        mediatype = mediatype%5B:-7%5D%0A    # End PSF software%0A    return data%0A%0A%0A
 # pylint
@@ -1211,13 +1211,11 @@
 ncod
-ing, 
+ed_
 cont
@@ -1251,180 +1251,50 @@
 ce'%5D
-.split(',', 1)%0A        if 'base64' in encoding:%0A            contents = base64.b64decode(contents).decode('utf-8')%0A        else:%0A            contents = urllib.parse.unquote(
+%0A        contents = get_file_data(encoded_
 cont

Enhance parse_ignition file content decoding