Completed
Push — master ( 8804a6...413071 )
by Sepand
01:04
created

logo_handler()   A

Complexity

Conditions 4

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

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