-
Notifications
You must be signed in to change notification settings - Fork 25
First Name + Last Name support #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,28 +10,51 @@ | |
| import csv | ||
| import json | ||
|
|
||
| default_input_file_format = { | ||
| 'first_name': None, | ||
| 'last_name': None, | ||
| 'full_name': None, | ||
| 'nickname': None, | ||
| 'org': None, | ||
| 'tel': None, | ||
| 'url': None, | ||
| 'bday': None, | ||
| 'role': None, | ||
| 'email': None, | ||
| 'note': None | ||
| } | ||
|
|
||
|
|
||
| def convert_to_vcard(input_file, single_output, input_file_format): | ||
|
|
||
| FN = input_file_format['name']-1 if 'name' in input_file_format else None | ||
| NICKNAME = input_file_format['nickname']-1 if 'nickname' in input_file_format else None | ||
| ORG = input_file_format['org']-1 if 'org' in input_file_format else None | ||
| TEL = input_file_format['tel']-1 if 'tel' in input_file_format else None | ||
| URL = input_file_format['url']-1 if 'url' in input_file_format else None | ||
| BDAY = input_file_format['bday']-1 if 'bday' in input_file_format else None | ||
| ROLE = input_file_format['role']-1 if 'role' in input_file_format else None | ||
| EMAIL = input_file_format['email']-1 if 'email' in input_file_format else None | ||
| NOTE = input_file_format['note']-1 if 'note' in input_file_format else None | ||
| input_file_format = {**default_input_file_format, **input_file_format} | ||
|
|
||
| FIRST_NAME = input_file_format['first_name'] | ||
| LAST_NAME = input_file_format['last_name'] | ||
| FULL_NAME = input_file_format['full_name'] | ||
|
|
||
| NICKNAME = input_file_format['nickname'] | ||
|
|
||
| ORG = input_file_format['org'] | ||
| TEL = input_file_format['tel'] | ||
| URL = input_file_format['url'] | ||
| BDAY = input_file_format['bday'] | ||
| ROLE = input_file_format['role'] | ||
| EMAIL = input_file_format['email'] | ||
| NOTE = input_file_format['note'] | ||
|
|
||
| # if single output option is selected | ||
| if single_output : | ||
| with open( input_file, 'r' ) as source_file: | ||
| reader = csv.reader( source_file ) | ||
| if single_output: | ||
| with open(input_file, 'r', encoding='utf-8-sig') as source_file: | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. removes the BOM character prefix if exists |
||
| reader = csv.DictReader(source_file) | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The Indexing by key titles is more convenient |
||
| single_vcf = open('csv2vcf/all_contacts.vcf', 'w') | ||
| i = 0 | ||
| for row in reader: | ||
|
|
||
| FN_VAL = row[FN] if FN is not None else '' | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. new logic for building the FN_VAL based on the |
||
| FN_VAL = row[FULL_NAME] if FULL_NAME is not None else '' | ||
| FN_VAL = row[FIRST_NAME] + ' ' + \ | ||
| row[LAST_NAME] if FN_VAL == '' else FN_VAL | ||
|
|
||
| NICKNAME_VAL = row[NICKNAME] if NICKNAME is not None else '' | ||
| ORG_VAL = row[ORG] if ORG is not None else '' | ||
| TEL_VAL = row[TEL] if TEL is not None else '' | ||
|
|
@@ -57,20 +80,20 @@ def convert_to_vcard(input_file, single_output, input_file_format): | |
| print('----------------------') | ||
|
|
||
| # write the single file | ||
| single_vcf.write( 'BEGIN:VCARD' + "\n") | ||
| single_vcf.write( 'VERSION:3.0' + "\n") | ||
| single_vcf.write( 'N:' + FN_VAL + ';' + "\n") | ||
| single_vcf.write( 'FN:' + FN_VAL + "\n") | ||
| single_vcf.write( 'NICKNAME:' + NICKNAME_VAL + "\n") | ||
| single_vcf.write( 'TEL;HOME;VOICE:' + TEL_VAL + "\n") | ||
| single_vcf.write( 'EMAIL:' + EMAIL_VAL + "\n") | ||
| single_vcf.write( 'BDAY:' + BDAY_VAL + "\n") | ||
| single_vcf.write( 'ORG:' + ORG_VAL + "\n") | ||
| single_vcf.write( 'ROLE:' + ROLE_VAL + "\n") | ||
| single_vcf.write( 'URL:' + URL_VAL + "\n") | ||
| single_vcf.write( 'NOTE:' + NOTE_VAL + "\n") | ||
| single_vcf.write( 'END:VCARD' + "\n") | ||
| single_vcf.write( "\n") | ||
| single_vcf.write('BEGIN:VCARD' + "\n") | ||
| single_vcf.write('VERSION:3.0' + "\n") | ||
| single_vcf.write('N:' + FN_VAL + ';' + "\n") | ||
| single_vcf.write('FN:' + FN_VAL + "\n") | ||
| single_vcf.write('NICKNAME:' + NICKNAME_VAL + "\n") | ||
| single_vcf.write('TEL;HOME;VOICE:' + TEL_VAL + "\n") | ||
| single_vcf.write('EMAIL:' + EMAIL_VAL + "\n") | ||
| single_vcf.write('BDAY:' + BDAY_VAL + "\n") | ||
| single_vcf.write('ORG:' + ORG_VAL + "\n") | ||
| single_vcf.write('ROLE:' + ROLE_VAL + "\n") | ||
| single_vcf.write('URL:' + URL_VAL + "\n") | ||
| single_vcf.write('NOTE:' + NOTE_VAL + "\n") | ||
| single_vcf.write('END:VCARD' + "\n") | ||
| single_vcf.write("\n") | ||
|
|
||
| i += 1 | ||
|
|
||
|
|
@@ -79,13 +102,16 @@ def convert_to_vcard(input_file, single_output, input_file_format): | |
| print('----------------------') | ||
|
|
||
| # default ( multi-file output ) | ||
| else : | ||
| with open( input_file, 'r' ) as source_file: | ||
| reader = csv.reader( source_file ) | ||
| else: | ||
| with open(input_file, 'r', encoding='utf-8-sig') as source_file: | ||
| reader = csv.DictReader(source_file) | ||
| i = 0 | ||
| for row in reader: | ||
|
|
||
| FN_VAL = row[FN] if FN is not None else '' | ||
| FN_VAL = row[FULL_NAME] if FULL_NAME is not None else '' | ||
| FN_VAL = row[FIRST_NAME] + ' ' + \ | ||
| row[LAST_NAME] if FN_VAL == '' else FN_VAL | ||
|
|
||
| NICKNAME_VAL = row[NICKNAME] if NICKNAME is not None else '' | ||
| ORG_VAL = row[ORG] if ORG is not None else '' | ||
| TEL_VAL = row[TEL] if TEL is not None else '' | ||
|
|
@@ -111,20 +137,21 @@ def convert_to_vcard(input_file, single_output, input_file_format): | |
| print('----------------------') | ||
|
|
||
| # write each entry | ||
| each_vcf = open('csv2vcf/' + FN_VAL + '_' + TEL_VAL + ".vcf", 'w') | ||
| each_vcf.write( 'BEGIN:VCARD' + "\n") | ||
| each_vcf.write( 'VERSION:3.0' + "\n") | ||
| each_vcf.write( 'N:' + FN_VAL + ';' + "\n") | ||
| each_vcf.write( 'FN:' + FN_VAL + "\n") | ||
| each_vcf.write( 'NICKNAME:' + NICKNAME_VAL + "\n") | ||
| each_vcf.write( 'TEL;HOME;VOICE:' + TEL_VAL + "\n") | ||
| each_vcf.write( 'EMAIL:' + EMAIL_VAL + "\n") | ||
| each_vcf.write( 'BDAY:' + BDAY_VAL + "\n") | ||
| each_vcf.write( 'ORG:' + ORG_VAL + "\n") | ||
| each_vcf.write( 'ROLE:' + ROLE_VAL + "\n") | ||
| each_vcf.write( 'URL:' + URL_VAL + "\n") | ||
| each_vcf.write( 'NOTE:' + NOTE_VAL + "\n") | ||
| each_vcf.write( 'END:VCARD' + "\n") | ||
| each_vcf = open('csv2vcf/' + FN_VAL + | ||
| '_' + TEL_VAL + ".vcf", 'w') | ||
| each_vcf.write('BEGIN:VCARD' + "\n") | ||
| each_vcf.write('VERSION:3.0' + "\n") | ||
| each_vcf.write('N:' + FN_VAL + ';' + "\n") | ||
| each_vcf.write('FN:' + FN_VAL + "\n") | ||
| each_vcf.write('NICKNAME:' + NICKNAME_VAL + "\n") | ||
| each_vcf.write('TEL;HOME;VOICE:' + TEL_VAL + "\n") | ||
| each_vcf.write('EMAIL:' + EMAIL_VAL + "\n") | ||
| each_vcf.write('BDAY:' + BDAY_VAL + "\n") | ||
| each_vcf.write('ORG:' + ORG_VAL + "\n") | ||
| each_vcf.write('ROLE:' + ROLE_VAL + "\n") | ||
| each_vcf.write('URL:' + URL_VAL + "\n") | ||
| each_vcf.write('NOTE:' + NOTE_VAL + "\n") | ||
| each_vcf.write('END:VCARD' + "\n") | ||
| each_vcf.write("\n") | ||
| each_vcf.close() | ||
|
|
||
|
|
@@ -137,44 +164,50 @@ def convert_to_vcard(input_file, single_output, input_file_format): | |
| def main(args): | ||
| args_len = len(args) | ||
|
|
||
| if args_len < 3 or args_len > 4 : | ||
| print ( "Usage:") | ||
| print ( args[0] + " filename") | ||
| if args_len < 3 or args_len > 4: | ||
| print("Usage:") | ||
| print(args[0] + " filename") | ||
| sys.exit() | ||
|
|
||
| if args_len == 3 : | ||
| if args_len == 3: | ||
| input_file = args[1] | ||
| try : | ||
|
|
||
| try: | ||
| input_file_format = json.loads(args[2]) | ||
| except Exception as e : | ||
| except Exception: | ||
| print('\033[91m'+"ERROR : json could not be parsed"+'\033[0m') | ||
| sys.exit() | ||
|
|
||
| single_output = 0 | ||
| elif args_len == 4 : | ||
| elif args_len == 4: | ||
| input_file = args[1] | ||
|
|
||
| if args[2] == '-s' or args[2] == '--single' : | ||
| if args[2] == '-s' or args[2] == '--single': | ||
| single_output = 1 | ||
| else : | ||
| print('\033[91m'+"ERROR : invalid argument `" + args[2] + "`"+'\033[0m') | ||
| else: | ||
| print('\033[91m'+"ERROR : invalid argument `" + | ||
| args[2] + "`"+'\033[0m') | ||
| sys.exit() | ||
|
|
||
| try : | ||
| try: | ||
| input_file_format = json.loads(args[3]) | ||
| except Exception as e : | ||
| except Exception: | ||
| print('\033[91m'+"ERROR : json could not be parsed"+'\033[0m') | ||
| sys.exit() | ||
|
|
||
| if not os.path.exists(input_file) : | ||
| print('\033[91m'+"ERROR : file `" + input_file + "` not found"+'\033[0m') | ||
| if not os.path.exists(input_file): | ||
| print('\033[91m'+"ERROR : file `" + | ||
| input_file + "` not found"+'\033[0m') | ||
| sys.exit() | ||
|
|
||
| if not os.path.exists('csv2vcf') : | ||
| if not os.path.exists('csv2vcf'): | ||
| os.makedirs('csv2vcf') | ||
|
|
||
| convert_to_vcard(input_file, single_output, input_file_format) | ||
|
|
||
| print('\033[92m'+"DONE"+'\033[0m') | ||
| print("Output files are in `csv2vcf` folder") | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. when generating output files it is always useful to print the path |
||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| main(sys.argv) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This aids readability, makes the supported keys visible