Пример #1
0
    def parse_content(self, content):
        # remove lssap version and bar text from content
        clean_content = content[2:-1]
        if len(clean_content) > 0 and "SID" in clean_content[0]:
            self.data = parse_delimited_table(clean_content,
                                              delim='|',
                                              header_delim=None)
        else:
            raise ParseException(
                "Lssap: Unable to parse {0} line(s) of content: ({1})".format(
                    len(content), content))

        self.running_inst_types = [
            row["Instance"].rstrip('1234567890') for row in self.data
            if "Instance" in row
        ]

        invalid_inst = [
            i for i in self.running_inst_types if i not in self.instance_dict
        ]

        if invalid_inst:
            raise ParseException(
                "Lssap: Invalid instance parsed in content: {0}".format(
                    invalid_inst))
Пример #2
0
    def parse_content(self, content):
        self.data = parse_delimited_table(content)

        self.names = [dm['Name'] for dm in self.data if 'Name' in dm]
        self.by_name = dict(
            (dm['Name'], dm) for dm in self.data if 'Name' in dm)
        self.uuids = [dm['UUID'] for dm in self.data if 'UUID' in dm]
        self.by_uuid = dict(
            (dm['UUID'], dm) for dm in self.data if 'UUID' in dm)
        self.info = []
        for dm in self.data:
            self.info.append(
                SetupInfo(name=dm.get('Name'),
                          major=int(dm.get('Maj'))
                          if 'Maj' in dm and dm.get('Maj').isdigit() else None,
                          minor=int(dm.get('Min'))
                          if 'Min' in dm and dm.get('Min').isdigit() else None,
                          open=int(dm.get('Open')) if 'Open' in dm
                          and dm.get('Open').isdigit() else None,
                          segments=int(dm.get('Targ')) if 'Targ' in dm
                          and dm.get('Targ').isdigit() else None,
                          events=int(dm.get('Event')) if 'Event' in dm
                          and dm.get('Event').isdigit() else None,
                          live_table=dm.get('Stat', '----')[0] == 'L',
                          inactive_table=dm.get('Stat', '----')[1] == 'I',
                          suspended=dm.get('Stat', '----')[2] == 's',
                          readonly=dm.get('Stat', '----')[3] == 'r',
                          uuid=dm.get('UUID')))
Пример #3
0
 def parse_content(self, content):
     """
     Parse each line in the file ``/etc/fstab``.
     """
     fstab_output = parse_delimited_table([FS_HEADINGS] +
                                          get_active_lines(content))
     self.data = []
     for line in fstab_output:
         line['fs_spec'] = line.get('fs_spec', '')
         line['fs_vfstype'] = line.get('fs_vfstype', '')
         # Decode fs_file to transfer the '\040' to ' '.
         # Encode first and then decode works for both Python2 and Python3.
         line['fs_file'] = line.get('fs_file',
                                    '').encode().decode("unicode-escape")
         line['fs_freq'] = int(line.get('fs_freq', '0'))
         line['fs_passno'] = int(line.get('fs_passno', '0'))
         # if there is no mntops, it is defaults.
         # (/dev/foo /foo somefs defaults   0 0) and (/dev/foo /foo somefs) are same
         line['raw_fs_mntops'] = line.get('raw_fs_mntops', 'defaults')
         # optlist_to_dict converts 'key=value' to key: value and 'key' to key: True
         line['fs_mntops'] = MountOpts(
             optlist_to_dict(line.get('raw_fs_mntops')))
         # add `raw` here for displaying convenience on front-end
         line['raw'] = [
             l for l in content if l.strip().startswith(line['fs_spec'])
         ][0]
         self.data.append(FSTabEntry(line))
     # assert: all mount points of valid entries are unique by definition
     self.mounted_on = dict((row.fs_file, row) for row in self.data)
Пример #4
0
 def parse_content(self, content):
     """Parse the command output"""
     with_header = ['advisory type package'] + content
     table = parse_delimited_table(with_header)
     if not table:
         raise SkipException('No data.')
     self._items = table
Пример #5
0
 def parse_content(self, content):
     # Skip 'IPv6/IPv6 Group Memberships' and '-----' lines.
     content = content[1:2] + content[3:]
     table = parse_delimited_table(content)
     self.data = [
         dict((k.lower(), v) for (k, v) in item.items()) for item in table
     ]
Пример #6
0
 def parse_content(self, content):
     self._group_by_iface = {}
     # heading_ignore is first line we _don't_ want to ignore...
     table = parse_delimited_table(content, heading_ignore=['Iface'])
     self.data = [dict((k, v) for (k, v) in item.items()) for item in table]
     for entry in self.data:
         self._group_by_iface[entry["Iface"]] = \
             dict((k, v) for (k, v) in entry.items() if k != 'Iface')
     return
Пример #7
0
    def parse_content(self, content):
        if not content:
            raise SkipException('Empty content')

        self.update(
            dict((row['name'], row) for row in parse_delimited_table(
                content,
                heading_ignore=['major', 'minor'],
                header_substitute=[('#blocks', 'blocks')]) if 'name' in row))
Пример #8
0
    def parse_content(self, content):
        self.data = parse_delimited_table(content)

        self.names = [dm['Name'] for dm in self.data if 'Name' in dm]
        self.by_name = dict(
            (dm['Name'], dm) for dm in self.data if 'Name' in dm)
        self.uuids = [dm['UUID'] for dm in self.data if 'UUID' in dm]
        self.by_uuid = dict(
            (dm['UUID'], dm) for dm in self.data if 'UUID' in dm)
Пример #9
0
 def parse_content(self, content):
     try:
         self.data = parse_delimited_table(
             content, heading_ignore=["NAME", "UUID", "TYPE", "DEVICE"])
     except:
         raise SkipException("Invalid Contents!")
     self._disconnected_connection = []
     for all_connection in self.data:
         if all_connection['DEVICE'] == "--":
             self._disconnected_connection.append(all_connection['NAME'])
Пример #10
0
 def parse_content(self, content):
     # Use headings without spaces and colons
     SSTULPN_TABLE_HEADER = [
         "Netid  State  Recv-Q  Send-Q  Local-Address-Port Peer-Address-Port  Process"
     ]
     content = [
         line for line in content
         if (('UNCONN' in line) or ('LISTEN' in line))
     ]
     self.data = parse_delimited_table(SSTULPN_TABLE_HEADER + content)
Пример #11
0
    def parse_content(self, content):
        if not content:
            raise SkipException("Empty output.")

        self.extend(parse_delimited_table(content))
        for entry in self:
            for key in ['SIZELIMIT', 'OFFSET', 'LOG-SEC']:
                if key in entry:
                    entry[key] = int(entry[key])
            for key in ['AUTOCLEAR', 'RO', 'DIO']:
                if key in entry:
                    entry[key] = True if entry[key] == '1' else False
Пример #12
0
 def parse_content(self, content):
     # heading_ignore is first line we _don't_ want to ignore...
     ids = ['semid', 'shmid', 'msqid']
     table = parse_delimited_table(content, heading_ignore=['key'] + ids)
     if not table:
         raise SkipException('Nothing to parse.')
     id_s = [i for i in table[0] if i in ids]
     if not id_s or len(id_s) != 1:
         raise ParseException('Unexpected heading line.')
     id_ok = id_s[0]
     self.data = {}
     for item in table:
         self.data[item.pop(id_ok)] = item
Пример #13
0
    def parse_content(self, content):
        self.data = []
        # remove lssap version and bar text from content
        clean_content = content[2:-1]
        if len(clean_content) > 0 and "SID" in clean_content[0]:
            self.data = parse_delimited_table(clean_content,
                                              delim='|',
                                              header_delim=None)
        else:
            raise ParseException(
                "Lssap: Unable to parse {0} line(s) of content: ({1})".format(
                    len(content), content))

        self.sid = sorted(set(row["SID"] for row in self.data if "SID" in row))
        self.instances = [
            row["Instance"] for row in self.data if "Instance" in row
        ]
        self.instance_types = sorted(
            set(inst.rstrip('1234567890') for inst in self.instances))
Пример #14
0
    def parse_content(self, content):
        if not content:
            raise SkipException("Empty output.")

        header = "hostname, instanceNr, status, starttime, endtime, dispstatus"
        if len(content) <= 3 or header not in content:
            raise ParseException("Incorrect content: '{0}'".format(
                content[-1]))

        header_sp = [i.strip() for i in header.split(',')]
        self.data = parse_delimited_table(content,
                                          delim=',',
                                          max_splits=5,
                                          strip=True,
                                          heading_ignore=header_sp)

        if not self.data:
            raise SkipException("Empty or useless output.")

        self.is_running = all(l['status'] == 'Running' for l in self.data)
        self.is_green = all(l['dispstatus'] == 'GREEN' for l in self.data)
Пример #15
0
    def parse_content(self, content):
        if not content:
            raise SkipException()

        self.data = []
        # remove lssap version and bar text from content
        start_ndx = end_index = -1
        for i, l in enumerate(content):
            if start_ndx == -1 and l.lstrip().startswith("========"):
                start_ndx = i
                continue
            if end_index == -1 and l.strip().startswith("========"):
                end_index = i
                break
        if start_ndx == -1 or end_index == -1:
            raise ParseException(
                "Lssap: Unable to parse {0} line(s) of content: ({1})".format(
                    len(content), content))

        clean_content = content[start_ndx + 1:end_index]
        if len(clean_content) > 0 and clean_content[0].lstrip().startswith(
                "SID"):
            self.data = parse_delimited_table(clean_content,
                                              delim='|',
                                              header_delim=None)
        else:
            raise ParseException(
                "Lssap: Unable to parse {0} line(s) of content: ({1})".format(
                    len(content), content))

        self.sid = sorted(set(row["SID"] for row in self.data if "SID" in row))
        self.instances = [
            row["Instance"] for row in self.data if "Instance" in row
        ]
        self.instance_types = sorted(
            set(inst.rstrip('1234567890') for inst in self.instances))
Пример #16
0
def test_parse_delimited_table():
    # No content?  No table.
    assert parse_delimited_table([]) == []

    # Test maximum splits and header 'ignore', which should actually be
    # called 'header_startswith'
    tbl = parse_delimited_table(PS_AUX_TEST.splitlines(),
                                max_splits=10,
                                heading_ignore=['USER'])
    assert tbl
    assert isinstance(tbl, list)
    assert len(tbl) == 6
    assert isinstance(tbl[0], dict)
    assert tbl[0] == {
        '%MEM': '0.0',
        'TTY': '?',
        'VSZ': '19356',
        'PID': '1',
        '%CPU': '0.0',
        'START': 'May31',
        'COMMAND': '/sbin/init',
        'USER': '******',
        'STAT': 'Ss',
        'TIME': '0:01',
        'RSS': '1544'
    }
    assert tbl[5]['COMMAND'] == \
        '/usr/libexec/qemu-kvm -name rhel7 -S -M rhel6.5.0 -enable-kvm -m 1024 -smp 2,sockets=2,cores=1,threads=1 -uuid 13798ffc-bc1e-d437-4f3f-2e0fa6c923ad'

    # Test trailing ignore not found
    tbl = parse_delimited_table(
        MISSING_DATA_TEST.splitlines(),
        delim='|',
        heading_ignore=['LVM2_PV_FMT'],
        trailing_ignore=['WARNING', 'ERROR', 'Cannot get lock'])
    assert isinstance(tbl, list)
    assert len(tbl) == 0

    # Header substitution
    tbl = parse_delimited_table(SUBSTITUTE_HEADERS_TEST.splitlines(),
                                delim=',',
                                strip=False,
                                header_substitute=[('read-only', 'read_only')])
    assert tbl
    assert isinstance(tbl, list)
    assert len(tbl) == 2
    assert isinstance(tbl[1], dict)
    assert tbl[1] == {
        'address': '10.76.19.184',
        'port': '37500',
        'state': 'ESTAB',
        'read_only': 'Y'
    }

    # Test change of delimiter and trailing_ignore
    tbl = parse_delimited_table(POSTGRESQL_LOG.splitlines(),
                                delim='|',
                                trailing_ignore=['('])
    assert isinstance(tbl, list)
    assert len(tbl) == 14
    assert isinstance(tbl[0], dict)
    assert tbl[0] == {
        'schema': 'public',
        'table': 'rhnsnapshotpackage',
        'rows': '47428950'
    }

    # Test using different header delimiter
    result = parse_delimited_table(TABLE3.splitlines(),
                                   delim="^",
                                   header_delim="|")
    assert isinstance(result, list)
    assert len(result) == 2
    assert isinstance(result[0], dict)
    expected = [{
        "THIS": "this",
        "IS": "is",
        "A": "some",
        "HEADER": "content"
    }, {
        "THIS": "This",
        "IS": "is",
        "A": "more",
        "HEADER": "content"
    }]
    assert expected == result

    # Test explicit None as header delimiter, different from content delimiter
    result = parse_delimited_table(TABLE2, delim='|', header_delim=None)
    assert isinstance(result, list)
    assert len(result) == 2
    assert isinstance(result[0], dict)
    expected = [{
        "SID": "HA2",
        "Nr": "16",
        "Instance": "D16",
        "SAPLOCALHOST": "lu0417",
        "Version": "749, patch 10, changelist 1698137",
        "DIR_EXECUTABLE": "/usr/sap/HA2/D16/exe"
    }, {
        "SID": "HA2",
        "Nr": "22",
        "Instance": "D22",
        "SAPLOCALHOST": "lu0417",
        "Version": "749, patch 10, changelist 1698137",
        "DIR_EXECUTABLE": "/usr/sap/HA2/D22/exe"
    }]
    assert expected == result

    # Test raw_line_key
    TABLE1_SP = TABLE1.splitlines()
    result = parse_delimited_table(TABLE1_SP, raw_line_key='raw_line')
    assert isinstance(result, list)
    assert len(result) == 2
    assert isinstance(result[0], dict)
    # Get the RAW line
    assert result[0]['raw_line'] == TABLE1_SP[1]
Пример #17
0
 def parse_content(self, content):
     self.data = parse_delimited_table(content)
     self._disconnected_connection = []
     for all_connection in self.data:
         if all_connection['DEVICE'] == "--":
             self._disconnected_connection.append(all_connection['NAME'])
Пример #18
0
 def parse_content(self, content):
     # Use headings without spaces and colons
     SSTUPNA_TABLE_HEADER = [
         "Netid  State  Recv-Q  Send-Q  Local-Address-Port Peer-Address-Port  Process"
     ]
     self.data = parse_delimited_table(SSTUPNA_TABLE_HEADER + content[1:])