-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstrip-images-from-apple-vcard.py
executable file
·76 lines (55 loc) · 2.02 KB
/
strip-images-from-apple-vcard.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
import os
import re
import argparse
def d(msg):
if verbose == False:
return
print(msg)
parser = argparse.ArgumentParser(description='Strips images from an Apple AddressBook.app VCF export for easier uploading to contact directories (e.g. Google Contacts)')
parser.add_argument('--inputfile', metavar='STRING', help='The input file (.vcf)', required=True)
parser.add_argument('--outputfile', metavar='STRING', help='The output file', required=False)
parser.add_argument('--verbose', help='Print debug information', action="store_true", required=False)
args = parser.parse_args()
# Logic to handle command line arguments
# ======================================================================
if args.verbose:
verbose = True
d('Enabling command line verbosity as requested by command line')
else:
verbose = False
filenameInput = args.inputfile
d('Setting filenameInput to "' + filenameInput + '"')
if args.outputfile:
filenameOutput = args.outputfile
else:
filenameOutput = re.sub("(\.[a-z]+)$", ".clean\\1", filenameInput)
d('Setting filenameOutput to "' + filenameOutput + '"')
# Check input and output files
# ======================================================================
if not os.path.exists(filenameInput):
print("ERROR: File '" + filenameInput + "' not found. Cannot continue.")
sys.exit(1)
if os.path.exists(filenameOutput):
print("ERROR: File '" + filenameOutput + "' already exists. Please rename or delete, then re-run this script.")
sys.exit(1)
# Convert/clean
# ======================================================================
d("Reading '" + filenameInput + "'")
infile = open(filenameInput)
clean = ""
for line in infile:
if re.match('PHOTO',line):
d("Found line starting with PHOTO. Skipping.")
continue;
if re.match('\s',line):
d("Found line starting with space. Skipping.")
continue;
clean += line
d("Writing " + filenameOutput)
outfile = open(filenameOutput,"w")
outfile.write(clean)
d("Done.")
sys.exit(0)