src/eric7/RemoteServerInterface/EricServerFileSystemInterface.py

branch
server
changeset 10539
4274f189ff78
child 10546
300487f5f517
equal deleted inserted replaced
10531:3308e8349e4c 10539:4274f189ff78
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2024 Detlev Offenbach <detlev@die-offenbachs.de>
4 #
5
6 """
7 Module implementing the file system interface to the eric-ide server.
8 """
9
10 import contextlib
11
12 from PyQt6.QtCore import QEventLoop, QObject
13
14 from eric7.RemoteServer.EricRequestCategory import EricRequestCategory
15
16
17 class EricServerFileSystemInterface(QObject):
18 """
19 Class implementing the file system interface to the eric-ide server.
20 """
21
22 def __init__(self, serverInterface):
23 """
24 Constructor
25
26 @param serverInterface reference to the eric-ide server interface
27 @type EricServerInterface
28 """
29 super().__init__(parent=serverInterface)
30
31 self.__serverInterface = serverInterface
32
33 def getcwd(self):
34 """
35 Public method to get the current working directory of the eric-ide server.
36
37 @return current working directory of the eric-ide server
38 @rtype str
39 """
40 loop = QEventLoop()
41 cwd = ""
42
43 def callback(reply, params):
44 """
45 Function to handle the server reply
46
47 @param reply name of the server reply
48 @type str
49 @param params dictionary containing the reply data
50 @type dict
51 """
52 nonlocal cwd
53
54 if reply == "Getcwd":
55 cwd = params["directory"]
56 loop.quit()
57
58 self.__serverInterface.sendJson(
59 category=EricRequestCategory.FileSystem,
60 request="Getcwd",
61 params={},
62 callback=callback,
63 )
64
65 loop.exec()
66 return cwd
67
68 def chdir(self, directory):
69 """
70 Public method to change the current working directory of the eric-ide server.
71
72 @param directory absolute path of the working directory to change to
73 @type str
74 @return tuple containing an OK flag and an error string in case of an issue
75 @rtype tuple of (bool, str)
76 """
77 loop = QEventLoop()
78 ok = False
79 error = ""
80
81 def callback(reply, params):
82 """
83 Function to handle the server reply
84
85 @param reply name of the server reply
86 @type str
87 @param params dictionary containing the reply data
88 @type dict
89 """
90 nonlocal ok, error
91
92 if reply == "Chdir":
93 ok = params["ok"]
94 with contextlib.suppress(KeyError):
95 error = params["error"]
96 loop.quit()
97
98 self.__serverInterface.sendJson(
99 category=EricRequestCategory.FileSystem,
100 request="Chdir",
101 params={"directory": directory},
102 callback=callback,
103 )
104
105 loop.exec()
106 return ok, error
107
108 def listdir(self, directory=""):
109 """
110 Public method to get a directory listing.
111
112 @param directory directory to be listed. An empty directory means to list
113 the eric-ide server current directory. (defaults to "")
114 @type str (optional)
115 @return tuple containing the listed directory, the path separartor and the
116 directory listing. Each directory listing entry contains a dictionary
117 with the relevant data.
118 @rtype tuple of (str, str, dict)
119 """
120 if directory is None:
121 # sanitize the directory in case it is None
122 directory = ""
123
124 loop = QEventLoop()
125 listedDirectory = ""
126 separator = ""
127 listing = []
128
129 def callback(reply, params):
130 """
131 Function to handle the server reply
132
133 @param reply name of the server reply
134 @type str
135 @param params dictionary containing the reply data
136 @type dict
137 """
138 nonlocal listedDirectory, listing, separator
139
140 if reply == "Listdir":
141 listedDirectory = params["directory"]
142 listing = params["listing"]
143 separator = params["separator"]
144 loop.quit()
145
146 self.__serverInterface.sendJson(
147 category=EricRequestCategory.FileSystem,
148 request="Listdir",
149 params={"directory": directory},
150 callback=callback,
151 )
152
153 loop.exec()
154 return listedDirectory, separator, listing
155
156 def mkdir(self, directory):
157 """
158 Public method to create a new directory on the eric-ide server.
159
160 @param directory absolute path of the new directory
161 @type str
162 @return tuple containing an OK flag and an error string in case of an issue
163 @rtype tuple of (bool, str)
164 """
165 loop = QEventLoop()
166 ok = False
167 error = ""
168
169 def callback(reply, params):
170 """
171 Function to handle the server reply
172
173 @param reply name of the server reply
174 @type str
175 @param params dictionary containing the reply data
176 @type dict
177 """
178 nonlocal ok, error
179
180 if reply == "Mkdir":
181 ok = params["ok"]
182 with contextlib.suppress(KeyError):
183 error = params["error"]
184 loop.quit()
185
186 self.__serverInterface.sendJson(
187 category=EricRequestCategory.FileSystem,
188 request="Mkdir",
189 params={"directory": directory},
190 callback=callback,
191 )
192
193 loop.exec()
194 return ok, error
195
196 def rmdir(self, directory):
197 """
198 Public method to delete a directory on the eric-ide server.
199
200 @param directory absolute path of the directory
201 @type str
202 @return tuple containing an OK flag and an error string in case of an issue
203 @rtype tuple of (bool, str)
204 """
205 loop = QEventLoop()
206 ok = False
207 error = ""
208
209 def callback(reply, params):
210 """
211 Function to handle the server reply
212
213 @param reply name of the server reply
214 @type str
215 @param params dictionary containing the reply data
216 @type dict
217 """
218 nonlocal ok, error
219
220 if reply == "Rmdir":
221 ok = params["ok"]
222 with contextlib.suppress(KeyError):
223 error = params["error"]
224 loop.quit()
225
226 self.__serverInterface.sendJson(
227 category=EricRequestCategory.FileSystem,
228 request="Rmdir",
229 params={"directory": directory},
230 callback=callback,
231 )
232
233 loop.exec()
234 return ok, error
235
236 def replace(self, oldName, newName):
237 """
238 Public method to rename a file or directory.
239
240 @param oldName current name of the file or directory
241 @type str
242 @param newName new name for the file or directory
243 @type str
244 @return tuple containing an OK flag and an error string in case of an issue
245 @rtype tuple of (bool, str)
246 """
247 loop = QEventLoop()
248 ok = False
249 error = ""
250
251 def callback(reply, params):
252 """
253 Function to handle the server reply
254
255 @param reply name of the server reply
256 @type str
257 @param params dictionary containing the reply data
258 @type dict
259 """
260 nonlocal ok, error
261
262 if reply == "Replace":
263 ok = params["ok"]
264 with contextlib.suppress(KeyError):
265 error = params["error"]
266 loop.quit()
267
268 self.__serverInterface.sendJson(
269 category=EricRequestCategory.FileSystem,
270 request="Replace",
271 params={"old_name": oldName, "new_name": newName},
272 callback=callback,
273 )
274
275 loop.exec()
276 return ok, error
277
278 def remove(self, filename):
279 """
280 Public method to delete a file on the eric-ide server.
281
282 @param filename absolute path of the file
283 @type str
284 @return tuple containing an OK flag and an error string in case of an issue
285 @rtype tuple of (bool, str)
286 """
287 loop = QEventLoop()
288 ok = False
289 error = ""
290
291 def callback(reply, params):
292 """
293 Function to handle the server reply
294
295 @param reply name of the server reply
296 @type str
297 @param params dictionary containing the reply data
298 @type dict
299 """
300 nonlocal ok, error
301
302 if reply == "Remove":
303 ok = params["ok"]
304 with contextlib.suppress(KeyError):
305 error = params["error"]
306 loop.quit()
307
308 self.__serverInterface.sendJson(
309 category=EricRequestCategory.FileSystem,
310 request="Remove",
311 params={"filename": filename},
312 callback=callback,
313 )
314
315 loop.exec()
316 return ok, error
317
318 #######################################################################
319 ## Methods for reading and writing files
320 #######################################################################
321
322 def readFile(self, filename):
323 """
324 Public method to read a file from the eric-ide server.
325
326 @param filename name of the file to read
327 @type str
328 @return tuple containing an OK flag, the read data and an error string in
329 case of an issue
330 @rtype tuple of (bool, str, str)
331 """
332 # TODO: 'readFile()' not implemented yet
333
334 def writeFile(self, filename, data):
335 """
336 Public method to write the data to a file on the eric-ide server.
337
338 @param filename name of the file to write
339 @type str
340 @param data data to be written
341 @type str
342 @return tuple containing an OK flag and an error string in case of an issue
343 @rtype tuple of (bool, str)
344 """
345 # TODO: 'writeFile()' not implemented yet

eric ide

mercurial