Better Code Style
This commit is contained in:
parent
3383ca5a80
commit
d6f1748061
2 changed files with 90 additions and 48 deletions
4
Makefile
4
Makefile
|
@ -1,6 +1,8 @@
|
||||||
all:
|
all:
|
||||||
|
|
||||||
install:
|
install:
|
||||||
|
cp -f scorelib.py scorelib
|
||||||
install scorelib /usr/bin
|
install scorelib /usr/bin
|
||||||
|
rm scorelib
|
||||||
uninstall:
|
uninstall:
|
||||||
rm /usr/bin/scorelib
|
rm /usr/bin/scorelib
|
||||||
|
|
|
@ -1,18 +1,29 @@
|
||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
"""Scorelib - A score library management programm"""
|
|
||||||
|
"""
|
||||||
|
Scorelib - A score library management programm
|
||||||
|
Marcel Kapfer (mmk2410)
|
||||||
|
MIT License
|
||||||
|
Version 0.1.1
|
||||||
|
"""
|
||||||
|
|
||||||
import sqlite3
|
import sqlite3
|
||||||
import readline
|
import readline
|
||||||
import getpass
|
import getpass
|
||||||
import os
|
import os
|
||||||
|
|
||||||
user = getpass.getuser()
|
USER = getpass.getuser()
|
||||||
dbdir = "/home/" + user + "/.scorelib/"
|
CONFIG = "/home/" + USER + "/.scorelib/"
|
||||||
dbpath = dbdir + "scorelib.db"
|
DBPATH = CONFIG + "scorelib.db"
|
||||||
|
|
||||||
class Piece:
|
class Piece:
|
||||||
""" A class for working with scores """
|
""" A class for working with scores """
|
||||||
|
|
||||||
def __init__(self, piece):
|
def __init__(self, piece=None):
|
||||||
|
if piece is None:
|
||||||
|
raise NameError("No piece list given")
|
||||||
|
if piece['id'] is None:
|
||||||
|
raise NameError("No piece id given")
|
||||||
self.pieceid = piece['id']
|
self.pieceid = piece['id']
|
||||||
self.name = piece['name']
|
self.name = piece['name']
|
||||||
self.composer = piece['composer']
|
self.composer = piece['composer']
|
||||||
|
@ -42,7 +53,7 @@ Book: {}
|
||||||
"""
|
"""
|
||||||
con = None
|
con = None
|
||||||
try:
|
try:
|
||||||
con = sqlite3.connect(dbpath)
|
con = sqlite3.connect(DBPATH)
|
||||||
cur = con.cursor()
|
cur = con.cursor()
|
||||||
sqlcmd = "DELETE FROM Scores WHERE id = " + str(self.pieceid)
|
sqlcmd = "DELETE FROM Scores WHERE id = " + str(self.pieceid)
|
||||||
cur.execute(sqlcmd)
|
cur.execute(sqlcmd)
|
||||||
|
@ -65,7 +76,7 @@ Book: {}
|
||||||
self.key = rlinput("Key: ", self.key)
|
self.key = rlinput("Key: ", self.key)
|
||||||
self.book = rlinput("Book: ", self.book)
|
self.book = rlinput("Book: ", self.book)
|
||||||
# Pushing changes to the database
|
# Pushing changes to the database
|
||||||
con = sqlite3.connect(dbpath)
|
con = sqlite3.connect(DBPATH)
|
||||||
print(self.get_list)
|
print(self.get_list)
|
||||||
with con:
|
with con:
|
||||||
cur = con.cursor()
|
cur = con.cursor()
|
||||||
|
@ -89,12 +100,12 @@ def initialize():
|
||||||
|
|
||||||
print("Initializing the database...")
|
print("Initializing the database...")
|
||||||
|
|
||||||
os.mkdir(dbdir, 0o755)
|
os.mkdir(CONFIG, 0o755)
|
||||||
|
|
||||||
con = None
|
con = None
|
||||||
|
|
||||||
try:
|
try:
|
||||||
con = sqlite3.connect(dbpath)
|
con = sqlite3.connect(DBPATH)
|
||||||
|
|
||||||
cur = con.cursor()
|
cur = con.cursor()
|
||||||
|
|
||||||
|
@ -116,6 +127,8 @@ def initialize():
|
||||||
if con:
|
if con:
|
||||||
con.close()
|
con.close()
|
||||||
|
|
||||||
|
return 0
|
||||||
|
|
||||||
def destroy():
|
def destroy():
|
||||||
"""
|
"""
|
||||||
Destroys the table.
|
Destroys the table.
|
||||||
|
@ -125,7 +138,7 @@ def destroy():
|
||||||
con = None
|
con = None
|
||||||
|
|
||||||
try:
|
try:
|
||||||
con = sqlite3.connect(dbpath)
|
con = sqlite3.connect(DBPATH)
|
||||||
|
|
||||||
cur = con.cursor()
|
cur = con.cursor()
|
||||||
|
|
||||||
|
@ -139,6 +152,8 @@ def destroy():
|
||||||
if con:
|
if con:
|
||||||
con.close()
|
con.close()
|
||||||
|
|
||||||
|
return 0
|
||||||
|
|
||||||
def add():
|
def add():
|
||||||
""" Add a entry to the database """
|
""" Add a entry to the database """
|
||||||
while True:
|
while True:
|
||||||
|
@ -153,14 +168,18 @@ def add():
|
||||||
piece['key'] = input("Key: ")
|
piece['key'] = input("Key: ")
|
||||||
piece['book'] = input("Book: ")
|
piece['book'] = input("Book: ")
|
||||||
|
|
||||||
newPiece = Piece(piece)
|
try:
|
||||||
|
new_piece = Piece(piece)
|
||||||
|
except NameError as err:
|
||||||
|
print("Error: %s" % err.args[0])
|
||||||
|
return -1
|
||||||
|
|
||||||
newPiece.print_piece()
|
new_piece.print_piece()
|
||||||
|
|
||||||
con = None
|
con = None
|
||||||
|
|
||||||
try:
|
try:
|
||||||
con = sqlite3.connect(dbpath)
|
con = sqlite3.connect(DBPATH)
|
||||||
|
|
||||||
cur = con.cursor()
|
cur = con.cursor()
|
||||||
|
|
||||||
|
@ -183,6 +202,8 @@ def add():
|
||||||
if con:
|
if con:
|
||||||
con.close()
|
con.close()
|
||||||
|
|
||||||
|
return 0
|
||||||
|
|
||||||
def list_scores():
|
def list_scores():
|
||||||
"""
|
"""
|
||||||
List all available scores
|
List all available scores
|
||||||
|
@ -192,7 +213,7 @@ def list_scores():
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
|
||||||
con = sqlite3.connect(dbpath)
|
con = sqlite3.connect(DBPATH)
|
||||||
|
|
||||||
cur = con.cursor()
|
cur = con.cursor()
|
||||||
|
|
||||||
|
@ -204,7 +225,7 @@ def list_scores():
|
||||||
|
|
||||||
row = cur.fetchone()
|
row = cur.fetchone()
|
||||||
|
|
||||||
if row == None:
|
if row is None:
|
||||||
break
|
break
|
||||||
|
|
||||||
print(
|
print(
|
||||||
|
@ -221,35 +242,41 @@ Book: {}
|
||||||
|
|
||||||
except sqlite3.Error as err:
|
except sqlite3.Error as err:
|
||||||
print("Error: %s" % err.args[0])
|
print("Error: %s" % err.args[0])
|
||||||
|
return 1
|
||||||
|
|
||||||
finally:
|
finally:
|
||||||
if con:
|
if con:
|
||||||
con.close()
|
con.close()
|
||||||
|
|
||||||
|
return 0
|
||||||
|
|
||||||
def edit():
|
def edit_get_id():
|
||||||
"""
|
""" Get the piece id from the user"""
|
||||||
Function for editing the pieces
|
|
||||||
"""
|
|
||||||
pieceid = None
|
pieceid = None
|
||||||
# Ask piece id from user
|
|
||||||
while True:
|
while True:
|
||||||
pieceid = input("Enter the piece number: ")
|
pieceid = input("Enter the piece number: ")
|
||||||
if pieceid == 'quit':
|
if pieceid == 'quit':
|
||||||
return 0
|
return 0
|
||||||
elif pieceid and pieceid.isdigit():
|
elif pieceid and pieceid.isdigit():
|
||||||
con = sqlite3.connect(dbpath)
|
con = sqlite3.connect(DBPATH)
|
||||||
piecerow = None
|
piecerow = None
|
||||||
with con:
|
with con:
|
||||||
cur = con.cursor()
|
cur = con.cursor()
|
||||||
cur.execute("SELECT * FROM Scores WHERE Id=" + str(pieceid))
|
cur.execute("SELECT * FROM Scores WHERE Id=" + str(pieceid))
|
||||||
piecerow = cur.fetchone()
|
piecerow = cur.fetchone()
|
||||||
if piecerow == None:
|
if piecerow is None:
|
||||||
print("No Piece with this number available.")
|
print("No Piece with this number available.")
|
||||||
else:
|
else:
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
print("Input must be a number!")
|
print("Input must be a valid number!")
|
||||||
|
return piecerow
|
||||||
|
|
||||||
|
|
||||||
|
def edit():
|
||||||
|
"""Function for editing the pieces"""
|
||||||
|
# Ask piece id from user
|
||||||
|
piecerow = edit_get_id()
|
||||||
|
|
||||||
# Getting the piece information
|
# Getting the piece information
|
||||||
piecedata = {"id": piecerow[0], "name": piecerow[1], "composer": piecerow[2],\
|
piecedata = {"id": piecerow[0], "name": piecerow[1], "composer": piecerow[2],\
|
||||||
|
@ -262,7 +289,7 @@ def edit():
|
||||||
while True:
|
while True:
|
||||||
edtcmd = input(" (edit) > ")
|
edtcmd = input(" (edit) > ")
|
||||||
if edtcmd in ['help', '?', 'usage']:
|
if edtcmd in ['help', '?', 'usage']:
|
||||||
helpedittext(False)
|
helpedittext()
|
||||||
elif edtcmd in ['done', 'q']:
|
elif edtcmd in ['done', 'q']:
|
||||||
return 0
|
return 0
|
||||||
elif edtcmd in ['print', 'p']:
|
elif edtcmd in ['print', 'p']:
|
||||||
|
@ -278,24 +305,24 @@ def edit():
|
||||||
def search():
|
def search():
|
||||||
""" Search through the database """
|
""" Search through the database """
|
||||||
term = input("Search for: ")
|
term = input("Search for: ")
|
||||||
con = sqlite3.connect(dbpath)
|
con = sqlite3.connect(DBPATH)
|
||||||
with con:
|
with con:
|
||||||
cur = con.cursor()
|
cur = con.cursor()
|
||||||
cur.execute("SELECT * FROM Scores")
|
cur.execute("SELECT * FROM Scores")
|
||||||
rows = cur.fetchall()
|
rows = cur.fetchall()
|
||||||
for x in range(1,6):
|
for item in range(1, 6):
|
||||||
if x == 1:
|
if item == 1:
|
||||||
print("By name\n")
|
print("By name\n")
|
||||||
elif x == 2:
|
elif item == 2:
|
||||||
print("By Composer\n")
|
print("By Composer\n")
|
||||||
elif x == 3:
|
elif item == 3:
|
||||||
print("By Opus\n")
|
print("By Opus\n")
|
||||||
elif x == 4:
|
elif item == 4:
|
||||||
print("By Key\n")
|
print("By Key\n")
|
||||||
elif x == 5:
|
elif item == 5:
|
||||||
print("By Book\n")
|
print("By Book\n")
|
||||||
for row in rows:
|
for row in rows:
|
||||||
if term in row[x]:
|
if term in row[item]:
|
||||||
piecedata = {"id": row[0], "name": row[1], "composer": row[2],\
|
piecedata = {"id": row[0], "name": row[1], "composer": row[2],\
|
||||||
"opus": row[3], "key": row[4], "book": row[5]}
|
"opus": row[3], "key": row[4], "book": row[5]}
|
||||||
piece = Piece(piecedata)
|
piece = Piece(piecedata)
|
||||||
|
@ -303,8 +330,7 @@ def search():
|
||||||
piece.print_piece()
|
piece.print_piece()
|
||||||
|
|
||||||
|
|
||||||
|
def helpedittext(doneshit=False):
|
||||||
def helpedittext(doneshit):
|
|
||||||
""" print the edit help text """
|
""" print the edit help text """
|
||||||
if doneshit:
|
if doneshit:
|
||||||
print("The entered command ist not available.")
|
print("The entered command ist not available.")
|
||||||
|
@ -320,13 +346,13 @@ edit, change, c, e Change the values of the item
|
||||||
"""
|
"""
|
||||||
)
|
)
|
||||||
|
|
||||||
def helptext(doneshit):
|
def helptext(doneshit=False):
|
||||||
""" print the help text """
|
""" print the help text """
|
||||||
if doneshit:
|
if doneshit:
|
||||||
print("The entered command is not available.")
|
print("The entered command is not available.")
|
||||||
print(
|
print(
|
||||||
"""
|
"""
|
||||||
Scorelib v0.1.0
|
Scorelib v0.1.1
|
||||||
A program for managing your music score collection
|
A program for managing your music score collection
|
||||||
|
|
||||||
Author:
|
Author:
|
||||||
|
@ -349,31 +375,42 @@ quit, exit, x, q close Scorelib
|
||||||
"""
|
"""
|
||||||
)
|
)
|
||||||
|
|
||||||
print(
|
|
||||||
"""
|
def main():
|
||||||
|
""" Main """
|
||||||
|
print(
|
||||||
|
"""
|
||||||
Welcome to Scorelib - A program for managing your music score collection
|
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 \
|
If you use this program the first time you have to initialize the database \
|
||||||
before you can use it.
|
before you can use it.
|
||||||
|
|
||||||
Type 'help' to see a list of commands
|
Type 'help' to see a list of commands
|
||||||
"""
|
"""
|
||||||
)
|
)
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
cmd = input(" > ")
|
cmd = input(" > ")
|
||||||
|
|
||||||
if cmd in ['usage', 'help', '?']:
|
if cmd in ['usage', 'help', '?']:
|
||||||
helptext(False)
|
helptext()
|
||||||
elif cmd in ['quit', 'exit', 'x', 'q']:
|
elif cmd in ['quit', 'exit', 'x', 'q']:
|
||||||
print("Good Bye!")
|
print("Good Bye!")
|
||||||
exit(0)
|
exit(0)
|
||||||
elif cmd == 'init':
|
elif cmd == 'init':
|
||||||
initialize()
|
status = initialize()
|
||||||
|
if status == 1:
|
||||||
|
print("A database error occurred. Please try again.")
|
||||||
elif cmd == 'kill':
|
elif cmd == 'kill':
|
||||||
destroy()
|
status = destroy()
|
||||||
|
if status == 1:
|
||||||
|
print("A database error occurred. Please try again.")
|
||||||
elif cmd in ['add', 'new', 'a']:
|
elif cmd in ['add', 'new', 'a']:
|
||||||
add()
|
status = add()
|
||||||
|
if status == 1:
|
||||||
|
print("A database error occurred. Please try again.")
|
||||||
|
elif status == -1:
|
||||||
|
print("An input you made was not accepted.")
|
||||||
elif cmd in ['list', 'l']:
|
elif cmd in ['list', 'l']:
|
||||||
list_scores()
|
list_scores()
|
||||||
elif cmd in ['edit', 'e']:
|
elif cmd in ['edit', 'e']:
|
||||||
|
@ -382,3 +419,6 @@ while True:
|
||||||
search()
|
search()
|
||||||
else:
|
else:
|
||||||
helptext(True)
|
helptext(True)
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
Reference in a new issue