History

Improved handling of strings, converting to and from Unicode strings where needed and using byte strings when sending socket data and interacting with the operating system. Tried to make the modules compatible with both Python 2 and Python 3.

David Boddie committed 8 years ago

Commit 20f573c1 parent c2476e84 files · +66 −51Browse files

Showing the changes to dictserver.py (1 of 4 files). Show all

Unified · Split

dictserver.py+21−19
@@ -1,4 +1,5 @@
11 #!/usr/bin/env python
2+# -*- encoding: utf8 -*-
23
34 # dictserver.py - Serves the contents of a Python dictionary.
45 #
@@ -38,8 +39,8 @@ class DictStore: · 33 unmodified lines
3839
3940 def get_root_qid(self, fid, afid, uname, aname):
4041
41− qid = self.make_qid("")
42− self.set_qid_path(fid, qid, "")
42+ qid = self.make_qid(u"/")
43+ self.set_qid_path(fid, qid, u"/")
4344 self.root_fid = fid
4445 return qid
4546
@@ -49,14 +50,14 @@ class DictStore: · 3 unmodified lines
4950
5051 def set_qid_path(self, fid, qid, path):
5152
52− path = path.lstrip("/")
53+ path = path.lstrip(u"/")
5354
5455 self.qids[fid] = qid
5556 self.paths[fid] = path
5657
5758 def make_qid(self, path):
5859
59− path = path.lstrip("/")
60+ path = path.lstrip(u"/")
6061 obj = self.traverse(path)
6162
6263 if obj == None:
@@ -90,7 +91,7 @@ class DictStore: · 27 unmodified lines
9091
9192 def _stat(self, qid, path):
9293
93− path = path.lstrip("/")
94+ path = path.lstrip(u"/")
9495
9596 obj = self.traverse(path)
9697
@@ -100,16 +101,16 @@ class DictStore: · 3 unmodified lines
100101 if type(obj) == dict:
101102 mode = 0x80000000
102103 size = 0
103− mode |= 0555
104+ mode |= 0o555
104105 else:
105106 mode = 0
106107 size = len(obj)
107− mode |= 0444
108+ mode |= 0o444
108109
109− name = path.split("/")[-1]
110+ name = path.split(u"/")[-1]
110111
111112 return styx.Stat(0, 0, qid, mode, self.now, self.now, size, name,
112− "inferno", "inferno", "")
113+ u"inferno", u"inferno", u"")
113114
114115 def create(self, fid, name, perm):
115116 return False
@@ -137,7 +138,7 @@ class DictStore: · 21 unmodified lines
137138 if obj == None:
138139 return None
139140
140− data = ""
141+ data = b""
141142
142143 if type(obj) == dict:
143144
@@ -147,12 +148,12 @@ class DictStore: · 3 unmodified lines
147148 files.sort()
148149
149150 for file_name in files:
150− qid = self.make_qid(path + "/" + file_name)
151− data += self._stat(qid, path + "/" + file_name).encode()
151+ qid = self.make_qid(path + u"/" + file_name)
152+ data += self._stat(qid, path + u"/" + file_name).encode()
152153
153154 return data[offset:offset + count]
154155 else:
155− return obj[offset:offset + count]
156+ return obj[offset:offset + count].encode("utf8")
156157
157158 def write(self, fid, offset, data):
158159
@@ -160,19 +161,19 @@ class DictStore: · 1 unmodified line
160161 return -2
161162
162163 def remove(self, fid):
163− return False
164+ return u"Cannot remove dictionary entries."
164165
165166 def wstat(self, fid, st):
166167 pass
167168
168169 def traverse(self, path):
169170
170− if path == "":
171+ if path == u"":
171172 return self.d
172173
173174 obj = self.d
174175
175− for element in path.split("/"):
176+ for element in path.split(u"/"):
176177
177178 try:
178179 obj = obj[element]
@@ -191,11 +192,12 @@ if __name__ == "__main__": · 12 unmodified lines
191192 port = int(sys.argv[1])
192193
193194 dictionary = {
194− "dir": {
195− "hello.txt": "Hello world!\n"
195+ u"dir": {
196+ u"hello.txt": u"Hello world!\n",
197+ u"\u263a": u"Forståelse"
196198 }
197199 }
198200
199201 store = DictStore(dictionary)
200202 server = styxserver.StyxServer(store)
201− server.serve("", port)
203+ server.serve(b"", port)