wepub

Check-in [91a4709d67]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:uncharenc() across large buffers (like, whole book in one epub "chapter") was very, very slow. Break up conversions to line-by-line, push out converted text incrementally.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | master | trunk
Files: files | file ages | folders
SHA3-256:91a4709d67567a5e383f1225c78d6930a55f0e08790a377d4920191be5a1f02e
User & Date: vandys 2019-03-09 21:48:13
Context
2019-03-09
21:49
Serialize writing to state files, as I seem to get competing writes every now and then. check-in: 34f89ce2a5 user: vandys tags: master, trunk
21:48
uncharenc() across large buffers (like, whole book in one epub "chapter") was very, very slow. Break up conversions to line-by-line, push out converted text incrementally. check-in: 91a4709d67 user: vandys tags: master, trunk
2019-01-14
00:16
Implement explicit thread excusion when updating var/state stuff. Bit by fact that int's/float's can't be keys in JS objects; map back to str of int for secnum storage. Add a bit of debug logging for corrupt JSON. check-in: 8382ca6fac user: vandys tags: master, trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to get.py.

6
7
8
9
10
11
12
13
14

15
16
17
18
19
20
21
...
151
152
153
154
155
156
157


158
159
160
161
162
163
164
165
166
167
168

169
170

171
172
173
174
175
176
177








178























179
180
181
182
183
184
185
#  /
#	Main UI.  Top part is playlist, bottom is file/dir browser
#  /media/<prefix>/...
#	For each prefix of files, its contents is served by
#	way of its path under here.
import os, time, stat, urllib, json, sys
import epub
import chore
from urllib import quote_plus, unquote_plus


# How many of their most recent books to offer
NRECENT = 4

# The GET part of our handling
class GET_mixin(object):

................................................................................
                doc = epub.open_epub(docpath, "r")
                book = epub.Book(doc)
                chap = book.chapters[chapnum]
            except:
                return False,None

            # Here's your chapter


            nm = book.titles[0] if book.titles else parts[-2]
            head = "%s chapter %d" % (nm, chapnum)
            buf = self.build_header(head)
            buf += '<script src="/js/reader.js"></script>\n'
            if chapnum > 0:
                buf += '<a href="%d">Previous Chapter</a>\n' % \
                 (chapnum,)
	    buf += '<link rel="stylesheet" type="text/css"'
	    buf += ' href="/css/epub.css" />\n'
            buf += '<div id="textview">\n'
            buf += chore.utils.uncharenc(chap.read())

            buf += '</div>\n'
            if chapnum < len(book.chapters)-1:

                buf += '<a href="%d">Next Chapter</a>\n' % \
                 (chapnum+2,)
            doc.close()
            buf += '<script>reading("%s", "%s", "%s");</script>\n' % \
                (self.user, docpath, "textview")
            buf = self.build_tailer(buf)









            return True,self.send_result(buf, "text/html")
























        # Index of book?
        if parts and parts[-1].endswith(".epub"):
            try:
                doc = epub.open_epub(
                 os.path.join(path, *parts), "r")
                book = epub.Book(doc)







<

>







 







>
>










|
>
|

>
|

<


|

>
>
>
>
>
>
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







6
7
8
9
10
11
12

13
14
15
16
17
18
19
20
21
...
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176

177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#  /
#	Main UI.  Top part is playlist, bottom is file/dir browser
#  /media/<prefix>/...
#	For each prefix of files, its contents is served by
#	way of its path under here.
import os, time, stat, urllib, json, sys
import epub

from urllib import quote_plus, unquote_plus
from chore.utils import uncharenc, splititer

# How many of their most recent books to offer
NRECENT = 4

# The GET part of our handling
class GET_mixin(object):

................................................................................
                doc = epub.open_epub(docpath, "r")
                book = epub.Book(doc)
                chap = book.chapters[chapnum]
            except:
                return False,None

            # Here's your chapter

	    # Header
            nm = book.titles[0] if book.titles else parts[-2]
            head = "%s chapter %d" % (nm, chapnum)
            buf = self.build_header(head)
            buf += '<script src="/js/reader.js"></script>\n'
            if chapnum > 0:
                buf += '<a href="%d">Previous Chapter</a>\n' % \
                 (chapnum,)
	    buf += '<link rel="stylesheet" type="text/css"'
	    buf += ' href="/css/epub.css" />\n'
            buf += '<div id="textview">\n'
	    buf1 = buf

            buf = '</div>\n'
            if chapnum < len(book.chapters)-1:
                buf += '<a onclick="end_chapter()"' \
		    ' href="%d">Next Chapter</a>\n' % \
                 (chapnum+2,)

            buf += '<script>reading("%s", "%s", "%s");</script>\n' % \
                (self.user, docpath, "textview")
            buf2 = self.build_tailer(buf)

	    # Push it out.
	    # We do it this way because the chapter can be quite
	    #  large (like, megabytes) so we'll do better to push
	    #  it over in parts rather than fill up one huge buffer.
	    # Also, the uncharenc() can be slow if it's a huge buffer
	    #  with a lot of UTF8 code points.

	    # HTTP header
	    self.send_response(200)
	    st = os.stat(docpath)
	    self.send_header("Content-type", "text/html")
	    self.send_header("Last-Modified",
		time.asctime(time.localtime(st.st_mtime)))
	    self.send_header("Cache-Control", "public, max-age=2592000")
	    self.end_headers()

	    # First part of page
	    self.wfile.write(buf1)

	    # Middle; the contents, with characters converted
            buf = chap.read()
	    for l in splititer(buf):
		l = uncharenc(l)
		self.wfile.write(l + '\n')
            doc.close()

	    # The final part of the page, flushed
	    self.wfile.write(buf2)
	    self.wfile.flush()

	    # We handled it, including pushing back the answer
            return True,None

        # Index of book?
        if parts and parts[-1].endswith(".epub"):
            try:
                doc = epub.open_epub(
                 os.path.join(path, *parts), "r")
                book = epub.Book(doc)