def prep_for_sync (self, dbid, pname, is_dry_run=False): if is_dry_run: ## No backup for Dry Run logging.info('BBDB database not backed up for dry run') return ## Make a backup of the BBDB store into the backup directory conf = self.get_config() db1 = conf.get_profile_db1(pname) bdir = utils.abs_pathname(conf, conf.get_backup_dir()) if not os.path.exists(bdir): logging.info('Creating backup directory at: %s', bdir) os.mkdir(bdir) stamp = string.replace(str(datetime.datetime.now()), ' ', '.') stamp = string.replace(stamp, ':', '-') backup_name = os.path.join(bdir, 'bbdb_backup.' + pname + '.' + stamp) if db1 == dbid: src = conf.get_stid2(pname) else: src = conf.get_stid1(pname) src = utils.abs_pathname(conf, src) logging.info('Backedup BBDB Store (%s) to file: %s', src, backup_name) shutil.copy2(src, backup_name)
def prep_for_sync(self, dbid, pname, is_dry_run=False): if is_dry_run: ## No backup for Dry Run logging.info("BBDB database not backed up for dry run") return ## Make a backup of the BBDB store into the backup directory conf = self.get_config() db1 = conf.get_profile_db1(pname) bdir = os.path.join(conf.get_user_dir(), conf.get_backup_dir()) if not os.path.exists(bdir): logging.info("Creating BBDB backup directory at: %s", bdir) os.mkdir(bdir) period = conf.get_backup_hold_period() logging.info("Deleting BBDB backup files older than %d days, " "if any...", period) utils.del_files_older_than(bdir, period) logging.info("Deleting BBDB backup files older than %d days, " "if any...done", period) stamp = string.replace(str(datetime.datetime.now()), " ", ".") stamp = string.replace(stamp, ":", "-") backup_name = os.path.join(bdir, "bbdb_backup." + pname + "." + stamp) if db1 == dbid: src = conf.get_stid2(pname) else: src = conf.get_stid1(pname) src = utils.abs_pathname(conf, src) logging.info("Backedup BBDB Store (%s) to file: %s", src, backup_name) shutil.copy2(src, backup_name)
def save_file (self, fn=None): if not fn: fn = self.get_name() fn = utils.abs_pathname(self.get_config(), fn) logging.info('Saving BBDB File %s...', fn) with codecs.open(fn, 'w', encoding=self.get_encoding()) as bbf: bbf.write(self.get_preamble()) for name, f in self.get_folders().iteritems(): f.write_to_file(bbf)
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)
def prep_for_sync(self, dbid, pname, is_dry_run=False): if is_dry_run: ## No backup for Dry Run logging.info('BBDB database not backed up for dry run') return ## Make a backup of the BBDB store into the backup directory conf = self.get_config() db1 = conf.get_profile_db1(pname) bdir = utils.abs_pathname(conf, conf.get_backup_dir()) if not os.path.exists(bdir): logging.info('Creating BBDB backup directory at: %s', bdir) os.mkdir(bdir) period = conf.get_backup_hold_period() logging.info( 'Deleting BBDB backup files older than %d days, ' 'if any...', period) utils.del_files_older_than(bdir, period) logging.info( 'Deleting BBDB backup files older than %d days, ' 'if any...done', period) stamp = string.replace(str(datetime.datetime.now()), ' ', '.') stamp = string.replace(stamp, ':', '-') backup_name = os.path.join(bdir, 'bbdb_backup.' + pname + '.' + stamp) if db1 == dbid: src = conf.get_stid2(pname) else: src = conf.get_stid1(pname) src = utils.abs_pathname(conf, src) logging.info('Backedup BBDB Store (%s) to file: %s', src, backup_name) shutil.copy2(src, backup_name)
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)
def save_file (self, fn=None): if not fn: fn = self.get_name() fn = utils.abs_pathname(self.get_config(), fn) logging.info('Saving BBDB File %s...', fn) with codecs.open(fn, 'w', encoding='utf-8') as bbf: bbf.write(';; -*-coding: utf-8-emacs;-*-\n') bbf.write(';;; file-format: 7\n') for name, f in self.get_folders().iteritems(): f.write_to_file(bbf) bbf.close()
def create_backup(self, pname): """Make a backup of the BBDB store into the backup directory""" conf = self.get_config() bdir = os.path.join(conf.get_user_dir(), conf.get_backup_dir()) stamp = string.replace(str(datetime.datetime.now()), ' ', '.') stamp = string.replace(stamp, ':', '-') backup_name = os.path.join(bdir, 'bbdb_backup.' + pname + '.' + stamp) src = self.get_name() src = utils.abs_pathname(self.get_config(), src) logging.info('Backedup BBDB Store (%s) to file: %s', src, backup_name) shutil.copy2(src, backup_name) self.set_last_backup_name(backup_name)
def create_backup (self, pname): """Make a backup of the BBDB store into the backup directory""" conf = self.get_config() bdir = os.path.join(conf.get_user_dir(), conf.get_backup_dir()) stamp = string.replace(str(datetime.datetime.now()), ' ', '.') stamp = string.replace(stamp, ':', '-') backup_name = os.path.join(bdir, 'bbdb_backup.' + pname + '.' + stamp) src = self.get_name() src = utils.abs_pathname(self.get_config(), src) logging.info('Backedup BBDB Store (%s) to file: %s', src, backup_name) shutil.copy2(src, backup_name) self.set_last_backup_name(backup_name)
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()