예제 #1
0
def test_parse_fixed_table():
    data = parse_fixed_table(FIXED_CONTENT_1.splitlines())
    assert len(data) == 3
    assert data[0] == {'Column1': 'data1', 'Column2': 'data 2', 'Column3': 'data   3'}
    assert data[1] == {'Column1': 'data4', 'Column2': 'data5', 'Column3': 'data6'}
    assert data[2] == {'Column1': 'data     7', 'Column2': '', 'Column3': 'data   9'}

    data = parse_fixed_table(FIXED_CONTENT_1A.splitlines(), heading_ignore=['Column1 '])
    assert len(data) == 3
    assert data[0] == {'Column1': 'data1', 'Column2': 'data 2', 'Column3': 'data   3'}
    assert data[1] == {'Column1': 'data4', 'Column2': 'data5', 'Column3': 'data6'}
    assert data[2] == {'Column1': 'data     7', 'Column2': '', 'Column3': 'data   9'}

    data = parse_fixed_table(FIXED_CONTENT_1B.splitlines())
    assert len(data) == 3
    assert data[0] == {'Column1': 'data1', 'Column2': 'data 2', 'Column3': ''}
    assert data[1] == {'Column1': 'data4', 'Column2': 'data5', 'Column3': 'data6'}
    assert data[2] == {'Column1': 'data   7', 'Column2': '', 'Column3': 'data   9'}

    data = parse_fixed_table(FIXED_CONTENT_2.splitlines(), heading_ignore=['Column1 '])
    assert len(data) == 3
    assert data[0] == {'Column1': 'data1', 'Column2': 'data 2', 'Column3': 'data   3'}
    assert data[1] == {'Column1': 'data4', 'Column2': 'data5', 'Column3': 'data6'}
    assert data[2] == {'Column1': 'data     7', 'Column2': '', 'Column3': 'data   9'}

    data = parse_fixed_table(FIXED_CONTENT_3.splitlines(),
                             heading_ignore=['Column1 '],
                             trailing_ignore=['Trailing', 'Another'])
    assert len(data) == 3
    assert data[0] == {'Column1': 'data1', 'Column2': 'data 2', 'Column3': 'data   3'}
    assert data[1] == {'Column1': 'data4', 'Column2': 'data5', 'Column3': 'data6'}
    assert data[2] == {'Column1': 'data     7', 'Column2': '', 'Column3': 'data   9'}

    data = parse_fixed_table(FIXED_CONTENT_4.splitlines(),
                             heading_ignore=['Column1 '],
                             header_substitute=[('Column 2', 'Column_2'), ('Column 3', 'Column_3')],
                             trailing_ignore=['Trailing', 'Another'])
    assert len(data) == 4
    assert data[0] == {'Column1': 'data1', 'Column_2': 'data 2', 'Column_3': 'data   3'}
    assert data[1] == {'Column1': 'data4', 'Column_2': 'data5', 'Column_3': 'data6'}
    assert data[2] == {'Column1': 'data     7', 'Column_2': '', 'Column_3': 'data   9'}
    assert data[3] == {'Column1': 'data10', 'Column_2': '', 'Column_3': ''}

    # Test that if we search for trailing data that is always found, then we
    # should get the whole thing parsed as a table from the header line
    data = parse_fixed_table(
        ['foo' + line for line in FIXED_CONTENT_4.splitlines()],
        heading_ignore=['fooColumn1 '],
        header_substitute=[('fooColumn1', 'Column1'), ('Column 2', 'Column_2'), ('Column 3', 'Column_3')],
        trailing_ignore=['foo']
    )
    assert len(data) == 6
    assert data[4] == {'Column1': 'fooTrailing', 'Column_2': 'non-data li', 'Column_3': 'ne'}
    assert data[5] == {'Column1': 'foo Another', 'Column_2': 'trailing no', 'Column_3': 'n-data line'}

    data = parse_fixed_table(FIXED_CONTENT_DUP_HEADER_PREFIXES.splitlines())
    assert data[0] == {'NAMESPACE': 'default', 'NAME': 'foo', 'LABELS': 'app=superawesome'}
    data = parse_fixed_table(FIXED_CONTENT_5.splitlines())
    assert len(data) == 3
예제 #2
0
 def parse_content(self, content):
     # Stored data in a dictionary data structure
     self.data = {}
     content = get_active_lines(content, '----')
     idxs = [
         i for i, l in enumerate(content)
         if l.startswith('Status of volume')
     ]
     for i, idx in enumerate(idxs):
         start = idx
         end = idxs[i + 1] if i < len(idxs) - 1 else -1
         _, val = content[idx].split(":", 1)
         body = parse_fixed_table(content[start:end],
                                  header_substitute=[
                                      ('Gluster process',
                                       'Gluster_process'),
                                      ('TCP Port', 'TCP_Port'),
                                      ('RDMA Port', 'RDMA_Port')
                                  ],
                                  heading_ignore=['Gluster process'],
                                  trailing_ignore=['Task Status'])
         self.data[val.strip()] = body
     if not self.data:
         # If no data is obtained in the command execution then throw an exception instead of returning an empty
         # object.  Rules depending solely on this parser will not be invoked, so they don't have to
         # explicitly check for invalid data.
         raise ParseException(
             "Unable to parse gluster volume status: {0}".format(content))
예제 #3
0
    def parse_content(self, content):
        """
        Parse the lines given into a list of dictionaries for each row.  This
        is stored in the ``rows`` attribute.

        If the ``key_field`` property is set, use this to key a ``data``
        dictionary attribute.
        """
        if not (self.key_field and self.attr_name):
            raise NotImplementedError("'key_field' or 'attr_name' is not defined")

        # There is another application named docker that's a kde system tray, that
        # will output help when the spec is run due to incorrect arguments. So check
        # the content for any lines starting with Usage: so it can be skipped.
        if any(l for l in content if l.startswith("Usage: ")):
            raise SkipException('No data only help output.')

        self.rows = parse_fixed_table(content,
                                      heading_ignore=self.heading_ignore,
                                      header_substitute=self.substitutions)

        if not self.rows:
            raise SkipException('No data.')

        data = {}
        for row in self.rows:
            k = row.get(self.key_field)
            for sub in self.substitutions:
                row[sub[0]] = row.pop(sub[1]) if sub[1] in row else None
            if k is not None and k != '<none>':
                data[k] = row
        setattr(self, self.attr_name, data)
예제 #4
0
    def parse_content(self, content):
        if not content:
            raise SkipException('No repolist.')

        trailing_line_prefix = [
            'repolist:',
            'Uploading Enabled',
            'Loaded plugins:',
            'Unable to upload',
        ]

        self.data = []
        self.repos = {}
        self.data = parse_fixed_table(content,
                                      heading_ignore=['repo id'],
                                      header_substitute=[
                                          ('repo id', 'id     '),
                                          ('repo name', 'name     ')
                                      ],
                                      trailing_ignore=trailing_line_prefix,
                                      empty_exception=True)

        if not self.data:
            raise SkipException('No repolist.')

        self.repos = dict(
            (d['id'].lstrip('!').lstrip('*'), d) for d in self.data)
예제 #5
0
    def parse_content(self, content):
        """
        Parse the lines given into a list of dictionaries for each row.  This
        is stored in the ``rows`` attribute.

        If the ``key_field`` property is set, use this to key a ``data``
        dictionary attribute.
        """
        if not (self.key_field and self.attr_name):
            raise NotImplementedError(
                "'key_field' or 'attr_name' is not defined")

        self.rows = parse_fixed_table(content,
                                      heading_ignore=self.heading_ignore,
                                      header_substitute=self.substitutions)

        if not self.rows:
            raise SkipException('No data.')

        data = {}
        for row in self.rows:
            k = row.get(self.key_field)
            for sub in self.substitutions:
                row[sub[0]] = row.pop(sub[1]) if sub[1] in row else None
            if k is not None and k != '<none>':
                data[k] = row
        setattr(self, self.attr_name, data)
예제 #6
0
    def parse_content(self, content):
        self.fields = []
        self.data = []
        if not content:
            raise SkipException("Empty file")

        self.data = parse_fixed_table(content,
                                      heading_ignore=[
                                          "COMMAND", "PID", "USER", "FD",
                                          "TYPE", "DEVICE", "SIZE/OFF", "NODE",
                                          "NAME"
                                      ],
                                      header_substitute=[
                                          ("COMMAND", "command"),
                                          ("PID", "pid"), ("USER", "user"),
                                          ("FD", "fd"), ("TYPE", "type"),
                                          ("DEVICE", "device"),
                                          ("SIZE/OFF", "size_off"),
                                          ("NODE", "node"), ("NAME", "name")
                                      ])
        if not self.data:
            raise SkipException("Useless data")

        for item in self.data:
            self.fields.append(
                self.keyvalue(item["command"], item["pid"], item["user"],
                              item["fd"], item["type"], item["device"],
                              item["size_off"], item["node"], item["name"]))
예제 #7
0
 def parse_content(self, content):
     data = parse_fixed_table(content,
                              heading_ignore=['Name '],
                              trailing_ignore=['Hint:', 'Error:'])
     if not data:
         raise SkipException('Nothing need to parse.')
     for m in data:
         self.update({m['Name']: DnfModuleBrief(m)})
예제 #8
0
 def parse_content(self, content):
     self.data = parse_fixed_table(
         [line for line in content if '========' not in line],
         heading_ignore=['queue']
     )
     self.by_queue = dict(
         (q['queue'], q)
         for q in self.data
     )
예제 #9
0
    def parse_content(self, content):
        self.cols = []
        self.keywords = []
        if not content:
            raise SkipException("No data.")

        self.cols = parse_fixed_table(content,
                                      header_substitute=[
                                          ('TARGET', 'target'),
                                          ('SOURCE', 'source'),
                                          ('FSTYPE', 'fstype'),
                                          ('OPTIONS', 'options'),
                                          ('PROPAGATION', 'propagation')
                                      ])
        self.keywords = [col['target'] for col in self.cols]
    def parse_content(self, content):
        if len(content) < 2 or "NAME" not in content[0]:
            raise ParseException("invalid content: {0}".
                                 format(content) if content else 'empty file')

        self.data = parse_fixed_table(content,
                                      header_substitute=[('SERVICE ACCOUNTS',
                                                          'SERVICE_ACCOUNTS')])
        self.rolebinding = {}

        for rolebinding in self.data:
            rolebinding_name = rolebinding["NAME"].strip()
            rolebinding_role = rolebinding["ROLE"].strip()
            if rolebinding_name and rolebinding_role:
                self.rolebinding[rolebinding_name] = rolebinding_role
예제 #11
0
    def parse_content(self, content):
        if not content:
            raise SkipException("Empty content")
        if len(content) < 21 or not ('Quorum information' in content[0] and
                                     'Votequorum information' in content[9] and
                                     'Membership information' in content[17]):
            raise ParseException("Incorrect content: '{0}'".format(content))

        self.quorum_info = {}
        for line in content[2:7]:
            key, value = line.split(':', 1)
            self.quorum_info[key.strip()] = value.strip()

        self.votequorum_info = {}
        for line in content[11:15]:
            key, value = line.split(':', 1)
            self.votequorum_info[key.strip()] = value.strip()

        self.membership_info = parse_fixed_table(content[19:])
예제 #12
0
    def parse_content(self, content):
        if not (self.key_field and self.attr_name):
            raise NotImplementedError(
                "'key_field' or 'attr_name' is not defined")

        self.rows = parse_fixed_table(content,
                                      heading_ignore=self.heading_ignore,
                                      header_substitute=self.substitutions)

        if not self.rows:
            raise SkipException('No data.')

        data = {}
        for row in self.rows:
            k = row.get(self.key_field)
            for sub in self.substitutions:
                row[sub[0]] = row.pop(sub[1])
            if k is not None and k != '<none>':
                data[k] = row
        setattr(self, self.attr_name, data)
예제 #13
0
    def parse_content(self, content):
        if not content:
            raise SkipException('No repolist.')

        if content[0].startswith('repolist:'):
            raise SkipException('No repolist.')

        trailing_line_prefix = [
            'repolist:',
            'Uploading Enabled',
            'Loaded plugins:',
            'Plugin ',
            'Unable to upload',
        ]

        self.data = []
        self.repos = {}
        try:
            self.data = parse_fixed_table(content,
                                          heading_ignore=['repo id'],
                                          header_substitute=[
                                              ('repo id', 'id     '),
                                              ('repo name', 'name     ')
                                          ],
                                          trailing_ignore=trailing_line_prefix,
                                          empty_exception=True)
        except ValueError as e:
            # ValueError raised by parse_fixed_table
            raise ParseException('Failed to parser yum repolist: {0}'.format(
                str(e)))

        if not self.data:
            raise SkipException('No repolist.')

        self.repos = dict(
            (d['id'].lstrip('!').lstrip('*'), d) for d in self.data)
예제 #14
0
def test_parse_fixed_table_empty_exception():
    with pytest.raises(ParseException) as pe:
        parse_fixed_table(FIXED_CONTENT_1B.splitlines(), empty_exception=True)
    assert "Incorrect line:" in str(pe.value)
예제 #15
0
 def _parse_data(self, heading_ignore):
     self.data.extend(
         parse_fixed_table(self._content, heading_ignore=heading_ignore))