fix: Use correct generator name

This commit is contained in:
oSumAtrIX 2023-11-04 18:18:48 +01:00
parent 62280eab91
commit fd019ea164
No known key found for this signature in database
GPG Key ID: A9B3094ACDB604B4

View File

@ -1,244 +1,244 @@
from os.path import join from os.path import join
from app import api from app import api
from app.utils import get_repository_name, to_json, write_json, read_json, create_if_not_exists from app.utils import get_repository_name, to_json, write_json, read_json, create_if_not_exists
from abc import ABC, abstractmethod from abc import ABC, abstractmethod
from dependency_injector.wiring import Provide, inject from dependency_injector.wiring import Provide, inject
from app.dependencies import ApiContainer from app.dependencies import ApiContainer
class Generator(ABC): class Generator(ABC):
_api: api.Api | None _api: api.Api | None
def __init__(self, name: str, api: api.Api | None = None): def __init__(self, name: str, api: api.Api | None = None):
""" """
Args: Args:
name (str): The name of the generator name (str): The name of the generator
api (Api | None): An optional api to use for the generator. api (Api | None): An optional api to use for the generator.
""" """
self.name = name self.name = name
self._api = api self._api = api
@abstractmethod @abstractmethod
async def generate(self, config, path): async def generate(self, config, path):
""" """
Generates static files based on the supplied config to the specified path. Generates static files based on the supplied config to the specified path.
Args: Args:
config (dict): The configuration for the generator config (dict): The configuration for the generator
path (str): The path to generate the static files to path (str): The path to generate the static files to
""" """
raise NotImplementedError raise NotImplementedError
class ReleasesGenerator(Generator): class ReleasesGenerator(Generator):
""" """
Generates a release file for each repository in the config: Generates a release file for each repository in the config:
- releases/<repository>/<tag>.json: Release information of the repository. - releases/<repository>/<tag>.json: Release information of the repository.
- releases/<repository>/latest.json: Latest release information of the repository. - releases/<repository>/latest.json: Latest release information of the repository.
- releases/<repository>.json: Index file containing all releases of the repository. - releases/<repository>.json: Index file containing all releases of the repository.
""" """
_api: api.Api _api: api.Api
@inject @inject
def __init__(self, api: api.Api = Provide[ApiContainer.api]): def __init__(self, api: api.Api = Provide[ApiContainer.api]):
super().__init__("releases", api) super().__init__("releases", api)
async def generate(self, config, path): async def generate(self, config, path):
path = join(path, "releases") path = join(path, "releases")
repositories = config["repositories"] repositories = config["repositories"]
for repository in repositories: for repository in repositories:
releases = await self._api.get_releases(repository) releases = await self._api.get_releases(repository)
repository_name = get_repository_name(repository) repository_name = get_repository_name(repository)
index_path = join(path, f"{repository_name}.json") index_path = join(path, f"{repository_name}.json")
index = read_json(index_path, []) index = read_json(index_path, [])
for i, release in enumerate(releases): for i, release in enumerate(releases):
tag = release["tag"] tag = release["tag"]
release_path = join(path, repository_name) release_path = join(path, repository_name)
release_json = to_json(releases) release_json = to_json(releases)
# Create the release directory if it doesn't exist and write the release file # Create the release directory if it doesn't exist and write the release file
create_if_not_exists(release_path) create_if_not_exists(release_path)
write_json(release_json, join( write_json(release_json, join(
release_path, f"{tag}.json"), overwrite=False) release_path, f"{tag}.json"), overwrite=False)
if i == 0: if i == 0:
# Overwrite the latest release file # Overwrite the latest release file
write_json(release_json, join(release_path, "latest.json")) write_json(release_json, join(release_path, "latest.json"))
if tag not in index: if tag not in index:
# Add the current tag to the index # Add the current tag to the index
index.append(tag) index.append(tag)
# Overwrite the index file with the new releases # Overwrite the index file with the new releases
write_json(index, index_path) write_json(index, index_path)
class ContributorsGenerator(Generator): class ContributorsGenerator(Generator):
""" """
Generates a contributor file for each repository in the config: Generates a contributor file for each repository in the config:
- contributors/<repository>.json: Contributors of the repository. - contributors/<repository>.json: Contributors of the repository.
""" """
_api: api.Api _api: api.Api
@inject @inject
def __init__(self, api: api.Api = Provide[ApiContainer.api]): def __init__(self, api: api.Api = Provide[ApiContainer.api]):
super().__init__("contributors", api) super().__init__("contributors", api)
async def generate(self, config, path): async def generate(self, config, path):
path = join(path, "contributors") path = join(path, "contributors")
create_if_not_exists(path) create_if_not_exists(path)
repositories = config["repositories"] repositories = config["repositories"]
for repository in repositories: for repository in repositories:
repository_name = get_repository_name(repository) repository_name = get_repository_name(repository)
contributors = await self._api.get_contributors(repository) contributors = await self._api.get_contributors(repository)
contributors_path = join(path, f"{repository_name}.json") contributors_path = join(path, f"{repository_name}.json")
write_json(contributors, contributors_path) write_json(contributors, contributors_path)
class ConnectionsGenerator(Generator): class ConnectionsGenerator(Generator):
""" """
Generates a file containing the connections of the organization: Generates a file containing the connections of the organization:
- connections.json: Connections of the organization. - connections.json: Connections of the organization.
""" """
def __init__(self): def __init__(self):
super().__init__("connections", None) super().__init__("connections", None)
async def generate(self, config, path): async def generate(self, config, path):
new_connections = config["connections"] new_connections = config["connections"]
connections_path = join(path, f"connections.json") connections_path = join(path, f"connections.json")
write_json(new_connections, connections_path) write_json(new_connections, connections_path)
class TeamGenerator(Generator): class TeamGenerator(Generator):
""" """
Generates a team file containing the members of the organization: Generates a team file containing the members of the organization:
- team.json: Members of the organization. - team.json: Members of the organization.
""" """
_api: api.Api _api: api.Api
@inject @inject
def __init__(self, api: api.Api = Provide[ApiContainer.api]): def __init__(self, api: api.Api = Provide[ApiContainer.api]):
super().__init__("team", api) super().__init__("team", api)
async def generate(self, config, path): async def generate(self, config, path):
organization = config["organization"] organization = config["organization"]
team = await self._api.get_members(organization) team = await self._api.get_members(organization)
team_path = join(path, f"team.json") team_path = join(path, f"team.json")
write_json(team, team_path) write_json(team, team_path)
class DonationsGenerator(Generator): class DonationsGenerator(Generator):
""" """
Generates a donation file containing ways to donate to the organization: Generates a donation file containing ways to donate to the organization:
- donations.json: Links and wallets to donate to the organization. - donations.json: Links and wallets to donate to the organization.
""" """
def __init__(self): def __init__(self):
super().__init__("donations") super().__init__("donations")
async def generate(self, config, path): async def generate(self, config, path):
links = config["links"] if "links" in config else [] links = config["links"] if "links" in config else []
wallets = config["wallets"] if "wallets" in config else [] wallets = config["wallets"] if "wallets" in config else []
donation_path = join(path, f"donations.json") donation_path = join(path, f"donations.json")
write_json( write_json(
{ {
"links": links, "links": links,
"wallets": wallets "wallets": wallets
}, },
donation_path donation_path
) )
class AnnouncementGenerator(Generator): class AnnouncementGenerator(Generator):
""" """
Generates announcement files: Generates announcement files:
- /announcements.json: Get a list of announcements from all channels. - /announcements.json: Get a list of announcements from all channels.
- /announcements/<channel>.json: Get a list of announcement from a channel. - /announcements/<channel>.json: Get a list of announcement from a channel.
- /announcements/latest.json: Get the latest announcement. - /announcements/latest.json: Get the latest announcement.
- /announcements/<channel>/latest.json: Get the latest announcement from a channel. - /announcements/<channel>/latest.json: Get the latest announcement from a channel.
""" """
def __init__(self): def __init__(self):
super().__init__("announcements") super().__init__("announcement")
async def generate(self, config, path): async def generate(self, config, path):
new_announcement = config["announcement"] new_announcement = config["announcement"]
new_announcement_channel = new_announcement["channel"] new_announcement_channel = new_announcement["channel"]
announcements_path = join(path, f"announcements.json") announcements_path = join(path, f"announcements.json")
announcements = read_json(announcements_path, []) announcements = read_json(announcements_path, [])
# Determine the id of the new announcement. The id is the highest id + 1 # Determine the id of the new announcement. The id is the highest id + 1
new_announcement_id = 0 new_announcement_id = 0
for announcement in announcements: for announcement in announcements:
if announcement["id"] >= new_announcement_id: if announcement["id"] >= new_announcement_id:
new_announcement_id = announcement["id"] + 1 new_announcement_id = announcement["id"] + 1
new_announcement["id"] = new_announcement_id new_announcement["id"] = new_announcement_id
# Add the new announcement to the list of announcements # Add the new announcement to the list of announcements
announcements.append(new_announcement) announcements.append(new_announcement)
write_json(announcements, announcements_path) write_json(announcements, announcements_path)
# Add the new announcement to the channel file # Add the new announcement to the channel file
channel_path = join( channel_path = join(
path, f"announcements/{new_announcement_channel}.json") path, f"announcements/{new_announcement_channel}.json")
create_if_not_exists(channel_path) create_if_not_exists(channel_path)
channel_announcements = read_json(channel_path, []) channel_announcements = read_json(channel_path, [])
channel_announcements.append(new_announcement) channel_announcements.append(new_announcement)
write_json(channel_announcements, channel_path) write_json(channel_announcements, channel_path)
# Update the latest announcement file # Update the latest announcement file
write_json(new_announcement, join(path, "announcements/latest.json")) write_json(new_announcement, join(path, "announcements/latest.json"))
# Update the latest announcement for the channel # Update the latest announcement for the channel
write_json(new_announcement, join( write_json(new_announcement, join(
path, f"announcements/{new_announcement_channel}/latest.json")) path, f"announcements/{new_announcement_channel}/latest.json"))
class RemoveAnnouncementGenerator(Generator): class RemoveAnnouncementGenerator(Generator):
""" """
Removes an announcement. Removes an announcement.
""" """
def __init__(self): def __init__(self):
super().__init__("remove_announcement") super().__init__("remove_announcement")
async def generate(self, config, path): async def generate(self, config, path):
# TODO: Implement this # TODO: Implement this
pass pass
# This function is needed. A variable in the global scope cannot be used, because the dependency injector is not initialized yet. # This function is needed. A variable in the global scope cannot be used, because the dependency injector is not initialized yet.
def get_generators(): def get_generators():
return { return {
generator.name: generator for generator in [ generator.name: generator for generator in [
ReleasesGenerator(), ReleasesGenerator(),
ContributorsGenerator(), ContributorsGenerator(),
TeamGenerator(), TeamGenerator(),
ConnectionsGenerator(), ConnectionsGenerator(),
DonationsGenerator(), DonationsGenerator(),
AnnouncementGenerator(), AnnouncementGenerator(),
RemoveAnnouncementGenerator() RemoveAnnouncementGenerator()
] ]
} }