From e6d36fa1fdf1ccc872f02842e084d3dd19a1cebf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torma=20Krist=C3=B3f?= Date: Sun, 18 Apr 2021 15:24:31 +0200 Subject: [PATCH] executor almost done --- server/executor.py | 62 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/server/executor.py b/server/executor.py index 63f77b6..3050f87 100644 --- a/server/executor.py +++ b/server/executor.py @@ -1,2 +1,64 @@ #!/usr/bin/env python3 +import os +import re + + +class Executor: + """This class executes commands recieved by the server""" + + def __init__(self, currentDiectory: str, baseDir: str = ""): + self.currentDirectory = currentDiectory + if baseDir == "": + self.baseDir = self.currentDirectory + else: + self.baseDir = baseDir + + def sanitizeDirectory(self, inDirectory: str) -> str: + return re.sub('[^a-zA-Z0-9]', '', inDirectory) + + def sanitizeFile(self, inFile: str) -> str: + return re.sub('[^a-zA-Z0-9].[^a-zA-Z0-9]', '', inFile) + + def createDirectory(self, dirName: str) -> str: + dirName = self.sanitizeDirectory(dirName) + actualDirName = self.currentDirectory + "/" + dirName + os.mkdir(actualDirName) + return actualDirName + + def removeDirectory(self, dirName: str) -> str: + dirName = self.sanitizeDirectory(dirName) + actualDirName = self.currentDirectory + "/" + dirName + os.rmdir(actualDirName) + return actualDirName + + def getCurrentDirectory(self) -> str: + return self.currentDirectory + + def setCurrentDirectory(self, dirName: str) -> str: + # TODO : Navigate up a directory structure + dirName = self.sanitizeDirectory(dirName) + self.currentDirectory = self.currentDirectory + "/" + dirName + return self.currentDirectory + + def listCurrentDirectoryContent(self) -> str: + contents = os.listdir(self.currentDirectory) + strdirectory = "" + for content in contents: + strdirectory += content + ", " + strdirectory = strdirectory[:-1] + return strdirectory + + def putFileInCurrentDirectory(self, filename: str, content) -> str: + pass + + def getFileInCurrentDirectory(self, file: str): + file = self.sanitizeFile(file) + currentfile = self.currentDirectory + file + return open(currentfile, "r") + + def removeFileInCurrentDirectory(self, file: str) -> str: + file = self.sanitizeFile(file) + currentfile = self.currentDirectory + file + os.remove(currentfile) + return currentfile