-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaddressbook.py
80 lines (70 loc) · 1.92 KB
/
addressbook.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
#!/usr/bin/env python
# encoding: utf-8
"""
addressbook.py
Python wrapper around Mac OSX Address Book.
"""
from AddressBook import *
import sys
import os
# List of properties to be ignored.
IGNORED_PROPS = ["com.apple.ABPersonMeProperty", "ABPersonFlags",
"com.apple.ABImageData",
"com.apple.ABGroupMembersProperty",
]
def getAddressBook():
"""
Returns the addressbook instance.
"""
return ABAddressBook.sharedAddressBook()
def all():
"""
Returns all the people from the addressbook.
"""
return _clist(getAddressBook().people())
def groups():
"""
Return groups
"""
return _clist(getAddressBook().groups())
def me():
"""
Returns the current logged in user.
"""
return _clist([getAddressBook().me()])[0]
def getByUID(uid):
"""
Returns a person or group by uid.
"""
return _clist([getAddressBook().recordForUniqueId_(uid)])[0]
def _clist(slist):
"""
Method to convert NSArray to python list
"""
retList = []
for p in slist:
aobj = {}
for prop in p.allProperties():
if prop in IGNORED_PROPS:
continue
tmpval = p.valueForProperty_(prop)
if type(tmpval) == ABMultiValueCoreDataWrapper:
aval = [_getVal(tmpval.valueAtIndex_(i)) for i in range(0, tmpval.count())]
else:
aval = _getVal(tmpval)
if aval is not None:
aobj[prop.lower()] = aval
retList.append(aobj)
return retList
def _getVal(tmpval):
"""
Extract value from unicode, Date or Dictionary object.
"""
aval = None
if type(tmpval) == objc.pyobjc_unicode:
aval = tmpval
elif issubclass(tmpval.__class__, NSDate):
aval = tmpval.description()
elif type(tmpval) == NSCFDictionary:
aval = dict([(k.lower(), tmpval[k]) for k in tmpval.keys()])
return aval