Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import re
- class Terminal:
- """This class emulates a simple Terminal promt with a basic login and CLI"""
- def __init__(self):
- """self.user will contain the username of the currently logged in user, otherwise None
- self.users is a dictionary that should contain all users as keys with their
- corresponding passwords as values
- self.run should be set to False to terminate the main loop"""
- self.user = None
- self.users = {}
- self.run = False
- def check_login(self):
- """This function checks if the currently logged in user, which is stored in
- the user string, exists in the users dictionary and return True, otherwise it
- will start a login attempt and return its value"""
- if self.user in self.users.keys():
- return True
- else:
- return self.do_login()
- def do_login(self):
- """This function will prompt the user for a name and password.
- If the given name exists as a key in the users dictionary it will check if
- the password matches the value of that key and set that user to be currently
- logged in.
- This function returns True for a successful login attempt, otherwise False."""
- username = raw_input('Username: ')
- password = raw_input('Password: ')
- if username in self.users.keys():
- if password == self.users[username]:
- print 'login ok!'
- print 'Welcome %s!' % (username)
- self.user = username
- return True
- print 'login failed!'
- return False
- def commandline(self):
- """This is a function to prompt for commands, check them and execute them.
- Sould not be called anywhere but the main loop"""
- cmd = raw_input('INPUT# ')
- if cmd == 'logout':
- print 'Bye!'
- self.user = None
- if cmd == 'quit':
- self.run = False
- def main(self):
- """This is the main loop. When this is called,
- control of the application is given to the Terminal class."""
- self.run = True
- while self.run:
- if self.check_login(): self.commandline()
- term = Terminal()
- term.users = { 'ndakota':'transltr', 'elite':'awesum' }
- term.main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement