From d0ba9cc049a049a31f1bea183dd141908ce26ad8 Mon Sep 17 00:00:00 2001 From: Yuval Adam <_@yuv.al> Date: Thu, 9 May 2024 13:52:23 +0200 Subject: [PATCH] Migrate build script to use ymlstash --- cli/base.py | 4 ++++ cli/build.py | 47 +++++++++++++++-------------------------------- cli/clean.py | 12 ++++-------- 3 files changed, 23 insertions(+), 40 deletions(-) diff --git a/cli/base.py b/cli/base.py index 6250192..e76da9d 100644 --- a/cli/base.py +++ b/cli/base.py @@ -1,10 +1,14 @@ import click +import ymlstash from pathlib import Path +from .model import Name ROOT_PATH = Path(__file__).parents[1] NAMES_DIR = ROOT_PATH / "names" +STASH = ymlstash.YmlStash(Name, NAMES_DIR, filter_none=True) + @click.group def cli(): diff --git a/cli/build.py b/cli/build.py index 38b6724..526e086 100644 --- a/cli/build.py +++ b/cli/build.py @@ -1,12 +1,10 @@ -import yaml - from jinja2 import Environment, PackageLoader, select_autoescape from os import listdir, mkdir from pathlib import Path from shutil import copyfile from unidecode import unidecode -from .base import cli +from .base import cli, STASH ROOT_PATH = Path(__file__).parents[1] @@ -14,9 +12,6 @@ BUILD_DIR = ROOT_PATH / "build" STATIC_DIR = ROOT_PATH / "static" -REQUIRED_FIELDS = set(["domain", "name"]) -OPTIONAL_FIELDS = set(["url", "title", "email", "github", "candidate", "invalid"]) - TEMPLATES = ["index.html"] @@ -32,38 +27,26 @@ def build(): names = [] candidates = [] - for name in listdir(NAMES_DIR): - with open(NAMES_DIR / name, "r") as f: - fields = yaml.load(f.read(), Loader=yaml.Loader) - field_set = set(fields.keys()) - - missing_fields = REQUIRED_FIELDS - field_set - if missing_fields: - raise Exception(f"Missing required fields {missing_fields} in {name}") - - invalid_fields = field_set - OPTIONAL_FIELDS - REQUIRED_FIELDS - if invalid_fields: - raise Exception(f"Invalid fields {invalid_fields} in {name}") - - if fields.get("invalid"): - continue - - if fields.get("candidate"): - candidates.append(fields) - else: - names.append(fields) + for key in STASH.list_keys(): + name = STASH.load(key) + if name.invalid is True: + continue + if name.candidate is True: + candidates.append(name) + else: + names.append(name) - names = list(sorted(names, key=lambda x: x["domain"])) - candidates = list(sorted(candidates, key=lambda x: x["domain"])) + names = list(sorted(names, key=lambda x: x.domain)) + candidates = list(sorted(candidates, key=lambda x: x.domain)) def render_link(value, classes): - name = unidecode(value["name"]).lower().split(" ") - domain = unidecode(value["domain"]).replace(".", "") + name = unidecode(value.name).lower().split(" ") + domain = unidecode(value.domain).replace(".", "") res = [] for part in name: if part == domain: - url = value.get("url") or "https://" + value.get("domain") - res.append(f'{value["domain"]}') + url = value.url or "https://" + value.domain + res.append(f'{value.domain}') else: res.append(part) return " ".join(res) diff --git a/cli/clean.py b/cli/clean.py index b23a80e..90fa2b5 100644 --- a/cli/clean.py +++ b/cli/clean.py @@ -1,13 +1,9 @@ -import ymlstash - -from .base import cli, NAMES_DIR -from .model import Name +from .base import cli, STASH @cli.command() def clean(): - stash = ymlstash.YmlStash(Name, NAMES_DIR, filter_none=True) - names = stash.list_keys() + names = STASH.list_keys() for name in names: - obj = stash.load(name) - stash.save(obj) + obj = STASH.load(name) + STASH.save(obj)