summaryrefslogtreecommitdiff
path: root/cli
diff options
context:
space:
mode:
Diffstat (limited to 'cli')
-rw-r--r--cli/__init__.py3
-rw-r--r--cli/base.py6
-rw-r--r--cli/build.py81
-rw-r--r--cli/clean.py6
4 files changed, 96 insertions, 0 deletions
diff --git a/cli/__init__.py b/cli/__init__.py
new file mode 100644
index 0000000..33e2b10
--- /dev/null
+++ b/cli/__init__.py
@@ -0,0 +1,3 @@
+from .base import cli
+from .clean import clean
+from .build import build
diff --git a/cli/base.py b/cli/base.py
new file mode 100644
index 0000000..2098d8d
--- /dev/null
+++ b/cli/base.py
@@ -0,0 +1,6 @@
+import click
+
+
+@click.group
+def cli():
+ pass
diff --git a/cli/build.py b/cli/build.py
new file mode 100644
index 0000000..38b6724
--- /dev/null
+++ b/cli/build.py
@@ -0,0 +1,81 @@
+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
+
+ROOT_PATH = Path(__file__).parents[1]
+
+NAMES_DIR = ROOT_PATH / "names"
+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"]
+
+
+@cli.command()
+def build():
+ # copy static files
+ if not BUILD_DIR.exists():
+ mkdir(BUILD_DIR)
+ for f in listdir(STATIC_DIR):
+ copyfile(STATIC_DIR / f, BUILD_DIR / f)
+
+ # process all name yamls
+ 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)
+
+ 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(".", "")
+ res = []
+ for part in name:
+ if part == domain:
+ url = value.get("url") or "https://" + value.get("domain")
+ res.append(f'<a href="{url}" class="{classes}">{value["domain"]}</a>')
+ else:
+ res.append(part)
+ return " ".join(res)
+
+ # render templates
+ env = Environment(
+ loader=PackageLoader("manage"),
+ autoescape=select_autoescape(),
+ )
+ env.filters["render_link"] = render_link
+ for template in TEMPLATES:
+ t = env.get_template(template)
+ index = t.render(names=names, candidates=candidates)
+ with open(BUILD_DIR / "index.html", "w") as f:
+ f.write(index)
diff --git a/cli/clean.py b/cli/clean.py
new file mode 100644
index 0000000..09eaa41
--- /dev/null
+++ b/cli/clean.py
@@ -0,0 +1,6 @@
+from .base import cli
+
+
+@cli.command()
+def clean():
+ print("Cleaning..")