hosts/updateReadme.py

121 lines
4.1 KiB
Python
Raw Permalink Normal View History

2022-07-05 18:39:02 +02:00
#!/usr/bin/env python3
2016-03-23 05:35:26 +01:00
# Script by Steven Black
# https://github.com/StevenBlack
#
# This Python script will update the readme files in this repo.
2018-08-10 15:52:20 +02:00
import json
2016-03-23 05:35:26 +01:00
import os
import time
2018-08-10 15:52:20 +02:00
from string import Template
2016-03-23 05:35:26 +01:00
# Project Settings
BASEDIR_PATH = os.path.dirname(os.path.realpath(__file__))
README_TEMPLATE = os.path.join(BASEDIR_PATH, "readme_template.md")
README_FILENAME = "readme.md"
2016-03-23 05:35:26 +01:00
README_DATA_FILENAME = "readmeData.json"
def main():
s = Template(
"${description} | [Readme](https://github.com/StevenBlack/"
"hosts/blob/master/${location}readme.md) | "
"[link](https://raw.githubusercontent.com/StevenBlack/"
"hosts/master/${location}hosts) | "
"${fmtentries} | "
"[link](http://sbc.io/hosts/${location}hosts)"
)
2020-05-27 15:40:55 +02:00
with open(README_DATA_FILENAME, "r", encoding="utf-8", newline="\n") as f:
data = json.load(f)
2016-03-23 05:35:26 +01:00
keys = list(data.keys())
# Sort by the number of en-dashes in the key
# and then by the key string itself.
keys.sort(key=lambda item: (item.replace("-only", "").count("-"), item.replace("-only", "")))
toc_rows = ""
2016-03-24 06:08:48 +01:00
for key in keys:
data[key]["fmtentries"] = "{:,}".format(data[key]["entries"])
2016-03-24 06:08:48 +01:00
if key == "base":
data[key]["description"] = "Unified hosts = **(adware + malware)**"
2016-03-24 06:08:48 +01:00
else:
if data[key]["no_unified_hosts"]:
data[key]["description"] = (
"**" + key.replace("-only", "").replace("-", " + ") + "**"
)
else:
data[key]["description"] = (
"Unified hosts **+ " + key.replace("-", " + ") + "**"
)
2016-03-24 06:08:48 +01:00
if "\\" in data[key]["location"]:
data[key]["location"] = data[key]["location"].replace("\\", "/")
toc_rows += s.substitute(data[key]) + "\n"
2016-03-24 06:08:48 +01:00
row_defaults = {
"name": "",
"homeurl": "",
"url": "",
"license": "",
"issues": "",
"description": "",
}
t = Template(
"${name} |[link](${homeurl})"
" | [raw](${url}) | ${license} | [issues](${issues})| ${description}"
)
size_history_graph = "![Size history](https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts_file_size_history.png)"
2016-03-23 05:35:26 +01:00
for key in keys:
extensions = key.replace("-only", "").replace("-", ", ")
extensions_str = "* Extensions: **" + extensions + "**."
if data[key]["no_unified_hosts"]:
extensions_header = "Limited to the extensions: " + extensions
else:
extensions_header = "Unified hosts file with " + extensions + " extensions"
source_rows = ""
source_list = data[key]["sourcesdata"]
for source in source_list:
this_row = {}
this_row.update(row_defaults)
this_row.update(source)
source_rows += t.substitute(this_row) + "\n"
with open(
2020-05-27 21:59:34 +02:00
os.path.join(data[key]["location"], README_FILENAME),
"wt",
encoding="utf-8",
newline="\n",
) as out:
2020-05-27 15:40:55 +02:00
for line in open(README_TEMPLATE, encoding="utf-8", newline="\n"):
line = line.replace(
"@GEN_DATE@", time.strftime("%B %d %Y", time.gmtime())
)
line = line.replace("@EXTENSIONS@", extensions_str)
line = line.replace("@EXTENSIONS_HEADER@", extensions_header)
line = line.replace(
"@NUM_ENTRIES@", "{:,}".format(data[key]["entries"])
)
line = line.replace(
"@SUBFOLDER@", os.path.join(data[key]["location"], "")
)
line = line.replace("@TOCROWS@", toc_rows)
line = line.replace("@SOURCEROWS@", source_rows)
# insert the size graph on the home readme only, for now.
if key == "base":
line = line.replace(
"@SIZEHISTORY@", size_history_graph
)
else:
line = line.replace("@SIZEHISTORY@", "")
out.write(line)
2016-03-23 05:35:26 +01:00
2016-03-23 05:35:26 +01:00
if __name__ == "__main__":
main()