home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_1051 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  4.4 KB  |  107 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_pdfs, is_encrypted
  18. from pyPdf import PdfFileWriter, PdfFileReader
  19. USAGE = '\n%prog %%name ' + _('[options] file1.pdf file2.pdf ...\n\nMetadata will be used from the first PDF specified.\n\nMerges individual PDFs.\n')
  20. OPTIONS = set([
  21.     OptionRecommendation(name = 'output', recommended_value = 'merged.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, _('Merge 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 merge_files(in_paths, 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.     for pdf_path in in_paths:
  59.         pdf = PdfFileReader(open(os.path.abspath(pdf_path), 'rb'))
  60.         for page in pdf.pages:
  61.             out_pdf.addPage(page)
  62.         
  63.     
  64.     
  65.     try:
  66.         out_file = _[1]
  67.         out_pdf.write(out_file)
  68.     finally:
  69.         pass
  70.  
  71.  
  72.  
  73. def main(args = sys.argv, name = ''):
  74.     log = Log()
  75.     parser = option_parser(name)
  76.     add_options(parser)
  77.     (opts, args) = parser.parse_args(args)
  78.     args = args[1:]
  79.     if len(args) < 2:
  80.         print 'Error: Two or more PDF files are required.\n'
  81.         print_help(parser, log)
  82.         return 1
  83.     bad_pdfs = is_valid_pdfs(args)
  84.     if bad_pdfs != []:
  85.         for pdf in bad_pdfs:
  86.             print 'Error: Could not read file `%s`.' % pdf
  87.         
  88.         return 1
  89.     enc = False
  90.     for pdf in args:
  91.         if is_encrypted(pdf):
  92.             enc = True
  93.             print 'Error: file `%s` is encrypted.' % pdf
  94.             continue
  95.         len(args) < 2
  96.     
  97.     if enc:
  98.         return 1
  99.     mi = metadata_from_formats([
  100.         args[0]])
  101.     merge_files(args, opts.output, mi)
  102.     return 0
  103.  
  104. if __name__ == '__main__':
  105.     sys.exit(main())
  106.  
  107.