1
|
|
|
import os |
2
|
|
|
from .params import * |
3
|
|
|
import sys |
4
|
|
|
import requests |
5
|
|
|
|
6
|
|
|
def check_update(DEBUG=False): |
7
|
|
|
''' |
8
|
|
|
This function check csv2latex site for newversion |
9
|
|
|
:param DEBUG: Flag for using Debug mode |
10
|
|
|
:type DEBUG:bool |
11
|
|
|
:return: True if new version is available |
12
|
|
|
''' |
13
|
|
|
try: |
14
|
|
|
new_version=requests.get(UPDATE_URL).text |
15
|
|
|
if float(new_version)>float(version): |
16
|
|
|
print("New Version ("+new_version+") Of csv2latex is available (visit github page)") |
17
|
|
|
except Exception as e: |
18
|
|
|
if DEBUG==True: |
19
|
|
|
print(str(e)) |
20
|
|
|
|
21
|
|
|
def line(number,char="-"): |
22
|
|
|
''' |
23
|
|
|
:param number: number of items to print |
24
|
|
|
:param char: each item of printed line |
25
|
|
|
:return: line string |
26
|
|
|
''' |
27
|
|
|
response="" |
28
|
|
|
i=0 |
29
|
|
|
while(i<number): |
30
|
|
|
response+=char |
31
|
|
|
i+=1 |
32
|
|
|
return response |
33
|
|
|
def logo_handler(): |
34
|
|
|
''' |
35
|
|
|
:return: None (print logo) |
36
|
|
|
''' |
37
|
|
|
try: |
38
|
|
|
print(logo) |
39
|
|
|
print("Version : "+str(version)) |
40
|
|
|
except: |
41
|
|
|
pass |
42
|
|
|
def signature(frame=41): |
43
|
|
|
''' |
44
|
|
|
:param frame: number of items in line |
45
|
|
|
:return: header string |
46
|
|
|
''' |
47
|
|
|
logo_handler() |
48
|
|
|
sign=line(frame,char="%") |
49
|
|
|
sign = sign + "\n" |
50
|
|
|
sign = sign + "For more info please visit : https://github.com/sepandhaghighi/csv2latex\n" |
51
|
|
|
sign=sign+line(frame,"%") |
52
|
|
|
return sign |
53
|
|
|
#% "ModernCV" CV and Cover Letter |
54
|
|
|
#% LaTeX Template |
55
|
|
|
#% Version 1.1 (9/12/12) |
56
|
|
|
|
57
|
|
|
def escape_char(lines_list): |
58
|
|
|
''' |
59
|
|
|
|
60
|
|
|
:param lines_list: list of lines as input (list of strings) |
61
|
|
|
:return: list of lines after escape character |
62
|
|
|
''' |
63
|
|
|
for i,item_1 in enumerate(lines_list): |
64
|
|
|
for j,item_2 in enumerate(item_1): |
65
|
|
|
for char in char_list: # instead of using for use a list to find and replace |
66
|
|
|
lines_list[i][j]=item_2.replace(char,"\\"+char) |
67
|
|
|
return lines_list |
68
|
|
|
|
69
|
|
|
|
70
|
|
|
def white_space(line_list): |
71
|
|
|
''' |
72
|
|
|
|
73
|
|
|
:param line_list: list of lines as input (list of strings) |
74
|
|
|
:return: list of comma seperated item in plain text for spacing in next step |
75
|
|
|
''' |
76
|
|
|
len_list = [] |
77
|
|
|
for line in line_list: |
78
|
|
|
if len(len_list)==0: |
79
|
|
|
len_list.extend(list(map(len,line))) |
80
|
|
|
else: |
81
|
|
|
if len(line) <= len(len_list): |
82
|
|
|
for i,item in enumerate(line): |
83
|
|
|
if len(item) > len_list[i]: |
84
|
|
|
len_list[i] = len(item) |
85
|
|
|
return len_list |
86
|
|
|
|
87
|
|
|
|
88
|
|
|
def header_handler(file,col_num,caption="my-caption",label="my-label"): |
89
|
|
|
''' |
90
|
|
|
|
91
|
|
|
:param file: input latex file |
92
|
|
|
:param col_num: number of col |
93
|
|
|
:param caption: caption for table |
94
|
|
|
:param label: label for table |
95
|
|
|
:return: None |
96
|
|
|
''' |
97
|
|
|
file.write(header["static_1"]) |
98
|
|
|
file.write(header["static_2"]) |
99
|
|
|
file.write(header["caption"]+"{"+caption+"}\n") |
100
|
|
|
file.write(header["label"]+"{"+label+"}\n") |
101
|
|
|
file.write(header["align"]+"{"+"c"*col_num+"}\n") |
102
|
|
|
|
103
|
|
|
def footer_handler(file): |
104
|
|
|
''' |
105
|
|
|
|
106
|
|
|
:param file: latex file |
107
|
|
|
:return: None |
108
|
|
|
''' |
109
|
|
|
file.write(footer+"\n") |
110
|
|
|
|
111
|
|
|
|
112
|
|
|
def read_csv(file_name): |
113
|
|
|
''' |
114
|
|
|
|
115
|
|
|
:param file_name: csv file name (as string) |
116
|
|
|
:return: list of csv file lines |
117
|
|
|
''' |
118
|
|
|
csv_file=open(file_name,"r") |
119
|
|
|
csv_lines=[] |
120
|
|
|
for line in csv_file: |
121
|
|
|
splited_line=line.split(",") |
122
|
|
|
csv_lines.append(splited_line) |
123
|
|
|
csv_file.close() |
124
|
|
|
return csv_lines |
125
|
|
|
|
126
|
|
|
def create_latex(file_name,dir_folder="LaTeX",empty_space=True): |
127
|
|
|
|
128
|
|
|
''' |
129
|
|
|
|
130
|
|
|
:param file_name: file name of latex file 9(as string) |
131
|
|
|
:param dir_folder: output folder name (as string) |
132
|
|
|
:param empty_space: boolean paramter (True-->modify empty space) |
133
|
|
|
:return: None |
134
|
|
|
''' |
135
|
|
|
try: |
136
|
|
|
print("\nCreate LaTeX table for "+file_name+" . . .") |
137
|
|
|
latex_file=open(os.path.join(os.getcwd(),dir_folder,file_name[:-4]+".tex"),"w") |
138
|
|
|
csv_read=read_csv(file_name) |
139
|
|
|
csv_lines=csv_read # continue using csv_read without declairing csv_lines |
140
|
|
|
|
141
|
|
|
col_num=len(csv_lines[0]) |
142
|
|
|
header_handler(latex_file,col_num=col_num) # col_num is useless here |
143
|
|
|
escape_out=escape_char(csv_lines) |
144
|
|
|
list_len = white_space(escape_out) |
145
|
|
|
for item in escape_out : |
146
|
|
|
for i,item_2 in enumerate(item): |
147
|
|
|
if i<len(item)-1: |
148
|
|
|
latex_file.write(item_2) |
149
|
|
|
if empty_space==True: |
150
|
|
|
latex_file.write(" " * (list_len[i]-len(item_2))) |
151
|
|
|
latex_file.write("&") |
152
|
|
|
if empty_space==True: |
153
|
|
|
latex_file.write(" "*list_len[i]) |
154
|
|
|
else: |
155
|
|
|
latex_file.write(item_2[:-1]) |
156
|
|
|
latex_file.write("\\\\"+"\n") |
157
|
|
|
footer_handler(latex_file) |
158
|
|
|
latex_file.close() |
159
|
|
|
print("Done!") |
160
|
|
|
print(line(70, "*")) |
161
|
|
|
except: |
162
|
|
|
print("Error In LaTeX Creation") |
163
|
|
|
|
164
|
|
|
def make_dir(): |
165
|
|
|
''' |
166
|
|
|
|
167
|
|
|
:return: None |
168
|
|
|
''' |
169
|
|
|
if "LaTeX" not in os.listdir(): |
170
|
|
|
try: |
171
|
|
|
os.mkdir("LaTeX") |
172
|
|
|
except: |
173
|
|
|
print("Access Error") |
174
|
|
|
sys.exit() |
175
|
|
|
|
176
|
|
|
|
177
|
|
|
|
178
|
|
|
|
179
|
|
|
|
180
|
|
|
|
181
|
|
|
|
182
|
|
|
|
183
|
|
|
|
184
|
|
|
|
185
|
|
|
|