src/eric7/RemoteServerInterface/EricServerFileSystemInterface.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 interface to the eric-ide server. 7 Module implementing the file system interface to the eric-ide server.
8 """ 8 """
9 9
10 import base64
10 import contextlib 11 import contextlib
11 12
12 from PyQt6.QtCore import QEventLoop, QObject 13 from PyQt6.QtCore import QEventLoop, QObject
13 14
14 from eric7.RemoteServer.EricRequestCategory import EricRequestCategory 15 from eric7.RemoteServer.EricRequestCategory import EricRequestCategory
15 16
16 17
18 # TODO: sanitize all file name with FileSystemUtilities.plainFileName()
17 class EricServerFileSystemInterface(QObject): 19 class EricServerFileSystemInterface(QObject):
18 """ 20 """
19 Class implementing the file system interface to the eric-ide server. 21 Class implementing the file system interface to the eric-ide server.
20 """ 22 """
21 23
151 ) 153 )
152 154
153 loop.exec() 155 loop.exec()
154 return listedDirectory, separator, listing 156 return listedDirectory, separator, listing
155 157
158 def stat(self, filename, stNames):
159 """
160 Public method to get the status of a file.
161
162 @param filename name of the file
163 @type str
164 @param stNames list of 'stat_result' members to retrieve
165 @type list of str
166 @return dictionary containing the requested status data
167 @rtype dict
168 @exception OSError raised in case the server reported an issue
169 """
170 loop = QEventLoop()
171 ok = False
172 error = ""
173 stResult = {}
174
175 def callback(reply, params):
176 """
177 Function to handle the server reply
178
179 @param reply name of the server reply
180 @type str
181 @param params dictionary containing the reply data
182 @type dict
183 """
184 nonlocal ok, error, stResult
185
186 if reply == "Stat":
187 ok = params["ok"]
188 if ok:
189 stResult = params["result"]
190 else:
191 error = params["error"]
192 loop.quit()
193
194 self.__serverInterface.sendJson(
195 category=EricRequestCategory.FileSystem,
196 request="Stat",
197 params={"filename": filename, "st_names": stNames},
198 callback=callback,
199 )
200
201 loop.exec()
202 if not ok:
203 raise OSError(error)
204
205 return stResult
206
207 def exists(self, name):
208 """
209 Public method the existence of a file or directory.
210
211 @param name name of the file or directory
212 @type str
213 @return flag indicating the file existence
214 @rtype bool
215 """
216 loop = QEventLoop()
217 nameExists = False
218
219 def callback(reply, params):
220 """
221 Function to handle the server reply
222
223 @param reply name of the server reply
224 @type str
225 @param params dictionary containing the reply data
226 @type dict
227 """
228 nonlocal nameExists
229
230 if reply == "Exists":
231 nameExists = params["exists"]
232 loop.quit()
233
234 self.__serverInterface.sendJson(
235 category=EricRequestCategory.FileSystem,
236 request="Exists",
237 params={"name": name},
238 callback=callback,
239 )
240
241 loop.exec()
242 return nameExists
243
244 def access(self, name, modes):
245 """
246 Public method to test the given access rights to a file or directory.
247
248 The modes to check for are 'read', 'write' or 'execute' or any combination.
249
250 @param name name of the file or directory
251 @type str
252 @param modes list of modes to check for
253 @type str or list of str
254 @return flag indicating the user has the asked for permissions
255 @rtype bool
256 """
257 if not modes:
258 raise ValueError(
259 "At least one of 'read', 'write' or 'execute' must be specified."
260 )
261
262 if isinstance(modes, str):
263 # convert to a list with one element
264 modes = [modes]
265
266 loop = QEventLoop()
267 accessOK = False
268
269 def callback(reply, params):
270 """
271 Function to handle the server reply
272
273 @param reply name of the server reply
274 @type str
275 @param params dictionary containing the reply data
276 @type dict
277 """
278 nonlocal accessOK
279
280 if reply == "Access":
281 accessOK = params["ok"]
282 loop.quit()
283
284 self.__serverInterface.sendJson(
285 category=EricRequestCategory.FileSystem,
286 request="Access",
287 params={"name": name, "modes":modes},
288 callback=callback,
289 )
290
291 loop.exec()
292 return accessOK
293
156 def mkdir(self, directory): 294 def mkdir(self, directory):
157 """ 295 """
158 Public method to create a new directory on the eric-ide server. 296 Public method to create a new directory on the eric-ide server.
159 297
160 @param directory absolute path of the new directory 298 @param directory absolute path of the new directory
317 455
318 ####################################################################### 456 #######################################################################
319 ## Methods for reading and writing files 457 ## Methods for reading and writing files
320 ####################################################################### 458 #######################################################################
321 459
322 def readFile(self, filename): 460 def readFile(self, filename, create=False):
323 """ 461 """
324 Public method to read a file from the eric-ide server. 462 Public method to read a file from the eric-ide server.
325 463
326 @param filename name of the file to read 464 @param filename name of the file to read
327 @type str 465 @type str
328 @return tuple containing an OK flag, the read data and an error string in 466 @param create flag indicating to create an empty file, if it does not exist
329 case of an issue 467 (defaults to False)
330 @rtype tuple of (bool, str, str) 468 @type bool (optional)
331 """ 469 @return bytes data read from the eric-ide server
332 # TODO: 'readFile()' not implemented yet 470 @rtype bytes
333 471 @exception OSError raised in case the server reported an issue
334 def writeFile(self, filename, data): 472 """
473 loop = QEventLoop()
474 ok = False
475 error = ""
476 bText = b""
477
478 def callback(reply, params):
479 """
480 Function to handle the server reply
481
482 @param reply name of the server reply
483 @type str
484 @param params dictionary containing the reply data
485 @type dict
486 """
487 nonlocal ok, error, bText
488
489 if reply == "ReadFile":
490 ok = params["ok"]
491 if ok:
492 bText = base64.b85decode(
493 bytes(params["filedata"], encoding="ascii")
494 )
495 else:
496 error = params["error"]
497 loop.quit()
498
499 self.__serverInterface.sendJson(
500 category=EricRequestCategory.FileSystem,
501 request="ReadFile",
502 params={"filename": filename, "create": create},
503 callback=callback,
504 )
505
506 loop.exec()
507 if not ok:
508 raise OSError(error)
509
510 return bText
511
512 def writeFile(self, filename, data, withBackup=False):
335 """ 513 """
336 Public method to write the data to a file on the eric-ide server. 514 Public method to write the data to a file on the eric-ide server.
337 515
338 @param filename name of the file to write 516 @param filename name of the file to write
339 @type str 517 @type str
340 @param data data to be written 518 @param data data to be written
341 @type str 519 @type bytes
342 @return tuple containing an OK flag and an error string in case of an issue 520 @param withBackup flag indicating to create a backup file first
343 @rtype tuple of (bool, str) 521 (defaults to False)
344 """ 522 @type bool (optional)
345 # TODO: 'writeFile()' not implemented yet 523 @exception OSError raised in case the server reported an issue
524 """
525 loop = QEventLoop()
526 ok = False
527 error = ""
528
529 def callback(reply, params):
530 """
531 Function to handle the server reply
532
533 @param reply name of the server reply
534 @type str
535 @param params dictionary containing the reply data
536 @type dict
537 """
538 nonlocal ok, error
539
540 if reply == "WriteFile":
541 ok = params["ok"]
542 with contextlib.suppress(KeyError):
543 error = params["error"]
544 loop.quit()
545
546 self.__serverInterface.sendJson(
547 category=EricRequestCategory.FileSystem,
548 request="WriteFile",
549 params={
550 "filename": filename,
551 "filedata": str(base64.b85encode(data), encoding="ascii"),
552 "with_backup": withBackup,
553 },
554 callback=callback,
555 )
556
557 loop.exec()
558 if not ok:
559 raise OSError(error)

eric ide

mercurial