home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_1438 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  5.2 KB  |  126 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. from calibre.customize.conversion import InputFormatPlugin, OptionRecommendation
  10. from calibre.constants import numeric_version
  11.  
  12. class RecipeDisabled(Exception):
  13.     pass
  14.  
  15.  
  16. class RecipeInput(InputFormatPlugin):
  17.     name = 'Recipe Input'
  18.     author = 'Kovid Goyal'
  19.     description = _('Download periodical content from the internet')
  20.     file_types = set([
  21.         'recipe',
  22.         'downloaded_recipe'])
  23.     recommendations = set([
  24.         ('chapter', None, OptionRecommendation.HIGH),
  25.         ('dont_split_on_page_breaks', True, OptionRecommendation.HIGH),
  26.         ('use_auto_toc', False, OptionRecommendation.HIGH),
  27.         ('input_encoding', None, OptionRecommendation.HIGH),
  28.         ('input_profile', 'default', OptionRecommendation.HIGH),
  29.         ('page_breaks_before', None, OptionRecommendation.HIGH),
  30.         ('insert_metadata', False, OptionRecommendation.HIGH)])
  31.     options = set([
  32.         OptionRecommendation(name = 'test', recommended_value = False, help = _('Useful for recipe development. Forces max_articles_per_feed to 2 and downloads at most 2 feeds.')),
  33.         OptionRecommendation(name = 'username', recommended_value = None, help = _('Username for sites that require a login to access content.')),
  34.         OptionRecommendation(name = 'password', recommended_value = None, help = _('Password for sites that require a login to access content.')),
  35.         OptionRecommendation(name = 'dont_download_recipe', recommended_value = False, help = _('Do not download latest version of builtin recipes from the calibre server')),
  36.         OptionRecommendation(name = 'lrf', recommended_value = False, help = 'Optimize fetching for subsequent conversion to LRF.')])
  37.     
  38.     def convert(self, recipe_or_file, opts, file_ext, log, accelerators):
  39.         compile_recipe = compile_recipe
  40.         import calibre.web.feeds.recipes
  41.         opts.output_profile.flow_size = 0
  42.         if file_ext == 'downloaded_recipe':
  43.             ZipFile = ZipFile
  44.             import calibre.utils.zipfile
  45.             zf = ZipFile(recipe_or_file, 'r')
  46.             zf.extractall()
  47.             zf.close()
  48.             self.recipe_source = open('download.recipe', 'rb').read()
  49.             recipe = compile_recipe(self.recipe_source)
  50.             recipe.needs_subscription = False
  51.             self.recipe_object = recipe(opts, log, self.report_progress)
  52.         elif os.access(recipe_or_file, os.R_OK):
  53.             self.recipe_source = open(recipe_or_file, 'rb').read()
  54.             recipe = compile_recipe(self.recipe_source)
  55.         else:
  56.             get_builtin_recipe_by_title = get_builtin_recipe_by_title
  57.             import calibre.web.feeds.recipes.collection
  58.             title = getattr(opts, 'original_recipe_input_arg', recipe_or_file)
  59.             title = os.path.basename(title).rpartition('.')[0]
  60.             raw = get_builtin_recipe_by_title(title, log = log, download_recipe = not (opts.dont_download_recipe))
  61.             builtin = False
  62.             
  63.             try:
  64.                 recipe = compile_recipe(raw)
  65.                 self.recipe_source = raw
  66.                 if recipe.requires_version > numeric_version:
  67.                     log.warn('Downloaded recipe needs calibre version at least: %s' % '.'.join(recipe.requires_version))
  68.                     builtin = True
  69.             except:
  70.                 log.exception('Failed to compile downloaded recipe. Falling back to builtin one')
  71.                 builtin = True
  72.  
  73.             if builtin:
  74.                 raw = get_builtin_recipe_by_title(title, log = log, download_recipe = False)
  75.                 if raw is None:
  76.                     raise ValueError('Failed to find builtin recipe: ' + title)
  77.                 raw is None
  78.                 recipe = compile_recipe(raw)
  79.                 self.recipe_source = raw
  80.             
  81.         if recipe is None:
  82.             raise ValueError('%r is not a valid recipe file or builtin recipe' % recipe_or_file)
  83.         recipe is None
  84.         ro = recipe(opts, log, self.report_progress)
  85.         disabled = getattr(ro, 'recipe_disabled', None)
  86.         if disabled is not None:
  87.             raise RecipeDisabled(disabled)
  88.         disabled is not None
  89.         ro.download()
  90.         self.recipe_object = ro
  91.         for key, val in self.recipe_object.conversion_options.items():
  92.             setattr(opts, key, val)
  93.         
  94.         for f in os.listdir('.'):
  95.             if f.endswith('.opf'):
  96.                 return os.path.abspath(f)
  97.         
  98.  
  99.     
  100.     def postprocess_book(self, oeb, opts, log):
  101.         if self.recipe_object is not None:
  102.             self.recipe_object.postprocess_book(oeb, opts, log)
  103.         
  104.  
  105.     
  106.     def specialize(self, oeb, opts, log, output_fmt):
  107.         if opts.no_inline_navbars:
  108.             XPath = XPath
  109.             import calibre.ebooks.oeb.base
  110.             for item in oeb.spine:
  111.                 for div in XPath('//h:div[contains(@class, "calibre_navbar")]')(item.data):
  112.                     div.getparent().remove(div)
  113.                 
  114.             
  115.         
  116.  
  117.     
  118.     def save_download(self, zf):
  119.         raw = self.recipe_source
  120.         if isinstance(raw, unicode):
  121.             raw = raw.encode('utf-8')
  122.         
  123.         zf.writestr('download.recipe', raw)
  124.  
  125.  
  126.