def batch_create(self, src_sl, src_dbid, items, op='create'): """See the documentation in folder.Folder""" my_dbid = self.get_dbid() c = self.get_config() pname = src_sl.get_pname() src_tag = c.make_sync_label(pname, src_dbid) dst_tag = c.make_sync_label(pname, my_dbid) if len(items) > 0: self.set_dirty() for item in items: try: bbc = BBContact(self, con=item) bbc.update_sync_tags(src_tag, item.get_itemid()) bbc.set_updated(pimdb_bb.BBPIMDB.get_bbdb_time()) self.add_contact(bbc) item.update_sync_tags(dst_tag, bbc.get_itemid()) logging.info('Successfully %sd BBDB entry for %30s (%s)', op, bbc.get_name(), bbc.get_itemid()) except BBDBParseError, e: logging.error('Could not instantiate BBDBContact object: %s', str(e))
def test_quotes_terminating_backslash(self): con = BBContact(self.deff) con.set_firstname('Test Quotes TB') con.set_lastname('Karra\\') self.deff.add_contact(con) self.deff.save() self.reparse(self.bbdbfn)
def test_quotes_4(self): con = BBContact(self.deff) con.set_firstname('Test Quotes 4') con.set_lastname("\\Karra") self.deff.add_contact(con) self.deff.save() self.reparse(self.bbdbfn)
def test_quotes_1(self): con = BBContact(self.deff) con.set_firstname(r'Test Quotes 1') con.set_lastname(r'"Karra"') self.deff.add_contact(con) self.deff.save() self.reparse(self.bbdbfn)
def test_unicode(self): con = BBContact(self.deff) con.set_firstname(u'Héctor') con.set_lastname(r'Tarrido-Picart') self.deff.add_contact(con) self.deff.save() self.reparse(self.bbdbfn)
def parse_with_encoding(self, def_f, fn, encoding): """Folder object to which the parsed contacts will be added. fn is the name of the BBDB file/message store. encoding is a string representing a text encoding such as utf-8, latin-1, etc.""" if not os.path.exists(fn): utils.touch(fn) with codecs.open(fn, encoding=encoding) as bbf: ver = self._parse_preamble(fn, bbf) if not ver: ## We encountered a blank BBDB file. ver = self._set_default_preamble() ## Now fetch and set up the parsing routines specific to the file ## format self._set_regexes(ver) cnt = 0 while True: try: ff = bbf.readline().strip() except UnicodeDecodeError, e: ## We got the encoding wrong. We will have to drop ## everything we have done, and start all over again. At ## a later stage, we could optimize by skipping over ## whatever we have read so far, but then we will need to ## evalute if the parsed strings will be in the same ## encoding or not. Tricky and shady business, this. raise ASynKBBDBUnicodeError('') if re.search('^\s*$', ff): break if re.search('^;', ff): self.append_preamble(ff + "\n") continue try: c = BBContact(def_f, rec=ff.rstrip()) except BBDBParseError, e: logging.error('Could not parse BBDB record: %s', ff) raise BBDBFileFormatError(('Cannot proceed with ' 'processing file "%s" ') % fn) fon = c.get_bbdb_folder() if fon: f = self.get_folder(fon) if not f: f = BBContactsFolder(self.get_db(), fon, self) self.add_folder(f) f.add_contact(c) else: def_f.add_contact(c) cnt += 1
def test_basic(self): con = BBContact(self.deff) con.set_firstname( 'Test Basic Sri Venkata Sri Rama Subramanya Anjeneya Annapurna Sharma' ) con.set_prefix('Mr.') con.set_nickname('Karra') con.set_gender('Male') con.add_phone_mob(('Mobile', '+91 90084 88997')) con.add_notes('And so it goes...') self.deff.add_contact(con) self.deff.save() self.reparse(self.bbdbfn) cons = self.deff.find_contacts_by_name(name='Venkata') assert (len(cons) == 1) con = cons[0] assert (con.get_prefix() == "Mr.") assert (con.get_gender() == "Male")
def parse_with_encoding(self, def_f, fn, encoding): """Folder object to which the parsed contacts will be added. fn is the name of the BBDB file/message store. encoding is a string representing a text encoding such as utf-8, latin-1, etc.""" with codecs.open(fn, encoding=encoding) as bbf: ff = bbf.readline() if re.search('coding:', ff): # Ignore first line if such: ;; -*-coding: utf-8-emacs;-*- self.append_preamble(ff) ff = bbf.readline() # Processing: ;;; file-format: 8 res = re.search(';;; file-(format|version):\s*(\d+)', ff) if not res: bbf.close() raise BBDBFileFormatError('Unrecognizable format line: %s' % ff) self.append_preamble(ff) ver = res.group(2) self.set_file_format(ver) supported = self.get_db().supported_file_formats() if not ver in supported: bbf.close() raise BBDBFileFormatError(('Cannot process file "%s" ' '(version %s). Supported versions ' 'are: %s' % (fn, ver, supported))) ## Now fetch and set up the parsign routines specific to the file ## format self._set_regexes(ver) cnt = 0 while True: try: ff = bbf.readline() except UnicodeDecodeError, e: ## We got the encoding wrong. We will have to drop ## everything we have done, and start all over again. ## At a later stage, we could optimize by skipping over ## whatever we have read so far, but then we will need to ## evalute if the parsed strings will be in the same ## encoding or not. Tricky and shady business, this. raise ASynKBBDBUnicodeError('') if ff == '': break if re.search('^;', ff): self.append_preamble(ff) continue try: c = BBContact(def_f, rec=ff.rstrip()) except BBDBParseError, e: logging.error( 'Could not instantiate BBDBContact object: %s', str(e)) continue fn = c.get_bbdb_folder() if fn: f = self.get_folder(fn) if not f: f = BBContactsFolder(self.get_db(), fn, self) self.add_folder(f) f.add_contact(c) else: def_f.add_contact(c) # self.add_contact(c) cnt += 1