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 @@
1#!/usr/bin/env python1#!/usr/bin/env python
2# -*- encoding: utf8 -*-
2 3
3# dictserver.py - Serves the contents of a Python dictionary.4# dictserver.py - Serves the contents of a Python dictionary.
4#5#
@@ -38,8 +39,8 @@ class DictStore: · 33 unmodified lines
38 39
39 def get_root_qid(self, fid, afid, uname, aname):40 def get_root_qid(self, fid, afid, uname, aname):
40 41
41 qid = self.make_qid("")42 qid = self.make_qid(u"/")
42 self.set_qid_path(fid, qid, "")43 self.set_qid_path(fid, qid, u"/")
43 self.root_fid = fid44 self.root_fid = fid
44 return qid45 return qid
45 46
@@ -49,14 +50,14 @@ class DictStore: · 3 unmodified lines
49 50
50 def set_qid_path(self, fid, qid, path):51 def set_qid_path(self, fid, qid, path):
51 52
52 path = path.lstrip("/")53 path = path.lstrip(u"/")
53 54
54 self.qids[fid] = qid55 self.qids[fid] = qid
55 self.paths[fid] = path56 self.paths[fid] = path
56 57
57 def make_qid(self, path):58 def make_qid(self, path):
58 59
59 path = path.lstrip("/")60 path = path.lstrip(u"/")
60 obj = self.traverse(path)61 obj = self.traverse(path)
61 62
62 if obj == None:63 if obj == None:
@@ -90,7 +91,7 @@ class DictStore: · 27 unmodified lines
90 91
91 def _stat(self, qid, path):92 def _stat(self, qid, path):
92 93
93 path = path.lstrip("/")94 path = path.lstrip(u"/")
94 95
95 obj = self.traverse(path)96 obj = self.traverse(path)
96 97
@@ -100,16 +101,16 @@ class DictStore: · 3 unmodified lines
100 if type(obj) == dict:101 if type(obj) == dict:
101 mode = 0x80000000102 mode = 0x80000000
102 size = 0103 size = 0
103 mode |= 0555104 mode |= 0o555
104 else:105 else:
105 mode = 0106 mode = 0
106 size = len(obj)107 size = len(obj)
107 mode |= 0444108 mode |= 0o444
108 109
109 name = path.split("/")[-1]110 name = path.split(u"/")[-1]
110 111
111 return styx.Stat(0, 0, qid, mode, self.now, self.now, size, name,112 return styx.Stat(0, 0, qid, mode, self.now, self.now, size, name,
112 "inferno", "inferno", "")113 u"inferno", u"inferno", u"")
113 114
114 def create(self, fid, name, perm):115 def create(self, fid, name, perm):
115 return False116 return False
@@ -137,7 +138,7 @@ class DictStore: · 21 unmodified lines
137 if obj == None:138 if obj == None:
138 return None139 return None
139 140
140 data = ""141 data = b""
141 142
142 if type(obj) == dict:143 if type(obj) == dict:
143 144
@@ -147,12 +148,12 @@ class DictStore: · 3 unmodified lines
147 files.sort()148 files.sort()
148 149
149 for file_name in files:150 for file_name in files:
150 qid = self.make_qid(path + "/" + file_name)151 qid = self.make_qid(path + u"/" + file_name)
151 data += self._stat(qid, path + "/" + file_name).encode()152 data += self._stat(qid, path + u"/" + file_name).encode()
152 153
153 return data[offset:offset + count]154 return data[offset:offset + count]
154 else:155 else:
155 return obj[offset:offset + count]156 return obj[offset:offset + count].encode("utf8")
156 157
157 def write(self, fid, offset, data):158 def write(self, fid, offset, data):
158 159
@@ -160,19 +161,19 @@ class DictStore: · 1 unmodified line
160 return -2161 return -2
161 162
162 def remove(self, fid):163 def remove(self, fid):
163 return False164 return u"Cannot remove dictionary entries."
164 165
165 def wstat(self, fid, st):166 def wstat(self, fid, st):
166 pass167 pass
167 168
168 def traverse(self, path):169 def traverse(self, path):
169 170
170 if path == "":171 if path == u"":
171 return self.d172 return self.d
172 173
173 obj = self.d174 obj = self.d
174 175
175 for element in path.split("/"):176 for element in path.split(u"/"):
176 177
177 try:178 try:
178 obj = obj[element]179 obj = obj[element]
@@ -191,11 +192,12 @@ if __name__ == "__main__": · 12 unmodified lines
191 port = int(sys.argv[1])192 port = int(sys.argv[1])
192 193
193 dictionary = {194 dictionary = {
194 "dir": {195 u"dir": {
195 "hello.txt": "Hello world!\n"196 u"hello.txt": u"Hello world!\n",
197 u"\u263a": u"Forståelse"
196 }198 }
197 }199 }
198 200
199 store = DictStore(dictionary)201 store = DictStore(dictionary)
200 server = styxserver.StyxServer(store)202 server = styxserver.StyxServer(store)
201 server.serve("", port)203 server.serve(b"", port)