home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 January / maximum-cd-2011-01.iso / DiscContents / calibre-0.7.26.msi / file_951 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-10-31  |  3.3 KB  |  113 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, Anatoly Shipitsin <norguhtar at gmail.com>'
  7. import mimetypes
  8. import os
  9. from base64 import b64decode
  10. from lxml import etree
  11. from calibre.ebooks.metadata import MetaInformation
  12. XLINK_NS = 'http://www.w3.org/1999/xlink'
  13.  
  14. def XLINK(name):
  15.     return '{%s}%s' % (XLINK_NS, name)
  16.  
  17.  
  18. def get_metadata(stream):
  19.     
  20.     XPath = lambda x: etree.XPath(x, namespaces = {
  21. 'fb2': 'http://www.gribuser.ru/xml/fictionbook/2.0',
  22. 'xlink': XLINK_NS })
  23.     
  24.     tostring = lambda x: etree.tostring(x, method = 'text', encoding = unicode).strip()
  25.     parser = etree.XMLParser(recover = True, no_network = True)
  26.     root = etree.fromstring(stream.read(), parser = parser)
  27.     authors = []
  28.     author_sort = None
  29.     for au in XPath('//fb2:author')(root):
  30.         fname = None
  31.         lname = None
  32.         author = None
  33.         fe = XPath('descendant::fb2:first-name')(au)
  34.         if fe:
  35.             fname = tostring(fe[0])
  36.             author = fname
  37.         
  38.         le = XPath('descendant::fb2:last-name')(au)
  39.         if le:
  40.             lname = tostring(le[0])
  41.             if author:
  42.                 author += ' ' + lname
  43.             else:
  44.                 author = lname
  45.         
  46.         if author:
  47.             authors.append(author)
  48.         
  49.         if len(authors) == 1 and author is not None:
  50.             if lname:
  51.                 author_sort = lname
  52.             
  53.             if fname:
  54.                 if author_sort:
  55.                     author_sort += ', ' + fname
  56.                 else:
  57.                     author_sort = fname
  58.             
  59.         fname
  60.     
  61.     title = os.path.splitext(os.path.basename(getattr(stream, 'name', _('Unknown'))))[0]
  62.     for x in XPath('//fb2:book-title')(root):
  63.         title = tostring(x)
  64.     
  65.     comments = ''
  66.     for x in XPath('//fb2:annotation')(root):
  67.         comments += tostring(x)
  68.     
  69.     if not comments:
  70.         comments = None
  71.     
  72.     tags = list(map(tostring, XPath('//fb2:genre')(root)))
  73.     cp = XPath('//fb2:coverpage')(root)
  74.     cdata = None
  75.     if cp:
  76.         cimage = XPath('descendant::fb2:image[@xlink:href]')(cp[0])
  77.         if cimage:
  78.             id = cimage[0].get(XLINK('href')).replace('#', '')
  79.             binary = XPath('//fb2:binary[@id="%s"]' % id)(root)
  80.             if binary:
  81.                 mt = binary[0].get('content-type', 'image/jpeg')
  82.                 exts = mimetypes.guess_all_extensions(mt)
  83.                 if not exts:
  84.                     exts = [
  85.                         '.jpg']
  86.                 
  87.                 cdata = (exts[0][1:], b64decode(tostring(binary[0])))
  88.             
  89.         
  90.     
  91.     series = None
  92.     series_index = 1
  93.     for x in XPath('//fb2:sequence')(root):
  94.         series = x.get('name', None)
  95.         if series is not None:
  96.             series_index = x.get('number', 1)
  97.             break
  98.             continue
  99.     
  100.     mi = MetaInformation(title, authors)
  101.     mi.comments = comments
  102.     mi.author_sort = author_sort
  103.     if tags:
  104.         mi.tags = tags
  105.     
  106.     mi.series = series
  107.     mi.series_index = series_index
  108.     if cdata:
  109.         mi.cover_data = cdata
  110.     
  111.     return mi
  112.  
  113.