Esempio n. 1
0
    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)
Esempio n. 2
0
    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)
Esempio n. 3
0
    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)
Esempio n. 4
0
    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)
Esempio n. 5
0
    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
Esempio n. 6
0
    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
Esempio n. 7
0
    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))
Esempio n. 8
0
    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))
Esempio n. 9
0
    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)
Esempio n. 10
0
    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)
Esempio n. 11
0
    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)
Esempio n. 12
0
    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)
Esempio n. 13
0
    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
Esempio n. 14
0
    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
Esempio n. 15
0
    def populate_folders (self, fn=None):
        """Parse a BBDB file contents, and create folders of contacts."""

        ## BBDB itself is not structured as logical folders. The concept of a
        ## BBDB folder is overlayed by ASynK. Any contact with a notes field
        ## with key called 'folder' (or as configured in config.json), is
        ## assigned to a folder of that name. If an object does not have a
        ## folder note, it is assgined to the default folder.

        ## This routine parses the BBDB file by reading one line at at time
        ## from top to bottom. Due to a limitation in how the Contact() and
        ## Folder() classes interact, we have to pass a valid Folder object to
        ## the Contact() constructor. So the way we do this is we start by
        ## assuming the contact is in the default folder. After successful
        ## parsing, if the folder name is available in the contact, we will
        ## move it from the dfault folder to that particular folder.

        if not fn:
            fn = self.get_name()

        fn = utils.abs_pathname(self.get_config(), fn)
        logging.info('Parsing BBDB file %s...', fn)

        def_fn = self.get_def_folder_name()
        def_f = BBContactsFolder(self.get_db(), def_fn, self)
        self.add_folder(def_f)

        with codecs.open(fn, encoding='utf-8') as bbf:
            ff = bbf.readline()
            if re.search('coding:', ff):
                # Ignore first line if it is: ;; -*-coding: utf-8-emacs;-*-
                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)

            ver = int(res.group(2))
            self.set_file_format(ver)

            if ver < 7:
                bbf.close()
                raise BBDBFileFormatError(('Need minimum file format ver 7. ' +
                                          '. File version is: %d' ) % ver)

            cnt = 0
            while True:
                ff = bbf.readline()
                if ff == '':
                    break

                if re.search('^;', ff):
                    continue

                c  = BBContact(def_f, rec=ff.rstrip())
                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

        logging.info('Successfully parsed %d entries.', cnt)
        bbf.close()
Esempio n. 16
0
    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")
Esempio n. 17
0
    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")