home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyc (Python 2.6)
-
- from __future__ import with_statement
- __license__ = 'GPL v3'
- __copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>'
- __docformat__ = 'restructuredtext en'
- import os
- import uuid
- import re
- from PyQt4.Qt import QPixmap, SIGNAL
- from calibre.gui2 import choose_images, error_dialog
- from calibre.gui2.convert.metadata_ui import Ui_Form
- from calibre.ebooks.metadata import authors_to_string, string_to_authors, MetaInformation
- from calibre.ebooks.metadata.opf2 import metadata_to_opf
- from calibre.ptempfile import PersistentTemporaryFile
- from calibre.gui2.convert import Widget
-
- def create_opf_file(db, book_id):
- mi = db.get_metadata(book_id, index_is_id = True)
- mi.application_id = uuid.uuid4()
- raw = metadata_to_opf(mi)
- opf_file = PersistentTemporaryFile('.opf')
- opf_file.write(raw)
- opf_file.close()
- return (mi, opf_file)
-
-
- def create_cover_file(db, book_id):
- cover = db.cover(book_id, index_is_id = True)
- cf = None
- if cover:
- cf = PersistentTemporaryFile('.jpeg')
- cf.write(cover)
- cf.close()
-
- return cf
-
-
- class MetadataWidget(Widget, Ui_Form):
- TITLE = _('Metadata')
- ICON = I('dialog_information.svg')
- HELP = _('Set the metadata. The output file will contain as much of this metadata as possible.')
-
- def __init__(self, parent, get_option, get_help, db = None, book_id = None):
- Widget.__init__(self, parent, 'metadata', [
- 'prefer_metadata_cover'])
- self.db = db
- self.book_id = book_id
- self.cover_changed = False
- self.cover_data = None
- if self.db is not None:
- self.initialize_metadata_options()
-
- self.initialize_options(get_option, get_help, db, book_id)
- self.connect(self.cover_button, SIGNAL('clicked()'), self.select_cover)
-
-
- def deduce_author_sort(self, *args):
- au = unicode(self.author.currentText())
- au = re.sub('\\s+et al\\.$', '', au)
- authors = string_to_authors(au)
- self.author_sort.setText(self.db.author_sort_from_authors(authors))
-
-
- def initialize_metadata_options(self):
- self.initialize_combos()
- self.author.editTextChanged.connect(self.deduce_author_sort)
- mi = self.db.get_metadata(self.book_id, index_is_id = True)
- self.title.setText(mi.title)
- if mi.publisher:
- self.publisher.setCurrentIndex(self.publisher.findText(mi.publisher))
-
- None(self.author_sort.setText if mi.author_sort else '')
- None(self.tags.setText(', '.join if mi.tags else []))
- self.tags.update_tags_cache(self.db.all_tags())
- None(self.comment.setPlainText if mi.comments else '')
- if mi.series:
- self.series.setCurrentIndex(self.series.findText(mi.series))
-
- if mi.series_index is not None:
-
- try:
- self.series_index.setValue(mi.series_index)
- self.series_index.setValue(1)
-
-
- cover = self.db.cover(self.book_id, index_is_id = True)
- if cover:
- pm = QPixmap()
- pm.loadFromData(cover)
- if not pm.isNull():
- self.cover.setPixmap(pm)
- self.cover_data = cover
-
- else:
- self.cover.setPixmap(QPixmap(I('default_cover.svg')))
-
-
- def initialize_combos(self):
- self.initalize_authors()
- self.initialize_series()
- self.initialize_publisher()
-
-
- def initalize_authors(self):
- all_authors = self.db.all_authors()
- all_authors.sort(cmp = (lambda x, y: cmp(x[1], y[1])))
- for i in all_authors:
- (id, name) = i
- name = []([ name.strip().replace('|', ',') for n in name.split(',') ])
- self.author.addItem(name)
-
- au = self.db.authors(self.book_id, True)
- au = []([ a.strip().replace('|', ',') for a in au.split(',') ])
- self.author.setEditText(au)
-
-
- def initialize_series(self):
- all_series = self.db.all_series()
- all_series.sort(cmp = (lambda x, y: cmp(x[1], y[1])))
- for i in all_series:
- (id, name) = i
- self.series.addItem(name)
-
- self.series.setCurrentIndex(-1)
-
-
- def initialize_publisher(self):
- all_publishers = self.db.all_publishers()
- all_publishers.sort(cmp = (lambda x, y: cmp(x[1], y[1])))
- for i in all_publishers:
- (id, name) = i
- self.publisher.addItem(name)
-
- self.publisher.setCurrentIndex(-1)
-
-
- def get_title_and_authors(self):
- title = unicode(self.title.text()).strip()
- if not title:
- title = _('Unknown')
-
- authors = unicode(self.author.text()).strip()
- authors = None if authors else [
- _('Unknown')]
- return (title, authors)
-
-
- def get_metadata(self):
- (title, authors) = self.get_title_and_authors()
- mi = MetaInformation(title, authors)
- publisher = unicode(self.publisher.text()).strip()
- if publisher:
- mi.publisher = publisher
-
- author_sort = unicode(self.author_sort.text()).strip()
- if author_sort:
- mi.author_sort = author_sort
-
- comments = unicode(self.comment.toPlainText()).strip()
- if comments:
- mi.comments = comments
-
- mi.series_index = float(self.series_index.value())
- series = unicode(self.series.currentText()).strip()
- if series:
- mi.series = series
-
- tags = [ t.strip() for t in unicode(self.tags.text()).strip().split(',') ]
- return mi
-
-
- def select_cover(self):
- files = choose_images(self, 'change cover dialog', _('Choose cover for ') + unicode(self.title.text()))
- if not files:
- return None
- _file = files[0]
-
-
- def get_recommendations(self):
- return {
- 'prefer_metadata_cover': bool(self.opt_prefer_metadata_cover.isChecked()) }
-
-
- def commit(self, save_defaults = False):
- recs = self.commit_options(save_defaults)
- self.user_mi = self.get_metadata()
- self.cover_file = None
- self.opf_file = None
- if self.db is not None:
- self.db.set_metadata(self.book_id, self.user_mi)
- (self.mi, self.opf_file) = create_opf_file(self.db, self.book_id)
- if self.cover_changed and self.cover_data is not None:
- self.db.set_cover(self.book_id, self.cover_data)
-
- cover = self.db.cover(self.book_id, index_is_id = True)
- if cover:
- cf = PersistentTemporaryFile('.jpeg')
- cf.write(cover)
- cf.close()
- self.cover_file = cf
-
-
- return recs
-
-
-