home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 January / maximum-cd-2011-01.iso / DiscContents / calibre-0.7.26.msi / file_1046 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-10-31  |  4.7 KB  |  110 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, John Schember <john@nachtimwald.com>'
  7. __docformat__ = 'restructuredtext en'
  8. import os
  9. import sys
  10. from optparse import OptionGroup, Option
  11. from calibre.utils.config import OptionParser
  12. from calibre.utils.logging import Log
  13. from calibre.constants import preferred_encoding
  14. from calibre.customize.conversion import OptionRecommendation
  15. from calibre.ebooks.pdf.verify import is_valid_pdf, is_encrypted
  16. from pyPdf import PdfFileWriter, PdfFileReader
  17. USAGE = '\n%prog %%name ' + _('[options] file.pdf password\n\nDecrypt a PDF.\n')
  18. OPTIONS = set([
  19.     OptionRecommendation(name = 'output', recommended_value = 'decrypted.pdf', level = OptionRecommendation.HIGH, long_switch = 'output', short_switch = 'o', help = _('Path to output file. By default a file is created in the current directory.'))])
  20.  
  21. class DecryptionError(Exception):
  22.     
  23.     def __init__(self, pdf_path):
  24.         self.value = 'Unable to decrypt file `%s`.' % pdf_path
  25.  
  26.     
  27.     def __str__(self):
  28.         return repr(self.value)
  29.  
  30.  
  31.  
  32. def print_help(parser, log):
  33.     help = parser.format_help().encode(preferred_encoding, 'replace')
  34.     log(help)
  35.  
  36.  
  37. def option_parser(name):
  38.     usage = USAGE.replace('%%name', name)
  39.     return OptionParser(usage = usage)
  40.  
  41.  
  42. def option_recommendation_to_cli_option(add_option, rec):
  43.     opt = rec.option
  44.     switches = None if opt.short_switch else []
  45.     switches.append('--' + opt.long_switch)
  46.     attrs = dict(dest = opt.name, help = opt.help, choices = opt.choices, default = rec.recommended_value)
  47.     add_option(Option(*switches, **attrs))
  48.  
  49.  
  50. def add_options(parser):
  51.     group = OptionGroup(parser, _('Decrypt Options:'), _('Options to control the transformation of pdf'))
  52.     parser.add_option_group(group)
  53.     add_option = group.add_option
  54.     for rec in OPTIONS:
  55.         option_recommendation_to_cli_option(add_option, rec)
  56.     
  57.  
  58.  
  59. def decrypt(pdf_path, out_path, password):
  60.     pdf = PdfFileReader(open(os.path.abspath(pdf_path), 'rb'))
  61.     if pdf.decrypt(str(password)) == 0:
  62.         raise DecryptionError(pdf_path)
  63.     pdf.decrypt(str(password)) == 0
  64.     title = None if pdf.documentInfo.title else _('Unknown')
  65.     author = None if pdf.documentInfo.author else _('Unknown')
  66.     out_pdf = PdfFileWriter(title = title, author = author)
  67.     for page in pdf.pages:
  68.         out_pdf.addPage(page)
  69.     
  70.     
  71.     try:
  72.         out_file = _[1]
  73.         out_pdf.write(out_file)
  74.     finally:
  75.         pass
  76.  
  77.  
  78.  
  79. def main(args = sys.argv, name = ''):
  80.     log = Log()
  81.     parser = option_parser(name)
  82.     add_options(parser)
  83.     (opts, args) = parser.parse_args(args)
  84.     args = args[1:]
  85.     if len(args) < 2:
  86.         print 'Error: A PDF file and decryption password is required.\n'
  87.         print_help(parser, log)
  88.         return 1
  89.     if not is_valid_pdf(args[0]):
  90.         print 'Error: Could not read file `%s`.' % args[0]
  91.         return 1
  92.     if not is_encrypted(args[0]):
  93.         print 'Error: file `%s` is not encrypted.' % args[0]
  94.         return 1
  95.     
  96.     try:
  97.         decrypt(args[0], opts.output, args[1])
  98.     except DecryptionError:
  99.         is_encrypted(args[0])
  100.         e = is_encrypted(args[0])
  101.         is_valid_pdf(args[0])
  102.         print e.value
  103.         return 1
  104.  
  105.     return 0
  106.  
  107. if __name__ == '__main__':
  108.     sys.exit(main())
  109.  
  110.