home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 January / maximum-cd-2011-01.iso / DiscContents / calibre-0.7.26.msi / file_1211 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-10-31  |  9.2 KB  |  212 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. from __future__ import with_statement
  5. __license__ = 'GPL v3'
  6. __copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>'
  7. __docformat__ = 'restructuredtext en'
  8. import os
  9. import uuid
  10. import re
  11. from PyQt4.Qt import QPixmap, SIGNAL
  12. from calibre.gui2 import choose_images, error_dialog
  13. from calibre.gui2.convert.metadata_ui import Ui_Form
  14. from calibre.ebooks.metadata import authors_to_string, string_to_authors, MetaInformation
  15. from calibre.ebooks.metadata.opf2 import metadata_to_opf
  16. from calibre.ptempfile import PersistentTemporaryFile
  17. from calibre.gui2.convert import Widget
  18.  
  19. def create_opf_file(db, book_id):
  20.     mi = db.get_metadata(book_id, index_is_id = True)
  21.     mi.application_id = uuid.uuid4()
  22.     old_cover = mi.cover
  23.     mi.cover = None
  24.     raw = metadata_to_opf(mi)
  25.     mi.cover = old_cover
  26.     opf_file = PersistentTemporaryFile('.opf')
  27.     opf_file.write(raw)
  28.     opf_file.close()
  29.     return (mi, opf_file)
  30.  
  31.  
  32. def create_cover_file(db, book_id):
  33.     cover = db.cover(book_id, index_is_id = True)
  34.     cf = None
  35.     if cover:
  36.         cf = PersistentTemporaryFile('.jpeg')
  37.         cf.write(cover)
  38.         cf.close()
  39.     
  40.     return cf
  41.  
  42.  
  43. class MetadataWidget(Widget, Ui_Form):
  44.     TITLE = _('Metadata')
  45.     ICON = I('dialog_information.png')
  46.     HELP = _('Set the metadata. The output file will contain as much of this metadata as possible.')
  47.     COMMIT_NAME = 'metadata'
  48.     
  49.     def __init__(self, parent, get_option, get_help, db = None, book_id = None):
  50.         Widget.__init__(self, parent, [
  51.             'prefer_metadata_cover'])
  52.         self.db = db
  53.         self.book_id = book_id
  54.         self.cover_changed = False
  55.         self.cover_data = None
  56.         if self.db is not None:
  57.             self.initialize_metadata_options()
  58.         
  59.         self.initialize_options(get_option, get_help, db, book_id)
  60.         self.connect(self.cover_button, SIGNAL('clicked()'), self.select_cover)
  61.  
  62.     
  63.     def deduce_author_sort(self, *args):
  64.         au = unicode(self.author.currentText())
  65.         au = re.sub('\\s+et al\\.$', '', au)
  66.         authors = string_to_authors(au)
  67.         self.author_sort.setText(self.db.author_sort_from_authors(authors))
  68.  
  69.     
  70.     def initialize_metadata_options(self):
  71.         self.initialize_combos()
  72.         self.author.editTextChanged.connect(self.deduce_author_sort)
  73.         mi = self.db.get_metadata(self.book_id, index_is_id = True)
  74.         self.title.setText(mi.title)
  75.         if mi.publisher:
  76.             self.publisher.setCurrentIndex(self.publisher.findText(mi.publisher))
  77.         
  78.         None(self.author_sort.setText if mi.author_sort else '')
  79.         None(self.tags.setText(', '.join if mi.tags else []))
  80.         self.tags.update_tags_cache(self.db.all_tags())
  81.         None(self.comment.setPlainText if mi.comments else '')
  82.         if mi.series:
  83.             self.series.setCurrentIndex(self.series.findText(mi.series))
  84.         
  85.         if mi.series_index is not None:
  86.             
  87.             try:
  88.                 self.series_index.setValue(mi.series_index)
  89.             self.series_index.setValue(1)
  90.  
  91.         
  92.         cover = self.db.cover(self.book_id, index_is_id = True)
  93.         if cover:
  94.             pm = QPixmap()
  95.             pm.loadFromData(cover)
  96.             if not pm.isNull():
  97.                 self.cover.setPixmap(pm)
  98.                 self.cover_data = cover
  99.             
  100.         else:
  101.             self.cover.setPixmap(QPixmap(I('default_cover.png')))
  102.  
  103.     
  104.     def initialize_combos(self):
  105.         self.initalize_authors()
  106.         self.initialize_series()
  107.         self.initialize_publisher()
  108.  
  109.     
  110.     def initalize_authors(self):
  111.         all_authors = self.db.all_authors()
  112.         all_authors.sort(cmp = (lambda x, y: cmp(x[1], y[1])))
  113.         for i in all_authors:
  114.             (id, name) = i
  115.             name = []([ name.strip().replace('|', ',') for n in name.split(',') ])
  116.             self.author.addItem(name)
  117.         
  118.         au = self.db.authors(self.book_id, True)
  119.         au = []([ a.strip().replace('|', ',') for a in au.split(',') ])
  120.         self.author.setEditText(au)
  121.  
  122.     
  123.     def initialize_series(self):
  124.         all_series = self.db.all_series()
  125.         all_series.sort(cmp = (lambda x, y: cmp(x[1], y[1])))
  126.         for i in all_series:
  127.             (id, name) = i
  128.             self.series.addItem(name)
  129.         
  130.         self.series.setCurrentIndex(-1)
  131.  
  132.     
  133.     def initialize_publisher(self):
  134.         all_publishers = self.db.all_publishers()
  135.         all_publishers.sort(cmp = (lambda x, y: cmp(x[1], y[1])))
  136.         for i in all_publishers:
  137.             (id, name) = i
  138.             self.publisher.addItem(name)
  139.         
  140.         self.publisher.setCurrentIndex(-1)
  141.  
  142.     
  143.     def get_title_and_authors(self):
  144.         title = unicode(self.title.text()).strip()
  145.         if not title:
  146.             title = _('Unknown')
  147.         
  148.         authors = unicode(self.author.text()).strip()
  149.         authors = None if authors else [
  150.             _('Unknown')]
  151.         return (title, authors)
  152.  
  153.     
  154.     def get_metadata(self):
  155.         (title, authors) = self.get_title_and_authors()
  156.         mi = MetaInformation(title, authors)
  157.         publisher = unicode(self.publisher.text()).strip()
  158.         if publisher:
  159.             mi.publisher = publisher
  160.         
  161.         author_sort = unicode(self.author_sort.text()).strip()
  162.         if author_sort:
  163.             mi.author_sort = author_sort
  164.         
  165.         comments = unicode(self.comment.toPlainText()).strip()
  166.         if comments:
  167.             mi.comments = comments
  168.         
  169.         mi.series_index = float(self.series_index.value())
  170.         series = unicode(self.series.currentText()).strip()
  171.         if series:
  172.             mi.series = series
  173.         
  174.         tags = [ t.strip() for t in unicode(self.tags.text()).strip().split(',') ]
  175.         return mi
  176.  
  177.     
  178.     def select_cover(self):
  179.         files = choose_images(self, 'change cover dialog', _('Choose cover for ') + unicode(self.title.text()))
  180.         if not files:
  181.             return None
  182.         _file = files[0]
  183.  
  184.     
  185.     def get_recommendations(self):
  186.         return {
  187.             'prefer_metadata_cover': bool(self.opt_prefer_metadata_cover.isChecked()) }
  188.  
  189.     
  190.     def commit(self, save_defaults = False):
  191.         recs = self.commit_options(save_defaults)
  192.         self.user_mi = self.get_metadata()
  193.         self.cover_file = None
  194.         self.opf_file = None
  195.         if self.db is not None:
  196.             self.db.set_metadata(self.book_id, self.user_mi)
  197.             (self.mi, self.opf_file) = create_opf_file(self.db, self.book_id)
  198.             if self.cover_changed and self.cover_data is not None:
  199.                 self.db.set_cover(self.book_id, self.cover_data)
  200.             
  201.             cover = self.db.cover(self.book_id, index_is_id = True)
  202.             if cover:
  203.                 cf = PersistentTemporaryFile('.jpeg')
  204.                 cf.write(cover)
  205.                 cf.close()
  206.                 self.cover_file = cf
  207.             
  208.         
  209.         return recs
  210.  
  211.  
  212.