def import_data(importer, library_path_map, config_location=None, progress1=None, progress2=None, abort=None): from calibre.db.cache import import_library config_location = config_location or config_dir config_location = os.path.abspath(os.path.realpath(config_location)) total = len(library_path_map) + 1 library_usage_stats = Counter() for i, (library_key, dest) in enumerate(library_path_map.iteritems()): if abort is not None and abort.is_set(): return if progress1 is not None: progress1(dest, i, total) try: os.makedirs(dest) except EnvironmentError as err: if err.errno != errno.EEXIST: raise if not os.path.isdir(dest): raise ValueError('%s is not a directory' % dest) import_library(library_key, importer, dest, progress=progress2, abort=abort).close() library_usage_stats[dest] = importer.metadata['libraries'].get( library_key, 1) if progress1 is not None: progress1(_('Settings and plugins'), total - 1, total) if abort is not None and abort.is_set(): return base_dir = tempfile.mkdtemp(dir=os.path.dirname(config_location)) importer.export_config(base_dir, library_usage_stats) if os.path.lexists(config_location): if os.path.islink(config_location) or os.path.isfile(config_location): os.remove(config_location) else: shutil.rmtree(config_location, ignore_errors=True) if os.path.exists(config_location): try: shutil.rmtree(config_location) except EnvironmentError: if not iswindows: raise time.sleep(1) shutil.rmtree(config_location) try: os.rename(base_dir, config_location) except EnvironmentError: time.sleep(2) os.rename(base_dir, config_location) from calibre.gui2 import gprefs gprefs.refresh() if progress1 is not None: progress1(_('Completed'), total, total)
def import_data(importer, library_path_map, config_location=None, progress1=None, progress2=None, abort=None): from calibre.db.cache import import_library config_location = config_location or config_dir config_location = os.path.abspath(os.path.realpath(config_location)) total = len(library_path_map) + 1 library_usage_stats = Counter() for i, (library_key, dest) in enumerate(library_path_map.iteritems()): if abort is not None and abort.is_set(): return if progress1 is not None: progress1(dest, i, total) try: os.makedirs(dest) except EnvironmentError as err: if err.errno != errno.EEXIST: raise if not os.path.isdir(dest): raise ValueError('%s is not a directory' % dest) import_library(library_key, importer, dest, progress=progress2, abort=abort).close() library_usage_stats[dest] = importer.metadata['libraries'].get(library_key, 1) if progress1 is not None: progress1(_('Settings and plugins'), total - 1, total) if abort is not None and abort.is_set(): return base_dir = tempfile.mkdtemp(dir=os.path.dirname(config_location)) importer.export_config(base_dir, library_usage_stats) if os.path.lexists(config_location): if os.path.islink(config_location) or os.path.isfile(config_location): os.remove(config_location) else: shutil.rmtree(config_location, ignore_errors=True) if os.path.exists(config_location): try: shutil.rmtree(config_location) except EnvironmentError: if not iswindows: raise time.sleep(1) shutil.rmtree(config_location) try: os.rename(base_dir, config_location) except EnvironmentError: time.sleep(2) os.rename(base_dir, config_location) from calibre.gui2 import gprefs gprefs.refresh() if progress1 is not None: progress1(_('Completed'), total, total)