git @ Cat's Eye Technologies The-Glosscubator / master script / extract_ratings.py
master

Tree @master (Download .tar.gz)

extract_ratings.py @masterraw · history · blame

#!/usr/bin/env python3

# SPDX-FileCopyrightText: Chris Pressey, the creator of this work, has dedicated it to the public domain.
# For more information, please refer to <https://unlicense.org/>
# SPDX-License-Identifier: Unlicense

from argparse import ArgumentParser
import json
import re
import os
import subprocess

from feedmark.loader import read_document_from


def load_feedmark_sections(topic):
    path = os.path.join(topic, "commentary", "cpressey.md")
    if os.path.isfile(path):
        return read_document_from(path).to_json_data()["sections"]
    else:
        return []


def rewrite_commentary(topic, sections):
    outdir = os.path.join(topic, "commentary")
    os.makedirs(outdir, exist_ok=True)
    outfilename = os.path.join(outdir, "cpressey.md")
    # if os.path.exists(outfilename):
    #     raise OSError("{} already exists".format(outfilename))
    with open(outfilename, 'w') as f:
        title = f"Commentary by cpressey on {topic} works"
        uline = '=' * len(title)

        f.write("""\
{}
{}

<!--
Copyright (c) 2025 Chris Pressey, Cat's Eye Technologies.

{}-License-Identifier: CC-BY-ND-4.0
-->

""".format(title, uline, "SPDX"))
        for entry in sections:
            f.write("### {}\n\n".format(entry["title"]))
            f.write(entry["body"] + "\n\n")


def write_ratings(topic, sections):
    outdir = os.path.join(topic, "ratings")
    os.makedirs(outdir, exist_ok=True)
    outfilename = os.path.join(outdir, "cpressey.md")
    # if os.path.exists(outfilename):
    #     raise OSError("{} already exists".format(outfilename))
    with open(outfilename, 'w') as f:
        title = f"Ratings by cpressey of {topic} works"
        uline = '=' * len(title)

        f.write("""\
{}
{}

<!--
{}-FileCopyrightText: Chris Pressey, the creator of this work, has dedicated it to the public domain.

{}-License-Identifier: CC0-1.0
-->

""".format(title, uline, "SPDX", "SPDX"))
        for entry in sections:
            f.write("### {}\n\n".format(entry["title"]))
            f.write("*   rating: {}\n\n".format(entry["properties"].get("rating", "TODO")))
            if "useful" in entry["properties"]:
                f.write("*   useful: {}\n\n".format(entry["properties"]["useful"]))
            f.write(".\n\n")


def main(args):
    topic = args[0]

    sections = load_feedmark_sections(topic)

    write_ratings(topic, sections)
    rewrite_commentary(topic, sections)

    # subprocess.run("sed -i '/^\*   rating/d' \"{}\"/src/*.md".format(topic), shell=True)

# sed -i '/^\*   rating/d' "$TOPIC"/src/*.md
# grep -r 'rating:' | grep -v commentary | sort

if __name__ == "__main__":
    import sys
    main(sys.argv[1:])