Completed
Push — master ( ebb2b3...a55a70 )
by Sepand
06:30
created

VCF_write()   A

Complexity

Conditions 4

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
dl 0
loc 15
rs 9.2
c 0
b 0
f 0
1
# -*- coding: utf-8 -*-
2
import os
3
from tkinter.filedialog import askopenfilename
4
from tkinter import messagebox
5
6
name_dict={}
7
unknown_index=0
8
def zero_insert(input_string):
9
    '''
10
    This function get a string as input if input is one digit add a zero
11
    :param input_string: input digit az string
12
    :type input_string:str
13
    :return: modified output as str
14
    '''
15
    if len(input_string)==1:
16
        return "0"+input_string
17
    return input_string
18
19
def time_convert(input_data):
20
    '''
21
    This function convert input_sec  from sec to DD,HH,MM,SS Format
22
    :param input_string: input time string  in sec
23
    :type input_string:str
24
    :return: converted time as string
25
    '''
26
    input_sec=input_data
27
    input_minute=input_sec//60
28
    input_sec=int(input_sec-input_minute*60)
29
    input_hour=input_minute//60
30
    input_minute=int(input_minute-input_hour*60)
31
    input_day=int(input_hour//24)
32
    input_hour=int(input_hour-input_day*24)
33
    return zero_insert(str(input_day))+" days, "+zero_insert(str(input_hour))+" hour, "+zero_insert(str(input_minute))+" minutes, "+zero_insert(str(input_sec))+" seconds"
34
35
def VCF_init(file):
36
    file.write("BEGIN:VCARD\n")
37
    file.write("VERSION:3.0\n")
38
39
def VCF_name(file,first_name,last_name):
40
    file.write("N:"+last_name+";"+first_name+";;;"+"\n")
41
    file.write("FN:" + first_name+" "+last_name + "\n")
42
43
def VCF_phone(file,tel_mobile,tel_home,tel_work):
44
    file.write("TEL;type=CELL:" + tel_mobile + "\n")
45
    file.write("TEL;type=HOME:" + tel_home + "\n")
46
    file.write("TEL;type=WORK:" + tel_work + "\n")
47
48
def VCF_email(file,email_home,email_mobile,email_work):
49
    file.write("EMAIL;type=INTERNET;type=WORK;type=pref:" + email_work + "\n")
50
    file.write("EMAIL;type=INTERNET;type=HOME;type=pref:" + email_home + "\n")
51
    file.write("EMAIL;type=INTERNET;type=CELL;type=pref:" + email_mobile + "\n")
52
53
54
def VCF_adr(file,adr_work,adr_home):
55
    file.write('item1.ADR;type=WORK:;; ' + adr_work + "\n")
56
    file.write('item2.ADR;type=HOME;type=pref:;; ' + adr_home + "\n")
57
58
def VCF_website(file,website_url):
59
    file.write('item3.URL;type=pref:' + website_url + "\n")
60
    file.write("END:VCARD")
61
    file.close()
62
63
def VCF_Folder(filename):
64
    folder_adr=os.path.dirname(filename)
65
    filename_split=filename.split("/")[-1].split(".")[0]
66
    VCF_folder_adr = os.path.join(folder_adr, "VCF_CONVERT_" + filename_split)
67
    if "VCF_CONVERT_"+filename_split not in os.listdir(folder_adr):
68
        os.mkdir(VCF_folder_adr)
69
    return VCF_folder_adr
70
71
def VCF_creator(folder_name,first_name,last_name,tel_mobile,tel_home,tel_work,email_home,email_work,email_mobile,adr_work,adr_home,website_url):
72
    file=open(os.path.join(folder_name,last_name+"_"+first_name+".vcf"),"w")
73
    VCF_init(file)
74
    VCF_name(file,first_name,last_name)
75
    VCF_phone(file,tel_mobile,tel_home,tel_work)
76
    VCF_email(file,email_home,email_mobile,email_work)
77
    VCF_adr(file,adr_work,adr_home)
78
    VCF_website(file,website_url)
79
80
def name_dict_update(name):
81
    global name_dict
82
    if name not in name_dict.keys():
83
        name_dict[name] = 0
84
    else:
85
        name_dict[name] = name_dict[name] + 1
86
87
def VCF_write(temp,name_dict,foldername):
88
    name = temp[0] + "," + temp[1]
89
    name_dict_update(name)
90
    global unknown_index
91
    if len(temp[0]) == 0 and len(temp[1]) == 0:
92
        unknown_index += 1
93
        VCF_creator(foldername, str(unknown_index),"Unknown ", temp[2], temp[3], temp[4], temp[5], temp[6], temp[7],
94
                    temp[8], temp[9], temp[10])
95
    else:
96
        if name_dict[name] != 0:
97
            VCF_creator(foldername, temp[0] + "_" + str(name_dict[name]), temp[1], temp[2], temp[3], temp[4], temp[5],
98
                        temp[6], temp[7], temp[8], temp[9], temp[10])
99
        else:
100
            VCF_creator(foldername, temp[0], temp[1], temp[2], temp[3], temp[4], temp[5], temp[6], temp[7], temp[8],
101
                        temp[9], temp[10])
102
def csv_reader(file_name):
103
    try:
104
        file=open(file_name,"r")
105
106
        vcf_counter=0
107
108
        foldername=VCF_Folder(file_name)
109
        for index,line in enumerate(file):
110
            if index>0:
111
                stripped_line=line.strip()
112
                temp=stripped_line.split(",")
113
                if len(temp)>11:
114
                    print("[Warning] CSV File Line "+str(index)+" Bad Format")
115
                    continue
116
                else:
117
                    VCF_write(temp,name_dict,foldername)
118
                    vcf_counter+=1
119
        return vcf_counter
120
121
    except FileNotFoundError:
122
        print("[Warning] Please Open CSV File")
123
    except Exception as e:
124
        messagebox.showinfo("CSV2VCF", "Error In Reading Input File")
125
        print("[Error] In Reading Input File")
126
127