home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 January / maximum-cd-2011-01.iso / DiscContents / calibre-0.7.26.msi / file_1481 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-10-31  |  3.0 KB  |  102 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__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>'
  7. __docformat__ = 'restructuredtext en'
  8. import sys
  9. import subprocess
  10. import os
  11. import errno
  12. from functools import partial
  13. from contextlib import nested
  14. from calibre.ptempfile import TemporaryFile
  15. from calibre.constants import iswindows
  16. PDFTK = 'pdftk'
  17. popen = subprocess.Popen
  18. if iswindows and hasattr(sys, 'frozen'):
  19.     PDFTK = os.path.join(os.path.dirname(sys.executable), 'pdftk.exe')
  20.     popen = partial(subprocess.Popen, creationflags = 8)
  21.  
  22.  
  23. class PdftkError(Exception):
  24.     pass
  25.  
  26.  
  27. def mi_to_info(mi):
  28.     ans = []
  29.     if mi.title:
  30.         ans.extend(('InfoKey: Title', 'InfoValue: ' + mi.title))
  31.     
  32.     if mi.authors:
  33.         authors_to_string = authors_to_string
  34.         import calibre.ebooks.metadata
  35.         ans.extend(('InfoKey: Author', 'InfoValue: ' + authors_to_string(mi.authors)))
  36.     
  37.     return u'\n'.join(ans)
  38.  
  39.  
  40. def set_metadata(stream, mi):
  41.     raw = mi_to_info(mi)
  42.     if not raw:
  43.         return None
  44.     raw = raw.encode('utf-8')
  45.     
  46.     try:
  47.         (input, output, meta) = _[1]
  48.         oi = getattr(stream, 'name', None)
  49.         
  50.         try:
  51.             f = _[3]
  52.             f.write(raw)
  53.         finally:
  54.             pass
  55.  
  56.         cmd = (PDFTK, input, 'update_info', meta, 'output', output)
  57.         p = popen(cmd)
  58.         while True:
  59.             
  60.             try:
  61.                 p.wait()
  62.             continue
  63.             except OSError:
  64.                 nested(TemporaryFile('.pdf'), TemporaryFile('.pdf'), TemporaryFile('.info')).__exit__ if not oi or not os.access(oi, os.R_OK) else open(input, 'wb') if os.path.exists(output) else open(meta, 'wb')
  65.                 e = nested(TemporaryFile('.pdf'), TemporaryFile('.pdf'), TemporaryFile('.info')).__exit__ if not oi or not os.access(oi, os.R_OK) else open(input, 'wb') if os.path.exists(output) else open(meta, 'wb')
  66.                 nested(TemporaryFile('.pdf'), TemporaryFile('.pdf'), TemporaryFile('.info'))
  67.                 if e.errno == errno.EINTR:
  68.                     continue
  69.                 else:
  70.                     raise 
  71.                 e.errno == errno.EINTR
  72.             
  73.  
  74.             nested(TemporaryFile('.pdf'), TemporaryFile('.pdf'), TemporaryFile('.info')).__exit__ if not oi or not os.access(oi, os.R_OK) else open(input, 'wb') if os.path.exists(output) else open(meta, 'wb')<EXCEPTION MATCH>OSError
  75.         if os.stat(output).st_size < 2048:
  76.             raise PdftkError('Output file too small')
  77.         os.stat(output).st_size < 2048
  78.         
  79.         try:
  80.             f = _[4]
  81.             raw = f.read()
  82.         finally:
  83.             pass
  84.  
  85.         if raw:
  86.             stream.seek(0)
  87.             stream.truncate()
  88.             stream.write(raw)
  89.             stream.flush()
  90.     finally:
  91.         pass
  92.  
  93.  
  94. if __name__ == '__main__':
  95.     args = sys.argv
  96.     from calibre.ebooks.metadata import MetaInformation
  97.     mi = MetaInformation(args[2], [
  98.         args[3]])
  99.     x = open(args[1], 'r+b')
  100.     set_metadata(x, mi)
  101.  
  102.