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

« back to all changes in this revision

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