def add_book(self, path, collection=None): """Add the archive at <path> to the library. If <collection> is not None, it is the collection that the books should be put in. Return True if the book was successfully added (or was already added). """ path = os.path.abspath(path) name = os.path.basename(path) info = archive_tools.get_archive_info(path) mi = archive_tools.get_archive_metadata(path) if info is None: return False format, pages, size = info if not mi: mi = [None, None, None, None, None, None] mi = ["NULL" if i == None else i for i in mi] # Thumbnail for the newly added book will be generated once it # is actually needed with get_book_thumbnail(). old = self._con.execute( '''select id from Book where path = ?''', (path, )).fetchone() try: cursor = self._con.cursor() if old is not None: cursor.execute( '''update Book set name = ?, pages = ?, format = ?, size = ?, writer = ?, summary = ?, characters = ?, genre = ?, year = ?, language = ?, where path = ?''', (name, pages, format, size, mi[0], mi[1], mi[2], mi[3], mi[4], mi[5], path)) book_id = old else: cursor.execute( '''insert into Book (name, pages, format, size, writer, summary, characters, genre, year, language, path) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)''', (name, pages, format, size, mi[0], mi[1], mi[2], mi[3], mi[4], mi[5], path)) book_id = cursor.lastrowid book = backend_types._Book(book_id, name, path, pages, format, size, datetime.datetime.now().isoformat(), mi[0], mi[1], mi[2], mi[3], mi[4], mi[5]) self.book_added(book) cursor.close() if collection is not None: self.add_book_to_collection(book_id, collection) return True except dbapi2.Error: log.error(_('! Could not add book "%s" to the library'), path) return False
def add_book(self, path, collection=None): '''Add the archive at <path> to the library. If <collection> is not None, it is the collection that the books should be put in. Return True if the book was successfully added (or was already added). ''' path = tools.relpath2root(path, abs_fallback=prefs['portable allow abspath']) if not path: # path is None, means running in portable mode # and currect path is out of same mount point # so do not add book to library return name = os.path.basename(path) info = archive_tools.get_archive_info(path) if info is None: return False format, pages, size = info # Thumbnail for the newly added book will be generated once it # is actually needed with get_book_thumbnail(). old = self._con.execute( '''select id from Book where path = ?''', (path, )).fetchone() try: cursor = self._con.cursor() if old is not None: cursor.execute( '''update Book set name = ?, pages = ?, format = ?, size = ? where path = ?''', (name, pages, format, size, path)) book_id = old else: cursor.execute( '''insert into Book (name, path, pages, format, size) values (?, ?, ?, ?, ?)''', (name, path, pages, format, size)) book_id = cursor.lastrowid book = backend_types._Book(book_id, name, path, pages, format, size, datetime.datetime.now().isoformat()) self.book_added(book) cursor.close() if collection is not None: self.add_book_to_collection(book_id, collection) return True except sqlite3.Error: log.error(_('! Could not add book "%s" to the library'), path) return False
def add_book(self, path, collection=None): """Add the archive at <path> to the library. If <collection> is not None, it is the collection that the books should be put in. Return True if the book was successfully added (or was already added). """ path = os.path.abspath(path) name = os.path.basename(path) info = archive_tools.get_archive_info(path) if info is None: return False format, pages, size = info # Thumbnail for the newly added book will be generated once it # is actually needed with get_book_thumbnail(). old = self._con.execute( """select id from Book where path = ?""", (path,), ).fetchone() try: cursor = self._con.cursor() if old is not None: cursor.execute( """update Book set name = ?, pages = ?, format = ?, size = ? where path = ?""", (name, pages, format, size, path), ) book_id = old else: cursor.execute( """insert into Book (name, path, pages, format, size) values (?, ?, ?, ?, ?)""", (name, path, pages, format, size), ) book_id = cursor.lastrowid book = backend_types._Book( book_id, name, path, pages, format, size, datetime.datetime.now().isoformat() ) self.book_added(book) cursor.close() if collection is not None: self.add_book_to_collection(book_id, collection) return True except dbapi2.Error: log.error(_('! Could not add book "%s" to the library'), path) return False
def get_book_by_id(self, id): """ Retrieves a book from the library, specified by C{id}. If the book doesn't exist, C{None} is returned. Otherwise, a L{backend_types._Book} instance is returned. """ cur = self.execute('''select id, name, path, pages, format, size, added from book where id = ?''', (id,)) book = cur.fetchone() cur.close() if book: return backend_types._Book(*book) else: return None
def get_book_by_id(self, id): """ Retrieves a book from the library, specified by C{id}. If the book doesn't exist, C{None} is returned. Otherwise, a L{backend_types._Book} instance is returned. """ cur = self.execute( '''select id, name, path, pages, format, size, added from book where id = ?''', (id, )) book = cur.fetchone() cur.close() if book: return backend_types._Book(*book) else: return None
def get_book_by_path(self, path): """ Retrieves a book from the library, specified by C{path}. If the book doesn't exist, None is returned. Otherwise, a L{backend_types._Book} instance is returned. """ path = os.path.abspath(path) cur = self.execute('''select id, name, path, pages, format, size, added from book where path = ?''', (path,)) book = cur.fetchone() cur.close() if book: return backend_types._Book(*book) else: return None
def get_book_by_path(self, path): """ Retrieves a book from the library, specified by C{path}. If the book doesn't exist, None is returned. Otherwise, a L{backend_types._Book} instance is returned. """ path = os.path.abspath(path) cur = self.execute( '''select id, name, path, pages, format, size, added from book where path = ?''', (path, )) book = cur.fetchone() cur.close() if book: return backend_types._Book(*book) else: return None
def get_book_by_path(self, path): ''' Retrieves a book from the library, specified by C{path}. If the book doesn't exist, None is returned. Otherwise, a L{backend_types._Book} instance is returned. ''' path = tools.relpath2root(path, abs_fallback=prefs['portable allow abspath']) if not path: # path is None, means running in portable mode # and currect path is out of same mount point # so book should not exist in library return cur = self.execute( '''select id, name, path, pages, format, size, added from book where path = ?''', (path, )) book = cur.fetchone() cur.close() if book: return backend_types._Book(*book) else: return None