Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
StevenBlack
GitHub Repository: StevenBlack/hosts
Path: blob/master/updateReadme.py
1181 views
1
#!/usr/bin/env python3
2
3
# Script by Steven Black
4
# https://github.com/StevenBlack
5
#
6
# This Python script will update the readme files in this repo.
7
8
import json
9
import os
10
import time
11
from string import Template
12
13
# Project Settings
14
BASEDIR_PATH = os.path.dirname(os.path.realpath(__file__))
15
README_TEMPLATE = os.path.join(BASEDIR_PATH, "readme_template.md")
16
README_FILENAME = "readme.md"
17
README_DATA_FILENAME = "readmeData.json"
18
19
20
def main():
21
s = Template(
22
"${description} | [Readme](https://github.com/StevenBlack/"
23
"hosts/blob/master/${location}readme.md) | "
24
"[link](https://raw.githubusercontent.com/StevenBlack/"
25
"hosts/master/${location}hosts) | "
26
"${fmtentries} | "
27
"[link](http://sbc.io/hosts/${location}hosts)"
28
)
29
with open(README_DATA_FILENAME, "r", encoding="utf-8", newline="\n") as f:
30
data = json.load(f)
31
32
keys = list(data.keys())
33
# Sort by the number of en-dashes in the key
34
# and then by the key string itself.
35
keys.sort(key=lambda item: (item.replace("-only", "").count("-"), item.replace("-only", "")))
36
37
toc_rows = ""
38
for key in keys:
39
data[key]["fmtentries"] = "{:,}".format(data[key]["entries"])
40
if key == "base":
41
data[key]["description"] = "Unified hosts = **(adware + malware)**"
42
else:
43
if data[key]["nounifiedhosts"]:
44
data[key]["description"] = (
45
"**" + key.replace("-only", "").replace("-", " + ") + "**"
46
)
47
else:
48
data[key]["description"] = (
49
"Unified hosts **+ " + key.replace("-", " + ") + "**"
50
)
51
52
if "\\" in data[key]["location"]:
53
data[key]["location"] = data[key]["location"].replace("\\", "/")
54
55
toc_rows += s.substitute(data[key]) + "\n"
56
57
row_defaults = {
58
"name": "",
59
"homeurl": "",
60
"url": "",
61
"license": "",
62
"issues": "",
63
"description": "",
64
}
65
66
t = Template(
67
"${name} |[link](${homeurl})"
68
" | [raw](${url}) | ${license} | [issues](${issues})| ${description}"
69
)
70
size_history_graph = "![Size history](https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts_file_size_history.png)"
71
for key in keys:
72
extensions = key.replace("-only", "").replace("-", ", ")
73
extensions_str = "* Extensions: **" + extensions + "**."
74
if data[key]["nounifiedhosts"]:
75
extensions_header = "Limited to the extensions: " + extensions
76
else:
77
extensions_header = "Unified hosts file with " + extensions + " extensions"
78
79
source_rows = ""
80
source_list = data[key]["sourcesdata"]
81
82
for source in source_list:
83
this_row = {}
84
this_row.update(row_defaults)
85
this_row.update(source)
86
source_rows += t.substitute(this_row) + "\n"
87
88
with open(
89
os.path.join(data[key]["location"], README_FILENAME),
90
"wt",
91
encoding="utf-8",
92
newline="\n",
93
) as out:
94
for line in open(README_TEMPLATE, encoding="utf-8", newline="\n"):
95
line = line.replace(
96
"@GEN_DATE@", time.strftime("%B %d %Y", time.gmtime())
97
)
98
line = line.replace("@EXTENSIONS@", extensions_str)
99
line = line.replace("@EXTENSIONS_HEADER@", extensions_header)
100
line = line.replace(
101
"@NUM_ENTRIES@", "{:,}".format(data[key]["entries"])
102
)
103
line = line.replace(
104
"@SUBFOLDER@", os.path.join(data[key]["location"], "")
105
)
106
line = line.replace("@TOCROWS@", toc_rows)
107
line = line.replace("@SOURCEROWS@", source_rows)
108
# insert the size graph on the home readme only, for now.
109
if key == "base":
110
line = line.replace(
111
"@SIZEHISTORY@", size_history_graph
112
)
113
else:
114
line = line.replace(
115
"@SIZEHISTORY@", "![Size history](stats.png)")
116
117
out.write(line)
118
119
120
if __name__ == "__main__":
121
main()
122
123