-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate-langs.py
More file actions
106 lines (82 loc) · 2.71 KB
/
update-langs.py
File metadata and controls
106 lines (82 loc) · 2.71 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import ssl
import os
import os
from urllib.request import urlretrieve
def ensurePackage(importName:str, packageName:str):
try:
__import__(importName)
except ModuleNotFoundError:
os.system("pip install "+packageName)
ensurePackage("yaml", "pyyaml")
import yaml
import json
LANGS_ALL="allLangs.yml"
LANGS_USED="usedLangs.json"
def getLangsYamlFile():
tmp = ssl._create_default_https_context
ssl._create_default_https_context = ssl._create_unverified_context
url = "https://raw.githubusercontent.com/github-linguist/linguist/master/lib/linguist/languages.yml"
urlretrieve(url, LANGS_ALL)
ssl._create_default_https_context = tmp
def getLangsUsedJsonFile():
tmp = ssl._create_default_https_context
ssl._create_default_https_context = ssl._create_unverified_context
url = "https://api.github.com/repos/Vlad-Zumer/AdventOfCode/languages"
urlretrieve(url, LANGS_USED)
ssl._create_default_https_context = tmp
def removeLangsFiles():
try:
os.remove(LANGS_ALL)
except:
pass
try:
os.remove(LANGS_USED)
except:
pass
removeLangsFiles()
getLangsYamlFile()
getLangsUsedJsonFile()
usedLangs = set()
allLangs = []
with open(LANGS_USED) as fstream:
usedLangsObj:dict = json.load(fstream)
for langName in usedLangsObj:
usedLangs.add(langName)
with open(LANGS_ALL, "r") as fstream:
langs:dict = yaml.safe_load(fstream)
langName:str
langData:dict
for langName, langData in langs.items():
if langData.get("type", None) == "programming" and langData.get("group", None) == None:
allLangs.append(langName)
allLangs.sort()
allLangsMDList:list[str] = []
usedLangsMDList:list[str] = []
for lang in allLangs:
if lang in usedLangs:
usedLangsMDList.append("<li> " + lang + "\n")
allLangsMDList.append("* [x] " + lang + "\n")
else:
allLangsMDList.append("* [ ] " + lang + "\n")
fileContent:list[str]= []
with open("README.md", "r") as fstream:
fileContent = fstream.readlines()
newFileContent:str = ""
inUsedLangsList:bool = False
indent:str = ""
for line in fileContent:
if line.strip() == "<!-- UsedLangListStart -->":
inUsedLangsList = True
indent = " " * (len(line) - len(line.lstrip()))
if line.strip() == "<!-- UsedLangListEnd -->":
inUsedLangsList = False
for usedLangLine in usedLangsMDList:
newFileContent += indent + usedLangLine
if inUsedLangsList and line.strip().startswith("<li>"):
continue
newFileContent += line
with open("README.md", "w") as fstream:
fstream.write(newFileContent)
with open("language_table.md", "w") as fstream:
fstream.write(''.join(allLangsMDList))
removeLangsFiles()