-
Notifications
You must be signed in to change notification settings - Fork 0
/
tg_functions.py
129 lines (97 loc) · 4.33 KB
/
tg_functions.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
126
127
128
129
# Import libraries
import os
#================================================================
#================================================================
# MAIN DRIVER FUNCTION
def GenerateTableFromFile(filename = ""):
#Retrieve the contents of the file and no. columns
contents, num_cols = ImportFile(filename)
#Generate the table
output_file_name = GenerateTable(num_cols, contents)
return output_file_name
#================================================================
#================================================================
# Fix Underscores
def ReplaceUnderscores(contents = ""):
return contents.replace("_", "\\_")
#================================================================
#================================================================
# Import file
def ImportFile(filename = ""):
f = open(filename)
file = f.read() #Import the file as string
f.close()
rows = ParseInputFile(file) #Get the rows as a list
num_cols = GetSize(rows[0]) #Get the number of columns
return rows, num_cols
#================================================================
#================================================================
# Parse the input file
def ParseInputFile(file = ""):
file = ReplaceUnderscores(file)
rows = file.split("\n") # Split the string by "\n" to get rows
return rows
#================================================================
#================================================================
# Get size of csv
def GetSize(row_one = ""):
length = len(row_one.split("\t"))
return length
#================================================================
#================================================================
# Generate code holder
def GenerateCodeHolder(col = 1):
l = "l" * col
top = "\\begin{tabular}{" + l + "}\n"
bot = "\\end{tabular}\n"
return top, bot
#================================================================
#================================================================
# Generate table row
def GenerateRow(row = ""):
variables = "\t" + row.replace("\t", " & ") + "\\\\\n"
return variables
#================================================================
#================================================================
# Generate file and return name
def GenerateFile(table = ""):
# Get output file name from user and format it
user_file_name = input("\n\t>>> Output file name: ")
user_file_name = user_file_name + '.tex'
# Create the final output path
out_path = os.path.join(os.path.realpath(__file__))
t_path = os.path.join("/Outputs", user_file_name)
out_path = out_path.replace('/tg_functions.py', t_path)
# Create the output file and write to it
output_file = open(out_path, "w")
output_file.write(table)
output_file.close()
# Return the output path
return out_path
#================================================================
#================================================================
# Generate table
def GenerateTable(num_cols, rows = []):
#Get the top and bot rows
top, bot = GenerateCodeHolder(num_cols)
#Get the row contents
row_contents = ""
for r in rows:
row_contents = row_contents + GenerateRow(r)
#Output the file
table = top + row_contents + bot
#Return the name of the file
return GenerateFile(table)
#================================================================
#================================================================
# Draw title (UI)
def DrawTitle():
print(' _ _____ __ __ _____ _ _ _____ _ ')
print('| | |_ _| \ \ / / |_ _| | | | | | __ \ | | ')
print('| | __ _| | ___ \ V / | | __ _| |__ | | ___ | | \/ ___ _ __ ___ _ __ __ _| |_ ___ _ __ ')
print('| | / _` | |/ _ \/ \ | |/ _` | \'_ \| |/ _ \ | | __ / _ \ \'_ \ / _ \ \'__/ _` | __/ _ \| \'__|')
print('| |___| (_| | | __/ /^\ \ | | (_| | |_) | | __/ | |_\ \ __/ | | | __/ | | (_| | || (_) | | ')
print('\_____/\__,_\_/\___\/ \/ \_/\__,_|_.__/|_|\___| \____/\___|_| |_|\___|_| \__,_|\__\___/|_| ')
print('\n\n\n')
print('NOTE: Please refer to the README.md in the GitHub repo for instructions for use.')
print('\n\n\n')