~ubuntu-branches/debian/sid/calibre/sid

« back to all changes in this revision

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

  • Committer: Package Import Robot
  • Author(s): Martin Pitt
  • Date: 2014-02-27 07:48:06 UTC
  • mto: This revision was merged to the branch mainline in revision 74.
  • Revision ID: package-import@ubuntu.com-20140227074806-64wdebb3ptosxhhx
Tags: upstream-1.25.0+dfsg
ImportĀ upstreamĀ versionĀ 1.25.0+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
__copyright__ = '2008, Kovid Goyal <kovid at kovidgoyal.net>'
4
4
'''Read meta information from PDF files'''
5
5
 
6
 
#import re
7
 
import os, subprocess, shutil
 
6
import os, subprocess, shutil, re
8
7
from functools import partial
9
8
 
10
9
from calibre import prints
11
10
from calibre.constants import iswindows
12
11
from calibre.ptempfile import TemporaryDirectory
13
 
from calibre.ebooks.metadata import MetaInformation, string_to_authors, check_isbn
 
12
from calibre.ebooks.metadata import (
 
13
    MetaInformation, string_to_authors, check_isbn, check_doi)
14
14
from calibre.utils.ipc.simple_worker import fork_job, WorkerError
15
15
 
16
16
#_isbn_pat = re.compile(r'ISBN[: ]*([-0-9Xx]+)')
32
32
    file, only for src.pdf.'''
33
33
    os.chdir(outputdir)
34
34
    pdfinfo, pdftoppm = get_tools()
 
35
    ans = {}
35
36
 
36
37
    try:
37
 
        raw = subprocess.check_output([pdfinfo, '-enc', 'UTF-8', 'src.pdf'])
 
38
        raw = subprocess.check_output([pdfinfo, '-meta', '-enc', 'UTF-8', 'src.pdf'])
38
39
    except subprocess.CalledProcessError as e:
39
40
        prints('pdfinfo errored out with return code: %d'%e.returncode)
40
41
        return None
 
42
    # The XMP metadata could be in an encoding other than UTF-8, so split it
 
43
    # out before trying to decode raw
 
44
    parts = re.split(br'^Metadata:', raw, 1, flags=re.MULTILINE)
 
45
    if len(parts) > 1:
 
46
        raw, ans['xmp_metadata'] = parts
41
47
    try:
42
48
        raw = raw.decode('utf-8')
43
49
    except UnicodeDecodeError:
44
50
        prints('pdfinfo returned no UTF-8 data')
45
51
        return None
46
52
 
47
 
    ans = {}
48
53
    for line in raw.splitlines():
49
54
        if u':' not in line:
50
55
            continue
127
132
    if subject:
128
133
        mi.tags.insert(0, subject)
129
134
 
 
135
    if 'xmp_metadata' in info:
 
136
        from calibre.ebooks.metadata.xmp import consolidate_metadata
 
137
        mi = consolidate_metadata(mi, info)
 
138
 
 
139
    # Look for recognizable identifiers in the info dict, if they were not
 
140
    # found in the XMP metadata
 
141
    for scheme, check_func in {'doi':check_doi, 'isbn':check_isbn}.iteritems():
 
142
        if scheme not in mi.get_identifiers():
 
143
            for k, v in info.iteritems():
 
144
                if k != 'xmp_metadata':
 
145
                    val = check_func(v)
 
146
                    if val:
 
147
                        mi.set_identifier(scheme, val)
 
148
                        break
 
149
 
130
150
    if cdata:
131
151
        mi.cover_data = ('jpeg', cdata)
132
 
 
133
152
    return mi
134
153
 
135
154
get_quick_metadata = partial(get_metadata, cover=False)