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