Example #1
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
Example #2
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
Example #3
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)
        failed = True

        for encoding in self.get_db().get_text_encodings():
            self.set_encoding(encoding)
            try:
                logging.info('Parsing BBDB Store with encoding %s...',
                             encoding)
                bbf, cnt = self.parse_with_encoding(def_f,
                                                    fn,
                                                    encoding=encoding)
                logging.info('Parsing BBDB Store with encoding %s...Success',
                             encoding)
                failed = False
                break
            except ASynKBBDBUnicodeError, e:
                ## Undo all state, and start afresh, pretty much.
                failed = True
                self.set_file_format(0)
                self.set_preamble('')
                self.set_folders({})
                def_f = BBContactsFolder(self.get_db(), def_fn, self)
                self.add_folder(def_f)
                logging.info('Parsing BBDB Store with encoding %s...Failed',
                             encoding)
Example #4
0
    def new_folder(self, fname, ftype=None, storeid=None):
        logging.debug('bb:new_folder(): fname: %s; ftype: %s', fname, ftype)
        if not ftype:
            ftype = Folder.CONTACT_t

        if ftype != Folder.CONTACT_t:
            logging.erorr('Only Contact Groups are supported at this time.')
            return None

        if storeid:
            ms = self.get_msgstore(storeid)
        else:
            ms = self.get_def_msgstore()

        f = BBContactsFolder(self, fname, ms)
        ms.add_folder(f)

        return f
Example #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."""

        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
Example #6
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()
Example #7
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