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

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. __license__ = 'GPL v3'
  5. __copyright__ = '2009, John Schember <john@nachtimwald.com>'
  6. __docformat__ = 'restructuredtext en'
  7. import os
  8. import glob
  9. import re
  10. from calibre.ebooks.metadata import MetaInformation
  11. from calibre.ptempfile import TemporaryDirectory
  12. from calibre.utils.zipfile import ZipFile
  13. from calibre import prepare_string_for_xml
  14.  
  15. def get_metadata(stream, extract_cover = True):
  16.     mi = MetaInformation(_('Unknown'), [
  17.         _('Unknown')])
  18.     stream.seek(0)
  19.     pml = ''
  20.     if stream.name.endswith('.pmlz'):
  21.         
  22.         try:
  23.             tdir = _[1]
  24.             zf = ZipFile(stream)
  25.             zf.extractall(tdir)
  26.             pmls = glob.glob(os.path.join(tdir, '*.pml'))
  27.             for p in pmls:
  28.                 
  29.                 try:
  30.                     p_stream = _[2]
  31.                     pml += p_stream.read()
  32.                 finally:
  33.                     pass
  34.  
  35.             
  36.             if extract_cover:
  37.                 mi.cover_data = get_cover(os.path.splitext(os.path.basename(stream.name))[0], tdir, True)
  38.         finally:
  39.             pass
  40.  
  41.     else:
  42.         pml = stream.read()
  43.         if extract_cover:
  44.             mi.cover_data = get_cover(os.path.splitext(os.path.basename(stream.name))[0], os.path.abspath(os.path.dirname(stream.name)))
  45.         
  46.     for comment in re.findall('(?mus)\\\\v.*?\\\\v', pml):
  47.         m = re.search('TITLE="(.*?)"', comment)
  48.         if m:
  49.             mi.title = re.sub('[\x00-\x1f]', '', prepare_string_for_xml(m.group(1).strip().decode('cp1252', 'replace')))
  50.         
  51.         m = re.search('AUTHOR="(.*?)"', comment)
  52.         if m:
  53.             if mi.authors == [
  54.                 _('Unknown')]:
  55.                 mi.authors = []
  56.             
  57.             mi.authors.append(re.sub('[\x00-\x1f]', '', prepare_string_for_xml(m.group(1).strip().decode('cp1252', 'replace'))))
  58.         
  59.         m = re.search('PUBLISHER="(.*?)"', comment)
  60.         if m:
  61.             mi.publisher = re.sub('[\x00-\x1f]', '', prepare_string_for_xml(m.group(1).strip().decode('cp1252', 'replace')))
  62.         
  63.         m = re.search('COPYRIGHT="(.*?)"', comment)
  64.         if m:
  65.             mi.rights = re.sub('[\x00-\x1f]', '', prepare_string_for_xml(m.group(1).strip().decode('cp1252', 'replace')))
  66.         
  67.         m = re.search('ISBN="(.*?)"', comment)
  68.         if m:
  69.             mi.isbn = re.sub('[\x00-\x1f]', '', prepare_string_for_xml(m.group(1).strip().decode('cp1252', 'replace')))
  70.             continue
  71.     
  72.     return mi
  73.  
  74.  
  75. def get_cover(name, tdir, top_level = False):
  76.     cover_path = ''
  77.     cover_data = None
  78.     if top_level:
  79.         cover_path = None if os.path.exists(os.path.join(tdir, 'cover.png')) else ''
  80.     
  81.     if not cover_path:
  82.         if os.path.exists(os.path.join(tdir, name + '_img', 'cover.png')):
  83.             pass
  84.         elif os.path.exists(os.path.join(os.path.join(tdir, 'images'), 'cover.png')):
  85.             pass
  86.         
  87.         cover_path = ''
  88.     
  89.     return ('png', cover_data)
  90.  
  91.