home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_1203 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  11.2 KB  |  296 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 sys
  9. import cPickle
  10. from PyQt4.Qt import QString, SIGNAL, QAbstractListModel, Qt, QVariant, QFont
  11. from calibre.gui2 import ResizableDialog, NONE
  12. from calibre.ebooks.conversion.config import GuiRecommendations, save_specifics, load_specifics
  13. from calibre.gui2.convert.single_ui import Ui_Dialog
  14. from calibre.gui2.convert.metadata import MetadataWidget
  15. from calibre.gui2.convert.look_and_feel import LookAndFeelWidget
  16. from calibre.gui2.convert.page_setup import PageSetupWidget
  17. from calibre.gui2.convert.structure_detection import StructureDetectionWidget
  18. from calibre.gui2.convert.toc import TOCWidget
  19. from calibre.gui2.convert.debug import DebugWidget
  20. from calibre.ebooks.conversion.plumber import Plumber, supported_input_formats
  21. from calibre.ebooks.conversion.config import delete_specifics
  22. from calibre.customize.ui import available_output_formats
  23. from calibre.customize.conversion import OptionRecommendation
  24. from calibre.utils.config import prefs
  25. from calibre.utils.logging import Log
  26.  
  27. class NoSupportedInputFormats(Exception):
  28.     pass
  29.  
  30.  
  31. def sort_formats_by_preference(formats, prefs):
  32.     
  33.     def fcmp(x, y):
  34.         
  35.         try:
  36.             x = prefs.index(x.upper())
  37.         except ValueError:
  38.             x = sys.maxint
  39.  
  40.         
  41.         try:
  42.             y = prefs.index(y.upper())
  43.         except ValueError:
  44.             y = sys.maxint
  45.  
  46.         return cmp(x, y)
  47.  
  48.     return sorted(formats, cmp = fcmp)
  49.  
  50.  
  51. class GroupModel(QAbstractListModel):
  52.     
  53.     def __init__(self, widgets):
  54.         self.widgets = widgets
  55.         QAbstractListModel.__init__(self)
  56.  
  57.     
  58.     def rowCount(self, *args):
  59.         return len(self.widgets)
  60.  
  61.     
  62.     def data(self, index, role):
  63.         
  64.         try:
  65.             widget = self.widgets[index.row()]
  66.         except:
  67.             return NONE
  68.  
  69.         if role == Qt.DisplayRole:
  70.             return QVariant(widget.config_title())
  71.         if role == Qt.DecorationRole:
  72.             return QVariant(widget.config_icon())
  73.         if role == Qt.FontRole:
  74.             f = QFont()
  75.             f.setBold(True)
  76.             return QVariant(f)
  77.         return NONE
  78.  
  79.  
  80.  
  81. def get_preferred_input_format_for_book(db, book_id):
  82.     recs = load_specifics(db, book_id)
  83.     if recs:
  84.         return recs.get('gui_preferred_input_format', None)
  85.  
  86.  
  87. def get_available_formats_for_book(db, book_id):
  88.     available_formats = db.formats(book_id, index_is_id = True)
  89.     if not available_formats:
  90.         available_formats = ''
  91.     
  92.     return []([ x.lower() for x in available_formats.split(',') ])
  93.  
  94.  
  95. def get_supported_input_formats_for_book(db, book_id):
  96.     available_formats = get_available_formats_for_book(db, book_id)
  97.     input_formats = []([ x.lower() for x in supported_input_formats() ])
  98.     input_formats = sorted(available_formats.intersection(input_formats))
  99.     if not input_formats:
  100.         raise NoSupportedInputFormats
  101.     input_formats
  102.     return input_formats
  103.  
  104.  
  105. def get_input_format_for_book(db, book_id, pref):
  106.     if pref is None:
  107.         pref = get_preferred_input_format_for_book(db, book_id)
  108.     
  109.     input_formats = get_supported_input_formats_for_book(db, book_id)
  110.     input_format = None if pref in input_formats else sort_formats_by_preference(input_formats, prefs['input_format_order'])[0]
  111.     return (input_format, input_formats)
  112.  
  113.  
  114. class Config(ResizableDialog, Ui_Dialog):
  115.     
  116.     def __init__(self, parent, db, book_id, preferred_input_format = None, preferred_output_format = None):
  117.         ResizableDialog.__init__(self, parent)
  118.         self.opt_individual_saved_settings.setVisible(False)
  119.         self.db = db
  120.         self.book_id = book_id
  121.         self.setup_input_output_formats(self.db, self.book_id, preferred_input_format, preferred_output_format)
  122.         self.setup_pipeline()
  123.         self.connect(self.input_formats, SIGNAL('currentIndexChanged(QString)'), self.setup_pipeline)
  124.         self.connect(self.output_formats, SIGNAL('currentIndexChanged(QString)'), self.setup_pipeline)
  125.         self.connect(self.groups, SIGNAL('activated(QModelIndex)'), self.show_pane)
  126.         self.connect(self.groups, SIGNAL('clicked(QModelIndex)'), self.show_pane)
  127.         self.connect(self.groups, SIGNAL('entered(QModelIndex)'), self.show_group_help)
  128.         rb = self.buttonBox.button(self.buttonBox.RestoreDefaults)
  129.         self.connect(rb, SIGNAL('clicked()'), self.restore_defaults)
  130.         self.groups.setMouseTracking(True)
  131.  
  132.     
  133.     def restore_defaults(self):
  134.         delete_specifics(self.db, self.book_id)
  135.         self.setup_pipeline()
  136.  
  137.     
  138.     def input_format(self):
  139.         return unicode(self.input_formats.currentText()).lower()
  140.  
  141.     input_format = property(input_format)
  142.     
  143.     def output_format(self):
  144.         return unicode(self.output_formats.currentText()).lower()
  145.  
  146.     output_format = property(output_format)
  147.     
  148.     def setup_pipeline(self, *args):
  149.         oidx = self.groups.currentIndex().row()
  150.         input_format = self.input_format
  151.         output_format = self.output_format
  152.         input_path = self.db.format_abspath(self.book_id, input_format, index_is_id = True)
  153.         self.input_path = input_path
  154.         output_path = 'dummy.' + output_format
  155.         log = Log()
  156.         log.outputs = []
  157.         self.plumber = Plumber(input_path, output_path, log)
  158.         
  159.         def widget_factory(cls):
  160.             return cls(self.stack, self.plumber.get_option_by_name, self.plumber.get_option_help, self.db, self.book_id)
  161.  
  162.         self.mw = widget_factory(MetadataWidget)
  163.         self.setWindowTitle(_('Convert') + ' ' + unicode(self.mw.title.text()))
  164.         lf = widget_factory(LookAndFeelWidget)
  165.         ps = widget_factory(PageSetupWidget)
  166.         sd = widget_factory(StructureDetectionWidget)
  167.         toc = widget_factory(TOCWidget)
  168.         debug = widget_factory(DebugWidget)
  169.         output_widget = None
  170.         name = self.plumber.output_plugin.name.lower().replace(' ', '_')
  171.         
  172.         try:
  173.             output_widget = __import__('calibre.gui2.convert.' + name, fromlist = [
  174.                 1])
  175.             pw = output_widget.PluginWidget
  176.             pw.ICON = I('back.svg')
  177.             pw.HELP = _('Options specific to the output format.')
  178.             output_widget = widget_factory(pw)
  179.         except ImportError:
  180.             (None,)
  181.             (None,)
  182.         except:
  183.             (None,)
  184.  
  185.         input_widget = None
  186.         name = self.plumber.input_plugin.name.lower().replace(' ', '_')
  187.         
  188.         try:
  189.             input_widget = __import__('calibre.gui2.convert.' + name, fromlist = [
  190.                 1])
  191.             pw = input_widget.PluginWidget
  192.             pw.ICON = I('forward.svg')
  193.             pw.HELP = _('Options specific to the input format.')
  194.             input_widget = widget_factory(pw)
  195.         except ImportError:
  196.             (None,)
  197.             (None,)
  198.         except:
  199.             (None,)
  200.  
  201.         while True:
  202.             c = self.stack.currentWidget()
  203.             if not c:
  204.                 break
  205.             
  206.             self.stack.removeWidget(c)
  207.         widgets = [
  208.             self.mw,
  209.             lf,
  210.             ps,
  211.             sd,
  212.             toc]
  213.         if input_widget is not None:
  214.             widgets.append(input_widget)
  215.         
  216.         if output_widget is not None:
  217.             widgets.append(output_widget)
  218.         
  219.         widgets.append(debug)
  220.         for w in widgets:
  221.             self.stack.addWidget(w)
  222.             self.connect(w, SIGNAL('set_help(PyQt_PyObject)'), self.help.setPlainText)
  223.         
  224.         self._groups_model = GroupModel(widgets)
  225.         self.groups.setModel(self._groups_model)
  226.         if oidx < oidx:
  227.             pass
  228.         elif oidx < self._groups_model.rowCount():
  229.             pass
  230.         
  231.         idx = 0
  232.         self.groups.setCurrentIndex(self._groups_model.index(idx))
  233.         self.stack.setCurrentIndex(idx)
  234.  
  235.     
  236.     def setup_input_output_formats(self, db, book_id, preferred_input_format, preferred_output_format):
  237.         if preferred_output_format:
  238.             preferred_output_format = preferred_output_format.lower()
  239.         
  240.         output_formats = sorted(available_output_formats())
  241.         output_formats.remove('oeb')
  242.         (input_format, input_formats) = get_input_format_for_book(db, book_id, preferred_input_format)
  243.         preferred_output_format = None if preferred_output_format in output_formats else sort_formats_by_preference(output_formats, prefs['output_format'])[0]
  244.         map(QString([]([], [ x.upper() for x in input_formats ])))
  245.         map(QString([]([], [ x.upper() for x in output_formats ])))
  246.         self.input_formats.setCurrentIndex(input_formats.index(input_format))
  247.         self.output_formats.setCurrentIndex(output_formats.index(preferred_output_format))
  248.  
  249.     
  250.     def show_pane(self, index):
  251.         self.stack.setCurrentIndex(index.row())
  252.  
  253.     
  254.     def accept(self):
  255.         recs = GuiRecommendations()
  256.         for w in self._groups_model.widgets:
  257.             if not w.pre_commit_check():
  258.                 return None
  259.             x = w.commit(save_defaults = False)
  260.             recs.update(x)
  261.         
  262.         self.opf_file = self.mw.opf_file
  263.         self.cover_file = self.mw.cover_file
  264.         self._recommendations = recs
  265.         if self.db is not None:
  266.             recs['gui_preferred_input_format'] = self.input_format
  267.             save_specifics(self.db, self.book_id, recs)
  268.         
  269.         self.break_cycles()
  270.         ResizableDialog.accept(self)
  271.  
  272.     
  273.     def reject(self):
  274.         self.break_cycles()
  275.         ResizableDialog.reject(self)
  276.  
  277.     
  278.     def break_cycles(self):
  279.         for i in range(self.stack.count()):
  280.             w = self.stack.widget(i)
  281.             w.break_cycles()
  282.         
  283.  
  284.     
  285.     def recommendations(self):
  286.         recs = [ (k, v, OptionRecommendation.HIGH) for k, v in self._recommendations.items() ]
  287.         return cPickle.dumps(recs, -1)
  288.  
  289.     recommendations = property(recommendations)
  290.     
  291.     def show_group_help(self, index):
  292.         widget = self._groups_model.widgets[index.row()]
  293.         self.help.setPlainText(widget.HELP)
  294.  
  295.  
  296.