wepub

Check-in [8382ca6fac]
Login

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

Overview
Comment: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.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | master | trunk
Files: files | file ages | folders
SHA3-256:8382ca6fac6dd50a602839dfcbab37f588defdad339dadbc3f9a834a0013952e
User & Date: vandys 2019-01-14 00:16:24
Context
2019-03-09
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
2018-06-29
00:22
We have a styling file, use it. check-in: e4c67a9fe8 user: vandys tags: master, trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to get.py.

4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
..
42
43
44
45
46
47
48

49




50
51
52
53
54
55
56
#
# Structure of paths in the server:
#  /
#	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
import epub
import chore
from urllib import quote_plus, unquote_plus

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

................................................................................
            for f in files:
                # Path to saved state
                fn = path + '/' + f

                # Get contents and modified time
                fd = open(fn, "r")
                buf = fd.read()

                d = json.loads(buf)




                url = d["url"]
                st = os.fstat(fd.fileno())
                fd.close()
                ftm = st.st_mtime

                # Assemble this entry
                newest.append( (url, ftm) )







|







 







>
|
>
>
>
>







4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
..
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#
# Structure of paths in the server:
#  /
#	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

................................................................................
            for f in files:
                # Path to saved state
                fn = path + '/' + f

                # Get contents and modified time
                fd = open(fn, "r")
                buf = fd.read()
		try:
		    d = json.loads(buf)
		except:
		    sys.stderr.write("Bad JSON content: %s\n" % (fn,))
		    fd.close()
		    continue
                url = d["url"]
                st = os.fstat(fd.fileno())
                fd.close()
                ftm = st.st_mtime

                # Assemble this entry
                newest.append( (url, ftm) )

Changes to put.py.

1
2
3
4
5

6
7
8
9
10

























11
12
13
14
15
16
17
18
19
20
21

22
23
24
25
26
27
28
29
30
31
32
33
34
35
36


37
38
39
40
41
42


43
44
45
46
47
48
49
50
51
52
#
# put.py
#       Handle PUT operations to the ePub reader
#
import os, sys, urllib, json


class PUT_mixin(object):

    def __init__(self):
        self.dispatchers.append( ("PUT", self.put_state) )


























    # Here's some fresh state?
    # /state.json?doc=X
    def put_state(self, buf):
        if not self.path_match("state.json"):
            return False,None
        doc = self.vals.get("doc")
        if doc is None:
            return False,None

        # Get a filesystem safe name, see if we can save the info

        try:
            os.mkdir("var/state/%s" % (self.user,))
        except:
            # Already exists, probably
            pass
        doc = urllib.quote_plus(urllib.unquote_plus(doc))
        statepath = "var/state/%s/%s.json" % (self.user, doc)

        # Get existing state
        try:
            f = open(statepath, "r")
            ovals = json.loads(f.read())
            f.close()
        except:
            ovals = {}



        # Update with this state
        newvals = json.loads(buf)
        ovals["url"] = str(newvals["url"])
        ovals["fontSize"] = int(newvals["fontSize"])
        ovals[int(newvals["secnum"])] = float(newvals["pos"])



        # Try and save back
        try:
            f = open(statepath, "w")
            f.write(json.dumps(ovals))
            f.close()
        except:
            sys.stderr.write("Can't save state to %s\n" % (statepath,))

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





>





>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>











>







<
<
|
<
|
<
|
<
>
>

<
<
<
<
<
>
>

<
<
<
<
<
<
<
<

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55


56

57

58

59
60
61





62
63
64








65
#
# put.py
#       Handle PUT operations to the ePub reader
#
import os, sys, urllib, json
from chore.utils import Exclusion

class PUT_mixin(object):

    def __init__(self):
        self.dispatchers.append( ("PUT", self.put_state) )

    # The actual file operations of a PUT; in its own function
    #  so we can mutex the sequence.
    def put_state2(self, doc, statepath, newvals):
        # Get existing state
        try:
            f = open(statepath, "r")
            ovals = json.loads(f.read())
            f.close()
        except:
            ovals = {}

        # Update with this state
        ovals["url"] = str(newvals["url"])
        ovals["fontSize"] = int(newvals["fontSize"])
        ovals[str(int(newvals["secnum"]))] = float(newvals["pos"])

        # Try and save back
        try:
            f = open(statepath, "w")
            f.write(json.dumps(ovals))
            f.close()
        except:
            sys.stderr.write("Can't save state to %s\n" % (statepath,))


    # Here's some fresh state?
    # /state.json?doc=X
    def put_state(self, buf):
        if not self.path_match("state.json"):
            return False,None
        doc = self.vals.get("doc")
        if doc is None:
            return False,None

        # Get a filesystem safe name, see if we can save the info
	sys.stderr.write("put_state: %s\n" % (buf,))
        try:
            os.mkdir("var/state/%s" % (self.user,))
        except:
            # Already exists, probably
            pass
        doc = urllib.quote_plus(urllib.unquote_plus(doc))
        statepath = "var/state/%s/%s.json" % (self.user, doc)


	try:

	    newvals = json.loads(buf)

	except:

	    sys.stderr.write("Bad newvals\n")
	    return True,self.send_error(400)






	with Exclusion():
	    self.put_state2(doc, statepath, newvals)









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