executor almost done
This commit is contained in:
parent
cb929266cd
commit
e6d36fa1fd
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user