src/eric7/RemoteServer/EricServerFileSystemRequestHandler.py

branch
server
changeset 10546
300487f5f517
parent 10539
4274f189ff78
child 10548
d3e21f44887b
equal deleted inserted replaced
10543:6afc0221140a 10546:300487f5f517
5 5
6 """ 6 """
7 Module implementing the file system request handler of the eric-ide server. 7 Module implementing the file system request handler of the eric-ide server.
8 """ 8 """
9 9
10 import base64
11 import contextlib
10 import os 12 import os
11 import stat 13 import stat
12 import time 14 import time
13 15
14 from .EricRequestCategory import EricRequestCategory 16 from .EricRequestCategory import EricRequestCategory
33 "Listdir": self.__listdir, 35 "Listdir": self.__listdir,
34 "Mkdir": self.__mkdir, 36 "Mkdir": self.__mkdir,
35 "Rmdir": self.__rmdir, 37 "Rmdir": self.__rmdir,
36 "Replace": self.__replace, 38 "Replace": self.__replace,
37 "Remove": self.__remove, 39 "Remove": self.__remove,
40 "Stat": self.__stat,
41 "Exists": self.__exists,
42 "Access": self.__access,
43
44 "ReadFile": self.__readFile,
45 "WriteFile": self.__writeFile,
38 } 46 }
39 47
40 def handleRequest(self, request, params, reqestUuid): 48 def handleRequest(self, request, params, reqestUuid):
41 """ 49 """
42 Public method handling the received file system requests. 50 Public method handling the received file system requests.
129 "directory": directory, 137 "directory": directory,
130 "listing": listing, 138 "listing": listing,
131 "separator": os.sep, 139 "separator": os.sep,
132 } 140 }
133 141
142 def __stat(self, params):
143 """
144 Private method to get the status of a file.
145
146 @param params dictionary containing the request data
147 @type dict
148 @return dictionary containing the reply data
149 @rtype dict
150 """
151 filename = params["filename"]
152 statItems = params["st_names"]
153
154 try:
155 result = os.stat(filename)
156 resultDict = {st: getattr(result, st) for st in statItems}
157 return {"ok": True, "result": resultDict}
158 except OSError as err:
159 return {
160 "ok": False,
161 "error": str(err),
162 }
163
164 def __exists(self, params):
165 """
166 Private method to check if a file or directory of the given name exists.
167
168 @param params dictionary containing the request data
169 @type dict
170 @return dictionary containing the reply data
171 @rtype dict
172 """
173 return {"exists": os.path.exists(params["name"])}
174
175 def __access(self, params):
176 """
177 Private method to test, if the eric-ide server has the given access rights
178 to a file or directory..
179
180 @param params dictionary containing the request data
181 @type dict
182 @return dictionary containing the reply data
183 @rtype dict
184 """
185 mode = os.F_OK
186 for modeStr in params["modes"]:
187 if modeStr == "read":
188 mode |= os.R_OK
189 elif modeStr == "write":
190 mode |= os.W_OK
191 elif modeStr == "execute":
192 mode |= os.X_OK
193
194 return {"ok": os.access(params["name"], mode)}
195
134 def __mkdir(self, params): 196 def __mkdir(self, params):
135 """ 197 """
136 Private method to create a new directory. 198 Private method to create a new directory.
137 199
138 @param params dictionary containing the request data 200 @param params dictionary containing the request data
193 "error": str(err), 255 "error": str(err),
194 } 256 }
195 257
196 def __remove(self, params): 258 def __remove(self, params):
197 """ 259 """
198 Private method to delete a directory. 260 Private method to delete a file.
199 261
200 @param params dictionary containing the request data 262 @param params dictionary containing the request data
201 @type dict 263 @type dict
202 @return dictionary containing the reply data 264 @return dictionary containing the reply data
203 @rtype dict 265 @rtype dict
210 except OSError as err: 272 except OSError as err:
211 return { 273 return {
212 "ok": False, 274 "ok": False,
213 "error": str(err), 275 "error": str(err),
214 } 276 }
277
278 def __readFile(self, params):
279 """
280 Private method to read the contents of a file.
281
282 @param params dictionary containing the request data
283 @type dict
284 @return dictionary containing the reply data
285 @rtype dict
286 """
287 filename = params["filename"]
288
289 if params["create"] and not os.path.exists(filename):
290 with open(filename, "wb"):
291 pass
292
293 try:
294 with open(filename, "rb") as f:
295 data = f.read()
296 return {
297 "ok": True,
298 "filedata": str(base64.b85encode(data), encoding="ascii")
299 }
300 except OSError as err:
301 return {
302 "ok": False,
303 "error": str(err),
304 }
305
306 def __writeFile(self, params):
307 """
308 Private method to write data into a file.
309
310 @param params dictionary containing the request data
311 @type dict
312 @return dictionary containing the reply data
313 @rtype dict
314 """
315 filename = params["filename"]
316 data = base64.b85decode(bytes(params["filedata"], encoding="ascii"))
317
318 # 1. create backup file if asked for
319 if params["with_backup"]:
320 if os.path.islink(filename):
321 filename = os.path.realpath(filename)
322 backupFilename = "{0}~".format(filename)
323 try:
324 permissions = os.stat(filename).st_mode
325 perms_valid = True
326 except OSError:
327 # if there was an error, ignore it
328 perms_valid = False
329 with contextlib.suppress(OSError):
330 os.remove(backupFilename)
331 with contextlib.suppress(OSError):
332 os.rename(filename, backupFilename)
333
334 # 2. write the data to the file and reset the permissions
335 try:
336 with open(filename, "wb") as f:
337 f.write(data)
338 if params["with_backup"] and perms_valid:
339 os.chmod(filename, permissions)
340 return {"ok": True}
341 except OSError as err:
342 return {
343 "ok": False,
344 "error": str(err),
345 }

eric ide

mercurial