forked from ari.lt/arivertisements
91 lines
2.5 KiB
Python
91 lines
2.5 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
"""Generate index.json"""
|
|
|
|
import base64
|
|
import json
|
|
import os
|
|
import typing as t
|
|
from warnings import filterwarnings as filter_warnings
|
|
|
|
|
|
def parse_meta(fp) -> t.Dict[str, str]:
|
|
"""Parse metadata"""
|
|
|
|
final: t.Dict[str, str] = {}
|
|
|
|
stated: bool = False
|
|
|
|
for line in fp:
|
|
key, value = line.strip().split(":", 1)
|
|
|
|
if not key or not value:
|
|
raise ValueError(
|
|
f"Failed to parse {fp.name!r} metadata! Missing key or value"
|
|
)
|
|
|
|
key = key.lower()
|
|
|
|
if key in final:
|
|
raise ValueError(f"{key} already defined in metadata {fp.name!r}!")
|
|
|
|
if key == "statement":
|
|
stated = True
|
|
continue
|
|
|
|
final[key] = value.strip()
|
|
|
|
if not stated:
|
|
raise ValueError(f"License statement missing in metadata {fp.name!r}!")
|
|
|
|
for rkey in ("filename", "alt", "author", "contact"):
|
|
if rkey not in final:
|
|
raise ValueError(f"{rkey} missing in metadata {fp.name!r}!")
|
|
|
|
final["license"] = "CC-BY-NC-SA 4.0"
|
|
|
|
if not os.path.isfile(os.path.join("img", final["filename"])):
|
|
raise ValueError(f"{final['filename']!r} is missing in metadata {fp.name!r}!")
|
|
|
|
return final
|
|
|
|
|
|
def main() -> int:
|
|
"""entry / main function"""
|
|
|
|
index: t.List[t.Dict[str, str]] = []
|
|
contributors: t.List[str] = []
|
|
|
|
for metaf in os.listdir("meta"):
|
|
with open(os.path.join("meta", metaf), "r", encoding="utf-8") as fp:
|
|
meta: t.Dict[str, str] = parse_meta(fp)
|
|
contributor: str = f"* {meta['author']} <{meta['contact']}>"
|
|
if contributor not in contributors:
|
|
contributors.append(contributor)
|
|
meta["contact"] = base64.b85encode(meta["contact"].encode("ascii")).decode(
|
|
"ascii"
|
|
)
|
|
index.append(meta)
|
|
|
|
index.sort(key=lambda meta: meta.get("filename", "").lower())
|
|
contributors.sort()
|
|
|
|
with open("index.json", "w") as ifp:
|
|
json.dump(index, ifp, indent=1)
|
|
|
|
print("Created index.json")
|
|
|
|
with open("CONTRIBUTORS", "w") as fp:
|
|
fp.write("Thank you for contributing to Arivertisements. Here is a complete list of contributors with semi-obfuscated e-mail addresses:\n\n")
|
|
fp.write("\n".join(contributors))
|
|
|
|
print("Created CONTRIBUTORS")
|
|
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
assert main.__annotations__.get("return") is int, "main() should return an integer"
|
|
|
|
filter_warnings("error", category=Warning)
|
|
raise SystemExit(main())
|