Erik Olof Gunnar Andersson b6e78fefdd pyupgrade changes for Python3.8+
Result of running

$ pyupgrade --py38-plus $(git ls-files | grep ".py$")

This was inspired by Nova [1] and Octavia [2]

Fixed PEP8 errors introduced by pyupgrade by running:

$ autopep8 --select=E127,E128,E501 --max-line-length 79 -r \
  --in-place designate

and manual updates.

[1]: https://review.opendev.org/c/openstack/nova/+/896986
[2]: https://review.opendev.org/c/openstack/octavia/+/899263

Change-Id: I27d09edb7e75081379002615f4ecdb1aa007dbcf
2023-11-17 09:42:13 -08:00

96 lines
3.2 KiB
Python

"""
Copyright 2015 Rackspace
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
from tempest.lib.cli import output_parser
class Model:
def __str__(self):
return str(self.__dict__)
class FieldValueModel(Model):
"""This converts cli output from messy lists/dicts to neat attributes."""
def __init__(self, out):
"""This parses output with fields and values like:
+----------------+------------------------------+
| Field | Value |
+----------------+------------------------------+
| action | CREATE |
| created_at | 2015-08-20T17:22:17.000000 |
| description | None |
+----------------+------------------------------+
These are then accessible as:
model.action
model.created_at
model.description
"""
table = output_parser.table(out)
# Because the output_parser handles Values with multiple lines
# in additional Field/Value pairs with Field name '', the following
# code is necessary to aggregate Values.
#
# The list of Field/Value pairs is in-order, so we can append Value
# continuation to the previously seen Field, with a newline separator.
value_lines = []
prev_field = None
for field, value in table['values']:
if field == '':
value_lines.append(value)
setattr(self, prev_field, '\n'.join(value_lines))
else:
setattr(self, field, value)
prev_field = field
value_lines = [value]
class ListEntryModel(Model):
def __init__(self, fields, values):
for k, v in zip(fields, values):
setattr(self, k, v)
class ListModel(Model, list):
def __init__(self, out):
"""This parses an output table with any number of headers, and any
number of entries:
+--------------------------------------+----------+---------+
| id | name | type |
+--------------------------------------+----------+---------+
| e658a875-1024-4f88-a347-e5b244ec5a10 | aaa.com. | PRIMARY |
+--------------------------------------+----------+---------+
| 98d1fb5f-2954-448e-988e-6f1df0f24c52 | bbb.com. | PRIMARY |
+--------------------------------------+----------+---------+
These are then accessible as:
model[0].name == 'aaa.com.'
model[1].name == 'bbb.com.'
"""
table = output_parser.table(out)
for entry in table['values']:
self.append(ListEntryModel(table['headers'], entry))