~ubuntu-branches/ubuntu/karmic/calibre/karmic

« back to all changes in this revision

Viewing changes to src/calibre/ebooks/pdf/manipulate/reverse.py

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2009-07-30 12:49:41 UTC
  • mfrom: (1.3.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20090730124941-qjdsmri25zt8zocn
Tags: 0.6.3+dfsg-0ubuntu1
* New upstream release. Please see http://calibre.kovidgoyal.net/new_in_6/
  for the list of new features and changes.
* remove_postinstall.patch: Update for new version.
* build_debug.patch: Does not apply any more, disable for now. Might not be
  necessary any more.
* debian/copyright: Fix reference to versionless GPL.
* debian/rules: Drop obsolete dh_desktop call.
* debian/rules: Add workaround for weird Python 2.6 setuptools behaviour of
  putting compiled .so files into src/calibre/plugins/calibre/plugins
  instead of src/calibre/plugins.
* debian/rules: Drop hal fdi moving, new upstream version does not use hal
  any more. Drop hal dependency, too.
* debian/rules: Install udev rules into /lib/udev/rules.d.
* Add debian/calibre.preinst: Remove unmodified
  /etc/udev/rules.d/95-calibre.rules on upgrade.
* debian/control: Bump Python dependencies to 2.6, since upstream needs
  it now.

Show diffs side-by-side

added added

removed removed

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