home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_963 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  3.2 KB  |  92 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. __license__ = 'GPL v3'
  5. __copyright__ = '2008, Kovid Goyal kovid@kovidgoyal.net'
  6. __docformat__ = 'restructuredtext en'
  7. import struct
  8. from zlib import decompress
  9. from lxml import etree
  10. from calibre.ebooks.metadata import MetaInformation, string_to_authors
  11.  
  12. def _read(f, at, amount):
  13.     f.seek(at)
  14.     return f.read(amount)
  15.  
  16.  
  17. def word_be(buf):
  18.     return struct.unpack('>L', buf)[0]
  19.  
  20.  
  21. def word_le(buf):
  22.     return struct.unpack('<L', buf)[0]
  23.  
  24.  
  25. def short_le(buf):
  26.     return struct.unpack('<H', buf)[0]
  27.  
  28.  
  29. def short_be(buf):
  30.     return struct.unpack('>H', buf)[0]
  31.  
  32.  
  33. def get_metadata(f):
  34.     
  35.     read = lambda at, amount: _read(f, at, amount)
  36.     f.seek(0)
  37.     buf = f.read(12)
  38.     if buf[4:] == 'ftypLRX2':
  39.         offset = 0
  40.         while True:
  41.             offset += word_be(buf[:4])
  42.             
  43.             try:
  44.                 buf = read(offset, 8)
  45.             except:
  46.                 (None,)
  47.                 raise ValueError('Not a valid LRX file')
  48.  
  49.             if buf[4:] == 'bbeb':
  50.                 break
  51.                 continue
  52.             (None,)
  53.         offset += 8
  54.         buf = read(offset, 16)
  55.         if buf[:8].decode('utf-16-le') != 'LRF\x00':
  56.             raise ValueError('Not a valid LRX file')
  57.         buf[:8].decode('utf-16-le') != 'LRF\x00'
  58.         lrf_version = word_le(buf[8:12])
  59.         offset += 76
  60.         compressed_size = short_le(read(offset, 2))
  61.         offset += 2
  62.         if lrf_version >= 800:
  63.             offset += 6
  64.         
  65.         compressed_size -= 4
  66.         uncompressed_size = word_le(read(offset, 4))
  67.         info = decompress(f.read(compressed_size))
  68.         if len(info) != uncompressed_size:
  69.             raise ValueError('LRX file has malformed metadata section')
  70.         len(info) != uncompressed_size
  71.         root = etree.fromstring(info)
  72.         bi = root.find('BookInfo')
  73.         title = bi.find('Title')
  74.         title_sort = title.get('reading', None)
  75.         title = title.text
  76.         author = bi.find('Author')
  77.         author_sort = author.get('reading', None)
  78.         mi = MetaInformation(title, string_to_authors(author.text))
  79.         mi.title_sort = title_sort
  80.         mi.author_sort = author_sort
  81.         author = author.text
  82.         publisher = bi.find('Publisher')
  83.         mi.publisher = getattr(publisher, 'text', None)
  84.         mi.tags = [ x.text for x in bi.findall('Category') ]
  85.         mi.language = root.find('DocInfo').find('Language').text
  86.         return mi
  87.     if buf[4:8] == 'LRX':
  88.         raise ValueError('Librie LRX format not supported')
  89.     buf[4:8] == 'LRX'
  90.     raise ValueError('Not a LRX file')
  91.  
  92.