home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_1052 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  4.2 KB  |  95 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.ebooks.metadata.meta import metadata_from_formats
  12. from calibre.ebooks.metadata import authors_to_string
  13. from calibre.utils.config import OptionParser
  14. from calibre.utils.logging import Log
  15. from calibre.constants import preferred_encoding
  16. from calibre.customize.conversion import OptionRecommendation
  17. from calibre.ebooks.pdf.verify import is_valid_pdf, is_encrypted
  18. from pyPdf import PdfFileWriter, PdfFileReader
  19. USAGE = '\n%prog %%name ' + _('[options] file.pdf\n\nReverse a PDF.\n')
  20. OPTIONS = set([
  21.     OptionRecommendation(name = 'output', recommended_value = 'reversed.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.'))])
  22.  
  23. def print_help(parser, log):
  24.     help = parser.format_help().encode(preferred_encoding, 'replace')
  25.     log(help)
  26.  
  27.  
  28. def option_parser(name):
  29.     usage = USAGE.replace('%%name', name)
  30.     return OptionParser(usage = usage)
  31.  
  32.  
  33. def option_recommendation_to_cli_option(add_option, rec):
  34.     opt = rec.option
  35.     switches = None if opt.short_switch else []
  36.     switches.append('--' + opt.long_switch)
  37.     attrs = dict(dest = opt.name, help = opt.help, choices = opt.choices, default = rec.recommended_value)
  38.     add_option(Option(*switches, **attrs))
  39.  
  40.  
  41. def add_options(parser):
  42.     group = OptionGroup(parser, _('Reverse Options:'), _('Options to control the transformation of pdf'))
  43.     parser.add_option_group(group)
  44.     add_option = group.add_option
  45.     for rec in OPTIONS:
  46.         option_recommendation_to_cli_option(add_option, rec)
  47.     
  48.  
  49.  
  50. def reverse(pdf_path, out_path, metadata = None):
  51.     if metadata == None:
  52.         title = _('Unknown')
  53.         author = _('Unknown')
  54.     else:
  55.         title = metadata.title
  56.         author = authors_to_string(metadata.authors)
  57.     out_pdf = PdfFileWriter(title = title, author = author)
  58.     pdf = PdfFileReader(open(os.path.abspath(pdf_path), 'rb'))
  59.     for page in reversed(pdf.pages):
  60.         out_pdf.addPage(page)
  61.     
  62.     
  63.     try:
  64.         out_file = _[1]
  65.         out_pdf.write(out_file)
  66.     finally:
  67.         pass
  68.  
  69.  
  70.  
  71. def main(args = sys.argv, name = ''):
  72.     log = Log()
  73.     parser = option_parser(name)
  74.     add_options(parser)
  75.     (opts, args) = parser.parse_args(args)
  76.     args = args[1:]
  77.     if len(args) < 1:
  78.         print 'Error: A PDF file is required.\n'
  79.         print_help(parser, log)
  80.         return 1
  81.     if not is_valid_pdf(args[0]):
  82.         print 'Error: Could not read file `%s`.' % args[0]
  83.         return 1
  84.     if is_encrypted(args[0]):
  85.         print 'Error: file `%s` is encrypted.' % args[0]
  86.         return 1
  87.     mi = metadata_from_formats([
  88.         args[0]])
  89.     reverse(args[0], opts.output, mi)
  90.     return 0
  91.  
  92. if __name__ == '__main__':
  93.     sys.exit(main())
  94.  
  95.