home *** CD-ROM | disk | FTP | other *** search
Wrap
# Source Generated with Decompyle++ # File: in.pyc (Python 2.6) from __future__ import with_statement __license__ = 'GPL v3' __copyright__ = '2009, John Schember <john@nachtimwald.com>' __docformat__ = 'restructuredtext en' import os import sys from optparse import OptionGroup, Option from calibre.utils.config import OptionParser from calibre.utils.logging import Log from calibre.constants import preferred_encoding from calibre.customize.conversion import OptionRecommendation from calibre.ebooks.pdf.verify import is_valid_pdf, is_encrypted from pyPdf import PdfFileWriter, PdfFileReader USAGE = '\n%prog %%name ' + _('[options] file.pdf password\n\nDecrypt a PDF.\n') OPTIONS = set([ 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.'))]) class DecryptionError(Exception): def __init__(self, pdf_path): self.value = 'Unable to decrypt file `%s`.' % pdf_path def __str__(self): return repr(self.value) def print_help(parser, log): help = parser.format_help().encode(preferred_encoding, 'replace') log(help) def option_parser(name): usage = USAGE.replace('%%name', name) return OptionParser(usage = usage) def option_recommendation_to_cli_option(add_option, rec): opt = rec.option switches = None if opt.short_switch else [] switches.append('--' + opt.long_switch) attrs = dict(dest = opt.name, help = opt.help, choices = opt.choices, default = rec.recommended_value) add_option(Option(*switches, **attrs)) def add_options(parser): group = OptionGroup(parser, _('Decrypt Options:'), _('Options to control the transformation of pdf')) parser.add_option_group(group) add_option = group.add_option for rec in OPTIONS: option_recommendation_to_cli_option(add_option, rec) def decrypt(pdf_path, out_path, password): pdf = PdfFileReader(open(os.path.abspath(pdf_path), 'rb')) if pdf.decrypt(str(password)) == 0: raise DecryptionError(pdf_path) pdf.decrypt(str(password)) == 0 title = None if pdf.documentInfo.title else _('Unknown') author = None if pdf.documentInfo.author else _('Unknown') out_pdf = PdfFileWriter(title = title, author = author) for page in pdf.pages: out_pdf.addPage(page) try: out_file = _[1] out_pdf.write(out_file) finally: pass def main(args = sys.argv, name = ''): log = Log() parser = option_parser(name) add_options(parser) (opts, args) = parser.parse_args(args) args = args[1:] if len(args) < 2: print 'Error: A PDF file and decryption password is required.\n' print_help(parser, log) return 1 if not is_valid_pdf(args[0]): print 'Error: Could not read file `%s`.' % args[0] return 1 if not is_encrypted(args[0]): print 'Error: file `%s` is not encrypted.' % args[0] return 1 try: decrypt(args[0], opts.output, args[1]) except DecryptionError: is_encrypted(args[0]) e = is_encrypted(args[0]) is_valid_pdf(args[0]) print e.value return 1 return 0 if __name__ == '__main__': sys.exit(main())