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

« back to all changes in this revision

Viewing changes to src/calibre/ebooks/pdf/verify.py

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2009-07-30 12:49:41 UTC
  • mto: This revision was merged to the branch mainline in revision 13.
  • Revision ID: james.westby@ubuntu.com-20090730124941-kviipg9ypwgppulc
Tags: upstream-0.6.3+dfsg
ImportĀ upstreamĀ versionĀ 0.6.3+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from __future__ import with_statement
 
2
# -*- coding: utf-8 -*-
 
3
 
 
4
__license__   = 'GPL v3'
 
5
__copyright__ = '2009, John Schember <john@nachtimwald.com>'
 
6
__docformat__ = 'restructuredtext en'
 
7
 
 
8
'''
 
9
Verify PDF files.
 
10
'''
 
11
 
 
12
import os
 
13
 
 
14
from pyPdf import PdfFileWriter, PdfFileReader
 
15
 
 
16
def is_valid_pdf(pdf_path):
 
17
    '''
 
18
    Returns True if the pdf file is valid.
 
19
    '''
 
20
    
 
21
    try:
 
22
        with open(os.path.abspath(pdf_path), 'rb') as pdf_file:
 
23
            pdf = PdfFileReader(pdf_file)
 
24
    except:
 
25
        return False
 
26
    return True
 
27
    
 
28
def is_valid_pdfs(pdf_paths):
 
29
    '''
 
30
    Returns a list of invalid pdf files.
 
31
    '''
 
32
    
 
33
    invalid = []
 
34
    for pdf_path in pdf_paths:
 
35
        if not is_valid_pdf(pdf_path):
 
36
            invalid.append(pdf_path)
 
37
    return invalid
 
38
 
 
39
def is_encrypted(pdf_path):
 
40
    with open(os.path.abspath(pdf_path), 'rb') as pdf_file:
 
41
        pdf = PdfFileReader(pdf_file)
 
42
        if pdf.isEncrypted:
 
43
            return True
 
44
    return False