<commit_before>"""Ansible action plugin to decode ignition payloads"""

import base64
import os

from ansible.plugins.action import ActionBase
from ansible import errors
from six.moves import urllib


def get_files(files_dict, systemd_dict, dir_list, data):
    """parse data to populate file_dict"""
    for item in data['storage']['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
    for item in data['systemd']['units']:
        contents = item['contents']
        mode = "0644"
        inode = {"contents": contents, "mode": mode}
        name = item['name']<commit_msg>Enhance parse_ignition file content decoding<commit_after>"""Ansible action plugin to decode ignition payloads"""

import base64
import os

from ansible.plugins.action import ActionBase
from ansible import errors
from six.moves import urllib


# pylint: disable=too-many-function-args
def get_file_data(encoded_contents):
    """Decode data URLs as specified in RFC 2397"""
    # The following source is adapted from Python3 source
    # License: https://github.com/python/cpython/blob/3.7/LICENSE
    # retrieved from: https://github.com/python/cpython/blob/3.7/Lib/urllib/request.py
    _, data = encoded_contents.split(":", 1)
    mediatype, data = data.split(",", 1)

    # even base64 encoded data URLs might be quoted so unquote in any case:
    data = urllib.parse.unquote(data)
    if mediatype.endswith(";base64"):
        data = base64.b64decode(data).decode('utf-8')
        mediatype = mediatype[:-7]
    # End PSF software
    return data


# pylint: disable=too-many-function-args
def get_files(files_dict, systemd_dict, dir_list, data):
    """parse data to populate file_dict"""
    for item in data['storage']['files']:
        path = item["path"]
        dir_list.add(os.path.dirname(path))
        # remove prefix "data:,"
        encoded_contents = item['contents']['source']
        contents = get_file_data(encoded_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
    for item in data['systemd']['units']:
        contents = item['contents']
        mode = "0644"
        inode = {"contents": contents, "mode": mode}
        name = item['name']
{'fraction_non_alphanumeric': 0.12819650067294752, 'fraction_numerical': 0.020524899057873486, 'mean_word_length': 3.1464435146443517, 'pattern_counts': {'":': 9, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '247354', 'n_tokens_mistral': 930, 'n_tokens_neox': 877, 'n_words': 260}