Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[release_table] Support specifying a column by its index #333

Merged
merged 2 commits into from
Mar 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions releases/amazon-linux.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
"name": "2023.3.20240312.0",
"date": "2024-03-16"
},
"2018.03.0.20231218.0": {
"name": "2018.03.0.20231218.0",
"date": "2024-03-16"
},
"2.0.20240306.2": {
"name": "2.0.20240306.2",
"date": "2024-03-16"
Expand Down Expand Up @@ -45,10 +49,6 @@
"name": "2023.3.20231218.0",
"date": "2023-12-20"
},
"2018.03.0.20231218.0": {
"name": "2018.03.0.20231218.0",
"date": "2023-12-20"
},
"2.0.20231218.0": {
"name": "2.0.20231218.0",
"date": "2023-12-20"
Expand Down
30 changes: 25 additions & 5 deletions src/release_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
- ignore_empty_releases (optional, default = false): A boolean value indicating whether to ignore releases with no
fields except the name.
- fields: A dictionary that maps release fields to the table's columns. Field definition include:
- column (mandatory): The name of the column in the table. This is case-insensitive.
- column (mandatory): The name or index (starts at 1) of the column in the table.
- type (mandatory, default = string): The type of the field. Supported types are:
- string: The raw string value.
- date : A full or year-month date (supported patterns available in common.dates).
Expand All @@ -38,6 +38,20 @@
- template (mandatory, default = DEFAULT_TEMPLATE): A liquid template used to clean up the value using the matched
groups from a 'regex'.

Note that defining the column attribute directly instead of its full definition is allowed when
there the column name or index is the only attribute. For example, this:
```
fields:
releaseCycle:
column: "End of life"
```

can be replaced with this:
```
fields:
releaseCycle: "End of life"
```

Supported CSS selectors are defined by BeautifulSoup and documented on its website. For more information, see
https://beautiful-soup-4.readthedocs.io/en/latest/index.html?highlight=selector#css-selectors."""

Expand All @@ -54,7 +68,9 @@

class Field:
def __init__(self, name: str, definition: str | dict) -> None:
if isinstance(definition, str):
# Directly specifying the column name or index instead of its full definition is allowed.
# In this case we must convert it to a full definition.
if isinstance(definition, (str, int)):
definition = {"column": definition}

self.name = name
Expand All @@ -63,7 +79,12 @@ def __init__(self, name: str, definition: str | dict) -> None:
definition["regex"] = definition.get("regex", [DEFAULT_RELEASE_REGEX])
definition["template"] = definition.get("template", DEFAULT_TEMPLATE)

self.column = definition["column"].lower()
self.is_index = isinstance(definition["column"], int)
if self.is_index:
self.column = definition["column"] - 1 # convert to 0-based index
else:
self.column = definition["column"].lower()

self.type = definition.get("type", "string")
if self.name in DATE_FIELDS and self.type not in DATE_TYPES:
self.type = "date" # override type for known date fields
Expand Down Expand Up @@ -150,7 +171,7 @@ def __repr__(self) -> str:
try:
fields_index = {"releaseCycle": headers.index(release_cycle_field.column)}
for field in fields:
fields_index[field.name] = headers.index(field.column)
fields_index[field.name] = field.column if field.is_index else headers.index(field.column)
min_column_count = max(fields_index.values()) + 1

for row in table.select(rows_selector):
Expand Down Expand Up @@ -183,6 +204,5 @@ def __repr__(self) -> str:
logging.info(f"removing future release '{release}'")
product_data.remove_release(release_name)


except ValueError as e:
logging.info(f"skipping table with headers {headers}: {e}")