src/eric7/RemoteServerInterface/EricServerFileSystemInterface.py

branch
server
changeset 10596
ea35c92a3c7c
parent 10589
75b656c80a40
child 10605
b6f5e27daeb5
equal deleted inserted replaced
10594:6156d9675f62 10596:ea35c92a3c7c
72 self.__serverPathSep = "" 72 self.__serverPathSep = ""
73 73
74 def __getPathSep(self): 74 def __getPathSep(self):
75 """ 75 """
76 Private method to get the path separator of the connected server. 76 Private method to get the path separator of the connected server.
77
78 @return path separator character of the server
79 @rtype str
77 """ 80 """
78 loop = QEventLoop() 81 loop = QEventLoop()
79 sep = "" 82 sep = ""
80 83
81 def callback(reply, params): 84 def callback(reply, params):
347 if dirname and not self.__hasMagic(dirname): 350 if dirname and not self.__hasMagic(dirname):
348 with contextlib.suppress(OSError): 351 with contextlib.suppress(OSError):
349 entries = self.direntries( 352 entries = self.direntries(
350 dirname, pattern=basename, recursive=recursive, filesonly=True 353 dirname, pattern=basename, recursive=recursive, filesonly=True
351 ) 354 )
352 if includeHidden: 355 result = (
353 result = entries 356 entries
354 else: 357 if includeHidden
355 result = [e for e in entries if not e.startswith(".")] 358 else [e for e in entries if not e.startswith(".")]
359 )
356 360
357 return result 361 return result
358 362
359 def stat(self, filename, stNames): 363 def stat(self, filename, stNames):
360 """ 364 """
567 if self.__serverInterface.isServerConnected(): 571 if self.__serverInterface.isServerConnected():
568 self.__serverInterface.sendJson( 572 self.__serverInterface.sendJson(
569 category=EricRequestCategory.FileSystem, 573 category=EricRequestCategory.FileSystem,
570 request="Mkdir", 574 request="Mkdir",
571 params={"directory": FileSystemUtilities.plainFileName(directory)}, 575 params={"directory": FileSystemUtilities.plainFileName(directory)},
576 callback=callback,
577 )
578
579 loop.exec()
580 return ok, error
581
582 else:
583 return False, "Not connected to an 'eric-ide' server."
584
585 def makedirs(self, directory, exist_ok=False):
586 """
587 Public method to create a new directory on the eric-ide serverincluding all
588 intermediate-level directories.
589
590 @param directory absolute path of the new directory
591 @type str
592 @param exist_ok flag indicating that the existence of the directory is
593 acceptable (defaults to False)
594 @type bool (optional)
595 @return tuple containing an OK flag and an error string in case of an issue
596 @rtype tuple of (bool, str)
597 """
598 loop = QEventLoop()
599 ok = False
600 error = ""
601
602 def callback(reply, params):
603 """
604 Function to handle the server reply
605
606 @param reply name of the server reply
607 @type str
608 @param params dictionary containing the reply data
609 @type dict
610 """
611 nonlocal ok, error
612
613 if reply == "MakeDirs":
614 ok = params["ok"]
615 with contextlib.suppress(KeyError):
616 error = params["error"]
617 loop.quit()
618
619 if self.__serverInterface.isServerConnected():
620 self.__serverInterface.sendJson(
621 category=EricRequestCategory.FileSystem,
622 request="MakeDirs",
623 params={
624 "directory": FileSystemUtilities.plainFileName(directory),
625 "exist_ok": exist_ok,
626 },
572 callback=callback, 627 callback=callback,
573 ) 628 )
574 629
575 loop.exec() 630 loop.exec()
576 return ok, error 631 return ok, error
842 @return final component 897 @return final component
843 @rtype str 898 @rtype str
844 """ 899 """
845 return self.split(p)[1] 900 return self.split(p)[1]
846 901
902 def toNativeSeparators(self, p):
903 """
904 Public method to convert a path to use server native separator characters.
905
906 @param p path name to be converted
907 @type str
908 @return path name with converted separator characters
909 @rtype str
910 """
911 if self.__serverPathSep == "/":
912 return p.replace("\\", "/")
913 else:
914 return p.replace("/", "\\")
915
916 def fromNativeSeparators(self, p):
917 """
918 Public method to convert a path using server native separator characters to
919 use "/" separator characters.
920
921 @param p path name to be converted
922 @type str
923 @return path name with converted separator characters
924 @rtype str
925 """
926 return p.replace(self.__serverPathSep, "/")
927
847 ####################################################################### 928 #######################################################################
848 ## Methods for reading and writing files 929 ## Methods for reading and writing files
849 ####################################################################### 930 #######################################################################
850 931
851 def readFile(self, filename, create=False): 932 def readFile(self, filename, create=False):
960 if not ok: 1041 if not ok:
961 raise OSError(error) 1042 raise OSError(error)
962 1043
963 def readEncodedFile(self, filename, create=False): 1044 def readEncodedFile(self, filename, create=False):
964 """ 1045 """
965 Function to read a file and decode its contents into proper text. 1046 Public method to read a file and decode its contents into proper text.
966 1047
967 @param filename name of the file to read 1048 @param filename name of the file to read
968 @type str 1049 @type str
969 @param create flag indicating to create an empty file, if it does not exist 1050 @param create flag indicating to create an empty file, if it does not exist
970 (defaults to False) 1051 (defaults to False)
975 data = self.readFile(filename, create=create) 1056 data = self.readFile(filename, create=create)
976 return Utilities.decode(data) 1057 return Utilities.decode(data)
977 1058
978 def readEncodedFileWithEncoding(self, filename, encoding, create=False): 1059 def readEncodedFileWithEncoding(self, filename, encoding, create=False):
979 """ 1060 """
980 Function to read a file and decode its contents into proper text. 1061 Public method to read a file and decode its contents into proper text.
981 1062
982 @param filename name of the file to read 1063 @param filename name of the file to read
983 @type str 1064 @type str
984 @param encoding encoding to be used to read the file 1065 @param encoding encoding to be used to read the file
985 @type str 1066 @type str
990 @rtype tuple of (str, str) 1071 @rtype tuple of (str, str)
991 """ 1072 """
992 data = self.readFile(filename, create=create) 1073 data = self.readFile(filename, create=create)
993 return Utilities.decodeWithEncoding(data, encoding) 1074 return Utilities.decodeWithEncoding(data, encoding)
994 1075
995 def writeEncodedFile(self, filename, text, origEncoding, forcedEncoding="", withBackup=False): 1076 def writeEncodedFile(
996 """ 1077 self, filename, text, origEncoding, forcedEncoding="", withBackup=False
997 Function to write a file with properly encoded text. 1078 ):
1079 """
1080 Public method to write a file with properly encoded text.
998 1081
999 @param filename name of the file to read 1082 @param filename name of the file to read
1000 @type str 1083 @type str
1001 @param text text to be written 1084 @param text text to be written
1002 @type str 1085 @type str

eric ide

mercurial