From 64c641b2079d386f0bc70701a061210132ba0d23 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sun, 9 Nov 2008 11:27:43 -0800 Subject: [PATCH] IGN:... --- src/calibre/ebooks/metadata/meta.py | 29 ++++++++++++++++++ src/calibre/gui2/dialogs/config.py | 8 ++--- src/calibre/library/static/gui.js | 7 ++--- src/calibre/libunrar.py | 30 ++++++++++++++++++- src/calibre/libunzip.py | 11 ++++++- .../trac/plugins/templates/binary.html | 3 +- 6 files changed, 76 insertions(+), 12 deletions(-) diff --git a/src/calibre/ebooks/metadata/meta.py b/src/calibre/ebooks/metadata/meta.py index 76d04045c2..266ef99cc2 100644 --- a/src/calibre/ebooks/metadata/meta.py +++ b/src/calibre/ebooks/metadata/meta.py @@ -1,3 +1,4 @@ +from __future__ import with_statement __license__ = 'GPL v3' __copyright__ = '2008, Kovid Goyal ' @@ -19,8 +20,11 @@ from calibre.ebooks.metadata.opf import OPFReader from calibre.ebooks.metadata.rtf import set_metadata as set_rtf_metadata from calibre.ebooks.lrf.meta import set_metadata as set_lrf_metadata from calibre.ebooks.metadata.epub import set_metadata as set_epub_metadata +from calibre.libunrar import extract_first as rar_extract_first +from calibre.libunzip import extract_first as zip_extract_first from calibre.ebooks.metadata import MetaInformation +from calibre.ptempfile import TemporaryDirectory _METADATA_PRIORITIES = [ 'html', 'htm', 'xhtml', 'xhtm', @@ -94,8 +98,33 @@ def get_metadata(stream, stream_type='lrf', use_libprs_metadata=False): if opf is not None: base.smart_update(opf) + if stream_type in ('cbr', 'cbz'): + try: + cdata = get_comic_cover(stream, stream_type) + if cdata is not None: + base.cover_data = cdata + except: + import traceback + traceback.print_exc() + pass + return base +def get_comic_cover(stream, type): + with TemporaryDirectory('_comic_cover') as tdir: + extract_first = zip_extract_first if type == 'zip' else rar_extract_first + extract_first(stream, tdir) + files = os.listdir(tdir) + print tdir, files + if files: + path = os.path.join(tdir, files[0]) + ext = os.path.splitext(path)[1].lower() + if ext: + ext = ext[1:] + return (ext, open(path, 'rb').read()) + + + def set_metadata(stream, mi, stream_type='lrf'): if stream_type: stream_type = stream_type.lower() if stream_type == 'lrf': diff --git a/src/calibre/gui2/dialogs/config.py b/src/calibre/gui2/dialogs/config.py index 17eb7225b7..73a5176857 100644 --- a/src/calibre/gui2/dialogs/config.py +++ b/src/calibre/gui2/dialogs/config.py @@ -55,10 +55,8 @@ class ConfigDialog(QDialog, Ui_Dialog): item = QListWidgetItem(BooksModel.headers[col], self.columns) item.setData(Qt.UserRole, QVariant(col)) item.setFlags(Qt.ItemIsEnabled|Qt.ItemIsUserCheckable|Qt.ItemIsSelectable) - if col in column_map: - item.setCheckState(Qt.Checked) - else: - item.setCheckState(Qt.Unchecked) + item.setCheckState(Qt.Checked if col in column_map else Qt.Unchecked) + self.connect(self.column_up, SIGNAL('clicked()'), self.up_column) self.connect(self.column_down, SIGNAL('clicked()'), self.down_column) @@ -204,6 +202,8 @@ class ConfigDialog(QDialog, Ui_Dialog): prefs['network_timeout'] = int(self.timeout.value()) path = qstring_to_unicode(self.location.text()) cols = [unicode(self.columns.item(i).data(Qt.UserRole).toString()) for i in range(self.columns.count()) if self.columns.item(i).checkState()==Qt.Checked] + if not cols: + cols = ['title'] config['column_map'] = cols config['toolbar_icon_size'] = self.ICON_SIZES[self.toolbar_button_size.currentIndex()] config['show_text_in_toolbar'] = bool(self.show_toolbar_text.isChecked()) diff --git a/src/calibre/library/static/gui.js b/src/calibre/library/static/gui.js index d6df4d32f7..5bead61a66 100644 --- a/src/calibre/library/static/gui.js +++ b/src/calibre/library/static/gui.js @@ -40,8 +40,7 @@ function create_table_headers() { function format_url(format, id, title) { - return self.location.pathname.substring(0,self.location.pathname.lastIndexOf('/')) + - '/get/'+format.toLowerCase() + '/'+title + '_' + id+'.'+format.toLowerCase(); + return 'get/'+format.toLowerCase() + '/'+title + '_' + id+'.'+format.toLowerCase(); } function render_book(book) { @@ -61,7 +60,7 @@ function render_book(book) { title += ' ({0} MB) '.format(size); } if (tags) title += '[{0}]'.format(tags); - title += ''.format(id, self.location.pathname.substring(0,self.location.pathname.lastIndexOf('/'))); + title += ''.format(id); title += '

{0}

'.format(comments) // Render authors cell var _authors = new Array(); @@ -124,7 +123,7 @@ function fetch_library_books(start, num, timeout, sort, order, search) { current_library_request = $.ajax({ type: "GET", - url: self.location.pathname.substring(0,self.location.pathname.lastIndexOf('/'))+"/library", + url: "library", data: data, cache: false, timeout: timeout, //milliseconds diff --git a/src/calibre/libunrar.py b/src/calibre/libunrar.py index 0d7a679f71..113ac92e20 100644 --- a/src/calibre/libunrar.py +++ b/src/calibre/libunrar.py @@ -1,3 +1,4 @@ +from __future__ import with_statement __license__ = 'GPL v3' __copyright__ = '2008, Kovid Goyal ' """ @@ -8,9 +9,10 @@ See ftp://ftp.rarlabs.com/rar/unrarsrc-3.7.5.tar.gz import os, ctypes, sys from ctypes import Structure, c_char_p, c_uint, c_void_p, POINTER, \ byref, c_wchar_p, c_int, c_char, c_wchar +from tempfile import NamedTemporaryFile from StringIO import StringIO -from calibre import iswindows, load_library +from calibre import iswindows, load_library, CurrentDir _librar_name = 'libunrar' cdll = ctypes.cdll @@ -182,3 +184,29 @@ def extract(path, dir): finally: os.chdir(cwd) _libunrar.RARCloseArchive(arc_data) + +def extract_first(path, dir): + if hasattr(path, 'read'): + data = path.read() + f = NamedTemporaryFile(suffix='.rar') + f.write(data) + f.flush() + path = f.name + if not os.path.isdir( dir ): + os.makedirs(dir) + with CurrentDir(dir): + open_archive_data = RAROpenArchiveDataEx(ArcName=path, OpenMode=RAR_OM_EXTRACT, CmtBuf=None) + arc_data = _libunrar.RAROpenArchiveEx(byref(open_archive_data)) + try: + if open_archive_data.OpenResult != 0: + raise UnRARException(_interpret_open_error(open_archive_data.OpenResult, path)) + header_data = RARHeaderDataEx(CmtBuf=None) + if _libunrar.RARReadHeaderEx(arc_data, byref(header_data)) != 0: + raise UnRARException('%s has no files'%path) + PFCode = _libunrar.RARProcessFileW(arc_data, RAR_EXTRACT, None, None) + if PFCode != 0: + raise UnRARException(_interpret_process_file_error(PFCode)) + finally: + _libunrar.RARCloseArchive(arc_data) + + diff --git a/src/calibre/libunzip.py b/src/calibre/libunzip.py index aec7865ffb..1c146c3b1f 100644 --- a/src/calibre/libunzip.py +++ b/src/calibre/libunzip.py @@ -3,6 +3,7 @@ __license__ = 'GPL v3' __copyright__ = '2008, Kovid Goyal kovid@kovidgoyal.net' __docformat__ = 'restructuredtext en' +import os from calibre.utils import zipfile def update(pathtozip, patterns, filepaths, names, compression=zipfile.ZIP_DEFLATED, verbose=True): @@ -40,4 +41,12 @@ def extract(filename, dir): Extract archive C{filename} into directory C{dir} """ zf = zipfile.ZipFile( filename ) - zf.extractall(dir) \ No newline at end of file + zf.extractall(dir) + +def extract_first(filename, dir): + zf = zipfile.ZipFile(filename) + names = zf.namelist() + if not names: + raise ValueError('%s has no files'%filename) + bytes = zf.read(names[0]) + open(os.path.join(dir, names[0]), 'wb').write(bytes) \ No newline at end of file diff --git a/src/calibre/trac/plugins/templates/binary.html b/src/calibre/trac/plugins/templates/binary.html index 8d8e9fb02c..cd67bf41f2 100644 --- a/src/calibre/trac/plugins/templates/binary.html +++ b/src/calibre/trac/plugins/templates/binary.html @@ -32,8 +32,7 @@
- - +

Note

$note