-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlecture_glossary.py
executable file
·125 lines (91 loc) · 4.21 KB
/
lecture_glossary.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/usr/bin/env python3
"""
Tool for creating a glossary for a lecture.
What it should be doing (not yet implemented like this):
Collect all trefis/defis/trefis that are displayed in the document (i.e. following inputrefs etc.)
Use them as keys in the glossary and find their definitions (somehow)
"""
import os
import lmh_harvest as harvest
import import_graph as graph
from make_glossary import *
import argparse
parser = argparse.ArgumentParser(description="Script for generating a glossary for a lecture")
parser.add_argument("INFILE", help=".tex file for which the glossary shall be generated (typically path/to/notes.tex)")
parser.add_argument("LANGUAGE", help="language of the glossary (e.g. 'en' or 'de')")
parser.add_argument("OUTFILE", help="output file")
parser.add_argument("-b", "--big", action="store_true", help="Cover the recursive closure")
args = parser.parse_args()
BIG = args.big
mathhub_dir = harvest.get_mathhub_dir(args.INFILE)
root_repo, root_doc = harvest.split_path_repo_doc(args.INFILE)
ONLY_COVERED_PART = True
print("Mathhub: " + mathhub_dir)
print("root repo: " + root_repo)
print("root doc: " + root_doc)
def getdefisandtrefis():
displayedgraph = graph.Graph()
displayedfiles = graph.add_omgroup_data(mathhub_dir, root_repo, root_doc, displayedgraph, ONLY_COVERED_PART)
displayedfiles += displayedgraph.module_nodes
logger = harvest.SimpleLogger(2)
ctx = harvest.HarvestContext(logger, harvest.DataGatherer(), mathhub_dir)
newfiles = displayedfiles[:]
while newfiles:
for filename in newfiles:
if not os.path.isfile(filename):
print("File " + filename + " doesn't exist")
continue
ctx.repo = os.path.join(mathhub_dir, harvest.split_path_repo_doc(filename)[0])
ctx.gatherer.push_repo(ctx.repo, ctx)
root, name = os.path.split(filename)
harvest.harvest_file(root, name, ctx)
newfiles = []
fringe = [m["dest_path"] for m in ctx.gatherer.mhinputrefs]
if BIG:
fringe += [m["dest_path"] for m in ctx.gatherer.importmhmodules]
# both signature and language bindings?
fringe += [os.path.join(mathhub_dir, m["dest_repo"], "source", m["dest_mod"]+"."+args.LANGUAGE+".tex") for m in ctx.gatherer.gimports]
fringe += [os.path.join(mathhub_dir, m["dest_repo"], "source", m["dest_mod"]+".tex") for m in ctx.gatherer.gimports]
for destfile in fringe:
if destfile not in displayedfiles:
newfiles.append(destfile)
displayedfiles.append(destfile)
return (ctx.gatherer.defis, ctx.gatherer.trefis)
defis, trefis = getdefisandtrefis()
# Now we need to gather everything imported to find definitions for trefis
logger = harvest.SimpleLogger(2)
mygraph = graph.Graph()
graph.fill_graph(mathhub_dir, root_repo, root_doc, mygraph, ONLY_COVERED_PART)
ctx = harvest.HarvestContext(logger, harvest.DataGatherer(), mathhub_dir)
relevantfiles = \
list(mygraph.g_nodes.keys()) +\
list(mygraph.module_nodes.keys()) +\
[k[0] for k in mygraph.omgroup_nodes.keys()]
extrafiles = []
for filename in relevantfiles:
# check computer.en.tex if computer.tex is used
lf = filename[:-4]+"." + args.LANGUAGE + ".tex"
if os.path.isfile(lf):
extrafiles.append(lf)
for filename in relevantfiles + extrafiles:
ctx.repo = harvest.split_path_repo_doc(filename)[1]
ctx.gatherer.push_repo(ctx.repo, ctx)
root, name = os.path.split(filename)
harvest.harvest_file(root, name, ctx)
glossary = Glossary(args.LANGUAGE, mathhub_dir)
acceptDefi = lambda defi : defi["lang"] == args.LANGUAGE or (defi["lang"] == "?" and args.LANGUAGE == "en")
for defi in defis:
if acceptDefi(defi):
glossary.fillDefi(defi)
defiIndex = {}
for defi in ctx.gatherer.defis:
if acceptDefi(defi):
defiIndex[(defi["mod_name"], defi["name"])] = defi
for trefi in trefis:
k = (trefi["target_mod"], trefi["name"])
if k not in defiIndex:
print("Failed to find definition for trefi " + k[0] + "?" + k[1] + " in " + trefi["path"])
continue
glossary.fillDefi(defiIndex[k])
with open(args.OUTFILE, "w") as fp:
fp.write(str(glossary))