Completed
Push — master ( b44136...ebb2b3 )
by Sepand
22s
created

csv_reader()   F

Complexity

Conditions 10

Size

Total Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 10
c 2
b 1
f 0
dl 0
loc 36
rs 3.1304

How to fix   Complexity   

Complexity

Complex classes like csv_reader() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

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