home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_1185 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  9.1 KB  |  208 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.     raw = metadata_to_opf(mi)
  23.     opf_file = PersistentTemporaryFile('.opf')
  24.     opf_file.write(raw)
  25.     opf_file.close()
  26.     return (mi, opf_file)
  27.  
  28.  
  29. def create_cover_file(db, book_id):
  30.     cover = db.cover(book_id, index_is_id = True)
  31.     cf = None
  32.     if cover:
  33.         cf = PersistentTemporaryFile('.jpeg')
  34.         cf.write(cover)
  35.         cf.close()
  36.     
  37.     return cf
  38.  
  39.  
  40. class MetadataWidget(Widget, Ui_Form):
  41.     TITLE = _('Metadata')
  42.     ICON = I('dialog_information.svg')
  43.     HELP = _('Set the metadata. The output file will contain as much of this metadata as possible.')
  44.     
  45.     def __init__(self, parent, get_option, get_help, db = None, book_id = None):
  46.         Widget.__init__(self, parent, 'metadata', [
  47.             'prefer_metadata_cover'])
  48.         self.db = db
  49.         self.book_id = book_id
  50.         self.cover_changed = False
  51.         self.cover_data = None
  52.         if self.db is not None:
  53.             self.initialize_metadata_options()
  54.         
  55.         self.initialize_options(get_option, get_help, db, book_id)
  56.         self.connect(self.cover_button, SIGNAL('clicked()'), self.select_cover)
  57.  
  58.     
  59.     def deduce_author_sort(self, *args):
  60.         au = unicode(self.author.currentText())
  61.         au = re.sub('\\s+et al\\.$', '', au)
  62.         authors = string_to_authors(au)
  63.         self.author_sort.setText(self.db.author_sort_from_authors(authors))
  64.  
  65.     
  66.     def initialize_metadata_options(self):
  67.         self.initialize_combos()
  68.         self.author.editTextChanged.connect(self.deduce_author_sort)
  69.         mi = self.db.get_metadata(self.book_id, index_is_id = True)
  70.         self.title.setText(mi.title)
  71.         if mi.publisher:
  72.             self.publisher.setCurrentIndex(self.publisher.findText(mi.publisher))
  73.         
  74.         None(self.author_sort.setText if mi.author_sort else '')
  75.         None(self.tags.setText(', '.join if mi.tags else []))
  76.         self.tags.update_tags_cache(self.db.all_tags())
  77.         None(self.comment.setPlainText if mi.comments else '')
  78.         if mi.series:
  79.             self.series.setCurrentIndex(self.series.findText(mi.series))
  80.         
  81.         if mi.series_index is not None:
  82.             
  83.             try:
  84.                 self.series_index.setValue(mi.series_index)
  85.             self.series_index.setValue(1)
  86.  
  87.         
  88.         cover = self.db.cover(self.book_id, index_is_id = True)
  89.         if cover:
  90.             pm = QPixmap()
  91.             pm.loadFromData(cover)
  92.             if not pm.isNull():
  93.                 self.cover.setPixmap(pm)
  94.                 self.cover_data = cover
  95.             
  96.         else:
  97.             self.cover.setPixmap(QPixmap(I('default_cover.svg')))
  98.  
  99.     
  100.     def initialize_combos(self):
  101.         self.initalize_authors()
  102.         self.initialize_series()
  103.         self.initialize_publisher()
  104.  
  105.     
  106.     def initalize_authors(self):
  107.         all_authors = self.db.all_authors()
  108.         all_authors.sort(cmp = (lambda x, y: cmp(x[1], y[1])))
  109.         for i in all_authors:
  110.             (id, name) = i
  111.             name = []([ name.strip().replace('|', ',') for n in name.split(',') ])
  112.             self.author.addItem(name)
  113.         
  114.         au = self.db.authors(self.book_id, True)
  115.         au = []([ a.strip().replace('|', ',') for a in au.split(',') ])
  116.         self.author.setEditText(au)
  117.  
  118.     
  119.     def initialize_series(self):
  120.         all_series = self.db.all_series()
  121.         all_series.sort(cmp = (lambda x, y: cmp(x[1], y[1])))
  122.         for i in all_series:
  123.             (id, name) = i
  124.             self.series.addItem(name)
  125.         
  126.         self.series.setCurrentIndex(-1)
  127.  
  128.     
  129.     def initialize_publisher(self):
  130.         all_publishers = self.db.all_publishers()
  131.         all_publishers.sort(cmp = (lambda x, y: cmp(x[1], y[1])))
  132.         for i in all_publishers:
  133.             (id, name) = i
  134.             self.publisher.addItem(name)
  135.         
  136.         self.publisher.setCurrentIndex(-1)
  137.  
  138.     
  139.     def get_title_and_authors(self):
  140.         title = unicode(self.title.text()).strip()
  141.         if not title:
  142.             title = _('Unknown')
  143.         
  144.         authors = unicode(self.author.text()).strip()
  145.         authors = None if authors else [
  146.             _('Unknown')]
  147.         return (title, authors)
  148.  
  149.     
  150.     def get_metadata(self):
  151.         (title, authors) = self.get_title_and_authors()
  152.         mi = MetaInformation(title, authors)
  153.         publisher = unicode(self.publisher.text()).strip()
  154.         if publisher:
  155.             mi.publisher = publisher
  156.         
  157.         author_sort = unicode(self.author_sort.text()).strip()
  158.         if author_sort:
  159.             mi.author_sort = author_sort
  160.         
  161.         comments = unicode(self.comment.toPlainText()).strip()
  162.         if comments:
  163.             mi.comments = comments
  164.         
  165.         mi.series_index = float(self.series_index.value())
  166.         series = unicode(self.series.currentText()).strip()
  167.         if series:
  168.             mi.series = series
  169.         
  170.         tags = [ t.strip() for t in unicode(self.tags.text()).strip().split(',') ]
  171.         return mi
  172.  
  173.     
  174.     def select_cover(self):
  175.         files = choose_images(self, 'change cover dialog', _('Choose cover for ') + unicode(self.title.text()))
  176.         if not files:
  177.             return None
  178.         _file = files[0]
  179.  
  180.     
  181.     def get_recommendations(self):
  182.         return {
  183.             'prefer_metadata_cover': bool(self.opt_prefer_metadata_cover.isChecked()) }
  184.  
  185.     
  186.     def commit(self, save_defaults = False):
  187.         recs = self.commit_options(save_defaults)
  188.         self.user_mi = self.get_metadata()
  189.         self.cover_file = None
  190.         self.opf_file = None
  191.         if self.db is not None:
  192.             self.db.set_metadata(self.book_id, self.user_mi)
  193.             (self.mi, self.opf_file) = create_opf_file(self.db, self.book_id)
  194.             if self.cover_changed and self.cover_data is not None:
  195.                 self.db.set_cover(self.book_id, self.cover_data)
  196.             
  197.             cover = self.db.cover(self.book_id, index_is_id = True)
  198.             if cover:
  199.                 cf = PersistentTemporaryFile('.jpeg')
  200.                 cf.write(cover)
  201.                 cf.close()
  202.                 self.cover_file = cf
  203.             
  204.         
  205.         return recs
  206.  
  207.  
  208.