Apply various minor refactorings

Improve readability and fix a few Python warnings (line too long, exception too broad...) through various minor refactorings.
This commit is contained in:
Marc Wrobel
2023-05-20 12:45:14 +02:00
parent 70f20da616
commit 208ab8e2f8
19 changed files with 106 additions and 87 deletions

View File

@@ -5,42 +5,44 @@ from datetime import datetime
# https://regex101.com/r/zPxBqT/1
REGEX = r"\d.\d+\.\d+-gke\.\d+"
CHANNELS = ['nochannel', 'stable', 'regular', 'rapid']
def fetch_channel(channel):
url = "https://cloud.google.com/kubernetes-engine/docs/release-notes-{}".format(channel)
url = f"https://cloud.google.com/kubernetes-engine/docs/release-notes-{channel}"
response = endoflife.fetch_url(url)
return BeautifulSoup(response, features="html5lib")
"""
Takes soup, and returns a dictionary of versions and their release dates
"""
def parse_soup_for_versions(soup):
""" Parse the soup """
"""Takes soup, and returns a dictionary of versions and their release dates
"""
versions = {}
for section in soup.find_all('section', class_='releases'):
# h2 contains the date, which we parse
for h2 in section.find_all('h2'):
date = h2.get('data-text')
date = datetime.strptime(date, '%B %d, %Y').strftime('%Y-%m-%d')
# The div next to the h2 contains the notes about changes made on that date
# The div next to the h2 contains the notes about changes made
# on that date
next_div = h2.find_next('div')
# New releases are noted in a nested list, so we look for that
# and parse it using the version regex
for li in next_div.find_all('li'):
# If the <li> text contains with "versions are now available:", get the <ul> inside the li
# If the <li> text contains with "versions are now available:",
# get the <ul> inside the li
if "versions are now available" in li.text:
ul = li.find('ul')
for version in re.findall(REGEX, ul.text):
versions[version] = date
print("%s: %s" % (version, date))
print(f"{version}: {date}")
return versions
CHANNELS = ['nochannel', 'stable', 'regular', 'rapid']
for channel in CHANNELS:
soup = fetch_channel(channel)
print("::group::GKE - {}".format(channel))
print(f"::group::GKE - {channel}")
versions = parse_soup_for_versions(soup)
name = 'gke' if channel == 'nochannel' else 'gke-{}'.format(channel)
name = 'gke' if channel == 'nochannel' else f'gke-{channel}'
endoflife.write_releases(name, versions)
print("::endgroup::")