home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 January / maximum-cd-2011-01.iso / DiscContents / calibre-0.7.26.msi / file_1133 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-10-31  |  8.7 KB  |  230 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. __license__ = 'GPL 3'
  5. __copyright__ = '2010, Li Fanxi <lifanxi@freemindworld.com>'
  6. __docformat__ = 'restructuredtext en'
  7. import os
  8. import string
  9. from lxml import etree
  10. from calibre.customize.conversion import OutputFormatPlugin, OptionRecommendation
  11. from calibre.ptempfile import TemporaryDirectory
  12. from calibre.constants import __appname__, __version__
  13. from calibre.ebooks.snb.snbfile import SNBFile
  14. from calibre.ebooks.snb.snbml import SNBMLizer, ProcessFileName
  15.  
  16. class SNBOutput(OutputFormatPlugin):
  17.     name = 'SNB Output'
  18.     author = 'Li Fanxi'
  19.     file_type = 'snb'
  20.     options = set([
  21.         OptionRecommendation(name = 'snb_output_encoding', recommended_value = 'utf-8', level = OptionRecommendation.LOW, help = _('Specify the character encoding of the output document. The default is utf-8.')),
  22.         OptionRecommendation(name = 'snb_max_line_length', recommended_value = 0, level = OptionRecommendation.LOW, help = _('The maximum number of characters per line. This splits on the first space before the specified value. If no space is found the line will be broken at the space after and will exceed the specified value. Also, there is a minimum of 25 characters. Use 0 to disable line splitting.')),
  23.         OptionRecommendation(name = 'snb_insert_empty_line', recommended_value = False, level = OptionRecommendation.LOW, help = _('Specify whether or not to insert an empty line between two paragraphs.')),
  24.         OptionRecommendation(name = 'snb_indent_first_line', recommended_value = True, level = OptionRecommendation.LOW, help = _('Specify whether or not to insert two space characters to indent the first line of each paragraph.')),
  25.         OptionRecommendation(name = 'snb_hide_chapter_name', recommended_value = False, level = OptionRecommendation.LOW, help = _('Specify whether or not to hide the chapter title for each chapter. Useful for image-only output (eg. comics).'))])
  26.     
  27.     def convert(self, oeb_book, output_path, input_plugin, opts, log):
  28.         self.opts = opts
  29.         SVGRasterizer = SVGRasterizer
  30.         Unavailable = Unavailable
  31.         import calibre.ebooks.oeb.transforms.rasterize
  32.         
  33.         try:
  34.             rasterizer = SVGRasterizer()
  35.             rasterizer(oeb_book, opts)
  36.         except Unavailable:
  37.             log.warn('SVG rasterizer unavailable, SVG will not be converted')
  38.  
  39.         
  40.         try:
  41.             tdir = _[1]
  42.             snbfDir = os.path.join(tdir, 'snbf')
  43.             snbcDir = os.path.join(tdir, 'snbc')
  44.             snbiDir = os.path.join(tdir, 'snbc/images')
  45.             os.mkdir(snbfDir)
  46.             os.mkdir(snbcDir)
  47.             os.mkdir(snbiDir)
  48.             meta = oeb_book.metadata
  49.             authors = _[2]
  50.             if meta.language:
  51.                 lang = unicode(meta.language[0]).upper()
  52.             else:
  53.                 lang = ''
  54.             if meta.description:
  55.                 abstract = unicode(meta.description[0])
  56.             else:
  57.                 abstract = ''
  58.             g = oeb_book.guide
  59.             m = oeb_book.manifest
  60.             s = oeb_book.spine
  61.             href = None
  62.             if 'titlepage' not in g:
  63.                 if 'cover' in g:
  64.                     href = g['cover'].href
  65.                 
  66.             
  67.             bookInfoTree = etree.Element('book-snbf', version = '1.0')
  68.             headTree = etree.SubElement(bookInfoTree, 'head')
  69.             etree.SubElement(headTree, 'name').text = title
  70.             etree.SubElement(headTree, 'author').text = ' '.join(authors)
  71.             etree.SubElement(headTree, 'language').text = lang
  72.             etree.SubElement(headTree, 'rights')
  73.             etree.SubElement(headTree, 'publisher').text = publishers
  74.             etree.SubElement(headTree, 'generator').text = __appname__ + ' ' + __version__
  75.             etree.SubElement(headTree, 'created')
  76.             etree.SubElement(headTree, 'abstract').text = abstract
  77.             if href != None:
  78.                 etree.SubElement(headTree, 'cover').text = ProcessFileName(href)
  79.             else:
  80.                 etree.SubElement(headTree, 'cover')
  81.             bookInfoFile = open(os.path.join(snbfDir, 'book.snbf'), 'wb')
  82.             bookInfoFile.write(etree.tostring(bookInfoTree, pretty_print = True, encoding = 'utf-8'))
  83.             bookInfoFile.close()
  84.             tocInfoTree = etree.Element('toc-snbf')
  85.             tocHead = etree.SubElement(tocInfoTree, 'head')
  86.             tocBody = etree.SubElement(tocInfoTree, 'body')
  87.             outputFiles = { }
  88.             if oeb_book.toc.count() == 0:
  89.                 log.warn('This SNB file has no Table of Contents. Creating a default TOC')
  90.                 first = iter(oeb_book.spine).next()
  91.                 oeb_book.toc.add(_('Start Page'), first.href)
  92.             else:
  93.                 first = iter(oeb_book.spine).next()
  94.                 if oeb_book.toc[0].href != first.href:
  95.                     ch = etree.SubElement(tocBody, 'chapter')
  96.                     ch.set('src', ProcessFileName(first.href) + '.snbc')
  97.                     ch.text = _('Cover Pages')
  98.                     outputFiles[first.href] = []
  99.                     outputFiles[first.href].append(('', _('Cover Pages')))
  100.                 
  101.             for tocitem in oeb_book.toc:
  102.                 if tocitem.href.find('#') != -1:
  103.                     item = string.split(tocitem.href, '#')
  104.                     if len(item) != 2:
  105.                         log.error('Error in TOC item: %s' % tocitem)
  106.                     elif item[0] in outputFiles:
  107.                         outputFiles[item[0]].append((item[1], tocitem.title))
  108.                     else:
  109.                         outputFiles[item[0]] = []
  110.                         if '' not in outputFiles[item[0]]:
  111.                             outputFiles[item[0]].append(('', tocitem.title + _(' (Preface)')))
  112.                             ch = etree.SubElement(tocBody, 'chapter')
  113.                             ch.set('src', ProcessFileName(item[0]) + '.snbc')
  114.                             ch.text = tocitem.title + _(' (Preface)')
  115.                         
  116.                         outputFiles[item[0]].append((item[1], tocitem.title))
  117.                 elif tocitem.href in outputFiles:
  118.                     outputFiles[tocitem.href].append(('', tocitem.title))
  119.                 else:
  120.                     outputFiles[tocitem.href] = []
  121.                     outputFiles[tocitem.href].append(('', tocitem.title))
  122.                 ch = etree.SubElement(tocBody, 'chapter')
  123.                 ch.set('src', ProcessFileName(tocitem.href) + '.snbc')
  124.                 ch.text = tocitem.title
  125.             
  126.             etree.SubElement(tocHead, 'chapters').text = '%d' % len(tocBody)
  127.             tocInfoFile = open(os.path.join(snbfDir, 'toc.snbf'), 'wb')
  128.             tocInfoFile.write(etree.tostring(tocInfoTree, pretty_print = True, encoding = 'utf-8'))
  129.             tocInfoFile.close()
  130.             oldTree = None
  131.             mergeLast = False
  132.             lastName = None
  133.             for item in s:
  134.                 OEB_DOCS = OEB_DOCS
  135.                 OEB_IMAGES = OEB_IMAGES
  136.                 import calibre.ebooks.oeb.base
  137.                 if m.hrefs[item.href].media_type in OEB_DOCS:
  138.                     if item.href not in outputFiles:
  139.                         log.debug('File %s is unused in TOC. Continue in last chapter' % item.href)
  140.                         mergeLast = True
  141.                     elif oldTree != None and mergeLast:
  142.                         log.debug('Output the modified chapter again: %s' % lastName)
  143.                         outputFile = open(os.path.join(snbcDir, lastName), 'wb')
  144.                         outputFile.write(etree.tostring(oldTree, pretty_print = True, encoding = 'utf-8'))
  145.                         outputFile.close()
  146.                         mergeLast = False
  147.                     
  148.                     log.debug('Converting %s to snbc...' % item.href)
  149.                     snbwriter = SNBMLizer(log)
  150.                     snbcTrees = None
  151.                     if not mergeLast:
  152.                         snbcTrees = snbwriter.extract_content(oeb_book, item, outputFiles[item.href], opts)
  153.                         for subName in snbcTrees:
  154.                             postfix = ''
  155.                             if subName != '':
  156.                                 postfix = '_' + subName
  157.                             
  158.                             lastName = ProcessFileName(item.href + postfix + '.snbc')
  159.                             oldTree = snbcTrees[subName]
  160.                             outputFile = open(os.path.join(snbcDir, lastName), 'wb')
  161.                             outputFile.write(etree.tostring(oldTree, pretty_print = True, encoding = 'utf-8'))
  162.                             outputFile.close()
  163.                         
  164.                     else:
  165.                         log.debug('Merge %s with last TOC item...' % item.href)
  166.                         snbwriter.merge_content(oldTree, oeb_book, item, [
  167.                             ('', _('Start'))], opts)
  168.                 mergeLast
  169.             
  170.             log.debug('Output the last modified chapter again: %s' % lastName)
  171.             if oldTree != None and mergeLast:
  172.                 outputFile = open(os.path.join(snbcDir, lastName), 'wb')
  173.                 outputFile.write(etree.tostring(oldTree, pretty_print = True, encoding = 'utf-8'))
  174.                 outputFile.close()
  175.                 mergeLast = False
  176.             
  177.             for item in m:
  178.                 if m.hrefs[item.href].media_type in OEB_IMAGES:
  179.                     log.debug('Converting image: %s ...' % item.href)
  180.                     content = m.hrefs[item.href].data
  181.                     self.HandleImage(content, os.path.join(snbiDir, ProcessFileName(item.href)))
  182.                     continue
  183.             
  184.             snbFile = SNBFile()
  185.             snbFile.FromDir(tdir)
  186.             snbFile.Output(output_path)
  187.         finally:
  188.             pass
  189.  
  190.  
  191.     
  192.     def HandleImage(self, imageData, imagePath):
  193.         Image = Image
  194.         import calibre.utils.magick
  195.         img = Image()
  196.         img.load(imageData)
  197.         (x, y) = img.size
  198.         if self.opts:
  199.             (SCREEN_X, SCREEN_Y) = self.opts.output_profile.comic_screen_size
  200.         else:
  201.             SCREEN_X = 540
  202.             SCREEN_Y = 700
  203.         if x > SCREEN_X or y > SCREEN_Y:
  204.             xScale = float(x) / SCREEN_X
  205.             yScale = float(y) / SCREEN_Y
  206.             scale = max(xScale, yScale)
  207.             img.size = (x / scale, y / scale)
  208.         
  209.         img.save(imagePath)
  210.  
  211.  
  212. if __name__ == '__main__':
  213.     from calibre.ebooks.oeb.reader import OEBReader
  214.     from calibre.ebooks.oeb.base import OEBBook
  215.     from calibre.ebooks.conversion.preprocess import HTMLPreProcessor
  216.     from calibre.customize.profiles import HanlinV3Output
  217.     
  218.     class OptionValues(object):
  219.         pass
  220.  
  221.     opts = OptionValues()
  222.     opts.output_profile = HanlinV3Output(None)
  223.     html_preprocessor = HTMLPreProcessor(None, None, opts)
  224.     from calibre.utils.logging import default_log
  225.     oeb = OEBBook(default_log, html_preprocessor)
  226.     reader = OEBReader
  227.     reader()(oeb, '/tmp/bbb/processed/')
  228.     SNBOutput(None).convert(oeb, '/tmp/test.snb', None, None, default_log)
  229.  
  230.