home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 January / maximum-cd-2011-01.iso / DiscContents / calibre-0.7.26.msi / file_714 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-10-31  |  9.0 KB  |  262 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__ = '2008, Kovid Goyal <kovid at kovidgoyal.net>'
  7. import os
  8. import sys
  9. import zipfile
  10. from calibre.constants import numeric_version
  11. from calibre.ptempfile import PersistentTemporaryFile
  12.  
  13. class Plugin(object):
  14.     supported_platforms = []
  15.     name = 'Trivial Plugin'
  16.     version = (1, 0, 0)
  17.     description = _('Does absolutely nothing')
  18.     author = _('Unknown')
  19.     priority = 1
  20.     minimum_calibre_version = (0, 4, 118)
  21.     can_be_disabled = True
  22.     type = _('Base')
  23.     
  24.     def __init__(self, plugin_path):
  25.         self.plugin_path = plugin_path
  26.         self.site_customization = None
  27.  
  28.     
  29.     def initialize(self):
  30.         pass
  31.  
  32.     
  33.     def customization_help(self, gui = False):
  34.         raise NotImplementedError
  35.  
  36.     
  37.     def temporary_file(self, suffix):
  38.         return PersistentTemporaryFile(suffix)
  39.  
  40.     
  41.     def is_customizable(self):
  42.         
  43.         try:
  44.             self.customization_help()
  45.             return True
  46.         except NotImplementedError:
  47.             return False
  48.  
  49.  
  50.     
  51.     def __enter__(self, *args):
  52.         if self.plugin_path is not None:
  53.             ZipFile = ZipFile
  54.             import calibre.utils.zipfile
  55.             zf = ZipFile(self.plugin_path)
  56.             extensions = []([ x.rpartition('.')[-1].lower() for x in zf.namelist() ])
  57.             zip_safe = True
  58.             for ext in ('pyd', 'so', 'dll', 'dylib'):
  59.                 if ext in extensions:
  60.                     zip_safe = False
  61.                     continue
  62.                 []
  63.             
  64.             if zip_safe:
  65.                 sys.path.insert(0, self.plugin_path)
  66.                 self.sys_insertion_path = self.plugin_path
  67.             else:
  68.                 TemporaryDirectory = TemporaryDirectory
  69.                 import calibre.ptempfile
  70.                 self._sys_insertion_tdir = TemporaryDirectory('plugin_unzip')
  71.                 self.sys_insertion_path = self._sys_insertion_tdir.__enter__(*args)
  72.                 zf.extractall(self.sys_insertion_path)
  73.                 sys.path.insert(0, self.sys_insertion_path)
  74.             zf.close()
  75.         
  76.  
  77.     
  78.     def __exit__(self, *args):
  79.         ip = getattr(self, 'sys_insertion_path', None)
  80.         it = getattr(self, '_sys_insertion_tdir', None)
  81.         if ip in sys.path:
  82.             sys.path.remove(ip)
  83.         
  84.         if hasattr(it, '__exit__'):
  85.             it.__exit__(*args)
  86.         
  87.  
  88.  
  89.  
  90. class FileTypePlugin(Plugin):
  91.     file_types = set([])
  92.     on_import = False
  93.     on_preprocess = False
  94.     on_postprocess = False
  95.     type = _('File type')
  96.     
  97.     def run(self, path_to_ebook):
  98.         return path_to_ebook
  99.  
  100.  
  101.  
  102. class MetadataReaderPlugin(Plugin):
  103.     file_types = set([])
  104.     supported_platforms = [
  105.         'windows',
  106.         'osx',
  107.         'linux']
  108.     version = numeric_version
  109.     author = 'Kovid Goyal'
  110.     type = _('Metadata reader')
  111.     
  112.     def __init__(self, *args, **kwargs):
  113.         Plugin.__init__(self, *args, **kwargs)
  114.         self.quick = False
  115.  
  116.     
  117.     def get_metadata(self, stream, type):
  118.         pass
  119.  
  120.  
  121.  
  122. class MetadataWriterPlugin(Plugin):
  123.     file_types = set([])
  124.     supported_platforms = [
  125.         'windows',
  126.         'osx',
  127.         'linux']
  128.     version = numeric_version
  129.     author = 'Kovid Goyal'
  130.     type = _('Metadata writer')
  131.     
  132.     def __init__(self, *args, **kwargs):
  133.         Plugin.__init__(self, *args, **kwargs)
  134.         self.apply_null = False
  135.  
  136.     
  137.     def set_metadata(self, stream, mi, type):
  138.         pass
  139.  
  140.  
  141.  
  142. class CatalogPlugin(Plugin):
  143.     resources_path = None
  144.     file_types = set([])
  145.     type = _('Catalog generator')
  146.     cli_options = []
  147.     
  148.     def search_sort_db(self, db, opts):
  149.         db.search(opts.search_text)
  150.         if opts.sort_by:
  151.             db.sort(opts.sort_by, True)
  152.         
  153.         return db.get_data_as_dict(ids = opts.ids)
  154.  
  155.     
  156.     def get_output_fields(self, opts):
  157.         all_fields = set([
  158.             'author_sort',
  159.             'authors',
  160.             'comments',
  161.             'cover',
  162.             'formats',
  163.             'id',
  164.             'isbn',
  165.             'ondevice',
  166.             'pubdate',
  167.             'publisher',
  168.             'rating',
  169.             'series_index',
  170.             'series',
  171.             'size',
  172.             'tags',
  173.             'timestamp',
  174.             'title',
  175.             'uuid'])
  176.         fields = all_fields
  177.         if opts.fields != 'all':
  178.             requested_fields = set(opts.fields.split(','))
  179.             fields = list(all_fields & requested_fields)
  180.         else:
  181.             fields = list(all_fields)
  182.         if not opts.connected_device['is_device_connected'] and 'ondevice' in fields:
  183.             fields.pop(int(fields.index('ondevice')))
  184.         
  185.         fields.sort()
  186.         if opts.sort_by and opts.sort_by in fields:
  187.             fields.insert(0, fields.pop(int(fields.index(opts.sort_by))))
  188.         
  189.         return fields
  190.  
  191.     
  192.     def initialize(self):
  193.         builtin_plugins = plugins
  194.         import calibre.customize.builtins
  195.         config = config
  196.         import calibre.customize.ui
  197.         PersistentTemporaryDirectory = PersistentTemporaryDirectory
  198.         import calibre.ptempfile
  199.         if type(self) not in builtin_plugins and self.name not in config['disabled_plugins']:
  200.             files_to_copy = [ '%s.%s' % (self.name.lower(), ext) for ext in [
  201.                 'ui',
  202.                 'py'] ]
  203.             resources = zipfile.ZipFile(self.plugin_path, 'r')
  204.             for file in files_to_copy:
  205.                 
  206.                 try:
  207.                     resources.extract(file, self.resources_path)
  208.                 continue
  209.                 print ' customize:__init__.initialize(): %s not found in %s' % (file, os.path.basename(self.plugin_path))
  210.                 continue
  211.                 continue
  212.  
  213.             
  214.             resources.close()
  215.         
  216.  
  217.     
  218.     def run(self, path_to_output, opts, db, ids, notification = None):
  219.         raise NotImplementedError('CatalogPlugin.generate_catalog() default method, should be overridden in subclass')
  220.  
  221.  
  222.  
  223. class InterfaceActionBase(Plugin):
  224.     supported_platforms = [
  225.         'windows',
  226.         'osx',
  227.         'linux']
  228.     author = 'Kovid Goyal'
  229.     type = _('User Interface Action')
  230.     can_be_disabled = False
  231.     actual_plugin = None
  232.  
  233.  
  234. class PreferencesPlugin(Plugin):
  235.     supported_platforms = [
  236.         'windows',
  237.         'osx',
  238.         'linux']
  239.     author = 'Kovid Goyal'
  240.     type = _('Preferences')
  241.     can_be_disabled = False
  242.     config_widget = None
  243.     category_order = 100
  244.     name_order = 100
  245.     category = None
  246.     gui_category = None
  247.     gui_name = None
  248.     icon = None
  249.     description = None
  250.     
  251.     def create_widget(self, parent = None):
  252.         (base, _, wc) = self.config_widget.partition(':')
  253.         if not wc:
  254.             wc = 'ConfigWidget'
  255.         
  256.         base = __import__(base, fromlist = [
  257.             1])
  258.         widget = getattr(base, wc)
  259.         return widget(parent)
  260.  
  261.  
  262.