-
Notifications
You must be signed in to change notification settings - Fork 10
/
vcard_helper.py
493 lines (386 loc) · 18.3 KB
/
vcard_helper.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
# Copyright (c) 2018 Kannan Subramani <[email protected]>
# SPDX-License-Identifier: GPL-3.0
# -*- coding: utf-8 -*-
"""VCard helper module is used to parse vcard strings to dict(version independent) and vice versa"""
# pylint: disable=invalid-name
import codecs
import copy
import logging
logger = logging.getLogger(__name__)
class VCard(object):
"""Constructs the vcard objects and parses the vcard string"""
def __init__(self, vcard, parsed=False):
self.vcf_dict = vcard if parsed else self.parse(vcard)
def unfold(self, vcard):
"""Unfolds the folded lines of vcard"""
unfolded_vcard = []
vcard_properties = tuple(VCardProperties.keys())
for line in vcard.splitlines():
if line.startswith(vcard_properties) or line.startswith("X-"):
unfolded_vcard.append(line)
else:
unfolded_vcard[-1] += line
return unfolded_vcard
def serialize(self, version="2.1"):
"""Serializes the current vcard object into string"""
vcf_str = ""
for vcard_prop_dict in self.denormalize(self.to_dict(), version)["vcard"]:
vcard_property_class = VCardProperties[vcard_prop_dict["type"]]
vcf_str += vcard_property_class(vcard_prop_dict, parsed=True).serialize(version)
return vcf_str
def normalize(self, vcf_dict):
"""converts version dependent vcard dict to version independent one"""
# removes begin, version, end properties which will be added on denormalization
vcf_dict_copy = copy.deepcopy(vcf_dict)
vcf_dict_copy['vcard'].pop(0)
vcf_dict_copy['vcard'].pop(0)
vcf_dict_copy['vcard'].pop()
return vcf_dict_copy
def denormalize(self, vcf_dict, version="2.1"):
"""converts version independent vcard dict to version dependent one"""
vcf_dict_copy = copy.deepcopy(vcf_dict)
begin_dict = {'values': ['VCARD'], 'type': 'BEGIN', 'parameters': []}
version_dict = {'values': ['{version}'.format(version=version)], 'type': 'VERSION', 'parameters': []}
end_dict = {'values': ['VCARD'], 'type': 'END', 'parameters': []}
vcf_dict_copy['vcard'].insert(0, begin_dict)
vcf_dict_copy['vcard'].insert(1, version_dict)
vcf_dict_copy['vcard'].append(end_dict)
return vcf_dict_copy
def to_dict(self):
"""Converts the current vcard object into python dict"""
return self.vcf_dict
def from_dict(self, vcf_dict, normalized=False):
"""Constructs the vcard object using the vcf_dict"""
self.vcf_dict = vcf_dict if normalized else self.normalize(vcf_dict)
def _get_property_type(self, prop):
"""Extracts the type of property from property string"""
return prop.partition(":")[0].partition(";")[0]
def _split_into_properties(self, vcf_str):
"""Splits vcf string into list of property objects"""
prop_obj_list = []
prop_list = self.unfold(vcf_str)
for prop in prop_list:
prop_type = self._get_property_type(prop)
if prop_type not in VCardProperties:
logger.error("=" * 80)
logger.error("Unsupported Property type: %s", prop_type)
logger.error("=" * 80)
else:
prop_obj_list.append(VCardProperties[prop_type.upper()](prop))
return prop_obj_list
def parse(self, vcf_str):
"""Parse the vcf_str and constructs the vcard objects using that"""
if not vcf_str:
raise TypeError("Given input vcard string shouldn't be empty")
self.vcf_str = vcf_str
self.vcard_properties = self._split_into_properties(self.vcf_str)
vcf_dict = {"vcard": [prop.to_dict() for prop in self.vcard_properties]}
return self.normalize(vcf_dict)
class VCardProperty(object):
"""Represents property of VCard"""
STR_TEMPLATE = "{type}{sep}{param}:{value}\r\n"
def __init__(self, vcard, parsed=False):
self.vcf_dict = vcard if parsed else self.parse(vcard)
def serialize(self, version="2.1"):
"""Serializes the current vcard property into string"""
vcf_dict = self.denormalize(self.vcf_dict, version)
params = self._tuples_to_params(vcf_dict["parameters"])
vcf_result = self.STR_TEMPLATE.format(type=vcf_dict["type"],
sep=";" if params else "",
param=params,
value=";".join([item.encode('utf-8') for item in vcf_dict["values"]]))
return vcf_result
def to_dict(self):
"""Converts the current vcard property into python dict"""
return self.vcf_dict
def from_dict(self, vcf_dict, normalized=False):
"""Constructs the vcard property using the vcf_dict"""
self.vcf_dict = vcf_dict if normalized else self.normalize(vcf_dict)
def normalize(self, vcf_dict):
"""converts version dependent vcard dict to version independent one"""
return vcf_dict
def denormalize(self, vcf_dict, version="2.1"):
"""converts version independent vcard dict to version dependent one"""
return vcf_dict
def _params_to_tuple(self, params):
"""splits the params into list of tuples"""
param_list = []
if not params:
return param_list
for param in params.split(";"):
type_, _, value = param.rpartition("=")
param_list.append((type_, value))
return param_list
def _tuples_to_params(self, param_tuples):
"""constructs the params string using the list of param tuples"""
param_str = ""
for param in param_tuples:
key, value = param
if not key:
param_str += "{value};".format(value=value)
else:
param_str += "{key}={value};".format(key=key, value=value)
return param_str.rstrip(";")
def parse(self, vcf_str):
"""Parse the vcf_str and constructs the vcard property using that"""
if not vcf_str:
raise TypeError("Input vcard string is empty")
self.vcf_str = vcf_str
lhs, _, rhs = self.vcf_str.partition(":")
type_, _, params = lhs.partition(";")
vcf_dict = {
"type": type_,
"values": rhs.split(";"),
"parameters": self._params_to_tuple(params)
}
return self.normalize(vcf_dict)
class VCardProperty_CharsetEncodingParamNormalized(VCardProperty):
"""Overloaded VCardProperty which normalizes charset and encoding Params"""
def normalize(self, vcf_dict):
"""converts version dependent vcard dict to version independent one"""
vcf_dict = super(VCardProperty_CharsetEncodingParamNormalized, self).normalize(vcf_dict)
vcf_dict = copy.deepcopy(vcf_dict)
charset = None
encoding = None
for param in vcf_dict["parameters"][:]:
if param[0].upper() == "ENCODING":
encoding = param[1]
vcf_dict["parameters"].remove(param)
elif param[0].upper() == "CHARSET":
charset = param[1]
vcf_dict["parameters"].remove(param)
elif param[0].upper() == "" or param[0].upper() == "TYPE":
vcf_dict["parameters"].remove(param)
vcf_dict["parameters"].append(("TYPE", param[1]))
if encoding:
value, _ = codecs.lookup(encoding).decode(";".join(vcf_dict["values"]))
vcf_dict["values"] = value.split(";")
if charset:
value, _ = codecs.lookup(charset).decode(";".join(vcf_dict["values"]), errors="replace")
vcf_dict["values"] = value.split(";")
vcf_dict["values"] = ";".join(vcf_dict["values"]).encode("utf8").split(";")
return vcf_dict
def denormalize(self, vcf_dict, version="2.1"):
"""converts version independent vcard dict to version dependent one"""
vcf_dict = super(VCardProperty_CharsetEncodingParamNormalized, self).denormalize(vcf_dict, version)
vcf_dict = copy.deepcopy(vcf_dict)
encoding = "QUOTED-PRINTABLE"
charset = "UTF-8"
if version == "2.1":
# value is already in "UTF-8" charset so no need to re-encode
value, _ = codecs.lookup(encoding).encode(";".join(vcf_dict["values"]))
vcf_dict["values"] = value.split(";")
for param in vcf_dict["parameters"][:]:
if param[0].upper() == "TYPE":
vcf_dict["parameters"].remove(param)
vcf_dict["parameters"].append(("", param[1]))
vcf_dict["parameters"].append(("CHARSET", charset))
vcf_dict["parameters"].append(("ENCODING", encoding))
return vcf_dict
class VCardProperty_BASE64EncodingParamNormalized(VCardProperty):
"""Overloaded VCardProperty with Type and Encoding Params Normalized"""
def normalize(self, vcf_dict):
"""converts version dependent vcard dict to version independent one"""
vcf_dict = super(VCardProperty_BASE64EncodingParamNormalized, self).normalize(vcf_dict)
vcf_dict = copy.deepcopy(vcf_dict)
normalized_params = []
for param in vcf_dict["parameters"]:
if param[0].upper() == "ENCODING":
normalized_params.append(("ENCODING", "b")) # key property only have base64 encoding
elif param[0].upper() == "" or param[0].upper() == "TYPE":
normalized_params.append(("TYPE", param[1]))
else:
normalized_params.append(param)
vcf_dict["parameters"] = normalized_params
return vcf_dict
def denormalize(self, vcf_dict, version="2.1"):
"""converts version independent vcard dict to version dependent one"""
vcf_dict = super(VCardProperty_BASE64EncodingParamNormalized, self).denormalize(vcf_dict, version)
vcf_dict = copy.deepcopy(vcf_dict)
denormalized_params = []
for param in vcf_dict["parameters"]:
if param[0].upper() == "ENCODING":
if version == "2.1":
denormalized_params.append(("ENCODING", "BASE64")) # key property only have base64 encoding
elif version == "3.0":
denormalized_params.append(("ENCODING", "b")) # key property only have base64 encoding
else:
raise TypeError("Unsupported version")
elif param[0].upper() == "TYPE":
if version == "2.1":
denormalized_params.append(("", param[1]))
elif version == "3.0":
denormalized_params.append(("TYPE", param[1]))
else:
raise TypeError("Unsupported version")
else:
denormalized_params.append(param)
vcf_dict["parameters"] = denormalized_params
return vcf_dict
class Adr(VCardProperty_CharsetEncodingParamNormalized):
"""A structured representation of the physical delivery address for the vCard object."""
class Agent(VCardProperty):
"""Information about another person who will act on behalf of the vCard object.
Typically this would be an area administrator, assistant, or secretary for the individual.
Can be either a URL or an embedded vCard.
"""
class Begin(VCardProperty):
"""All vCards must start with this property."""
class End(VCardProperty):
"""All vCards must end with this property."""
class Version(VCardProperty):
"""The version of the vCard specification.
In versions 3.0 and 4.0, this must come right after the BEGIN property.
"""
class N(VCardProperty_CharsetEncodingParamNormalized):
"""A structured representation of the name of the person,
place or thing associated with the vCard object."""
class FN(VCardProperty_CharsetEncodingParamNormalized):
"""The formatted name string associated with the vCard object."""
class Birthday(VCardProperty):
"""Date of birth of the individual associated with the vCard."""
class Categories(VCardProperty):
"""A list of "tags" that can be used to describe the object represented by this vCard."""
class Class(VCardProperty):
"""Describes the sensitivity of the information in the vCard."""
class Email(VCardProperty_CharsetEncodingParamNormalized):
"""The address for electronic mail communication with the vCard object."""
class GEO(VCardProperty):
"""Specifies a latitude and longitude."""
class IMPP(VCardProperty_CharsetEncodingParamNormalized):
"""Defines an instant messenger handle."""
class Key(VCardProperty_BASE64EncodingParamNormalized):
"""The public encryption key associated with the vCard object.
It may point to an external URL, may be plain text, or
may be embedded in the vCard as a Base64 encoded block of text.
"""
class Label(VCardProperty_CharsetEncodingParamNormalized):
"""Represents the actual text that should be put on the mailing label when delivering a
physical package to the person/object associated with the vCard (related to the ADR property)
"""
class Logo(VCardProperty_BASE64EncodingParamNormalized):
"""An image or graphic of the logo of the organization that is associated with the individual
to which the vCard belongs. It may point to an external URL or
may be embedded in the vCard as a Base64 encoded block of text.
"""
class Mailer(VCardProperty_CharsetEncodingParamNormalized):
"""Type of email program used."""
class Name(VCardProperty):
"""Provides a textual representation of the SOURCE property."""
class Nickname(VCardProperty):
"""One or more descriptive/familiar names for the object represented by this vCard."""
class Note(VCardProperty):
"""Specifies supplemental information or a comment that is associated with the vCard."""
class Org(VCardProperty_CharsetEncodingParamNormalized):
"""The name and optionally the unit(s) of the organization associated with the vCard object.
This property is based on the X.520 Organization Name attribute and
the X.520 Organization Unit attribute."""
class Photo(VCardProperty_BASE64EncodingParamNormalized):
"""An image or photograph of the individual associated with the vCard.
It may point to an external URL or
may be embedded in the vCard as a Base64 encoded block of text.
"""
def denormalize(self, vcf_dict, version="2.1"):
vcf_dict = super(Photo, self).denormalize(vcf_dict, version)
if version == "2.1":
for param in vcf_dict["parameters"][:]:
if param[0].upper() == "VALUE":
vcf_dict["parameters"].remove(param)
elif not version == "3.0":
raise TypeError("unsupported version")
return vcf_dict
class ProdID(VCardProperty):
"""The identifier for the product that created the vCard object."""
class Profile(VCardProperty):
"""States that the vCard is a vCard."""
class Rev(VCardProperty):
"""A timestamp for the last time the vCard was updated."""
class Role(VCardProperty):
"""The role, occupation, or business category of the vCard object within an organization."""
class SortString(VCardProperty):
"""Defines a string that should be used when an application sorts this vCard in some way."""
class Sound(VCardProperty_BASE64EncodingParamNormalized):
"""By default, if this property is not grouped with other properties
it specifies the pronunciation of the FN property of the vCard object.
It may point to an external URL or may be embedded in the vCard as
a Base64 encoded block of text.
"""
class Source(VCardProperty):
"""A URL that can be used to get the latest version of this vCard."""
class Tel(VCardProperty_BASE64EncodingParamNormalized):
"""The canonical number string for a telephone number
for telephony communication with the vCard object.
"""
class Title(VCardProperty_CharsetEncodingParamNormalized):
"""Specifies the job title, functional position or function of the individual
associated with the vCard object within an organization.
"""
class TZ(VCardProperty):
"""The time zone of the vCard object."""
class UID(VCardProperty_CharsetEncodingParamNormalized):
"""Specifies a value that represents a persistent,
globally unique identifier associated with the object.
"""
class URL(VCardProperty_CharsetEncodingParamNormalized):
"""A URL pointing to a website that represents the person in some way."""
class X_IRMC_CALL_DATETIME(VCardProperty_CharsetEncodingParamNormalized):
"""Call History extension
The time of each call found in och, ich, mch and cch folder, can be shown using the
IrMC [13] defined X-IRMC-CALL-DATETIME property that extends the vCard
specification. This attribute can be used in combination with three newly created
property parameters:
- MISSED
- RECEIVED
- DIALED
These are used to indicate the nature of the call that is time-stamped with X-IRMCCALL-DATETIME.
For instance, a call that was missed on March 20th, 2005 at 10 am would be stamped:
X-IRMC-CALL-DATETIME;MISSED:20050320T100000
It is strongly recommended to use this property parameter whenever possible. They are
especially useful in vCards that are retrieved from the cch folder ( see 3.1.2 ).
Note that it is legal to use this property with no data ie,
X-IRMC-CALL-DATETIME;MISSED:
This scenario may occur if the device did not have the time/date set when the call was
received. The phone number would be recorded but no date/time could be attached to it.
It will still need to be added to the vCard as the cch log needs it to indicate the type of
call that the record identifies.
"""
VCardProperties = {
#############
"BEGIN": Begin,
"VERSION": Version,
"N": N,
"FN": FN,
"END": End,
#############
"ADR": Adr,
"AGENT": Agent,
"BDAY": Birthday,
"CATEGORIES": Categories,
"CLASS": Class,
"EMAIL": Email,
"GEO": GEO,
"IMPP": IMPP,
"KEY": Key,
"LABEL": Label,
"LOGO": Logo,
"MAILER": Mailer,
"NAME": Name,
"NICKNAME": Nickname,
"NOTE": Note,
"ORG": Org,
"PHOTO": Photo,
"PRODID": ProdID,
"PROFILE": Profile,
"REV": Rev,
"ROLE": Role,
"SORT-STRING": SortString,
"SOUND": Sound,
"SOURCE": Source,
"TEL": Tel,
"TITLE": Title,
"TZ": TZ,
"UID": UID,
"URL": URL,
##############
"X-IRMC-CALL-DATETIME": X_IRMC_CALL_DATETIME,
}