Skip to content

Commit

Permalink
Added ID insertion script
Browse files Browse the repository at this point in the history
  • Loading branch information
HendrikHuebner committed Jan 19, 2024
1 parent 6445bf8 commit 5a1cd42
Showing 1 changed file with 44 additions and 30 deletions.
74 changes: 44 additions & 30 deletions .github/workflows/id_fixup.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import os

import click
import ruamel.yaml
from ruamel.yaml import CommentedMap
Expand All @@ -23,55 +25,67 @@ def collect_existingIDs(cards):
return found_ids


def get_yaml_files(folder_path: str):
yaml_files = []
for root, dirs, files in os.walk(folder_path):
for file in files:
if file.lower().endswith(".yaml") or file.lower().endswith(".yml"):
yaml_files.append(os.path.join(root, file))

return yaml_files


@click.command()
@click.argument("file_path", type=click.Path(exists=True))
def insert_ids(file_path, verbose=True):
@click.argument("folder_path", type=click.Path(exists=True))
def insert_ids(folder_path, verbose=True):
"""
Inserts missing IDs into yaml file. If duplicate IDs exist, returns exit code 1
"""
yaml = ruamel.yaml.YAML()
yaml_paths = get_yaml_files(folder_path)

with open(file_path, "r") as file:
try:
data = yaml.load(file)
for file_path in yaml_paths:
with open(file_path, "r") as file:
try:
data = yaml.load(file)

if "cards" in data and isinstance(data["cards"], list):
try:
existingIDs: set[int] = collect_existingIDs(data["cards"])
if "cards" in data and isinstance(data["cards"], list):
try:
existingIDs: set[int] = collect_existingIDs(data["cards"])

except DuplicateIDError as e:
click.echo(f"Duplicate IDs {e.id}! Please fix manually")
exit(1)
except DuplicateIDError as e:
click.echo(f"Duplicate IDs {e.id}! Please fix manually")
exit(1)

index = 0
index = 0

if verbose:
click.echo(f"Found existing IDs: {existingIDs}")
if verbose:
click.echo(f"Found existing IDs: {existingIDs}")

for card in data["cards"]:
if isinstance(card, CommentedMap):
for card in data["cards"]:
if isinstance(card, CommentedMap):

if "id" in card:
continue
if "id" in card:
continue

# skip existing IDs
while index in existingIDs:
index += 1
# skip existing IDs
while index in existingIDs:
index += 1

existingIDs.add(index)
existingIDs.add(index)

card.insert(1, "id", index, "(generated)")
card.insert(1, "id", index, "(generated)")

with open(file_path, "w") as output_file:
yaml.dump(data, output_file)
with open(file_path, "w") as output_file:
yaml.dump(data, output_file)

click.echo(f"Inserted \"id\" attributes into each object in \"cards\" list in {file_path}")
click.echo(f"Inserted \"id\" attributes into each object in \"cards\" list in {file_path}")

else:
click.echo("Error: \"cards\" key not found or is not a list in the YAML file.", err=True)
else:
click.echo("Error: \"cards\" key not found or is not a list in the YAML file.", err=True)

except ruamel.yaml.YAMLError as e:
click.echo(f"Error parsing YAML in file {file_path}: {e}", err=True)
except ruamel.yaml.YAMLError as e:
click.echo(f"Error parsing YAML in file {file_path}: {e}", err=True)


if __name__ == "__main__":
Expand Down

0 comments on commit 5a1cd42

Please sign in to comment.