Advertisement
Guest User

Untitled

a guest
Jan 29th, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.12 KB | None | 0 0
  1. import os
  2. import sys
  3.  
  4. class NoDatabaseFile(BaseException):
  5.     pass
  6.  
  7. class system:
  8.     @staticmethod
  9.     def __EXIT__(stdout):
  10.         print(" | %s." % (stdout))
  11.         sys.exit()
  12.        
  13.     @staticmethod   # Decorator for __DEBUG__
  14.     def __DEBUG__(is_debug, stdout, *args):   # Debugging
  15.         if is_debug: sys.stderr.write(" | %s -- %s\n" % (stdout, args))
  16.         else: pass  # Unnecessary line, but there because why not
  17.        
  18.     def __init__(self, mode, username, password, is_debug=True):
  19.         ":__init__: initializes values and login attributes for module"
  20.         self.__DEBUG__(is_debug, "Started initialization")
  21.  
  22.         self.mode = mode
  23.         self.is_debug = is_debug    # Debug over several functions/methods
  24.         self.username = username    # Set global class variables
  25.         self.password = password    # Stored as plain-text sadly.
  26.         self.database = os.getcwd() + "\\database.dbs"    # Presuming file exists
  27.         self.dbs_exists = True if os.path.exists(self.database) else False  # Ternary operators, checks if database file exists, if not set to False
  28.         self.users =\
  29.         {
  30.  
  31.         }
  32.         self._ = None   # Temporary variable for I/O
  33.         self.user, self.passwd = False, False
  34.         self.__DEBUG__(self.is_debug, "Ended initialization", is_debug, self.username, self.password, self.dbs_exists)
  35.        
  36.     def login(self):
  37.         # PRESUMING DATABASE FORMAT OF:
  38.         # username:password\r\n
  39.         # Pretty simple, no need for complications
  40.         if mode.lower() != "l" or mode.lower() != "login":
  41.             self.__EXIT__(
  42.         if not self.dbs_exists:     # If database file doesn't exist
  43.             raise NoDatabaseFile("Database file, %s - not existent, please create it and rerun." % (self.database))
  44.         elif self.dbs_exists:   # If file exists
  45.             self.__DEBUG__(self.is_debug, "Opening database-file", self.database)
  46.            
  47.             with open(self.database, "r") as read_dbs:  # Open the file in read-only mode.
  48.                 self._ = read_dbs.readlines()
  49.             for line in self._:
  50.                 self.users.update({line.split(":")[0]: line.split(":")[1].replace("\n", "")})
  51.             for user in self.users.keys():
  52.                     if self.username == user and user == self.username:
  53.                         self.__DEBUG__(self.is_debug, "Found user", user, self.username)
  54.                         self.user = True
  55.                         break
  56.                     else:
  57.                         self.user = False   # Redundant
  58.                         pass
  59.  
  60.             self.__EXIT__("User not found") if not self.user else None
  61.  
  62.             if self.password == self.users[self.username] and self.users[self.username] == self.password:
  63.                 self.passwd = True
  64.                 self.__DEBUG__(self.is_debug, "Logged in", self.username, self.password)
  65.             self.__DEBUG__(self.is_debug, "Finished reading file", self.users)
  66.            
  67.     def register(self):
  68.         pass
  69.  
  70. #if __name__ == "__main__":
  71. #    raise Exception("You cannot run this directly as a script, please import it.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement