simplifies adding of commands, as those can be added in commands-dictionary. Prepares help-texts as well.
This commit is contained in:
61
rep_cli.py
61
rep_cli.py
@@ -6,8 +6,19 @@ class Session:
|
||||
self.user = -1
|
||||
self.db_agent = db_agent
|
||||
self.resultstring = 'no results yet'
|
||||
self.commands = {'a': (self.set_user, ''),
|
||||
'c': (self.show_composers, ''),
|
||||
'm': (self.mark_work_as_mastered, ''),
|
||||
'n': (self.create_new_user, ''),
|
||||
's': (self.show_works, ''),
|
||||
'sc': (self.show_works, ''),
|
||||
'sp': (self.show_movements, ''),
|
||||
'su': (self.show_users, ''),
|
||||
'q': (self.quit, '')
|
||||
}
|
||||
|
||||
def set_user(self, db_index):
|
||||
def set_user(self, arguments):
|
||||
db_index = arguments[0]
|
||||
self.user = db_index
|
||||
return 'User set'
|
||||
|
||||
@@ -27,39 +38,24 @@ class Session:
|
||||
|
||||
def invoke_command(self, command, arguments):
|
||||
self.resultstring = ''
|
||||
if command == 'a':
|
||||
self.resultstring += self.set_user(arguments[0])
|
||||
elif command == 'c':
|
||||
self.resultstring += self.show_composers()
|
||||
elif command == 'm':
|
||||
self.resultstring += self.mark_work_as_mastered(arguments)
|
||||
elif command == 'n':
|
||||
self.create_new_user(arguments[0], arguments[1])
|
||||
self.resultstring += f'Created new user {arguments[0]} {arguments[1]}.'
|
||||
elif command == 's':
|
||||
self.resultstring += self.show_works()
|
||||
elif command == 'sc':
|
||||
self.resultstring += self.show_works(restraint=f'comp_id = {arguments[0]}')
|
||||
elif command == 'sp':
|
||||
self.resultstring += self.show_movements(arguments[0])
|
||||
elif command == 'su':
|
||||
self.resultstring += self.show_users()
|
||||
elif command == 'q':
|
||||
self.resultstring += 'Bye-bye'
|
||||
if command in self.commands.keys():
|
||||
self.resultstring += self.commands[command][0](arguments)
|
||||
else:
|
||||
self.resultstring += 'Could not understand command.'
|
||||
|
||||
def result(self):
|
||||
return self.resultstring
|
||||
|
||||
def create_new_user(self, first_name, name):
|
||||
def create_new_user(self, arguments):
|
||||
first_name, name = arguments[0], arguments[1]
|
||||
sql_command = f'''
|
||||
INSERT INTO pianist (first_name, sec_name)
|
||||
VALUES ("{first_name}", "{name}");
|
||||
'''
|
||||
self.db_agent.execute(sql_command)
|
||||
return f'Created new user {first_name} {name}.'
|
||||
|
||||
def show_composers(self):
|
||||
def show_composers(self, _):
|
||||
sql_command = '''
|
||||
SELECT id, first_name, name
|
||||
FROM composer
|
||||
@@ -71,14 +67,15 @@ class Session:
|
||||
fun_resultstring += f'{item[0]}: {item[2]}, {item[1]}\n'
|
||||
return fun_resultstring
|
||||
|
||||
def show_movements(self, work_id):
|
||||
def show_movements(self, arguments):
|
||||
work_id = arguments[0]
|
||||
work = work_under_id(work_id, self.db_agent)
|
||||
fun_resultstring = ''
|
||||
for mov_number in work.values['movements'].keys():
|
||||
fun_resultstring += f'{mov_number}. {work.pretty_mov(mov_number)}\n'
|
||||
return fun_resultstring
|
||||
|
||||
def show_users(self):
|
||||
def show_users(self, _):
|
||||
sql_command = '''
|
||||
SELECT * FROM pianist;
|
||||
'''
|
||||
@@ -88,7 +85,11 @@ class Session:
|
||||
fun_resultstring += f'{item[0]}: {item[1]} {item[2]}\n'
|
||||
return fun_resultstring
|
||||
|
||||
def show_works(self, restraint='1=1'):
|
||||
def show_works(self, arguments):
|
||||
if len(arguments) > 0:
|
||||
restraint = f'comp_id = {arguments[0]}'
|
||||
else:
|
||||
restraint = '1 = 1'
|
||||
sql_command = f'''
|
||||
SELECT id
|
||||
FROM work
|
||||
@@ -126,6 +127,9 @@ class Session:
|
||||
resultstring += f'{work.pretty_mov(mov_number)}\n'
|
||||
return resultstring
|
||||
|
||||
def quit(self, _):
|
||||
return 'Bye bye!'
|
||||
|
||||
# End session
|
||||
|
||||
class work_under_id:
|
||||
@@ -236,10 +240,10 @@ class work_under_id:
|
||||
|
||||
# End work_under_id
|
||||
|
||||
def parse_user_input(user_in):
|
||||
def parse_user_input(user_in, session):
|
||||
split_user_in = user_in.split()
|
||||
command = split_user_in[0]
|
||||
if command in ('a', 'c', 'm', 'n', 'q', 's', 'sc', 'sp', 'su'):
|
||||
if command in session.commands.keys():
|
||||
arguments = split_user_in[1:]
|
||||
return (command, arguments)
|
||||
else:
|
||||
@@ -249,9 +253,8 @@ def command_line_loop(session):
|
||||
user_in = ''
|
||||
while not user_in == 'q':
|
||||
user_in = input(f'Piano-Repertoire: {session.get_active_user()} >>> ')
|
||||
command, arguments = parse_user_input(user_in)
|
||||
command, arguments = parse_user_input(user_in, session)
|
||||
session.invoke_command(command, arguments)
|
||||
# print(f'command: {command}, arguments: {arguments}')
|
||||
print(session.result())
|
||||
|
||||
def main():
|
||||
|
||||
Reference in New Issue
Block a user