Example #1
0
 def doit(self):
     from calibre.db.legacy import LibraryDatabase
     newdb = LibraryDatabase(self.loc, is_second_db=True)
     with closing(newdb):
         self._doit(newdb)
     newdb.break_cycles()
     del newdb
Example #2
0
 def doit(self):
     from calibre.db.legacy import LibraryDatabase
     newdb = LibraryDatabase(self.loc, is_second_db=True)
     with closing(newdb):
         if self.check_for_duplicates:
             self.find_identical_books_data = newdb.new_api.data_for_find_identical_books()
         self._doit(newdb)
     newdb.break_cycles()
     del newdb
Example #3
0
 def doit(self):
     from calibre.db.legacy import LibraryDatabase
     newdb = LibraryDatabase(self.loc, is_second_db=True)
     with closing(newdb):
         if self.check_for_duplicates:
             self.find_identical_books_data = newdb.new_api.data_for_find_identical_books(
             )
         self._doit(newdb)
     newdb.break_cycles()
     del newdb
Example #4
0
def lc_cli_add_custom_column(guidb, param, dbpath):

    #~ param = --is-multiple + '|||' + label + '|||' + name + '|||' +  datatype
    #~ param = label + '|||' + name + '|||' +  datatype

    if "is-multiple" in param:
        is_multiple = True
        param = param.replace("--is-multiple|||", "")
    else:
        is_multiple = False

    if DEBUG: print("final param:", param)

    param = param.strip()
    args = param.split("|||")
    args_list = []
    for row in args:
        s = row.strip()
        if s > " ":
            s = s.replace('"', '')
            args_list.append(s)
            if DEBUG: print("arg: ", as_unicode(s))
    #END FOR

    label = args_list[0]
    name = args_list[1]
    datatype = args_list[2]

    display = {}

    db = LibraryDatabase(dbpath)

    db.create_custom_column(label,
                            name,
                            datatype,
                            is_multiple,
                            display=display)

    if DEBUG:
        print("Custom column created:  ", label, "  ", name, "  ", datatype)

    # Re-open the DB so that  field_metadata reflects the column changes
    guidb.prefs['field_metadata'] = db.field_metadata.all_metadata()

    #~ if DEBUG:
    #~ s = guidb.prefs['field_metadata']
    #~ print("new guidb field_metadata: ", as_unicode(s))

    send_rc_message('')
Example #5
0
def make_app():
    init_calibre()

    import handlers
    from calibre.db.legacy import LibraryDatabase
    from calibre.utils.date import fromtimestamp

    auth_db_path = CONF['user_database']
    logging.info("Init library with [%s]" % options.with_library)
    logging.info("Init AuthDB  with [%s]" % auth_db_path)
    logging.info("Init Static  with [%s]" % CONF['static_path'])
    logging.info("Init HTML    with [%s]" % CONF['html_path'])
    book_db = LibraryDatabase(os.path.expanduser(options.with_library))
    cache = book_db.new_api

    # hook 1: 按字母作为第一级目录,解决书库子目录太多的问题
    if CONF['BOOK_NAMES_FORMAT'].lower() == 'utf8':
        bind_utf8_book_names(cache)
    else:
        bind_topdir_book_names(cache)

    # hook 2: don't force GUI
    from calibre import gui2
    old_must_use_qt = gui2.must_use_qt

    def new_must_use_qt(headless=True):
        try:
            old_must_use_qt(headless)
        except:
            pass

    gui2.must_use_qt = new_must_use_qt

    # build sql session factory
    engine = create_engine(auth_db_path, echo=False)
    ScopedSession = scoped_session(
        sessionmaker(bind=engine, autoflush=True, autocommit=False))
    models.bind_session(ScopedSession)
    init_social(models.Base, ScopedSession, CONF)

    if options.syncdb:
        models.user_syncdb(engine)
        logging.info("Create tables into DB")
        sys.exit(0)

    path = CONF['static_path'] + '/calibre/default_cover.jpg'
    with open(path, 'rb') as cover_file:
        default_cover = cover_file.read()
    app_settings = dict(CONF)
    app_settings.update({
        "legacy": book_db,
        "cache": cache,
        "ScopedSession": ScopedSession,
        "build_time": fromtimestamp(os.stat(path).st_mtime),
        "default_cover": default_cover,
    })

    logging.info("Now, Running...")
    return web.Application(
        social_routes.SOCIAL_AUTH_ROUTES + handlers.routes(), **app_settings)
Example #6
0
def main(opts, args, dbctx):
    if len(args) < 1:
        raise SystemExit(
            _('Error: You must specify the path to the cloned library'))
    prefs, library_path = dbctx.run('clone')
    loc = os.path.abspath(args[0])
    if not os.path.exists(loc):
        os.makedirs(loc)

    if patheq(loc, library_path):
        raise SystemExit(
            _('The location for the new library is the same as the current library'
              ))
    empty = not os.listdir(loc)
    if not empty:
        raise SystemExit(
            _('%s is not empty. You must choose an empty directory for the new library.'
              ) % loc)
    if iswindows and len(loc) > LibraryDatabase.WINDOWS_LIBRARY_PATH_LIMIT:
        raise SystemExit(
            _('Path to library too long. Must be less than'
              ' %d characters.') % LibraryDatabase.WINDOWS_LIBRARY_PATH_LIMIT)
    LibraryDatabase(loc, default_prefs=prefs)

    return 0
Example #7
0
    def initialize_db_stage2(self, db, tb):
        from calibre.db.legacy import LibraryDatabase

        if db is None and tb is not None:
            # DB Repair failed
            self.show_error(_('Repairing failed'), _(
                'The database repair failed. Starting with a new empty library.'),
                            det_msg=tb)
        if db is None:
            candidate = self.choose_dir(get_default_library_path())
            if not candidate:
                self.initialization_failed()

            try:
                self.library_path = candidate
                db = LibraryDatabase(candidate)
            except:
                self.show_error(_('Bad database location'), _(
                    'Bad database location %r. calibre will now quit.')%self.library_path,
                    det_msg=traceback.format_exc())
                self.initialization_failed()

        try:
            self.start_gui(db)
        except Exception:
            self.show_error(_('Startup error'), _(
                'There was an error during {0} startup. Parts of {0} may not function.'
                ' Click Show details to learn more.').format(__appname__),
                         det_msg=traceback.format_exc())
Example #8
0
 def __init__(self, libraries):
     self.lock = Lock()
     self.lmap = OrderedDict()
     self.library_name_map = {}
     self.original_path_map = {}
     seen = set()
     for original_path in libraries:
         path = canonicalize_path(original_path)
         if path in seen:
             continue
         is_samefile = False
         for s in seen:
             if samefile(s, path):
                 is_samefile = True
                 break
         seen.add(path)
         if is_samefile or not LibraryDatabase.exists_at(path):
             continue
         library_id = library_id_from_path(original_path, self.lmap)
         self.lmap[library_id] = path
         self.library_name_map[library_id] = basename(original_path)
         self.original_path_map[path] = original_path
     self.loaded_dbs = {}
     self.category_caches, self.search_caches, self.tag_browser_caches = (
         defaultdict(OrderedDict), defaultdict(OrderedDict),
         defaultdict(OrderedDict))
Example #9
0
def main(args=sys.argv):
    opts, args = create_option_parser().parse_args(args)
    if opts.auto_reload:
        if getattr(opts, 'daemonize', False):
            raise SystemExit(
                'Cannot specify --auto-reload and --daemonize at the same time')
        from calibre.srv.auto_reload import auto_reload, NoAutoReload
        try:
            from calibre.utils.logging import default_log
            return auto_reload(default_log, listen_on=opts.listen_on)
        except NoAutoReload as e:
            raise SystemExit(error_message(e))

    ensure_single_instance()
    if opts.userdb:
        opts.userdb = os.path.abspath(os.path.expandvars(os.path.expanduser(opts.userdb)))
        connect(opts.userdb, exc_class=SystemExit).close()
    if opts.manage_users:
        try:
            manage_users_cli(opts.userdb)
        except (KeyboardInterrupt, EOFError):
            raise SystemExit(_('Interrupted by user'))
        raise SystemExit(0)

    libraries = args[1:]
    for lib in libraries:
        if not lib or not LibraryDatabase.exists_at(lib):
            raise SystemExit(_('There is no calibre library at: %s') % lib)
    libraries = libraries or load_gui_libraries()
    if not libraries:
        if not prefs['library_path']:
            raise SystemExit(_('You must specify at least one calibre library'))
        libraries = [prefs['library_path']]

    opts.auto_reload_port = int(os.environ.get('CALIBRE_AUTORELOAD_PORT', 0))
    opts.allow_console_print = 'CALIBRE_ALLOW_CONSOLE_PRINT' in os.environ
    if opts.log and os.path.isdir(opts.log):
        raise SystemExit('The --log option must point to a file, not a directory')
    if opts.access_log and os.path.isdir(opts.access_log):
        raise SystemExit('The --access-log option must point to a file, not a directory')
    server = Server(libraries, opts)
    if getattr(opts, 'daemonize', False):
        if not opts.log and not iswindows:
            raise SystemExit(
                'In order to daemonize you must specify a log file, you can use /dev/stdout to log to screen even as a daemon'
            )
        daemonize()
    if opts.pidfile:
        with lopen(opts.pidfile, 'wb') as f:
            f.write(unicode_type(os.getpid()).encode('ascii'))
    signal.signal(signal.SIGTERM, lambda s, f: server.stop())
    if not getattr(opts, 'daemonize', False) and not iswindows:
        signal.signal(signal.SIGHUP, lambda s, f: server.stop())
    # Needed for dynamic cover generation, which uses Qt for drawing
    from calibre.gui2 import ensure_app, load_builtin_fonts
    ensure_app(), load_builtin_fonts()
    try:
        server.serve_forever()
    finally:
        shutdown_delete_service()
Example #10
0
 def __init__(self, libraries):
     self.lock = Lock()
     self.lmap = OrderedDict()
     self.library_name_map = {}
     self.original_path_map = {}
     seen = set()
     for original_path in libraries:
         path = canonicalize_path(original_path)
         if path in seen:
             continue
         is_samefile = False
         for s in seen:
             if samefile(s, path):
                 is_samefile = True
                 break
         seen.add(path)
         if is_samefile or not LibraryDatabase.exists_at(path):
             continue
         corrected_path = correct_case_of_last_path_component(original_path)
         library_id = library_id_from_path(corrected_path, self.lmap)
         self.lmap[library_id] = path
         self.library_name_map[library_id] = basename(corrected_path)
         self.original_path_map[path] = original_path
     self.loaded_dbs = {}
     self.category_caches, self.search_caches, self.tag_browser_caches = (
         defaultdict(OrderedDict), defaultdict(OrderedDict),
         defaultdict(OrderedDict))
Example #11
0
def main(args=sys.argv):
    opts, args = create_option_parser().parse_args(args)
    libraries = args[1:]
    for lib in libraries:
        if not lib or not LibraryDatabase.exists_at(lib):
            raise SystemExit(_('There is no calibre library at: %s') % lib)
    if not libraries:
        if not prefs['library_path']:
            raise SystemExit(_('You must specify at least one calibre library'))
        libraries = [prefs['library_path']]

    if opts.auto_reload:
        if opts.daemonize:
            raise SystemExit('Cannot specify --auto-reload and --daemonize at the same time')
        from calibre.srv.auto_reload import auto_reload, NoAutoReload
        try:
            from calibre.utils.logging import default_log
            return auto_reload(default_log)
        except NoAutoReload as e:
            raise SystemExit(e.message)
    server = Server(libraries, opts)
    if opts.daemonize:
        if not opts.log and not iswindows:
            raise SystemExit('In order to daemonize you must specify a log file, you can use /dev/stdout to log to screen even as a daemon')
        daemonize()
    if opts.pidfile:
        with lopen(opts.pidfile, 'wb') as f:
            f.write(str(os.getpid()))
    signal.signal(signal.SIGTERM, lambda s,f: server.stop())
    if not opts.daemonize and not iswindows:
        signal.signal(signal.SIGHUP, lambda s,f: server.stop())
    server.serve_forever()
Example #12
0
def main(args=sys.argv):
    opts, args = create_option_parser().parse_args(args)
    ensure_single_instance()
    if opts.userdb:
        opts.userdb = os.path.abspath(os.path.expandvars(os.path.expanduser(opts.userdb)))
        connect(opts.userdb, exc_class=SystemExit).close()
    if opts.manage_users:
        try:
            manage_users_cli(opts.userdb)
        except (KeyboardInterrupt, EOFError):
            raise SystemExit(_('Interrupted by user'))
        raise SystemExit(0)

    libraries = args[1:]
    for lib in libraries:
        if not lib or not LibraryDatabase.exists_at(lib):
            raise SystemExit(_('There is no calibre library at: %s') % lib)
    libraries = libraries or load_gui_libraries()
    if not libraries:
        if not prefs['library_path']:
            raise SystemExit(_('You must specify at least one calibre library'))
        libraries = [prefs['library_path']]

    if opts.auto_reload:
        if getattr(opts, 'daemonize', False):
            raise SystemExit(
                'Cannot specify --auto-reload and --daemonize at the same time')
        from calibre.srv.auto_reload import auto_reload, NoAutoReload
        try:
            from calibre.utils.logging import default_log
            return auto_reload(default_log, listen_on=opts.listen_on)
        except NoAutoReload as e:
            raise SystemExit(e.message)
    opts.auto_reload_port = int(os.environ.get('CALIBRE_AUTORELOAD_PORT', 0))
    opts.allow_console_print = 'CALIBRE_ALLOW_CONSOLE_PRINT' in os.environ
    if opts.log and os.path.isdir(opts.log):
        raise SystemExit('The --log option must point to a file, not a directory')
    if opts.access_log and os.path.isdir(opts.access_log):
        raise SystemExit('The --access-log option must point to a file, not a directory')
    server = Server(libraries, opts)
    if getattr(opts, 'daemonize', False):
        if not opts.log and not iswindows:
            raise SystemExit(
                'In order to daemonize you must specify a log file, you can use /dev/stdout to log to screen even as a daemon'
            )
        daemonize()
    if opts.pidfile:
        with lopen(opts.pidfile, 'wb') as f:
            f.write(str(os.getpid()))
    signal.signal(signal.SIGTERM, lambda s, f: server.stop())
    if not getattr(opts, 'daemonize', False) and not iswindows:
        signal.signal(signal.SIGHUP, lambda s, f: server.stop())
    # Needed for dynamic cover generation, which uses Qt for drawing
    from calibre.gui2 import ensure_app, load_builtin_fonts
    ensure_app(), load_builtin_fonts()
    try:
        server.serve_forever()
    finally:
        shutdown_delete_service()
Example #13
0
def move_library(oldloc, newloc, parent, callback_on_complete):
    from calibre.db.legacy import LibraryDatabase
    callback = Callback(callback_on_complete)
    try:
        if not os.path.exists(os.path.join(newloc, 'metadata.db')):
            if oldloc and os.access(os.path.join(oldloc, 'metadata.db'), os.R_OK):
                # Move old library to new location
                try:
                    db = LibraryDatabase(oldloc)
                except:
                    return move_library(None, newloc, parent,
                        callback)
                else:
                    rq = Queue()
                    m = MoveLibrary(oldloc, newloc,
                            len(db.get_top_level_move_items()[0]), rq)
                    global _mm
                    _mm = MoveMonitor(m, rq, callback, parent)
                    return
            else:
                # Create new library at new location
                db = LibraryDatabase(newloc)
                callback(newloc)
                return

        # Try to load existing library at new location
        try:
            LibraryDatabase(newloc)
        except Exception as err:
            det = traceback.format_exc()
            error_dialog(parent, _('Invalid database'),
                _('<p>An invalid library already exists at '
                    '%(loc)s, delete it before trying to move the '
                    'existing library.<br>Error: %(err)s')%dict(loc=newloc,
                        err=str(err)), det, show=True)
            callback(None)
            return
        else:
            callback(newloc)
            return
    except Exception as err:
        det = traceback.format_exc()
        error_dialog(parent, _('Could not move library'),
                unicode(err), det, show=True)
    callback(None)
def main(opts, args, dbctx):
    if len(args) < 3:
        raise SystemExit(_('You must specify label, name and datatype'))
    do_add_custom_column(dbctx.db, args[0], args[1], args[2], opts.is_multiple,
                         json.loads(opts.display))
    # Update the stored field_metadata
    dbctx.db.close()
    db = LibraryDatabase(dbctx.db.library_path)
    m = db.field_metadata.all_metadata()
    db.new_api.set_pref('field_metadata', m)
    return 0
def main(opts, args, dbctx):
    if len(args) < 1:
        raise SystemExit(_('Error: You must specify a column label'))

    do_remove_custom_column(dbctx.db, args[0], opts.force)
    # Update the stored field_metadata
    dbctx.db.close()
    db = LibraryDatabase(dbctx.db.library_path)
    m = db.field_metadata.all_metadata()
    db.new_api.set_pref('field_metadata', m)
    return 0
Example #16
0
def update_all_by_isbn(library_path):
    def do_book_update(id):
        book_id = int(id)
        mi = db.get_metadata(book_id, index_is_id=True)
        douban_mi = douban.get_douban_metadata(mi)
        if not douban_mi:
            logging.error("-- erro %d" % book_id)
            raise
        if mi.cover_data[0]:
            douban_mi.cover_data = None
        mi.smart_update(douban_mi, replace_metadata=True)
        db.set_metadata(book_id, mi)
        logging.error("** done %d" % book_id)

    db = LibraryDatabase(os.path.expanduser(library_path))
    ids = db.search_getting_ids('', None)
    for i in ids:
        if i < 2837: continue
        try: do_book_update(i)
        except: pass
    return 0
def main(opts, args, dbctx):
    if opts.report is None:
        checks = CHECKS
    else:
        checks = []
        for r in opts.report.split(','):
            found = False
            for c in CHECKS:
                if c[0] == r:
                    checks.append(c)
                    found = True
                    break
            if not found:
                prints(_('Unknown report check'), r)
                return 1

    if opts.names is None:
        names = []
    else:
        names = [f.strip() for f in opts.names.split(',') if f.strip()]
    if opts.exts is None:
        exts = []
    else:
        exts = [f.strip() for f in opts.exts.split(',') if f.strip()]

    if not LibraryDatabase.exists_at(dbctx.library_path):
        prints('No library found at', dbctx.library_path, file=sys.stderr)
        raise SystemExit(1)

    db = LibraryDatabase(dbctx.library_path)
    prints(_('Vacuuming database...'))
    db.new_api.vacuum()
    checker = CheckLibrary(dbctx.library_path, db)
    checker.scan_library(names, exts)
    for check in checks:
        _print_check_library_results(checker, check, as_csv=opts.csv)

    return 0
Example #18
0
def update_all_by_isbn(library_path):
    def do_book_update(id):
        book_id = int(id)
        mi = db.get_metadata(book_id, index_is_id=True)
        douban_mi = douban.get_douban_metadata(mi)
        if not douban_mi:
            logging.error("-- erro %d" % book_id)
            raise
        if mi.cover_data[0]:
            douban_mi.cover_data = None
        mi.smart_update(douban_mi, replace_metadata=True)
        db.set_metadata(book_id, mi)
        logging.error("** done %d" % book_id)

    db = LibraryDatabase(os.path.expanduser(library_path))
    ids = db.search_getting_ids('', None)
    for i in ids:
        if i < 2837: continue
        try:
            do_book_update(i)
        except:
            pass
    return 0
Example #19
0
 def __init__(self, libraries):
     self.lock = Lock()
     self.lmap = {}
     for path in libraries:
         if not LibraryDatabase.exists_at(path):
             continue
         library_id = base = force_unicode(os.path.basename(path))
         c = 0
         while library_id in self.lmap:
             c += 1
             library_id = base + ' (1)'
         if path is libraries[0]:
             self.default_library = library_id
         self.lmap[library_id] = path
Example #20
0
    def initialize_db(self):
        from calibre.db.legacy import LibraryDatabase
        db = None
        self.timed_print('Initializing db...')
        try:
            db = LibraryDatabase(self.library_path)
        except apsw.Error:
            with self.app:
                self.hide_splash_screen()
                repair = question_dialog(
                    self.splash_screen,
                    _('Corrupted database'),
                    _('The library database at %s appears to be corrupted. Do '
                      'you want calibre to try and rebuild it automatically? '
                      'The rebuild may not be completely successful. '
                      'If you say No, a new empty calibre library will be created.'
                      ) %
                    force_unicode(self.library_path, filesystem_encoding),
                    det_msg=traceback.format_exc())
            if repair:
                if iswindows:
                    # On some windows systems the existing db file gets locked
                    # by something when running restore from the main process.
                    # So run the restore in a separate process.
                    windows_repair(self.library_path)
                    self.app.quit()
                    return
                if repair_library(self.library_path):
                    db = LibraryDatabase(self.library_path)
        except:
            self.show_error(_('Bad database location'),
                            _('Bad database location %r. Will start with '
                              ' a new, empty calibre library') %
                            self.library_path,
                            det_msg=traceback.format_exc())

        self.initialize_db_stage2(db, None)
Example #21
0
def main(args=sys.argv):
    opts, args = create_option_parser().parse_args(args)
    if opts.manage_users:
        try:
            manage_users(opts.userdb)
        except (KeyboardInterrupt, EOFError):
            raise SystemExit(_('Interrupted by user'))
        raise SystemExit(0)

    libraries = args[1:]
    for lib in libraries:
        if not lib or not LibraryDatabase.exists_at(lib):
            raise SystemExit(_('There is no calibre library at: %s') % lib)
    if not libraries:
        if not prefs['library_path']:
            raise SystemExit(
                _('You must specify at least one calibre library'))
        libraries = [prefs['library_path']]

    if opts.auto_reload:
        if opts.daemonize:
            raise SystemExit(
                'Cannot specify --auto-reload and --daemonize at the same time'
            )
        from calibre.srv.auto_reload import auto_reload, NoAutoReload
        try:
            from calibre.utils.logging import default_log
            return auto_reload(default_log)
        except NoAutoReload as e:
            raise SystemExit(e.message)
    opts.auto_reload_port = int(os.environ.get('CALIBRE_AUTORELOAD_PORT', 0))
    server = Server(libraries, opts)
    if opts.daemonize:
        if not opts.log and not iswindows:
            raise SystemExit(
                'In order to daemonize you must specify a log file, you can use /dev/stdout to log to screen even as a daemon'
            )
        daemonize()
    if opts.pidfile:
        with lopen(opts.pidfile, 'wb') as f:
            f.write(str(os.getpid()))
    signal.signal(signal.SIGTERM, lambda s, f: server.stop())
    if not opts.daemonize and not iswindows:
        signal.signal(signal.SIGHUP, lambda s, f: server.stop())
    # Needed for dynamic cover generation, which uses Qt for drawing
    from calibre.gui2 import ensure_app, load_builtin_fonts
    ensure_app(), load_builtin_fonts()
    server.serve_forever()
Example #22
0
def main(args=sys.argv):
    opts, args = create_option_parser().parse_args(args)
    if opts.manage_users:
        try:
            manage_users(opts.userdb)
        except (KeyboardInterrupt, EOFError):
            raise SystemExit(_("Interrupted by user"))
        raise SystemExit(0)

    libraries = args[1:]
    for lib in libraries:
        if not lib or not LibraryDatabase.exists_at(lib):
            raise SystemExit(_("There is no calibre library at: %s") % lib)
    if not libraries:
        if not prefs["library_path"]:
            raise SystemExit(_("You must specify at least one calibre library"))
        libraries = [prefs["library_path"]]

    if opts.auto_reload:
        if opts.daemonize:
            raise SystemExit("Cannot specify --auto-reload and --daemonize at the same time")
        from calibre.srv.auto_reload import auto_reload, NoAutoReload

        try:
            from calibre.utils.logging import default_log

            return auto_reload(default_log)
        except NoAutoReload as e:
            raise SystemExit(e.message)
    opts.auto_reload_port = int(os.environ.get("CALIBRE_AUTORELOAD_PORT", 0))
    try:
        server = Server(libraries, opts)
    except InvalidCredentials as e:
        raise SystemExit(e.message)
    if opts.daemonize:
        if not opts.log and not iswindows:
            raise SystemExit(
                "In order to daemonize you must specify a log file, you can use /dev/stdout to log to screen even as a daemon"
            )
        daemonize()
    if opts.pidfile:
        with lopen(opts.pidfile, "wb") as f:
            f.write(str(os.getpid()))
    signal.signal(signal.SIGTERM, lambda s, f: server.stop())
    if not opts.daemonize and not iswindows:
        signal.signal(signal.SIGHUP, lambda s, f: server.stop())
    server.serve_forever()
Example #23
0
def make_app():
    try:
        import local_settings
        settings.update(local_settings.settings)
    except Exception as e:
        logging.error("read local_settings fail")
        pass

    init_calibre()

    import handlers
    from calibre.db.legacy import LibraryDatabase

    auth_db_path = settings['user_database']
    logging.info("Init library with [%s]" % options.with_library)
    logging.info("Init AuthDB  with [%s]" % auth_db_path)
    logging.info("Init Static  with [%s]" % settings['static_path'])
    logging.info("Init LANG    with [%s]" % P('localization/locales.zip'))
    book_db = LibraryDatabase(os.path.expanduser(options.with_library))
    cache = book_db.new_api

    Base = declarative_base()
    engine = create_engine(auth_db_path, echo=False)
    session = scoped_session(
        sessionmaker(bind=engine, autoflush=True, autocommit=False))
    init_social(Base, session, settings)
    models.bind_session(session)

    load_calibre_translations()

    if options.syncdb:
        models.user_syncdb(engine)
        sys.exit(0)

    if options.test:
        from test import email
        email.do_send_mail()
        sys.exit(0)

    settings.update({
        "legacy": book_db,
        "cache": cache,
        "session": session,
    })

    return web.Application(SOCIAL_AUTH_ROUTES + handlers.routes(), **settings)
Example #24
0
    def initialize_db_stage2(self, db, tb):
        from calibre.db.legacy import LibraryDatabase

        if db is None and tb is not None:
            # DB Repair failed
            error_dialog(self.splash_screen,
                         _('Repairing failed'),
                         _('The database repair failed. Starting with '
                           'a new empty library.'),
                         det_msg=tb,
                         show=True)
        if db is None:
            candidate = choose_dir(
                self.splash_screen,
                'choose calibre library',
                _('Choose a location for your new calibre e-book library'),
                default_dir=get_default_library_path())
            if not candidate:
                self.initialization_failed()

            try:
                self.library_path = candidate
                db = LibraryDatabase(candidate)
            except:
                error_dialog(
                    self.splash_screen,
                    _('Bad database location'),
                    _('Bad database location %r. calibre will now quit.') %
                    self.library_path,
                    det_msg=traceback.format_exc(),
                    show=True)
                self.initialization_failed()

        try:
            self.start_gui(db)
        except Exception:
            error_dialog(
                self.main,
                _('Startup error'),
                _('There was an error during {0} startup.'
                  ' Parts of {0} may not function. Click Show details to learn more.'
                  ).format(__appname__),
                det_msg=traceback.format_exc(),
                show=True)
Example #25
0
def main(args=sys.argv):
    opts, args=create_option_parser().parse_args(args)
    if opts.manage_users:
        try:
            manage_users(opts.userdb)
        except (KeyboardInterrupt, EOFError):
            raise SystemExit(_('Interrupted by user'))
        raise SystemExit(0)

    libraries=args[1:]
    for lib in libraries:
        if not lib or not LibraryDatabase.exists_at(lib):
            raise SystemExit(_('There is no calibre library at: %s') % lib)
    if not libraries:
        if not prefs['library_path']:
            raise SystemExit(_('You must specify at least one calibre library'))
        libraries=[prefs['library_path']]

    if opts.auto_reload:
        if opts.daemonize:
            raise SystemExit('Cannot specify --auto-reload and --daemonize at the same time')
        from calibre.srv.auto_reload import auto_reload, NoAutoReload
        try:
            from calibre.utils.logging import default_log
            return auto_reload(default_log, listen_on=opts.listen_on)
        except NoAutoReload as e:
            raise SystemExit(e.message)
    opts.auto_reload_port=int(os.environ.get('CALIBRE_AUTORELOAD_PORT', 0))
    opts.allow_console_print = 'CALIBRE_ALLOW_CONSOLE_PRINT' in os.environ
    server=Server(libraries, opts)
    if opts.daemonize:
        if not opts.log and not iswindows:
            raise SystemExit('In order to daemonize you must specify a log file, you can use /dev/stdout to log to screen even as a daemon')
        daemonize()
    if opts.pidfile:
        with lopen(opts.pidfile, 'wb') as f:
            f.write(str(os.getpid()))
    signal.signal(signal.SIGTERM, lambda s,f: server.stop())
    if not opts.daemonize and not iswindows:
        signal.signal(signal.SIGHUP, lambda s,f: server.stop())
    # Needed for dynamic cover generation, which uses Qt for drawing
    from calibre.gui2 import ensure_app, load_builtin_fonts
    ensure_app(), load_builtin_fonts()
    server.serve_forever()
Example #26
0
def move_library(oldloc, newloc, parent, callback_on_complete):
    from calibre.db.legacy import LibraryDatabase
    callback = Callback(callback_on_complete)
    try:
        if not os.path.exists(os.path.join(newloc, 'metadata.db')):
            if oldloc and os.access(os.path.join(oldloc, 'metadata.db'),
                                    os.R_OK):
                # Move old library to new location
                try:
                    db = LibraryDatabase(oldloc)
                except:
                    return move_library(None, newloc, parent, callback)
                else:
                    rq = Queue()
                    m = MoveLibrary(oldloc, newloc,
                                    len(db.get_top_level_move_items()[0]), rq)
                    global _mm
                    _mm = MoveMonitor(m, rq, callback, parent)
                    return
            else:
                # Create new library at new location
                db = LibraryDatabase(newloc)
                callback(newloc)
                return

        # Try to load existing library at new location
        try:
            LibraryDatabase(newloc)
        except Exception as err:
            det = traceback.format_exc()
            error_dialog(parent,
                         _('Invalid database'),
                         _('<p>An invalid library already exists at '
                           '%(loc)s, delete it before trying to move the '
                           'existing library.<br>Error: %(err)s') %
                         dict(loc=newloc, err=str(err)),
                         det,
                         show=True)
            callback(None)
            return
        else:
            callback(newloc)
            return
    except Exception as err:
        det = traceback.format_exc()
        error_dialog(parent,
                     _('Could not move library'),
                     unicode(err),
                     det,
                     show=True)
    callback(None)
Example #27
0
 def __init__(self, libraries):
     self.lock = Lock()
     self.lmap = {}
     seen = set()
     for i, path in enumerate(os.path.abspath(p) for p in libraries):
         if path in seen:
             continue
         seen.add(path)
         if not LibraryDatabase.exists_at(path):
             continue
         bname = library_id = hexlify(os.path.basename(path).encode('utf-8')).decode('ascii')
         c = 0
         while library_id in self.lmap:
             c += 1
             library_id = bname + '%d' % c
         if i == 0:
             self.default_library = library_id
         self.lmap[library_id] = path
     self.category_caches = {lid:OrderedDict() for lid in self.lmap}
     self.search_caches = {lid:OrderedDict() for lid in self.lmap}
Example #28
0
 def __init__(self, libraries):
     self.lock = Lock()
     self.lmap = {}
     seen = set()
     for i, path in enumerate(os.path.abspath(p) for p in libraries):
         if path in seen:
             continue
         seen.add(path)
         if not LibraryDatabase.exists_at(path):
             continue
         bname = library_id = hexlify(os.path.basename(path).encode('utf-8')).decode('ascii')
         c = 0
         while library_id in self.lmap:
             c += 1
             library_id = bname + '%d' % c
         if i == 0:
             self.default_library = library_id
         self.lmap[library_id] = path
     self.category_caches = {lid:OrderedDict() for lid in self.lmap}
     self.search_caches = {lid:OrderedDict() for lid in self.lmap}
Example #29
0
def main(args=sys.argv):
    from calibre.db.legacy import LibraryDatabase
    parser = option_parser()
    opts, args = parser.parse_args(args)
    if opts.daemonize and not iswindows:
        daemonize()
    if opts.pidfile is not None:
        from cherrypy.process.plugins import PIDFile
        PIDFile(cherrypy.engine, opts.pidfile).subscribe()
    cherrypy.log.screen = True
    from calibre.utils.config import prefs
    if opts.with_library is None:
        opts.with_library = prefs['library_path']
    if not opts.with_library:
        print('No saved library path. Use the --with-library option'
                ' to specify the path to the library you want to use.')
        return 1
    db = LibraryDatabase(os.path.expanduser(opts.with_library))
    server = LibraryServer(db, opts, show_tracebacks=opts.develop)
    server.start()
    return 0
Example #30
0
 def __init__(self, libraries):
     self.lock = Lock()
     self.lmap = {}
     seen = set()
     for i, path in enumerate(os.path.abspath(p) for p in libraries):
         if path in seen:
             continue
         seen.add(path)
         if not LibraryDatabase.exists_at(path):
             continue
         bname = library_id = force_unicode(os.path.basename(path), filesystem_encoding).replace(' ', '_')
         c = 0
         while library_id in self.lmap:
             c += 1
             library_id = bname + '%d' % c
         if i == 0:
             self.default_library = library_id
         self.lmap[library_id] = path
     self.category_caches = {lid:OrderedDict() for lid in self.lmap}
     self.search_caches = {lid:OrderedDict() for lid in self.lmap}
     self.tag_browser_caches = {lid:OrderedDict() for lid in self.lmap}
Example #31
0
 def __init__(self, libraries):
     self.lock = Lock()
     self.lmap = {}
     seen = set()
     for i, path in enumerate(os.path.abspath(p) for p in libraries):
         if path in seen:
             continue
         seen.add(path)
         if not LibraryDatabase.exists_at(path):
             continue
         bname = library_id = force_unicode(os.path.basename(path),
                                            filesystem_encoding)
         c = 0
         while library_id in self.lmap:
             c += 1
             library_id = bname + '%d' % c
         if i == 0:
             self.default_library = library_id
         self.lmap[library_id] = path
     self.category_caches = {lid: OrderedDict() for lid in self.lmap}
     self.search_caches = {lid: OrderedDict() for lid in self.lmap}
     self.tag_browser_caches = {lid: OrderedDict() for lid in self.lmap}
Example #32
0
def main(opts, args, dbctx):
    if opts.report is None:
        checks = CHECKS
    else:
        checks = []
        for r in opts.report.split(','):
            found = False
            for c in CHECKS:
                if c[0] == r:
                    checks.append(c)
                    found = True
                    break
            if not found:
                prints(_('Unknown report check'), r)
                return 1

    if opts.names is None:
        names = []
    else:
        names = [f.strip() for f in opts.names.split(',') if f.strip()]
    if opts.exts is None:
        exts = []
    else:
        exts = [f.strip() for f in opts.exts.split(',') if f.strip()]

    if not LibraryDatabase.exists_at(dbctx.library_path):
        prints('No library found at', dbctx.library_path, file=sys.stderr)
        raise SystemExit(1)

    db = LibraryDatabase(dbctx.library_path)
    prints(_('Vacuuming database...'))
    db.new_api.vacuum()
    checker = CheckLibrary(dbctx.library_path, db)
    checker.scan_library(names, exts)
    for check in checks:
        _print_check_library_results(checker, check, as_csv=opts.csv)

    return 0
Example #33
0
def initdb(path):
    global db
    db = LibraryDatabase(os.path.expanduser(path))
Example #34
0
 def init_library(self, library_path, is_default_library):
     library_path = self.original_path_map.get(library_path, library_path)
     return LibraryDatabase(library_path, is_second_db=True)
Example #35
0
 def is_library_dir_suitable(self, x):
     try:
         return LibraryDatabase.exists_at(x) or not os.listdir(x)
     except:
         return False
Example #36
0
    def library_moved(self, newloc, copy_structure=False, call_close=True,
            allow_rebuild=False):
        if newloc is None:
            return
        default_prefs = None
        try:
            olddb = self.library_view.model().db
            if copy_structure:
                default_prefs = olddb.prefs

            from calibre.utils.formatter_functions import unload_user_template_functions
            unload_user_template_functions(olddb.library_id)
        except:
            olddb = None
        try:
            db = LibraryDatabase(newloc, default_prefs=default_prefs)
        except apsw.Error:
            if not allow_rebuild:
                raise
            import traceback
            repair = question_dialog(self, _('Corrupted database'),
                    _('The library database at %s appears to be corrupted. Do '
                    'you want calibre to try and rebuild it automatically? '
                    'The rebuild may not be completely successful.')
                    % force_unicode(newloc, filesystem_encoding),
                    det_msg=traceback.format_exc()
                    )
            if repair:
                from calibre.gui2.dialogs.restore_library import repair_library_at
                if repair_library_at(newloc, parent=self):
                    db = LibraryDatabase(newloc, default_prefs=default_prefs)
                else:
                    return
            else:
                return
        if self.content_server is not None:
            self.content_server.set_database(db)
        self.library_path = newloc
        prefs['library_path'] = self.library_path
        self.book_on_device(None, reset=True)
        db.set_book_on_device_func(self.book_on_device)
        self.library_view.set_database(db)
        self.tags_view.set_database(db, self.alter_tb)
        self.library_view.model().set_book_on_device_func(self.book_on_device)
        self.status_bar.clear_message()
        self.search.clear()
        self.saved_search.clear()
        self.book_details.reset_info()
        # self.library_view.model().count_changed()
        db = self.library_view.model().db
        self.iactions['Choose Library'].count_changed(db.count())
        self.set_window_title()
        self.apply_named_search_restriction('')  # reset restriction to null
        self.saved_searches_changed(recount=False)  # reload the search restrictions combo box
        if db.prefs['virtual_lib_on_startup']:
            self.apply_virtual_library(db.prefs['virtual_lib_on_startup'])
        self.rebuild_vl_tabs()
        for action in self.iactions.values():
            action.library_changed(db)
        if olddb is not None:
            try:
                if call_close:
                    olddb.close()
            except:
                import traceback
                traceback.print_exc()
            olddb.break_cycles()
        if self.device_connected:
            self.set_books_in_library(self.booklists(), reset=True)
            self.refresh_ondevice()
            self.memory_view.reset()
            self.card_a_view.reset()
            self.card_b_view.reset()
        self.set_current_library_information(current_library_name(), db.library_id,
                                             db.field_metadata)
        self.library_view.set_current_row(0)
        # Run a garbage collection now so that it does not freeze the
        # interface later
        gc.collect()
Example #37
0
 def __init__(self, library_path):
     self.fmt = "[%6s] [%4d/%4d] [ISBN=%13s] [Title=%s]"
     if os.path.isfile(library_path):
         library_path = os.path.dirname(library_path)
     self.db = LibraryDatabase(os.path.expanduser(library_path))
Example #38
0
 def is_library_dir_suitable(self, x):
     from calibre.db.legacy import LibraryDatabase
     try:
         return LibraryDatabase.exists_at(x) or not os.listdir(x)
     except:
         return False
Example #39
0
 def is_library_dir_suitable(self, x):
     from calibre.db.legacy import LibraryDatabase
     try:
         return LibraryDatabase.exists_at(x) or not os.listdir(x)
     except:
         return False
Example #40
0
def move_library(from_, to, notification=lambda x:x):
    from calibre.db.legacy import LibraryDatabase
    time.sleep(1)
    old = LibraryDatabase(from_)
    old.move_library_to(to, notification)
    return True
Example #41
0
def move_library(from_, to, notification=lambda x: x):
    from calibre.db.legacy import LibraryDatabase
    time.sleep(1)
    old = LibraryDatabase(from_)
    old.move_library_to(to, notification)
    return True
Example #42
0
 def init_legacy(self, library_path=None):
     from calibre.db.legacy import LibraryDatabase
     return LibraryDatabase(library_path or self.library_path)
Example #43
0
def make_app():
    init_calibre()

    import handlers
    from calibre.db.legacy import LibraryDatabase
    from calibre.utils.date import fromtimestamp

    auth_db_path = CONF['user_database']
    logging.info("Init library with [%s]" % options.with_library)
    logging.info("Init AuthDB  with [%s]" % auth_db_path )
    logging.info("Init Static  with [%s]" % CONF['static_path'] )
    logging.info("Init HTML    with [%s]" % CONF['html_path'] )
    logging.info("Init LANG    with [%s]" % P('localization/locales.zip') )
    book_db = LibraryDatabase(os.path.expanduser(options.with_library))
    cache = book_db.new_api


    # hook 1: 按字母作为第一级目录,解决书库子目录太多的问题
    old_construct_path_name = cache.backend.construct_path_name
    def new_construct_path_name(*args, **kwargs):
        s = old_construct_path_name(*args, **kwargs)
        ns = s[0] + "/" + s
        logging.debug("new str = %s" % ns)
        return ns
    cache.backend.construct_path_name = new_construct_path_name

    # hook 2: don't force GUI
    from calibre import gui2
    old_must_use_qt = gui2.must_use_qt
    def new_must_use_qt(headless=True):
        try:
            old_must_use_qt(headless)
        except:
            pass
    gui2.must_use_qt = new_must_use_qt

    # build sql session factory
    engine = create_engine(auth_db_path, echo=False)
    ScopedSession = scoped_session(sessionmaker(bind=engine, autoflush=True, autocommit=False))
    models.bind_session(ScopedSession)
    init_social(models.Base, ScopedSession, CONF)

    if options.syncdb:
        models.user_syncdb(engine)
        logging.info("Create tables into DB")
        sys.exit(0)

    path = CONF['static_path'] + '/calibre/default_cover.jpg'
    app_settings = dict(CONF)
    app_settings.update({
        "legacy": book_db,
        "cache": cache,
        "ScopedSession": ScopedSession ,
        "build_time": fromtimestamp(os.stat(path).st_mtime),
        "default_cover": open(path, 'rb').read(),
        })

    #load_calibre_translations()
    logging.info("Now, Running...")
    return web.Application(
            SOCIAL_AUTH_ROUTES + handlers.routes(),
            **app_settings)
Example #44
0
 def db(self):
     if self._db is None:
         self._db = LibraryDatabase(self.library_path)
     return self._db
Example #45
0
 def library_moved(self,
                   newloc,
                   copy_structure=False,
                   call_close=True,
                   allow_rebuild=False):
     if newloc is None:
         return
     default_prefs = None
     try:
         olddb = self.library_view.model().db
         if copy_structure:
             default_prefs = olddb.prefs
     except:
         olddb = None
     try:
         db = LibraryDatabase(newloc, default_prefs=default_prefs)
     except apsw.Error:
         if not allow_rebuild:
             raise
         import traceback
         repair = question_dialog(
             self,
             _('Corrupted database'),
             _('The library database at %s appears to be corrupted. Do '
               'you want calibre to try and rebuild it automatically? '
               'The rebuild may not be completely successful.') %
             force_unicode(newloc, filesystem_encoding),
             det_msg=traceback.format_exc())
         if repair:
             from calibre.gui2.dialogs.restore_library import repair_library_at
             if repair_library_at(newloc, parent=self):
                 db = LibraryDatabase(newloc, default_prefs=default_prefs)
             else:
                 return
         else:
             return
     if self.content_server is not None:
         self.content_server.set_database(db)
     self.library_path = newloc
     prefs['library_path'] = self.library_path
     self.book_on_device(None, reset=True)
     db.set_book_on_device_func(self.book_on_device)
     self.library_view.set_database(db)
     self.tags_view.set_database(db, self.alter_tb)
     self.library_view.model().set_book_on_device_func(self.book_on_device)
     self.status_bar.clear_message()
     self.search.clear()
     self.saved_search.clear()
     self.book_details.reset_info()
     # self.library_view.model().count_changed()
     db = self.library_view.model().db
     self.iactions['Choose Library'].count_changed(db.count())
     self.set_window_title()
     self.apply_named_search_restriction('')  # reset restriction to null
     self.saved_searches_changed(
         recount=False)  # reload the search restrictions combo box
     if db.prefs['virtual_lib_on_startup']:
         self.apply_virtual_library(db.prefs['virtual_lib_on_startup'])
     self.rebuild_vl_tabs()
     for action in self.iactions.values():
         action.library_changed(db)
     if olddb is not None:
         try:
             if call_close:
                 olddb.close()
         except:
             import traceback
             traceback.print_exc()
         olddb.break_cycles()
     if self.device_connected:
         self.set_books_in_library(self.booklists(), reset=True)
         self.refresh_ondevice()
         self.memory_view.reset()
         self.card_a_view.reset()
         self.card_b_view.reset()
     self.set_current_library_information(current_library_name(),
                                          db.library_id, db.field_metadata)
     self.library_view.set_current_row(0)
     # Run a garbage collection now so that it does not freeze the
     # interface later
     gc.collect()
Example #46
0
def db(path=None, read_only=False):
    from calibre.db.legacy import LibraryDatabase
    from calibre.utils.config import prefs
    from calibre.utils.filenames import expanduser
    return LibraryDatabase(expanduser(path) if path else prefs['library_path'],
                           read_only=read_only)
Example #47
0
class App:
    def __init__(self, library_path):
        self.fmt = "[%6s] [%4d/%4d] [ISBN=%13s] [Title=%s]"
        if os.path.isfile(library_path):
            library_path = os.path.dirname(library_path)
        self.db = LibraryDatabase(os.path.expanduser(library_path))

    def log_error(self, msg):
        print "\033[1;31;40m%s\033[0m" % msg

    def log_succ(self, msg):
        print "\033[1;32;40m%s\033[0m" % msg

    def get_baike_metadata(self, title):
        from baidubaike import Page

        try: baike = Page(title)
        except: return None

        info = baike.get_info()
        print "\n".join( "%s:\t%s" % v for v in info.items())
        mi = Metadata(info['title'])
        plat = info.get(u'首发网站', None)
        if not plat:
            plat = info.get(u'首发状态', "网络小说平台")
        plat = plat.replace(u'首发', '')
        mi.publisher = info.get(u'连载平台', plat)
        mi.authors   = [info[u'作者']]
        mi.isbn      = '0000000000001'
        mi.tags      = baike.get_tags()
        mi.pubdate   = datetime.datetime.now()
        mi.timestamp = datetime.datetime.now()
        mi.comments  = baike.get_summary()
        if u'完结' in info.get(u'连载状态', ""):
            day = re.findall('\d*-\d*-\d*', info[u'连载状态'])
            try: mi.pubdate = datetime.datetime.strptime(day[0], '%Y-%m-%d')
            except: pass
        return mi

    def do_book_update(self, book_id):
        book_id = int(book_id)
        mi = self.db.get_metadata(book_id, index_is_id=True)
        #if not mi.comments or mi.comments == "None":
            #logging.error("\n\n[%s]\n=================\nCalibre Metadata:\n%s\n===========" % (mi.title, mi))

        title = mi.title
        mi.title = title.split("(")[0].split("(")[0].strip()
        #douban_mi = douban.select_douban_metadata(mi)
        douban_mi = douban.get_douban_metadata(mi)
        if not douban_mi:
            try:
                douban_mi = self.get_baike_metadata(mi.title)
            except Exception as e:
                logging.error(e)
                pass

        if not douban_mi:
            self.log_error(self.fmt % ("404", book_id, self.total, "", title))
            return

        if mi.cover_data[0]: douban_mi.cover_data = None
        mi.smart_update(douban_mi, replace_metadata=True)
        self.db.set_metadata(book_id, mi)
        self.log_succ(self.fmt % ("Update", book_id, self.total, douban_mi.isbn, mi.title))

    def update_all_by_isbn(self):
        ids = self.db.search_getting_ids('', None)
        self.total = max(ids)
        books = self.db.get_data_as_dict(ids=ids)

        need_update = []
        for book in books:
            if book['comments']: continue
            if book['tags']: continue
            if book['isbn']: continue
            if int(book['id']) < 6722: continue
            print self.fmt%("Todo", book['id'], self.total, book['isbn'], book['title'])
            self.do_book_update(book['id'])