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

« back to all changes in this revision

Viewing changes to src/calibre/ebooks/metadata/ereader.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
 
 
3
'''
 
4
Read meta information from eReader pdb files.
 
5
'''
 
6
 
 
7
__license__   = 'GPL v3'
 
8
__copyright__ = '2009, John Schember <john@nachtimwald.com>'
 
9
__docformat__ = 'restructuredtext en'
 
10
 
 
11
import struct
 
12
 
 
13
from calibre.ebooks.metadata import MetaInformation
 
14
from calibre.ebooks.metadata import authors_to_string
 
15
from calibre.ebooks.pdb.ereader.reader132 import HeaderRecord
 
16
from calibre.ebooks.pdb.header import PdbHeaderBuilder
 
17
from calibre.ebooks.pdb.header import PdbHeaderReader
 
18
 
 
19
def get_metadata(stream, extract_cover=True):
 
20
    """
 
21
    Return metadata as a L{MetaInfo} object
 
22
    """
 
23
    mi = MetaInformation(None, [_('Unknown')])
 
24
    stream.seek(0)
 
25
 
 
26
    pheader = PdbHeaderReader(stream)
 
27
 
 
28
    # Only Dropbook produced 132 byte record0 files are supported
 
29
    if len(pheader.section_data(0)) == 132:
 
30
        hr = HeaderRecord(pheader.section_data(0))
 
31
 
 
32
        if hr.version in (2, 10) and hr.has_metadata == 1:
 
33
            try:
 
34
                mdata = pheader.section_data(hr.metadata_offset)
 
35
 
 
36
                mdata = mdata.split('\x00')
 
37
                mi.title = mdata[0]
 
38
                mi.authors = [mdata[1]]
 
39
                mi.publisher = mdata[3]
 
40
                mi.isbn = mdata[4]
 
41
            except:
 
42
                pass
 
43
 
 
44
    if not mi.title:
 
45
        mi.title = pheader.title if pheader.title else _('Unknown')
 
46
 
 
47
    return mi
 
48
 
 
49
def set_metadata(stream, mi):
 
50
    pheader = PdbHeaderReader(stream)
 
51
 
 
52
    # Only Dropbook produced 132 byte record0 files are supported
 
53
    if pheader.section_data(0) != 132:
 
54
        return
 
55
 
 
56
    sections = [pheader.section_data(x) for x in range(0, pheader.section_count())]
 
57
    hr = HeaderRecord(sections[0])
 
58
 
 
59
    if hr.version not in (2, 10):
 
60
        return
 
61
 
 
62
    # Create a metadata record for the file if one does not alreay exist
 
63
    if not hr.has_metadata:
 
64
        sections += ['', 'MeTaInFo\x00']
 
65
        last_data = len(sections) - 1
 
66
 
 
67
        for i in range(0, 132, 2):
 
68
            val, = struct.unpack('>H', sections[0][i:i + 2])
 
69
            if val >= hr.last_data_offset:
 
70
                sections[0][i:i + 2] = struct.pack('>H', last_data)
 
71
 
 
72
        sections[0][24:26] = struct.pack('>H', 1) # Set has metadata
 
73
        sections[0][44:46] = struct.pack('>H', last_data - 1) # Set location of metadata
 
74
        sections[0][52:54] = struct.pack('>H', last_data) # Ensure last data offset is updated
 
75
 
 
76
    # Merge the metadata into the file
 
77
    file_mi = get_metadata(stream, False)
 
78
    file_mi.smart_update(mi)
 
79
    sections[hr.metadata_offset] = '%s\x00%s\x00%s\x00%s\x00%s\x00' % \
 
80
        (file_mi.title, authors_to_string(file_mi.authors), '', file_mi.publisher, file_mi.isbn)
 
81
 
 
82
    # Rebuild the PDB wrapper because the offsets have changed due to the
 
83
    # new metadata.
 
84
    pheader_builder = PdbHeaderBuilder(pheader.ident, pheader.title)
 
85
    stream.seek(0)
 
86
    stream.truncate(0)
 
87
    pheader_builder.build_header([len(x) for x in sections], stream)
 
88
 
 
89
    # Write the data back to the file
 
90
    for item in sections:
 
91
        stream.write(item)