<commit_before>from falafel.core.plugins import mapper

FILTER_LIST = ['COMMAND', 'libssl', 'libcrypto', 'libssl.so', 'multipath']


class Splitter(object):

    def __init__(self, lines):
        self.lines = lines
        # In some sosreport the lsof does not start with the header
        self.header_lines = [(i, l) for i, l in enumerate(lines) if 'COMMAND ' in l]
        if self.header_lines:
            self.header_idx, self.header_row = self.header_lines[0]
            self.name_idx = self.header_row.index(" NAME")
            self.pid_idx = self.header_row.index("  PID")
            _, self.mid_cols = self.header_row[:self.name_idx].split(None, 1)
            self.mid_cols = "  " + self.mid_cols
            self.indexes = {}
            for col_name in self.mid_cols.split():
                self.indexes[col_name] = self.mid_cols.index(col_name) + len(col_name)
            self.indexes["FD"] += 2

    def split_middle(self, middle):
        mid_dict = {}
        offset = 0
        for col in self.mid_cols.split():
            idx = self.indexes[col]
            mid_dict[col] = middle[offset:idx].strip()
            offset = idx
        return mid_dict

    def parse_lines(self):
        # If no header line could be found, then we are dead in the water
        if self.header_lines:
            for line in self.lines[self.header_idx + 1:]:
                command, rest = line[:self.pid_idx], line[self.pid_idx:]
                name = line[self.name_idx:]
                middle_dict = self.split_middle(rest[:self.name_idx - len(command)])
                middle_dict["COMMAND"] = command.strip()
                middle_dict["NAME"] = name.strip()
                yield middle_dict


@mapper('lsof', FILTER_LIST)
def get_lsof(context):
    return list(Splitter(context.content).parse_lines())<commit_msg>Convert lsof mapper to class<commit_after>from falafel.core.plugins import mapper
from falafel.core import MapperOutput

FILTER_LIST = ['COMMAND', 'libssl', 'libcrypto', 'libssl.so', 'multipath']


class Splitter(object):

    def __init__(self, lines):
        self.lines = lines
        # In some sosreport the lsof does not start with the header
        self.header_lines = [(i, l) for i, l in enumerate(lines) if 'COMMAND ' in l]
        if self.header_lines:
            self.header_idx, self.header_row = self.header_lines[0]
            self.name_idx = self.header_row.index(" NAME")
            self.pid_idx = self.header_row.index("  PID")
            _, self.mid_cols = self.header_row[:self.name_idx].split(None, 1)
            self.mid_cols = "  " + self.mid_cols
            self.indexes = {}
            for col_name in self.mid_cols.split():
                self.indexes[col_name] = self.mid_cols.index(col_name) + len(col_name)
            self.indexes["FD"] += 2

    def split_middle(self, middle):
        mid_dict = {}
        offset = 0
        for col in self.mid_cols.split():
            idx = self.indexes[col]
            mid_dict[col] = middle[offset:idx].strip()
            offset = idx
        return mid_dict

    def parse_lines(self):
        # If no header line could be found, then we are dead in the water
        if self.header_lines:
            for line in self.lines[self.header_idx + 1:]:
                command, rest = line[:self.pid_idx], line[self.pid_idx:]
                name = line[self.name_idx:]
                middle_dict = self.split_middle(rest[:self.name_idx - len(command)])
                middle_dict["COMMAND"] = command.strip()
                middle_dict["NAME"] = name.strip()
                yield middle_dict


@mapper('lsof', FILTER_LIST)
class Lsof(MapperOutput):

    @staticmethod
    def parse_content(content):
        return list(Splitter(content).parse_lines())

    def __iter__(self):
        return iter(self.data)
{'fraction_non_alphanumeric': 0.11567652447003403, 'fraction_numerical': 0.0026171159382360636, 'mean_word_length': 2.2472387425658455, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, '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': '6401999', 'n_tokens_mistral': 1209, 'n_tokens_neox': 1166, 'n_words': 270}