Mon, 19 Feb 2024 15:33:33 +0100
Added methods to read and write files with a given encoding to the eric-ide server file system interface and adapted the code accordingly.
--- a/src/eric7/DataViews/CodeMetrics.py Sun Feb 18 17:46:53 2024 +0100 +++ b/src/eric7/DataViews/CodeMetrics.py Mon Feb 19 15:33:33 2024 +0100 @@ -232,10 +232,9 @@ remotefsInterface = ( ericApp().getObject("EricServer").getServiceInterface("FileSystem") ) - bText = remotefsInterface.readFile( - FileSystemUtilities.plainFileName(filename) - ) - text = Utilities.decode(bText)[0] + text = remotefsInterface.readEncodedFile( + filename + )[0] else: text = Utilities.readEncodedFile(filename)[0] except (OSError, UnicodeError):
--- a/src/eric7/Plugins/CheckerPlugins/CodeStyleChecker/CodeStyleCheckerDialog.py Sun Feb 18 17:46:53 2024 +0100 +++ b/src/eric7/Plugins/CheckerPlugins/CodeStyleChecker/CodeStyleCheckerDialog.py Mon Feb 19 15:33:33 2024 +0100 @@ -1098,10 +1098,9 @@ else: try: if FileSystemUtilities.isRemoteFileName(self.filename): - bSource = self.__remotefsInterface.readFile( - FileSystemUtilities.plainFileName(self.filename) + source, encoding = self.__remotefsInterface.readEncodedFile( + self.filename ) - source, encoding = Utilities.decode(bSource) else: source, encoding = Utilities.readEncodedFile(self.filename) source = source.splitlines(True) @@ -1150,10 +1149,9 @@ try: if FileSystemUtilities.isRemoteFileName(self.filename): - bSource = self.__remotefsInterface.readFile( - FileSystemUtilities.plainFileName(self.filename) + source, encoding = self.__remotefsInterface.readEncodedFile( + self.filename ) - source, encoding = Utilities.decode(bSource) else: source, encoding = Utilities.readEncodedFile(filename) source = source.splitlines(True)
--- a/src/eric7/Plugins/CheckerPlugins/SyntaxChecker/SyntaxCheckerDialog.py Sun Feb 18 17:46:53 2024 +0100 +++ b/src/eric7/Plugins/CheckerPlugins/SyntaxChecker/SyntaxCheckerDialog.py Mon Feb 19 15:33:33 2024 +0100 @@ -426,10 +426,9 @@ else: try: if FileSystemUtilities.isRemoteFileName(self.filename): - bSource = self.__remotefsInterface.readFile( - FileSystemUtilities.plainFileName(self.filename) - ) - self.source = Utilities.decode(bSource)[0] + self.source = self.__remotefsInterface.readEncodedFile( + self.filename + )[0] else: self.source = Utilities.readEncodedFile(self.filename)[0] self.source = Utilities.normalizeCode(self.source) @@ -469,10 +468,9 @@ try: if FileSystemUtilities.isRemoteFileName(self.filename): - bSource = self.__remotefsInterface.readFile( - FileSystemUtilities.plainFileName(self.filename) - ) - source = Utilities.decode(bSource)[0] + source = self.__remotefsInterface.readEncodedFile( + self.filename + )[0] else: source = Utilities.readEncodedFile(filename)[0] source = Utilities.normalizeCode(source)
--- a/src/eric7/QScintilla/Editor.py Sun Feb 18 17:46:53 2024 +0100 +++ b/src/eric7/QScintilla/Editor.py Mon Feb 19 15:33:33 2024 +0100 @@ -3522,15 +3522,16 @@ with EricOverrideCursor(): if FileSystemUtilities.isRemoteFileName(fn) or isRemote: title = self.tr("Open Remote File") - bText = self.__remotefsInterface.readFile( - FileSystemUtilities.plainFileName(fn), create=True - ) if encoding: - txt, self.encoding = Utilities.decodeWithEncoding( - bText, encoding + txt, self.encoding = ( + self.__remotefsInterface.readEncodedFileWithEncoding( + fn, encoding, create=True + ) ) else: - txt, self.encoding = Utilities.decode(bText) + txt, self.encoding = self.__remotefsInterface.readEncodedFile( + fn, create=True + ) else: title = self.tr("Open File") if createIt and not os.path.exists(fn): @@ -3686,11 +3687,12 @@ os.chmod(fn, permissions) else: title = self.tr("Save Remote File") - bText, self.encoding = Utilities.encode( - txt, self.encoding, forcedEncoding=editorConfigEncoding - ) - self.__remotefsInterface.writeFile( - FileSystemUtilities.plainFileName(fn), bText, createBackup + self.encoding = self.__remotefsInterface.writeEncodedFile( + fn, + txt, + self.encoding, + forcedEncoding=editorConfigEncoding, + createBackup=createBackup, ) return True except (OSError, UnicodeError, Utilities.CodingError) as why:
--- a/src/eric7/RemoteServerInterface/EricServerFileSystemInterface.py Sun Feb 18 17:46:53 2024 +0100 +++ b/src/eric7/RemoteServerInterface/EricServerFileSystemInterface.py Mon Feb 19 15:33:33 2024 +0100 @@ -15,6 +15,7 @@ from PyQt6.QtCore import QEventLoop, QObject, pyqtSlot +from eric7 import Utilities from eric7.RemoteServer.EricRequestCategory import EricRequestCategory from eric7.SystemUtilities import FileSystemUtilities @@ -958,3 +959,61 @@ loop.exec() if not ok: raise OSError(error) + + def readEncodedFile(self, filename, create=False): + """ + Function to read a file and decode its contents into proper text. + + @param filename name of the file to read + @type str + @param create flag indicating to create an empty file, if it does not exist + (defaults to False) + @type bool (optional) + @return tuple of decoded text and encoding + @rtype tuple of (str, str) + """ + data = self.readFile(filename, create=create) + return Utilities.decode(data) + + def readEncodedFileWithEncoding(self, filename, encoding, create=False): + """ + Function to read a file and decode its contents into proper text. + + @param filename name of the file to read + @type str + @param encoding encoding to be used to read the file + @type str + @param create flag indicating to create an empty file, if it does not exist + (defaults to False) + @type bool (optional) + @return tuple of decoded text and encoding + @rtype tuple of (str, str) + """ + data = self.readFile(filename, create=create) + return Utilities.decodeWithEncoding(data, encoding) + + def writeEncodedFile(self, filename, text, origEncoding, forcedEncoding="", withBackup=False): + """ + Function to write a file with properly encoded text. + + @param filename name of the file to read + @type str + @param text text to be written + @type str + @param origEncoding type of the original encoding + @type str + @param forcedEncoding encoding to be used for writing, if no coding + line is present (defaults to "") + @type str (optional) + @param withBackup flag indicating to create a backup file first + (defaults to False) + @type bool (optional) + @return encoding used for writing the file + @rtype str + """ + data, encoding = Utilities.encode( + text, origEncoding, forcedEncoding=forcedEncoding + ) + self.writeFile(filename, data, withBackup=withBackup) + + return encoding
--- a/src/eric7/Utilities/ModuleParser.py Sun Feb 18 17:46:53 2024 +0100 +++ b/src/eric7/Utilities/ModuleParser.py Mon Feb 19 15:33:33 2024 +0100 @@ -1778,13 +1778,12 @@ mod = Module(modname, file, moduleType) with contextlib.suppress(UnicodeError, OSError): if isRemoteFileName: - bSource = ( + src = ( ericApp() .getObject("EricServer") .getServiceInterface("FileSystem") - .readFile(file) + .readEncodedFile(file)[0] ) - src = Utilities.decode(bSource)[0] else: src = Utilities.readEncodedFile(file)[0] mod.scan(src)