Compare commits

...

3 Commits

2 changed files with 48 additions and 31 deletions

View File

@@ -18,9 +18,10 @@ At the moment, an alpha version is undertaken, with these features:
- Set an active user (`a number_of_user`)
- Show the pieces stored in the database (`s`)
- Mark pieces as mastered for the active user (`m number_of_work`)
- Show pieces by composer (`sc` shows composers, `c number_of_composer` shows pieces)
- Show movements through pieces (`sp piece_number`)
- Show pieces by composer (`c` shows composers, `s number_of_composer` shows pieces)
- Show movements through works (`sw work_number`)
- Mark movement as mastered for active user (`m number_of_work number_of_movement`)
- (planned) Show possible commands (`h`)
- (planned) Show mastered works/movements for activated user
- (planned) Save recordings per movement in db
- (planned) Listen to saved recordings

View File

@@ -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, 'Set an active user\n Usage: a number_of_user'),
'c': (self.show_composers, 'Shows composers'),
'h': (self.show_help, 'Displays this help document'),
'm': (self.mark_work_as_mastered, 'Marks work or movement as mastered for the active user\n Usage: m number_of_work, m number_of_work number_of_movement'),
'n': (self.create_new_user, 'Creates new user\n Usage: n firstname name'),
's': (self.show_works, 'Show the works stored in the database\n Usage: s, s number_of_composer'),
'sw': (self.show_movements, 'Show movements of a work\n Usage: sw work_number'),
'su': (self.show_users, 'Show all users'),
'q': (self.quit, 'Quits the program')
}
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,28 @@ class Session:
fun_resultstring += f'{item[0]}: {item[2]}, {item[1]}\n'
return fun_resultstring
def show_movements(self, work_id):
def show_help(self, arguments):
fun_resultstring = ''
if len(arguments) > 0:
qu_com = arguments[0]
if qu_com in self.commands:
fun_resultstring += f'{qu_com}: {self.commands[qu_com][1]}'
else:
fun_resultstring += f'Command {qu_com} not known, for help press h'
else:
for key, value in self.commands.items():
fun_resultstring += f'{key}: {value[1]}\n'
return fun_resultstring
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 +98,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 +140,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 +253,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 +266,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():