-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathGlossaryMaker.py
111 lines (87 loc) · 3.17 KB
/
GlossaryMaker.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
#coding=utf-8
'''
This script reads CamelForth source files and parses the glossary
definition into a text file summary.
Created on 12.02.2012
@author: ulli hoffmann, christopherkalus
'''
import os
import glob
import codecs
import re
import string
GLOSSARYDEFS=re.compile("^;C|^;X|^;Z|^;U|^;A|===\W*$")
def parseInputFile(filename):
"""Parses glossary definitions from file 'filename' output: list of lines"""
infile=open(filename)
(pfad, filename)=os.path.split(filename)
result=['',filename]
for zeile in infile:
if GLOSSARYDEFS.search(zeile):
elemente = filter(lambda e:e!='', zeile.strip().split(" "))
elemente.append(" ")
if len(elemente) < 3:
result.append("".join(map(lambda e:e.ljust(80,"=") if "===" in e else e, elemente))+" ")
else:
name, stackcomment, comment = map(string.strip, elemente[0:3])
result.append("%-16s%-30s%s " % (name, stackcomment, comment))
infile.close()
return result
def parseInputFiles(inputfileGlob):
"""Parse glossary definitions form files matching 'glob'"""
result=[]
for filename in glob.glob(os.path.join(os.getcwd(), inputfileGlob)):
print "parsing %s ..." % (filename,),
result.extend(parseInputFile(filename))
print "ok"
return result
def writeOutputFile(filename, lines):
print "writing %s ..." % (filename,),
outfile=codecs.open(filename,"w","utf-8").write("\n".join(lines)+"\n")
print "ok"
def deleteTrash(zeile):
if zeile == "" or ';' not in zeile or "===" in zeile or "---" in zeile:
return False
else:
return zeile[2:len(zeile)]
def getCategory(zeile):
if len(zeile)>2:
return zeile[0:2]
else:
return zeile
def makeGlossaryTrashfree(infile):
for zeile in infile:
category = getCategory(zeile)
zeile = deleteTrash(zeile)
if zeile:
trashFreeGlossary[zeile]=category
else:
continue
def sortAndPrint(infile,outfile):
makeGlossaryTrashfree(infile)
print "writing Glossary_Sorted.txt ... ",
for element in sorted(trashFreeGlossary):
print >> outfile,trashFreeGlossary[element], element,
print "ok"
def mergeFuncAndSorted(infile1,infile2,outfile):
print "writing Glossary.txt ... ",
for zeile in infile1:
print >> outfile, zeile,
print >> outfile, "\n\n","words sorted".center(60,"-"), "\n\n"
for zeile in infile2:
print >> outfile, zeile,
print "ok"
if __name__=="__main__":
print "GlossaryMaker.py - generate glossory summary for CamelForth"
writeOutputFile("Glossary_functional.txt", parseInputFiles("*.asm"))
trashFreeGlossary={}
infile=codecs.open("Glossary_functional.txt","rU","utf-8")
outfile=codecs.open("Glossary_Sorted.txt","w","utf-8")
sortAndPrint(infile, outfile)
infile.close()
outfile.close()
infile1=codecs.open("Glossary_functional.txt","rU","utf-8")
infile2=codecs.open("Glossary_Sorted.txt","rU","utf-8")
outfile=codecs.open("Glossary.txt","w","utf-8")
mergeFuncAndSorted(infile1,infile2,outfile)
print "done"