home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_722 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  7.7 KB  |  218 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.             'pubdate',
  166.             'publisher',
  167.             'rating',
  168.             'series_index',
  169.             'series',
  170.             'size',
  171.             'tags',
  172.             'timestamp',
  173.             'title',
  174.             'uuid'])
  175.         fields = all_fields
  176.         if opts.fields != 'all':
  177.             requested_fields = set(opts.fields.split(','))
  178.             fields = list(all_fields & requested_fields)
  179.         else:
  180.             fields = list(all_fields)
  181.         fields.sort()
  182.         if opts.sort_by and opts.sort_by in fields:
  183.             fields.insert(0, fields.pop(int(fields.index(opts.sort_by))))
  184.         
  185.         return fields
  186.  
  187.     
  188.     def initialize(self):
  189.         builtin_plugins = plugins
  190.         import calibre.customize.builtins
  191.         config = config
  192.         import calibre.customize.ui
  193.         PersistentTemporaryDirectory = PersistentTemporaryDirectory
  194.         import calibre.ptempfile
  195.         if type(self) not in builtin_plugins and self.name not in config['disabled_plugins']:
  196.             files_to_copy = [ '%s.%s' % (self.name.lower(), ext) for ext in [
  197.                 'ui',
  198.                 'py'] ]
  199.             resources = zipfile.ZipFile(self.plugin_path, 'r')
  200.             for file in files_to_copy:
  201.                 
  202.                 try:
  203.                     resources.extract(file, self.resources_path)
  204.                 continue
  205.                 print ' customize:__init__.initialize(): %s not found in %s' % (file, os.path.basename(self.plugin_path))
  206.                 continue
  207.                 continue
  208.  
  209.             
  210.             resources.close()
  211.         
  212.  
  213.     
  214.     def run(self, path_to_output, opts, db, ids, notification = None):
  215.         raise NotImplementedError('CatalogPlugin.generate_catalog() default method, should be overridden in subclass')
  216.  
  217.  
  218.