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

« back to all changes in this revision

Viewing changes to src/calibre/ebooks/djvu/djvu.py

  • Committer: Package Import Robot
  • Author(s): Martin Pitt
  • Date: 2014-05-14 18:17:50 UTC
  • mfrom: (1.5.10)
  • Revision ID: package-import@ubuntu.com-20140514181750-xyrxqa47dbw0qfhu
Tags: 1.36.0+dfsg-1
* New upstream release:
  - Fixes editing of metadata (Closes: #741638)

Show diffs side-by-side

added added

removed removed

Lines of Context:
13
13
 
14
14
import sys
15
15
import struct
16
 
from cStringIO import StringIO
17
16
 
18
 
from .djvubzzdec import BZZDecoder
 
17
from calibre.ebooks.djvu.djvubzzdec import BZZDecoder
 
18
from calibre.constants import plugins
19
19
 
20
20
class DjvuChunk(object):
21
21
    def __init__(self, buf, start, end, align=True, bigendian=True,
22
22
            inclheader=False, verbose=0):
 
23
        self.speedup, err = plugins['bzzdec']
 
24
        if self.speedup is None:
 
25
            raise RuntimeError('Failed to load bzzdec plugin: %s' % err)
23
26
        self.subtype = None
24
27
        self._subchunks = []
25
28
        self.buf = buf
65
68
            out.write(b'%s%s [%d]\n' % (self.type,
66
69
                b':' + self.subtype if self.subtype else b'', self.size))
67
70
        if txtout and self.type == b'TXTz':
68
 
            inbuf = StringIO(self.buf[self.datastart: self.dataend])
69
 
            outbuf = StringIO()
70
 
            decoder = BZZDecoder(inbuf, outbuf)
71
 
            while True:
72
 
                xxres = decoder.convert(1024 * 1024)
73
 
                if not xxres:
74
 
                    break
75
 
            res = outbuf.getvalue()
76
 
            l = 0
77
 
            for x in res[:3]:
78
 
                l <<= 8
79
 
                l += ord(x)
80
 
            if verbose > 0 and out:
81
 
                print >> out, l
82
 
            txtout.write(res[3:3+l])
83
 
            txtout.write(b'\n\f')
 
71
            if True:
 
72
                # Use the C BZZ decode implementation
 
73
                txtout.write(self.speedup.decompress(self.buf[self.datastart:self.dataend]))
 
74
            else:
 
75
                inbuf = bytearray(self.buf[self.datastart: self.dataend])
 
76
                outbuf = bytearray()
 
77
                decoder = BZZDecoder(inbuf, outbuf)
 
78
                while True:
 
79
                    xxres = decoder.convert(1024 * 1024)
 
80
                    if not xxres:
 
81
                        break
 
82
                res = bytes(outbuf)
 
83
                if not res.strip(b'\0'):
 
84
                    raise ValueError('TXTz block is completely null')
 
85
                l = 0
 
86
                for x in res[:3]:
 
87
                    l <<= 8
 
88
                    l += ord(x)
 
89
                if verbose > 0 and out:
 
90
                    print (l, file=out)
 
91
                txtout.write(res[3:3+l])
 
92
            txtout.write(b'\037')
84
93
        if txtout and self.type == b'TXTa':
85
94
            res = self.buf[self.datastart: self.dataend]
86
95
            l = 0
88
97
                l <<= 8
89
98
                l += ord(x)
90
99
            if verbose > 0 and out:
91
 
                print >> out, l
 
100
                print (l, file=out)
92
101
            txtout.write(res[3:3+l])
93
 
            txtout.write(b'\n\f')
 
102
            txtout.write(b'\037')
94
103
        if indent >= maxlevel:
95
104
            return
96
105
        for schunk in self._subchunks:
111
120
        self.dc.dump(out=outfile, maxlevel=maxlevel)
112
121
 
113
122
def main():
114
 
    from ruamel.util.program import Program
115
 
    class DJVUDecoder(Program):
116
 
        def __init__(self):
117
 
            Program.__init__(self)
118
 
 
119
 
        def parser_setup(self):
120
 
            Program.parser_setup(self)
121
 
            #self._argparser.add_argument('--combine', '-c', action=CountAction, const=1, nargs=0)
122
 
            #self._argparser.add_argument('--combine', '-c', type=int, default=1)
123
 
            #self._argparser.add_argument('--segments', '-s', action='append', nargs='+')
124
 
            #self._argparser.add_argument('--force', '-f', action='store_true')
125
 
            #self._argparser.add_argument('classname')
126
 
            self._argparser.add_argument('--text', '-t', action='store_true')
127
 
            self._argparser.add_argument('--dump', type=int, default=0)
128
 
            self._argparser.add_argument('file', nargs='+')
129
 
 
130
 
        def run(self):
131
 
            if self._args.verbose > 1: # can be negative with --quiet
132
 
                print (self._args.file)
133
 
            x = DJVUFile(file(self._args.file[0], 'rb'), verbose=self._args.verbose)
134
 
            if self._args.text:
135
 
                print (x.get_text(sys.stdout))
136
 
            if self._args.dump:
137
 
                x.dump(sys.stdout, maxlevel=self._args.dump)
138
 
            return 0
139
 
 
140
 
    tt = DJVUDecoder()
141
 
    res = tt.result
142
 
    if res != 0:
143
 
        print (res)
 
123
    f = DJVUFile(open(sys.argv[-1], 'rb'))
 
124
    print (f.get_text(sys.stdout))
144
125
 
145
126
if __name__ == '__main__':
146
127
    main()