home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_1053 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  4.2 KB  |  94 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. __license__ = 'GPL v3'
  5. __copyright__ = '2009, John Schember <john@nachtimwald.com>'
  6. __docformat__ = 'restructuredtext en'
  7. import os
  8. import sys
  9. from optparse import OptionGroup, Option
  10. from calibre.ebooks.metadata.meta import metadata_from_formats
  11. from calibre.ebooks.metadata import authors_to_string
  12. from calibre.utils.config import OptionParser
  13. from calibre.utils.logging import Log
  14. from calibre.constants import preferred_encoding
  15. from calibre.customize.conversion import OptionRecommendation
  16. from calibre.ebooks.pdf.verify import is_valid_pdf, is_encrypted
  17. from pyPdf import PdfFileWriter, PdfFileReader
  18. USAGE = '\n%prog %%name ' + _('file.pdf degrees\n\nRotate pages of a PDF clockwise.\n')
  19. OPTIONS = set([
  20.     OptionRecommendation(name = 'output', recommended_value = 'rotated.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.'))])
  21.  
  22. def print_help(parser, log):
  23.     help = parser.format_help().encode(preferred_encoding, 'replace')
  24.     log(help)
  25.  
  26.  
  27. def option_parser(name):
  28.     usage = USAGE.replace('%%name', name)
  29.     return OptionParser(usage = usage)
  30.  
  31.  
  32. def option_recommendation_to_cli_option(add_option, rec):
  33.     opt = rec.option
  34.     switches = None if opt.short_switch else []
  35.     switches.append('--' + opt.long_switch)
  36.     attrs = dict(dest = opt.name, help = opt.help, choices = opt.choices, default = rec.recommended_value)
  37.     add_option(Option(*switches, **attrs))
  38.  
  39.  
  40. def add_options(parser):
  41.     group = OptionGroup(parser, _('Rotate Options:'), _('Options to control the transformation of pdf'))
  42.     parser.add_option_group(group)
  43.     add_option = group.add_option
  44.     for rec in OPTIONS:
  45.         option_recommendation_to_cli_option(add_option, rec)
  46.     
  47.  
  48.  
  49. def rotate(pdf_path, out_path, degrees, metadata = None):
  50.     if metadata == None:
  51.         title = _('Unknown')
  52.         author = _('Unknown')
  53.     else:
  54.         title = metadata.title
  55.         author = authors_to_string(metadata.authors)
  56.     out_pdf = PdfFileWriter(title = title, author = author)
  57.     pdf = PdfFileReader(open(os.path.abspath(pdf_path), 'rb'))
  58.     for page in pdf.pages:
  59.         out_pdf.addPage(page.rotateClockwise(int(degrees)))
  60.     
  61.     
  62.     try:
  63.         out_file = _[1]
  64.         out_pdf.write(out_file)
  65.     finally:
  66.         pass
  67.  
  68.  
  69.  
  70. def main(args = sys.argv, name = ''):
  71.     log = Log()
  72.     parser = option_parser(name)
  73.     add_options(parser)
  74.     (opts, args) = parser.parse_args(args)
  75.     args = args[1:]
  76.     if len(args) < 2:
  77.         print 'Error: A PDF file and how many degrees to rotate is required.\n'
  78.         print_help(parser, log)
  79.         return 1
  80.     if not is_valid_pdf(args[0]):
  81.         print 'Error: Could not read file `%s`.' % args[0]
  82.         return 1
  83.     if is_encrypted(args[0]):
  84.         print 'Error: file `%s` is encrypted.' % args[0]
  85.         return 1
  86.     mi = metadata_from_formats([
  87.         args[0]])
  88.     rotate(args[0], opts.output, args[1], mi)
  89.     return 0
  90.  
  91. if __name__ == '__main__':
  92.     sys.exit(main())
  93.  
  94.