home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_955 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  3.2 KB  |  109 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.     root = etree.fromstring(stream.read())
  26.     authors = []
  27.     author_sort = None
  28.     for au in XPath('//fb2:author')(root):
  29.         fname = None
  30.         lname = None
  31.         author = None
  32.         fe = XPath('descendant::fb2:first-name')(au)
  33.         if fe:
  34.             fname = tostring(fe[0])
  35.             author = fname
  36.         
  37.         le = XPath('descendant::fb2:last-name')(au)
  38.         if le:
  39.             lname = tostring(le[0])
  40.             author += ' ' + lname
  41.         
  42.         if author:
  43.             authors.append(author)
  44.         
  45.         if len(authors) == 1 and author is not None:
  46.             if lname:
  47.                 author_sort = lname
  48.             
  49.             if fname:
  50.                 if author_sort:
  51.                     author_sort += ', ' + fname
  52.                 else:
  53.                     author_sort = fname
  54.             
  55.         fname
  56.     
  57.     title = os.path.splitext(os.path.basename(getattr(stream, 'name', _('Unknown'))))[0]
  58.     for x in XPath('//fb2:book-title')(root):
  59.         title = tostring(x)
  60.     
  61.     comments = ''
  62.     for x in XPath('//fb2:annotation')(root):
  63.         comments += tostring(x)
  64.     
  65.     if not comments:
  66.         comments = None
  67.     
  68.     tags = list(map(tostring, XPath('//fb2:genre')(root)))
  69.     cp = XPath('//fb2:coverpage')(root)
  70.     cdata = None
  71.     if cp:
  72.         cimage = XPath('descendant::fb2:image[@xlink:href]')(cp[0])
  73.         if cimage:
  74.             id = cimage[0].get(XLINK('href')).replace('#', '')
  75.             binary = XPath('//fb2:binary[@id="%s"]' % id)(root)
  76.             if binary:
  77.                 mt = binary[0].get('content-type', 'image/jpeg')
  78.                 exts = mimetypes.guess_all_extensions(mt)
  79.                 if not exts:
  80.                     exts = [
  81.                         '.jpg']
  82.                 
  83.                 cdata = (exts[0][1:], b64decode(tostring(binary[0])))
  84.             
  85.         
  86.     
  87.     series = None
  88.     series_index = 1
  89.     for x in XPath('//fb2:sequence')(root):
  90.         series = x.get('name', None)
  91.         if series is not None:
  92.             series_index = x.get('number', 1)
  93.             break
  94.             continue
  95.     
  96.     mi = MetaInformation(title, authors)
  97.     mi.comments = comments
  98.     mi.author_sort = author_sort
  99.     if tags:
  100.         mi.tags = tags
  101.     
  102.     mi.series = series
  103.     mi.series_index = series_index
  104.     if cdata:
  105.         mi.cover_data = cdata
  106.     
  107.     return mi
  108.  
  109.