def process_commands(db, input_command): utils.cls() utils.hr() command_info() utils.hr() if input_command == '': return False for command_group in commands: for command_set in commands[command_group]: for command in [command_set[0], command_set[1]]: match = re.match('^{0}(?: (?P<data>.+))?$'.format(command), input_command) if match is not None: command_set[2](db, match.group('data')) return True return False
def main(): client = pymongo.MongoClient() db = client.lowfarm utils.cls() utils.hr() command_info() utils.hr() sys.stdout.write('Enter a command: ') previous_command = '' while True: try: input_command = raw_input() except: sys.stdout.write('\n') break if input_command == 'quit': print('Bye') break if input_command == '': input_command = previous_command previous_command = input_command if not process_commands(db, input_command): print('Command not regognised... Try again') utils.hr() sys.stdout.write('Enter a command: ') time.sleep(0.1)
def command_info(): command_list_len = 0 for command_list_key in commands: command_list = commands[command_list_key] if len(command_list) > command_list_len: command_list_len = len(command_list) for command_group in commands: sys.stdout.write(' {0: <23}|'.format(command_group.upper())) sys.stdout.write('\n') utils.hr() for row in range(command_list_len): for command_list_key in commands: command_group = commands[command_list_key] if len(command_group) > row: text = '{0}, {1}'.format(command_group[row][0], command_group[row][1]) sys.stdout.write(' {0: <23}|'.format(text)) else: sys.stdout.write(' {0: <23}|'.format('')) sys.stdout.write('\n')