This repository has been archived on 2020-09-23. You can view files and clone it, but cannot push or open issues or pull requests.
scorelib/scorelib
2015-09-30 11:33:49 +02:00

384 lines
9.3 KiB
Python
Executable file

#!/usr/bin/env python3
"""Scorelib - A score library management programm"""
import sqlite3
import readline
import getpass
import os
user = getpass.getuser()
dbdir = "/home/" + user + "/.scorelib/"
dbpath = dbdir + "scorelib.db"
class Piece:
""" A class for working with scores """
def __init__(self, piece):
self.pieceid = piece['id']
self.name = piece['name']
self.composer = piece['composer']
self.opus = piece['opus']
self.key = piece['key']
self.book = piece['book']
def get_list(self):
""" Returns a list for sending to the database """
piece = (self.name, self.composer, self.opus, self.key, self.book, self.pieceid)
return piece
def print_piece(self):
""" prints the current piece """
print(
"""
Name: {}
Composer: {}
Opus: {}
Key: {}
Book: {}
""".format(self.name, self.composer, self.opus, self.key, self.book))
def delete(self):
"""
Delete a item
"""
con = None
try:
con = sqlite3.connect(dbpath)
cur = con.cursor()
sqlcmd = "DELETE FROM Scores WHERE id = " + str(self.pieceid)
cur.execute(sqlcmd)
con.commit()
except sqlite3.Error as err:
print("Error: %s" % err.args[0])
finally:
if con:
con.close()
def change(self):
""")
Change a item
"""
# Updating the values
name = rlinput("Name: ", self.name)
self.name = name or self.name
self.composer = rlinput("Composer: ", self.composer)
self.opus = rlinput("Opus: ", self.opus)
self.key = rlinput("Key: ", self.key)
self.book = rlinput("Book: ", self.book)
# Pushing changes to the database
con = sqlite3.connect(dbpath)
print(self.get_list)
with con:
cur = con.cursor()
cur.execute("UPDATE Scores SET Name=?, Composer=?, Opus=?, Key=?, Book=? WHERE Id=?",\
self.get_list())
con.commit()
def rlinput(prompt, prefill=''):
""" Function for advanced input (preset user input) """
readline.set_startup_hook(lambda: readline.insert_text(prefill))
try:
return input(prompt)
finally:
readline.set_startup_hook()
def initialize():
"""
Initializing the database by creating a table
"""
print("Initializing the database...")
os.mkdir(dbdir, 0o755)
con = None
try:
con = sqlite3.connect(dbpath)
cur = con.cursor()
cur.execute(
"CREATE TABLE Scores( \
Id INTEGER PRIMARY KEY ASC NOT NULL, \
Name TEXT NOT NULL, \
Composer TEXT, \
Opus TEXT, \
Key TEXT, \
Book TEXT);"
)
except sqlite3.Error as err:
print("Error: %s" % err.args[0])
return 1
finally:
if con:
con.close()
def destroy():
"""
Destroys the table.
"""
print("Destroying all informations...")
con = None
try:
con = sqlite3.connect(dbpath)
cur = con.cursor()
cur.execute("DROP TABLE IF EXISTS Scores")
except sqlite3.Error as err:
print("Error: %s" % err.args[0])
return 1
finally:
if con:
con.close()
def add():
""" Add a entry to the database """
while True:
title = input("Title: ")
if title:
break
else:
print("You have to enter the name of the piece")
piece = {"id": 0, "name":title}
piece['composer'] = input("Composer: ")
piece['opus'] = input("Opus: ")
piece['key'] = input("Key: ")
piece['book'] = input("Book: ")
newPiece = Piece(piece)
newPiece.print_piece()
con = None
try:
con = sqlite3.connect(dbpath)
cur = con.cursor()
sqlcmd = "INSERT INTO Scores VALUES(NULL,'"\
+ piece['name'] + "','" \
+ piece['composer'] + "','" \
+ piece['opus'] + "','" \
+ piece['key'] + "','" \
+ piece['book'] + "')"
cur.execute(sqlcmd)
con.commit()
except sqlite3.Error as err:
print("Error: %s" % err.args[0])
return 1
finally:
if con:
con.close()
def list_scores():
"""
List all available scores
"""
con = None
try:
con = sqlite3.connect(dbpath)
cur = con.cursor()
cur.execute("SELECT * FROM Scores")
print()
while True:
row = cur.fetchone()
if row == None:
break
print(
"""
Piece number: {}
Name: {}
Composer: {}
Opus: {}
Key: {}
Book: {}
""".format(row[0], row[1], row[2], row[3], row[4], row[5])
)
except sqlite3.Error as err:
print("Error: %s" % err.args[0])
finally:
if con:
con.close()
def edit():
"""
Function for editing the pieces
"""
pieceid = None
# Ask piece id from user
while True:
pieceid = input("Enter the piece number: ")
if pieceid == 'quit':
return 0
elif pieceid and pieceid.isdigit():
con = sqlite3.connect(dbpath)
piecerow = None
with con:
cur = con.cursor()
cur.execute("SELECT * FROM Scores WHERE Id=" + str(pieceid))
piecerow = cur.fetchone()
if piecerow == None:
print("No Piece with this number available.")
else:
break
else:
print("Input must be a number!")
# Getting the piece information
piecedata = {"id": piecerow[0], "name": piecerow[1], "composer": piecerow[2],\
"opus": piecerow[3], "key": piecerow[4], "book": piecerow[5]}
piece = Piece(piecedata)
print("Piece Information:")
piece.print_piece()
# edit prompt
while True:
edtcmd = input(" (edit) > ")
if edtcmd in ['help', '?', 'usage']:
helpedittext(False)
elif edtcmd in ['done', 'q']:
return 0
elif edtcmd in ['print', 'p']:
piece.print_piece()
elif edtcmd in ['delete', 'd']:
piece.delete()
return 0
elif edtcmd in ['edit', 'change', 'c', 'e']:
piece.change()
else:
helpedittext(True)
def search():
""" Search through the database """
term = input("Search for: ")
con = sqlite3.connect(dbpath)
with con:
cur = con.cursor()
cur.execute("SELECT * FROM Scores")
rows = cur.fetchall()
for x in range(1,6):
if x == 1:
print("By name\n")
elif x == 2:
print("By Composer\n")
elif x == 3:
print("By Opus\n")
elif x == 4:
print("By Key\n")
elif x == 5:
print("By Book\n")
for row in rows:
if term in row[x]:
piecedata = {"id": row[0], "name": row[1], "composer": row[2],\
"opus": row[3], "key": row[4], "book": row[5]}
piece = Piece(piecedata)
print("Piece number: %s" % str(row[0]))
piece.print_piece()
def helpedittext(doneshit):
""" print the edit help text """
if doneshit:
print("The entered command ist not available.")
print(
"""
Edit help text:
help, ?, usage Print edit help
done, q Done editing piece; back to main menu
print, p Print piece information
delete, d Delete this item and go back to main menu
edit, change, c, e Change the values of the item
"""
)
def helptext(doneshit):
""" print the help text """
if doneshit:
print("The entered command is not available.")
print(
"""
Scorelib v0.1.0
A program for managing your music score collection
Author:
Marcel Kapfer (mmk2410) <marcelmichaelkapfer@yahoo.co.nz>
License:
MIT License
Help:
help, usage, ? prints this text
add, new, a add a new piece
list, l list all pieces
edit, e edit a piece (piece number required)
search, s search for a piece
init initializes the database
kill destroys the database
quit, exit, x, q close Scorelib
"""
)
print(
"""
Welcome to Scorelib - A program for managing your music score collection
If you use this program the first time you have to initialize the database \
before you can use it.
Type 'help' to see a list of commands
"""
)
while True:
cmd = input(" > ")
if cmd in ['usage', 'help', '?']:
helptext(False)
elif cmd in ['quit', 'exit', 'x', 'q']:
print("Good Bye!")
exit(0)
elif cmd == 'init':
initialize()
elif cmd == 'kill':
destroy()
elif cmd in ['add', 'new', 'a']:
add()
elif cmd in ['list', 'l']:
list_scores()
elif cmd in ['edit', 'e']:
edit()
elif cmd in ['search', 's']:
search()
else:
helptext(True)