📄 contacts.py
字号:
self.db=db
self._iter=_iter
def next(self):
return self.db[self._iter.next()]
def __iter__(self):
return self
class Contact(object):
def __init__(self,_contact,db,locked=0):
self._contact=_contact
self.db=db
self._locked=locked
def _data(self,key):
return self._contact.entry_data()[key]
id=property(lambda self:self._data('uniqueid'))
last_modified=property(lambda self:self._data('lastmodified'))
title=property(lambda self:self._data('title'))
def add_field(self,type,value=None,location=None,label=None):
fieldtype=nativefieldtype_from_pythontype_and_location(type,location)
kw={}
if value is not None:
if fieldtype[0] != _contacts.date:
kw['value']=unicode(value)
else:
kw['value']=value
if label is not None: kw['label']=unicode(label)
if not self._locked:
self._begin()
self._contact.add_field(fieldtype,**kw)
if not self._locked:
self._contact.commit()
def __len__(self):
return len(self._contact)
def __getitem__(self,key):
#print "getitem",self,index
if isinstance(key,int):
if key >= len(self._contact):
raise IndexError
return ContactsDb.ContactField(self,key)
raise TypeError('field indices must be integers')
def __delitem__(self,index):
self[index] # Check validity of index
### NOTE: After this all ContactFields after this will have incorrect indices!
if not self._locked:
self._begin()
del self._contact[index]
if not self._locked:
self._contact.commit()
def find(self,type=None,location=None):
if type:
if type == 'phone_number':
if not location:
return (self.find(type,'none')+
self.find(type,'home')+
self.find(type,'work'))
typecode=_phonenumber_location_map[location]
else:
typecode=fieldtypemap[type]
return [ContactsDb.ContactField(self,x)
for x in self._contact.find_field_indexes(typecode,locationmap[location])]
else:
if location: # this is slow, but this should be a rare case
return [x for x in self if x.location==location]
else: # no search terms, return all fields
return list(self)
def keys(self):
return [x['fieldindex'] for x in self._contact]
def __str__(self):
return '<Contact #%d: "%s">'%(self.id,self.title)
__repr__=__str__
def _set(self,index,value=None,label=None):
if not self._locked:
self._begin()
kw={}
if value is not None: kw['value']=unicode(value)
if label is not None: kw['label']=unicode(label)
self._contact.modify_field(index,**kw)
if not self._locked:
self._contact.commit()
def _begin(self):
try:
self._contact.begin()
except SymbianError:
raise ContactBusy
def begin(self):
if self._locked:
raise RuntimeError('contact already open')
self._begin()
self._locked=1
def commit(self):
if not self._locked:
raise RuntimeError('contact not open')
self._contact.commit()
self._locked=0
def rollback(self):
if not self._locked:
raise RuntimeError('contact not open')
if self._locked == 'as_new_contact':
# clear the content of new uncommited _contact by creating a new _contact.
self._contact=self.db._db.add_contact()
else:
# clear the content of old committed _contact by fetching the last committed data from the database.
self._contact.rollback()
self._locked=0
def __del__(self):
if self._locked:
import warnings
warnings.warn("contact still locked in destructor", RuntimeWarning)
def as_vcard(self):
return self.db.export_vcards((self.id,))
def _is_group(self):
return self._contact.is_contact_group()
is_group = property(_is_group)
class ContactField(object):
def __init__(self,contact,index):
#print "Create field",contact,index
self.contact=contact
self.index=index
schema=property(lambda self: self.contact.db.field_schema(self.contact._contact.field_info_index(self.index)))
type=property(lambda self:_pythontype_from_nativefieldtype_map[self.contact._contact[self.index]['fieldid']])
label=property(lambda self: self.contact._contact[self.index]['label'],
lambda self,x: self.contact._set(self.index,label=x))
value=property(lambda self: self.contact._contact[self.index]['value'],
lambda self,x: self.contact._set(self.index,value=x))
location=property(lambda self:self.schema['location'])
def __str__(self):
return '<field #%d of %s: type=%s value=%s location=%s label=%s>'%(self.index,
self.contact,
self.type,
self.value,
self.location,
self.label)
__repr__=__str__
# Group handling
class Groups(object):
def __init__(self,db):
self._db=db
def __getitem__(self, group_id):
return ContactsDb.Group(group_id,self._db)
def add_group(self,name=None):
grp = ContactsDb.Group(self._db._db.create_contact_group(),self._db)
if name is not None:
grp.name=name
return grp
def __delitem__(self, group_id):
if self._db[group_id].is_group == 0:
raise RuntimeError('not a group')
del self._db[group_id]
def __iter__(self):
return iter(self._db._db.contact_groups())
def __len__(self):
return self._db._db.contact_group_count()
class Group(object):
def __init__(self,group_id,db):
self._group_id=group_id
self._db=db
def __iter__(self):
return iter(self._db._db.contact_group_ids(self._group_id))
def __getitem__(self,index):
return self._db._db.contact_group_ids(self._group_id)[index]
def append(self,contact_id):
self._db._db.add_contact_to_group(contact_id,self._group_id)
def __delitem__(self,index):
self._db._db.remove_contact_from_group(self[index],self._group_id)
def __len__(self):
return len(self._db._db.contact_group_ids(self._group_id))
def _get_id(self):
return self._group_id
id=property(_get_id)
def _set_name(self,newname):
self._db._db.contact_group_set_label(self._group_id,unicode(newname))
def _get_name(self):
return self._db._db.contact_group_label(self._group_id)
name=property(_get_name,_set_name)
groups=property(lambda self: ContactsDb.Groups(self))
def open(dbfile=None,mode=None):
return ContactsDb(dbfile,mode)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -