From 51459386810206580046b5f640c18ccfab10627f Mon Sep 17 00:00:00 2001 From: John Schember Date: Fri, 2 Jan 2009 19:26:47 -0500 Subject: [PATCH] Extract the title and author from filename when reading books from a Cybook --- src/calibre/devices/cybookg3/books.py | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/calibre/devices/cybookg3/books.py b/src/calibre/devices/cybookg3/books.py index fb385f5442..b5859e40d0 100644 --- a/src/calibre/devices/cybookg3/books.py +++ b/src/calibre/devices/cybookg3/books.py @@ -37,11 +37,27 @@ class BookList(_BookList): self.return_books(mountpath) def return_books(self, mountpath): - books = []; + # Get all books in all directories under the root EBOOK_DIR directory for path, dirs, files in os.walk(os.path.join(mountpath, EBOOK_DIR)): + # Filter out anything that isn't in the list of supported ebook types for book_type in EBOOK_TYPES: for filename in fnmatch.filter(files, '*.%s' % (book_type)): - self.append(Book(os.path.join(path, filename), filename, "")) + # Calibre uses a specific format for file names. They take the form + # title_-_author_number.extention We want to see if the file name is + # in this format. + if fnmatch.fnmatchcase(filename, '*_-_*.*'): + # Get the title and author from the file name + title, sep, author = filename.rpartition('_-_') + author, sep, ext = author.rpartition('_') + book_title = title.replace('_', ' ') + book_author = author.replace('_', ' ') + # if the filename did not match just set the title to + # the filename without the extension + else: + book_title = os.path.splitext(filename)[0].replace('_', ' ') + + book_path = os.path.join(path, filename) + self.append(Book(book_path, book_title, book_author)) def add_book(self, path, title): self.append(Book(path, title, ""))