-
Notifications
You must be signed in to change notification settings - Fork 0
/
csv2gls.py
executable file
·106 lines (64 loc) · 2.07 KB
/
csv2gls.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
#!/usr/bin/python
"""
CSV2GLS
Matt Luckcuck <[email protected]>
Reads in a csv file and produces a tex file of acronyms compatible with the LaTeX glossary package
"""
import argparse
from acronym import Acronym
import datetime
import os.path
parser = argparse.ArgumentParser()
parser.add_argument("input", help="The input csv file")
parser.add_argument("-o", help="The output file location; if no location is given, a file is generated in the same directory as the input file")
args = parser.parse_args()
print("+++ CSV2GPL +++" )
print("+++ Matt Luckcuck 2018 +++")
inputFile = args.input
outputFile = args.o
if not outputFile:
inputFileName = os.path.splitext(inputFile)[0]
outputFile = inputFileName + ".tex"
print("+++ Converting " + inputFile + " to " + outputFile)
def openInputFile(inputFile):
try:
csvFile = open(inputFile, "r")
return csvFile
except:
print("exception opening file " + inputFile)
def openOutputFile(outputFile):
try:
file = open(outputFile, "w")
return file
except:
print("exception opening file " + f)
def processFile(csvFile):
print("+++ Processing CSV File +++")
firstLine = True
glsList = []
with csvFile:
for line in csvFile:
if firstLine:
firstLine = False
elif len(line)>2:
glsList.append(processRow(line))
return glsList
def processRow(pair):
pair = pair.split(",", maxsplit=1)
acronymString = pair[0]
description = pair[1]
gls = Acronym(acronymString, description)
return gls
def writeFile(glsList):
print("+++ Writing File +++")
glsFile = openOutputFile(outputFile)
glsFile.write("%Converted by CSV2GLS from " + inputFile+"\n")
glsFile.write("%Converted on " + str(datetime.datetime.today())+"\n" )
for i in glsList:
glsFile.write(i.getGLSLine())
glsFile.close()
csvFile = openInputFile(inputFile)
glsList = processFile(csvFile)
writeFile(glsList)
csvFile.close()
print("+++ Done +++")