Advertisement
Guest User

Untitled

a guest
May 27th, 2019
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.18 KB | None | 0 0
  1. from os import system, name, execv, mkdir, getcwd
  2. from uuid import uuid4
  3. from os.path import exists, realpath
  4. from time import sleep
  5. from sys import executable, exit
  6. from tabulate import tabulate
  7. from sys import argv
  8. from pickle import dump, load
  9. from getpass import getpass
  10.  
  11.  
  12. access = {0: "User", 1: "Admin", 2: "System"}
  13. path = []
  14.  
  15.  
  16. class User:
  17.     def __init__(self, access, name, password):
  18.         self.access = access
  19.         self.name = name
  20.         self.password = password
  21.  
  22.  
  23. class Folder:
  24.     def __init__(self, name, access, container):
  25.         self.name = name
  26.         self.access = access
  27.         self.container = container
  28.         self.size = sum([el.size for el in container])
  29.  
  30.  
  31. def restart():
  32.     execv(executable, [executable.split("/")[-1]] + argv)
  33.  
  34.  
  35. def restoreUsers():
  36.     print("No users found. Creating administrator account...")
  37.     userlist = [User(1, "admin", "nimda"), User(2, "Cosmic47", uuid4()), User(0, "User", None)]
  38.     if not exists(getcwd() + "\\data"):
  39.         print('"data" folder anot found. Making "data" folder...')
  40.         mkdir(getcwd() + "\\data")
  41.     with open("data//users.csms", "wb") as users:
  42.         dump(userlist, users)
  43.     print("Rebooting...")
  44.     sleep(1.5)
  45.     restart()
  46.  
  47.  
  48. def restoreFiles():
  49.     print("File system not found. Creating file system")
  50.     with open("data//files.csms", "wb") as files:
  51.         dump(Folder("root", 2, []), files)
  52.     print("Rebooting...")
  53.     sleep(1.5)
  54.     restart()
  55.  
  56.  
  57. def listdir(curdir):
  58.     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"]))
  59.  
  60.  
  61. def clear():
  62.     system('cls' if name == "nt" else "clear")
  63.  
  64.  
  65. def chooseUser(users):
  66.     print(tabulate([(num + 1, user.name, access[user.access]) for num, user in enumerate(users)], headers=["Number", "Name", "Access"]))
  67.     return users[int(input("Choose user: ")) - 1]
  68.  
  69.  
  70. def logIn(usrlst):
  71.     message = ""
  72.     choosing = True
  73.     while choosing:
  74.         currUser = chooseUser(usrlst)
  75.         passing = True
  76.         attempts = 3
  77.         if currUser.access > 0:
  78.             while passing:
  79.                 clear()
  80.                 print(message)
  81.                 pwd = getpass("Input password: ")
  82.                 if pwd == currUser.password:
  83.                     clear()
  84.                     choosing = False
  85.                     break
  86.                 elif pwd != currUser.password and pwd != "[CHANGE USER]":
  87.                     attempts -= 1
  88.                     if attempts == 0:
  89.                         exit()
  90.                     else:
  91.                         message = "Incorrect password. {0} attempt(s) left".format(attempts)
  92.                 else:
  93.                     passing = False
  94.                     clear()
  95.         else:
  96.             clear()
  97.             break
  98.     return currUser
  99.  
  100.  
  101. def save(usrlst, filelist):
  102.     with open("data//users.csms", "wb") as users:
  103.         dump(usrlst, users)
  104.     with open("data//files.csms", "wb") as users:
  105.         dump(filelist, users)
  106.  
  107.  
  108. def startup(usrlst):
  109.     currUser = logIn(usrlst)
  110.     with open("data//files.csms", "rb") as files:
  111.         rootDir = load(files)
  112.     currDir = rootDir
  113.     return currUser, rootDir, currDir
  114.  
  115.  
  116. def comm(inp, curdir, usracs, rootdir):
  117.     global path
  118.     parsed = inp.split(" ")
  119.     command, *args = parsed
  120.     if command == "exit" and len(args) == 0:
  121.         return "exiting..."
  122.     elif command == "mkdir" and len(args) == 2:
  123.         if args[0] not in [elem.name for elem in curdir.container if type(elem) is Folder]:
  124.             if usracs >= int(args[1]):
  125.                 curdir.container.append(Folder(args[0], int(args[1]), []))
  126.                 return ""
  127.             else:
  128.                 return "Access denied."
  129.         return "Folder already exists."
  130.     elif command == "rmdir" and len(args) == 1:
  131.         chosenFold = next((elem for elem in curdir.container if elem.name == args[0]), None)
  132.         if type(chosenFold) is Folder:
  133.             if chosenFold.access <= usracs:
  134.                 curdir.container.remove(chosenFold)
  135.                 return ""
  136.             else:
  137.                 return "Access denied"
  138.         return "No folder found"
  139.     elif command == "rename" and len(args) == 2:
  140.         chosenFold = next((elem for elem in curdir.container if elem.name == args[0]), None)
  141.         if type(chosenFold) is Folder:
  142.             if chosenFold.access <= usracs:
  143.                 curdir.container[curdir.container.index(chosenFold)].name = args[1]
  144.                 return ""
  145.             else:
  146.                 return "Access denied"
  147.     elif command == "moveto" and len(args) == 1:
  148.         chosenFold = next((elem for elem in curdir.container if elem.name == args[0]), None)
  149.         if type(chosenFold) is Folder:
  150.             path.append(curdir.container.index(chosenFold))
  151.             curdir = chosenFold
  152.             return curdir
  153.         else:
  154.             return "Folder not found"
  155.     elif command == "mvback" and len(args) == 0:
  156.         curdir = rootdir
  157.         for _ in path[ : len(path) -1]:
  158.             curdir = curdir.container[_]
  159.         path = path[ : len(path) -1]
  160.         return curdir
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement