home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_946 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  4.8 KB  |  161 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__ = '2010, Kovid Goyal <kovid@kovidgoyal.net>'
  7. __docformat__ = 'restructuredtext en'
  8. import os
  9. from contextlib import closing
  10. from calibre.customize import FileTypePlugin
  11. from calibre.utils.zipfile import ZipFile, stringFileHeader
  12.  
  13. def is_comic(list_of_names):
  14.     extensions = []([ x.rpartition('.')[-1].lower() for x in list_of_names ])
  15.     comic_extensions = set([
  16.         'jpg',
  17.         'jpeg',
  18.         'png'])
  19.     return len(extensions - comic_extensions) == 0
  20.  
  21.  
  22. def archive_type(stream):
  23.     
  24.     try:
  25.         pos = stream.tell()
  26.     except:
  27.         pos = 0
  28.  
  29.     id_ = stream.read(4)
  30.     ans = None
  31.     if id_ == stringFileHeader:
  32.         ans = 'zip'
  33.     elif id_.startswith('Rar'):
  34.         ans = 'rar'
  35.     
  36.     
  37.     try:
  38.         stream.seek(pos)
  39.     except:
  40.         pass
  41.  
  42.     return ans
  43.  
  44.  
  45. class ArchiveExtract(FileTypePlugin):
  46.     name = 'Archive Extract'
  47.     author = 'Kovid Goyal'
  48.     description = _('Extract common e-book formats from archives (zip/rar) files. Also try to autodetect if they are actually cbz/cbr files.')
  49.     file_types = set([
  50.         'zip',
  51.         'rar'])
  52.     supported_platforms = [
  53.         'windows',
  54.         'osx',
  55.         'linux']
  56.     on_import = True
  57.     
  58.     def run(self, archive):
  59.         is_rar = archive.lower().endswith('.rar')
  60.         if is_rar:
  61.             extract_member = extract_member
  62.             names = names
  63.             import calibre.libunrar
  64.         else:
  65.             zf = ZipFile(archive, 'r')
  66.         if is_rar:
  67.             fnames = names(archive)
  68.         else:
  69.             fnames = zf.namelist()
  70.         fnames = _[1]
  71.         if is_comic(fnames):
  72.             ext = [] if is_rar else '.cbz'
  73.             of = self.temporary_file('_archive_extract' + ext)
  74.             
  75.             try:
  76.                 f = _[2]
  77.                 of.write(f.read())
  78.             finally:
  79.                 pass
  80.  
  81.             of.close()
  82.             return of.name
  83.         if len(fnames) > 1 or not fnames:
  84.             return archive
  85.         fname = fnames[0]
  86.         ext = os.path.splitext(fname)[1][1:]
  87.         if ext.lower() not in ('lit', 'epub', 'mobi', 'prc', 'rtf', 'pdf', 'mp3', 'pdb', 'azw', 'azw1'):
  88.             return archive
  89.         of = self.temporary_file('_archive_extract.' + ext)
  90.         closing(of).__enter__()
  91.         
  92.         try:
  93.             if is_rar:
  94.                 data = extract_member(archive, match = None, name = fname)[1]
  95.                 of.write(data)
  96.             else:
  97.                 of.write(zf.read(fname))
  98.         finally:
  99.             pass
  100.  
  101.         return of.name
  102.  
  103.  
  104.  
  105. def get_comic_book_info(d, mi):
  106.     series = d.get('series', '')
  107.     if series.strip():
  108.         mi.series = series
  109.         if d.get('volume', -1) > -1:
  110.             mi.series_index = float(d['volume'])
  111.         
  112.     
  113.     if d.get('rating', -1) > -1:
  114.         mi.rating = d['rating']
  115.     
  116.     for x in ('title', 'publisher'):
  117.         y = d.get(x, '').strip()
  118.         if y:
  119.             setattr(mi, x, y)
  120.             continue
  121.     
  122.     tags = d.get('tags', [])
  123.     if tags:
  124.         mi.tags = tags
  125.     
  126.     authors = []
  127.     for credit in d.get('credits', []):
  128.         if credit.get('role', '') in ('Writer', 'Artist', 'Cartoonist', 'Creator'):
  129.             x = credit.get('person', '')
  130.             if x:
  131.                 x = ' '.join(reversed(x.split(', ')))
  132.                 authors.append(x)
  133.             
  134.         x
  135.     
  136.     if authors:
  137.         mi.authors = authors
  138.     
  139.  
  140.  
  141. def get_cbz_metadata(stream):
  142.     ZipFile = ZipFile
  143.     import calibre.utils.zipfile
  144.     MetaInformation = MetaInformation
  145.     import calibre.ebooks.metadata
  146.     import json
  147.     zf = ZipFile(stream)
  148.     mi = MetaInformation(None, None)
  149.     if zf.comment:
  150.         m = json.loads(zf.comment)
  151.         if hasattr(m, 'keys'):
  152.             for cat in m.keys():
  153.                 if cat.startswith('ComicBookInfo'):
  154.                     get_comic_book_info(m[cat], mi)
  155.                     continue
  156.             
  157.         
  158.     
  159.     return mi
  160.  
  161.