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

Tree @master (Download .tar.gz)

main.py @masterraw · history · blame

#!/usr/bin/env python3

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

"""
Main script that coordinates the readme building process
"""
from argparse import ArgumentParser
import os
import subprocess
import re

from collector import Collector
from formatters import format_see_also_link
from readme_writer import write_readme_file, dump_rating_page, update_main_readme


def run(cmd):
    print(cmd)
    subprocess.run(cmd, shell=True)


def main(args):
    argparser = ArgumentParser()

    argparser.add_argument('base_dir', metavar='DIRNAME', type=str,
        help='Directory in which the topic subdirectories reside'
    )

    options = argparser.parse_args(args)

    webpages = {}
    repos = {}
    books = {}
    papers = {}

    c = Collector(options.base_dir, webpages, repos, books, papers)

    topic_sections = {}
    seen_dirs = set()
    for section in sorted(c.topics["sections"], key=lambda s: s["title"]):
        # print(json.dumps(section, indent=4))
        topic = section["title"]
        used_in = section["properties"]["used-in"]
        if "bookmarks" not in used_in:
            continue
        c.load_commentary(topic)
        c.load_topic(topic)
        topic_sections[topic] = section

    c.check_entry_topics()
    c.process_secondary_topics()

    assert c.topic_dirs == c.seen_dirs, "dirs that are not topics: {}".format(c.topic_dirs - seen_dirs)

    for topic in sorted(webpages.keys()):
        print("Writing out '{}'/README.md...".format(topic))
        primary, secondary = c.get_entries_for_topic(topic)
        write_readme_file(c.base_dir, topic, topic_sections[topic], primary, secondary)

    rating_counts = {}
    for (rating, rating_name) in [
        ("TODO", "Unrated"),
        ("2", "Very Interesting"),
        ("3", "Top-rated"),
        ("classic", "Classic"),
    ]:
        rating_counts[rating] = dump_rating_page(c, rating, rating_name, webpages, repos, books, papers)

    totals1 = "Currently it consists of **{}** web pages, **{}** repositories, **{}** papers, and **{}** books in **{}** topics.".format(
        c.counts["webpages"], c.counts["repos"], c.counts["papers"], c.counts["books"], len(c.topic_dirs)
    )
    totals2 = """
Of these, [**{}** have the highest rating](by-rating/Top-rated.md),
[**{}** are considered classics](by-rating/Classic.md),
[**{}** are considered very interesting](by-rating/Very%20Interesting.md),
while [**{}** are yet to be rated](by-rating/Unrated.md).""".format(
    rating_counts["3"],
    rating_counts["classic"],
    rating_counts["2"],
    rating_counts["TODO"],
)

    totals = totals1 + totals2
    print(totals1)

    topics = []
    for topic in sorted(webpages.keys()):
        topics.append("*   {}".format(format_see_also_link(topic, parent="by-topic")))
    topics = '\n'.join(topics)

    with open(os.path.join(c.base_dir, "README.md"), "r") as f:
        readme = f.read()

    pattern = r"\<\!-- TOTALS --\>.*?\<\!-- \/TOTALS --\>"
    repl = "<!-- TOTALS -->\n\n{}\n\n<!-- /TOTALS -->".format(totals)
    readme = re.sub(pattern, repl, readme, count=1, flags=re.DOTALL)

    pattern = r"\<\!-- TOPICS --\>.*?\<\!-- \/TOPICS --\>"
    repl = "<!-- TOPICS -->\n\n{}\n\n<!-- /TOPICS -->".format(topics)
    readme = re.sub(pattern, repl, readme, count=1, flags=re.DOTALL)

    with open(os.path.join(c.base_dir, "README.md"), "w") as f:
        f.write(readme)


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