-
Notifications
You must be signed in to change notification settings - Fork 0
/
to_markdown.py
executable file
·86 lines (76 loc) · 2.65 KB
/
to_markdown.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/env python3
"""
Create markdown output from a CSV instance list
"""
from collections import defaultdict
import csv
from datetime import date
import sys
repo_url = "https://github.com/chrisacheson/instance_list_management"
md_indent = " " * 4
def make_instance_text(instance):
hostname = instance["hostname"]
url = f"https://{hostname}/"
description = ""
if instance["description"] != "":
description = f" - {instance['description']}"
return f"[{hostname}]({url}){description}"
instances_file = sys.argv[1]
topic_instances = list()
location_instances = defaultdict(lambda: defaultdict(list))
with open(instances_file, newline="") as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
country = row["country"]
if country == "":
topic_instances.append(row)
else:
region = row["region"]
location_instances[country][region].append(row)
countries = list(location_instances.keys())
countries.sort()
print(f"Updated {date.today().isoformat()}")
print()
print(f"A CSV version of this list is available at [{repo_url}]({repo_url})")
print()
print("Topic/audience focused:")
print()
for instance in topic_instances:
print(f"- {make_instance_text(instance)}")
print()
print("Location/language focused:")
print()
print("(For some of these it's a little unclear to me whether they should be "
"associated with a country or a language. Please let me know if "
"anything needs to be corrected.)")
print()
for country in countries:
country_regions = location_instances[country]
if (
len(country_regions) == 1 and
"" in country_regions and
len(country_regions[""]) == 1
):
instance = country_regions[""][0]
print(f"- {country} - {make_instance_text(instance)}")
else:
print(f"- {country}")
regions = list(country_regions.keys())
regions.sort()
for region in regions:
region_instances = country_regions[region]
if len(region_instances) == 1:
region_text = ""
if region != "":
region_text = f"{region} - "
instance = region_instances[0]
instance_text = make_instance_text(instance)
print(f"{md_indent}- {region_text}{instance_text}")
else:
if region != "":
print(f"{md_indent}- {region}")
for instance in region_instances:
indent = md_indent
if region != "":
indent += md_indent
print(f"{indent}- {make_instance_text(instance)}")