src/eric7/RemoteServer/EricServerFileSystemRequestHandler.py

Mon, 05 Feb 2024 11:15:47 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Mon, 05 Feb 2024 11:15:47 +0100
branch
server
changeset 10546
300487f5f517
parent 10539
4274f189ff78
child 10548
d3e21f44887b
permissions
-rw-r--r--

eric-ide Server
- Integrated the eric-ide server into the ViewManager and Editor file handling logic.

10539
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1 # -*- coding: utf-8 -*-
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
2
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
3 # Copyright (c) 2024 Detlev Offenbach <detlev@die-offenbachs.de>
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
4 #
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
5
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
6 """
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
7 Module implementing the file system request handler of the eric-ide server.
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
8 """
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
9
10546
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
10 import base64
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
11 import contextlib
10539
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
12 import os
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
13 import stat
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
14 import time
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
15
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
16 from .EricRequestCategory import EricRequestCategory
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
17
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
18 class EricServerFileSystemRequestHandler:
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
19 """
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
20 Class implementing the file system request handler of the eric-ide server.
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
21 """
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
22
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
23 def __init__(self, server):
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
24 """
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
25 Constructor
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
26
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
27 @param server reference to the eric-ide server object
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
28 @type EricServer
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
29 """
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
30 self.__server = server
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
31
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
32 self.__requestMethodMapping = {
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
33 "Chdir": self.__chdir,
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
34 "Getcwd": self.__getcwd,
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
35 "Listdir": self.__listdir,
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
36 "Mkdir": self.__mkdir,
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
37 "Rmdir": self.__rmdir,
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
38 "Replace": self.__replace,
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
39 "Remove": self.__remove,
10546
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
40 "Stat": self.__stat,
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
41 "Exists": self.__exists,
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
42 "Access": self.__access,
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
43
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
44 "ReadFile": self.__readFile,
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
45 "WriteFile": self.__writeFile,
10539
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
46 }
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
47
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
48 def handleRequest(self, request, params, reqestUuid):
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
49 """
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
50 Public method handling the received file system requests.
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
51
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
52 @param request request name
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
53 @type str
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
54 @param params dictionary containing the request parameters
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
55 @type dict
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
56 @param reqestUuid UUID of the associated request as sent by the eric IDE
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
57 @type str
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
58 """
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
59 try:
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
60 result = self.__requestMethodMapping[request](params)
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
61 self.__server.sendJson(
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
62 category=EricRequestCategory.FileSystem,
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
63 reply=request,
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
64 params=result,
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
65 reqestUuid=reqestUuid,
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
66 )
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
67
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
68 except KeyError:
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
69 self.__server.sendJson(
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
70 category=EricRequestCategory.FileSystem,
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
71 reply=request,
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
72 params={"Error": f"Request type '{request}' is not supported."},
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
73 )
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
74
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
75 def __chdir(self, params):
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
76 """
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
77 Private method to change the current working directory.
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
78
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
79 @param params dictionary containing the request data
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
80 @type dict
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
81 @return dictionary containing the reply data
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
82 @rtype dict
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
83 """
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
84 directory = params["directory"]
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
85 try:
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
86 os.chdir(directory)
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
87 return {"ok": True}
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
88 except OSError as err:
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
89 return {
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
90 "ok": False,
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
91 "error": str(err),
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
92 }
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
93
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
94 def __getcwd(self, params):
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
95 """
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
96 Private method to report the current working directory.
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
97
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
98 @param params dictionary containing the request data
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
99 @type dict
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
100 @return dictionary containing the reply data
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
101 @rtype dict
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
102 """
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
103 return {"directory": os.getcwd()}
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
104
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
105 def __listdir(self, params):
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
106 """
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
107 Private method to report a directory listing.
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
108
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
109 @param params dictionary containing the request data
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
110 @type dict
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
111 @return dictionary containing the reply data
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
112 @rtype dict
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
113 """
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
114 directory = params["directory"]
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
115 if not directory:
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
116 directory = os.getcwd()
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
117
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
118 listing = []
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
119 for dirEntry in os.scandir(directory):
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
120 filestat = dirEntry.stat()
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
121 entry = {
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
122 "name": dirEntry.name,
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
123 "path": dirEntry.path,
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
124 "is_dir": dirEntry.is_dir(),
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
125 "is_file": dirEntry.is_file(),
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
126 "is_link": dirEntry.is_symlink(),
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
127 "mode": filestat.st_mode,
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
128 "mode_str": stat.filemode(filestat.st_mode),
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
129 "size": filestat.st_size,
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
130 "mtime": time.strftime(
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
131 "%Y-%m-%d %H:%M:%S", time.localtime(filestat.st_mtime)
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
132 ),
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
133 }
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
134 listing.append(entry)
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
135
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
136 return {
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
137 "directory": directory,
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
138 "listing": listing,
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
139 "separator": os.sep,
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
140 }
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
141
10546
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
142 def __stat(self, params):
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
143 """
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
144 Private method to get the status of a file.
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
145
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
146 @param params dictionary containing the request data
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
147 @type dict
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
148 @return dictionary containing the reply data
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
149 @rtype dict
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
150 """
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
151 filename = params["filename"]
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
152 statItems = params["st_names"]
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
153
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
154 try:
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
155 result = os.stat(filename)
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
156 resultDict = {st: getattr(result, st) for st in statItems}
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
157 return {"ok": True, "result": resultDict}
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
158 except OSError as err:
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
159 return {
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
160 "ok": False,
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
161 "error": str(err),
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
162 }
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
163
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
164 def __exists(self, params):
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
165 """
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
166 Private method to check if a file or directory of the given name exists.
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
167
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
168 @param params dictionary containing the request data
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
169 @type dict
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
170 @return dictionary containing the reply data
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
171 @rtype dict
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
172 """
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
173 return {"exists": os.path.exists(params["name"])}
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
174
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
175 def __access(self, params):
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
176 """
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
177 Private method to test, if the eric-ide server has the given access rights
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
178 to a file or directory..
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
179
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
180 @param params dictionary containing the request data
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
181 @type dict
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
182 @return dictionary containing the reply data
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
183 @rtype dict
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
184 """
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
185 mode = os.F_OK
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
186 for modeStr in params["modes"]:
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
187 if modeStr == "read":
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
188 mode |= os.R_OK
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
189 elif modeStr == "write":
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
190 mode |= os.W_OK
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
191 elif modeStr == "execute":
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
192 mode |= os.X_OK
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
193
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
194 return {"ok": os.access(params["name"], mode)}
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
195
10539
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
196 def __mkdir(self, params):
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
197 """
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
198 Private method to create a new directory.
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
199
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
200 @param params dictionary containing the request data
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
201 @type dict
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
202 @return dictionary containing the reply data
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
203 @rtype dict
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
204 """
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
205 directory = params["directory"]
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
206
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
207 try:
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
208 os.makedirs(directory)
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
209 return {"ok": True}
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
210 except OSError as err:
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
211 return {
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
212 "ok": False,
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
213 "error": str(err),
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
214 }
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
215
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
216 def __rmdir(self, params):
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
217 """
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
218 Private method to delete a directory.
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
219
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
220 @param params dictionary containing the request data
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
221 @type dict
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
222 @return dictionary containing the reply data
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
223 @rtype dict
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
224 """
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
225 directory = params["directory"]
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
226
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
227 try:
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
228 os.rmdir(directory)
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
229 return {"ok": True}
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
230 except OSError as err:
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
231 return {
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
232 "ok": False,
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
233 "error": str(err),
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
234 }
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
235
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
236 def __replace(self, params):
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
237 """
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
238 Private method to replace (rename) a file or directory.
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
239
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
240
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
241 @param params dictionary containing the request data
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
242 @type dict
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
243 @return dictionary containing the reply data
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
244 @rtype dict
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
245 """
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
246 oldName = params["old_name"]
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
247 newName = params["new_name"]
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
248
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
249 try:
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
250 os.replace(oldName, newName)
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
251 return {"ok": True}
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
252 except OSError as err:
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
253 return {
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
254 "ok": False,
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
255 "error": str(err),
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
256 }
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
257
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
258 def __remove(self, params):
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
259 """
10546
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
260 Private method to delete a file.
10539
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
261
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
262 @param params dictionary containing the request data
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
263 @type dict
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
264 @return dictionary containing the reply data
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
265 @rtype dict
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
266 """
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
267 filename = params["filename"]
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
268
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
269 try:
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
270 os.remove(filename)
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
271 return {"ok": True}
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
272 except OSError as err:
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
273 return {
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
274 "ok": False,
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
275 "error": str(err),
4274f189ff78 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
276 }
10546
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
277
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
278 def __readFile(self, params):
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
279 """
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
280 Private method to read the contents of a file.
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
281
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
282 @param params dictionary containing the request data
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
283 @type dict
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
284 @return dictionary containing the reply data
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
285 @rtype dict
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
286 """
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
287 filename = params["filename"]
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
288
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
289 if params["create"] and not os.path.exists(filename):
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
290 with open(filename, "wb"):
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
291 pass
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
292
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
293 try:
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
294 with open(filename, "rb") as f:
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
295 data = f.read()
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
296 return {
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
297 "ok": True,
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
298 "filedata": str(base64.b85encode(data), encoding="ascii")
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
299 }
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
300 except OSError as err:
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
301 return {
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
302 "ok": False,
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
303 "error": str(err),
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
304 }
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
305
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
306 def __writeFile(self, params):
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
307 """
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
308 Private method to write data into a file.
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
309
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
310 @param params dictionary containing the request data
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
311 @type dict
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
312 @return dictionary containing the reply data
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
313 @rtype dict
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
314 """
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
315 filename = params["filename"]
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
316 data = base64.b85decode(bytes(params["filedata"], encoding="ascii"))
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
317
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
318 # 1. create backup file if asked for
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
319 if params["with_backup"]:
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
320 if os.path.islink(filename):
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
321 filename = os.path.realpath(filename)
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
322 backupFilename = "{0}~".format(filename)
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
323 try:
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
324 permissions = os.stat(filename).st_mode
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
325 perms_valid = True
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
326 except OSError:
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
327 # if there was an error, ignore it
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
328 perms_valid = False
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
329 with contextlib.suppress(OSError):
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
330 os.remove(backupFilename)
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
331 with contextlib.suppress(OSError):
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
332 os.rename(filename, backupFilename)
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
333
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
334 # 2. write the data to the file and reset the permissions
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
335 try:
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
336 with open(filename, "wb") as f:
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
337 f.write(data)
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
338 if params["with_backup"] and perms_valid:
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
339 os.chmod(filename, permissions)
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
340 return {"ok": True}
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
341 except OSError as err:
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
342 return {
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
343 "ok": False,
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
344 "error": str(err),
300487f5f517 eric-ide Server
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 10539
diff changeset
345 }

eric ide

mercurial