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

« back to all changes in this revision

Viewing changes to src/calibre/ebooks/pdb/ereader/inspector.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
Inspect the header of ereader files. This is primarily used for debugging.
 
4
'''
 
5
 
 
6
__license__   = 'GPL v3'
 
7
__copyright__ = '2009, John Schember <john@nachtimwald.com>'
 
8
__docformat__ = 'restructuredtext en'
 
9
 
 
10
import struct
 
11
import sys
 
12
 
 
13
from calibre.ebooks.pdb.ereader import EreaderError
 
14
from calibre.ebooks.pdb.header import PdbHeaderReader
 
15
 
 
16
def ereader_header_info(header):
 
17
    h0 = header.section_data(0)
 
18
 
 
19
    print 'Header Size:     %s' % len(h0)
 
20
 
 
21
    if len(h0) == 132:
 
22
        print 'Header Type:     Dropbook compatible'
 
23
        print ''
 
24
        ereader_header_info132(h0)
 
25
    elif len(h0) == 202:
 
26
        print 'Header Type:     Makebook compatible'
 
27
        print ''
 
28
        ereader_header_info202(h0)
 
29
    else:
 
30
        raise EreaderError('Size mismatch. eReader header record size %i KB is not supported.' % len(h0))
 
31
 
 
32
def pdb_header_info(header):
 
33
    print 'PDB Header Info:'
 
34
    print ''
 
35
    print 'Identity:        %s' % header.ident
 
36
    print 'Total Sectons:   %s' % header.num_sections
 
37
    print 'Title:           %s' % header.title
 
38
    print ''
 
39
 
 
40
def ereader_header_info132(h0):
 
41
    print 'Ereader Record 0 (Header) Info:'
 
42
    print ''
 
43
    print '0-2 Version:             %i' % struct.unpack('>H', h0[0:2])[0]
 
44
    print '2-4:                     %i' % struct.unpack('>H', h0[2:4])[0]
 
45
    print '4-6:                     %i' % struct.unpack('>H', h0[4:6])[0]
 
46
    print '6-8 Codepage:            %i' % struct.unpack('>H', h0[6:8])[0]
 
47
    print '8-10:                    %i' % struct.unpack('>H', h0[8:10])[0]
 
48
    print '10-12:                   %i' % struct.unpack('>H', h0[10:12])[0]
 
49
    print '12-14 Non-Text offset:   %i' % struct.unpack('>H', h0[12:14])[0]
 
50
    print '14-16:                   %i' % struct.unpack('>H', h0[14:16])[0]
 
51
    print '16-18:                   %i' % struct.unpack('>H', h0[16:18])[0]
 
52
    print '18-20:                   %i' % struct.unpack('>H', h0[18:20])[0]
 
53
    print '20-22 Image Count:       %i' % struct.unpack('>H', h0[20:22])[0]
 
54
    print '22-24:                   %i' % struct.unpack('>H', h0[22:24])[0]
 
55
    print '24-26 Has Metadata?:     %i' % struct.unpack('>H', h0[24:26])[0]
 
56
    print '26-28:                   %i' % struct.unpack('>H', h0[26:28])[0]
 
57
    print '28-30 Footnote Count:    %i' % struct.unpack('>H', h0[28:30])[0]
 
58
    print '30-32 Sidebar Count:     %i' % struct.unpack('>H', h0[30:32])[0]
 
59
    print '32-34 Bookmark Offset:   %i' % struct.unpack('>H', h0[32:34])[0]
 
60
    print '34-36 MAGIC:             %i' % struct.unpack('>H', h0[34:36])[0]
 
61
    print '36-38:                   %i' % struct.unpack('>H', h0[36:38])[0]
 
62
    print '38-40:                   %i' % struct.unpack('>H', h0[38:40])[0]
 
63
    print '40-42 Image Data Offset: %i' % struct.unpack('>H', h0[40:42])[0]
 
64
    print '42-44:                   %i' % struct.unpack('>H', h0[42:44])[0]
 
65
    print '44-46 Metadata Offset:   %i' % struct.unpack('>H', h0[44:46])[0]
 
66
    print '46-48:                   %i' % struct.unpack('>H', h0[46:48])[0]
 
67
    print '48-50 Footnote Offset:   %i' % struct.unpack('>H', h0[48:50])[0]
 
68
    print '50-52 Sidebar Offset:    %i' % struct.unpack('>H', h0[50:52])[0]
 
69
    print '52-54 Last Data Offset:  %i' % struct.unpack('>H', h0[52:54])[0]
 
70
 
 
71
    for i in range(54, 131, 2):
 
72
        print '%i-%i:                   %i' % (i, i+2, struct.unpack('>H', h0[i:i+2])[0])
 
73
 
 
74
    print ''
 
75
 
 
76
def ereader_header_info202(h0):
 
77
    print 'Ereader Record 0 (Header) Info:'
 
78
    print ''
 
79
    print '0-2 Version:             %i' % struct.unpack('>H', h0[0:2])[0]
 
80
    print '2-4 Garbage:             %i' % struct.unpack('>H', h0[2:4])[0]
 
81
    print '4-6 Garbage:             %i' % struct.unpack('>H', h0[4:6])[0]
 
82
    print '6-8 Garbage:             %i' % struct.unpack('>H', h0[6:8])[0]
 
83
    print '8-10 Non-Text Offset:    %i' % struct.unpack('>H', h0[8:10])[0]
 
84
    print '10-12:                   %i' % struct.unpack('>H', h0[10:12])[0]
 
85
    print '12-14:                   %i' % struct.unpack('>H', h0[12:14])[0]
 
86
    print '14-16 Garbage:           %i' % struct.unpack('>H', h0[14:16])[0]
 
87
    print '16-18 Garbage:           %i' % struct.unpack('>H', h0[16:18])[0]
 
88
    print '18-20 Garbage:           %i' % struct.unpack('>H', h0[18:20])[0]
 
89
    print '20-22 Garbage:           %i' % struct.unpack('>H', h0[20:22])[0]
 
90
    print '22-24 Garbage:           %i' % struct.unpack('>H', h0[22:24])[0]
 
91
    print '24-26:                   %i' % struct.unpack('>H', h0[24:26])[0]
 
92
    print '26-28:                   %i' % struct.unpack('>H', h0[26:28])[0]
 
93
    for i in range(28, 98, 2):
 
94
        print '%i-%i Garbage:           %i' % (i, i+2, struct.unpack('>H', h0[i:i+2])[0])
 
95
    print '98-100:                  %i' % struct.unpack('>H', h0[98:100])[0]
 
96
    for i in range(100, 110, 2):
 
97
        print '%i-%i Garbage:         %i' % (i, i+2, struct.unpack('>H', h0[i:i+2])[0])
 
98
    print '110-112:                 %i' % struct.unpack('>H', h0[110:112])[0]
 
99
    print '112-114:                 %i' % struct.unpack('>H', h0[112:114])[0]
 
100
    print '114-116 Garbage:         %i' % struct.unpack('>H', h0[114:116])[0]
 
101
    for i in range(116, 202, 2):
 
102
        print '%i-%i:                 %i' % (i, i+2, struct.unpack('>H', h0[i:i+2])[0])
 
103
 
 
104
    print ''
 
105
    print '* Garbage: Random values.'
 
106
    print ''
 
107
    
 
108
 
 
109
def section_lengths(header):
 
110
    print 'Section Sizes'
 
111
    print ''
 
112
 
 
113
    for i in range(0, header.section_count()):
 
114
        size = len(header.section_data(i))
 
115
        if size > 65505:
 
116
            message = '<--- Over!'
 
117
        else:
 
118
            message = ''
 
119
 
 
120
        print 'Section %i:   %i %s' % (i, size, message)
 
121
 
 
122
def main(args=sys.argv):
 
123
    if len(args) < 2:
 
124
        print 'Error: requires input file.'
 
125
        return 1
 
126
 
 
127
    f = open(sys.argv[1], 'rb')
 
128
 
 
129
    pheader = PdbHeaderReader(f)
 
130
 
 
131
    pdb_header_info(pheader)
 
132
    ereader_header_info(pheader)
 
133
    section_lengths(pheader)
 
134
 
 
135
    return 0
 
136
 
 
137
if __name__ == '__main__':
 
138
    sys.exit(main())