Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from os import system, name, execv, mkdir, getcwd
- from uuid import uuid4
- from os.path import exists, realpath
- from time import sleep
- from sys import executable, exit
- from tabulate import tabulate
- from sys import argv
- from pickle import dump, load
- from getpass import getpass
- access = {0: "User", 1: "Admin", 2: "System"}
- path = []
- class User:
- def __init__(self, access, name, password):
- self.access = access
- self.name = name
- self.password = password
- class Folder:
- def __init__(self, name, access, container):
- self.name = name
- self.access = access
- self.container = container
- self.size = sum([el.size for el in container])
- def restart():
- execv(executable, [executable.split("/")[-1]] + argv)
- def restoreUsers():
- print("No users found. Creating administrator account...")
- userlist = [User(1, "admin", "nimda"), User(2, "Cosmic47", uuid4()), User(0, "User", None)]
- if not exists(getcwd() + "\\data"):
- print('"data" folder anot found. Making "data" folder...')
- mkdir(getcwd() + "\\data")
- with open("data//users.csms", "wb") as users:
- dump(userlist, users)
- print("Rebooting...")
- sleep(1.5)
- restart()
- def restoreFiles():
- print("File system not found. Creating file system")
- with open("data//files.csms", "wb") as files:
- dump(Folder("root", 2, []), files)
- print("Rebooting...")
- sleep(1.5)
- restart()
- def listdir(curdir):
- print(tabulate([(elem.name, "Folder" if type(elem) is Folder else elem.type, elem.size, access[elem.access]) for elem in curdir.container], headers=["Name", "Type", "Size", "Access"]))
- def clear():
- system('cls' if name == "nt" else "clear")
- def chooseUser(users):
- print(tabulate([(num + 1, user.name, access[user.access]) for num, user in enumerate(users)], headers=["Number", "Name", "Access"]))
- return users[int(input("Choose user: ")) - 1]
- def logIn(usrlst):
- message = ""
- choosing = True
- while choosing:
- currUser = chooseUser(usrlst)
- passing = True
- attempts = 3
- if currUser.access > 0:
- while passing:
- clear()
- print(message)
- pwd = getpass("Input password: ")
- if pwd == currUser.password:
- clear()
- choosing = False
- break
- elif pwd != currUser.password and pwd != "[CHANGE USER]":
- attempts -= 1
- if attempts == 0:
- exit()
- else:
- message = "Incorrect password. {0} attempt(s) left".format(attempts)
- else:
- passing = False
- clear()
- else:
- clear()
- break
- return currUser
- def save(usrlst, filelist):
- with open("data//users.csms", "wb") as users:
- dump(usrlst, users)
- with open("data//files.csms", "wb") as users:
- dump(filelist, users)
- def startup(usrlst):
- currUser = logIn(usrlst)
- with open("data//files.csms", "rb") as files:
- rootDir = load(files)
- currDir = rootDir
- return currUser, rootDir, currDir
- def comm(inp, curdir, usracs, rootdir):
- global path
- parsed = inp.split(" ")
- command, *args = parsed
- if command == "exit" and len(args) == 0:
- return "exiting..."
- elif command == "mkdir" and len(args) == 2:
- if args[0] not in [elem.name for elem in curdir.container if type(elem) is Folder]:
- if usracs >= int(args[1]):
- curdir.container.append(Folder(args[0], int(args[1]), []))
- return ""
- else:
- return "Access denied."
- return "Folder already exists."
- elif command == "rmdir" and len(args) == 1:
- chosenFold = next((elem for elem in curdir.container if elem.name == args[0]), None)
- if type(chosenFold) is Folder:
- if chosenFold.access <= usracs:
- curdir.container.remove(chosenFold)
- return ""
- else:
- return "Access denied"
- return "No folder found"
- elif command == "rename" and len(args) == 2:
- chosenFold = next((elem for elem in curdir.container if elem.name == args[0]), None)
- if type(chosenFold) is Folder:
- if chosenFold.access <= usracs:
- curdir.container[curdir.container.index(chosenFold)].name = args[1]
- return ""
- else:
- return "Access denied"
- elif command == "moveto" and len(args) == 1:
- chosenFold = next((elem for elem in curdir.container if elem.name == args[0]), None)
- if type(chosenFold) is Folder:
- path.append(curdir.container.index(chosenFold))
- curdir = chosenFold
- return curdir
- else:
- return "Folder not found"
- elif command == "mvback" and len(args) == 0:
- curdir = rootdir
- for _ in path[ : len(path) -1]:
- curdir = curdir.container[_]
- path = path[ : len(path) -1]
- return curdir
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement