Initial commit

This commit is contained in:
Marcel Kapfer 2015-09-30 11:33:49 +02:00
commit fe1e17ac89
5 changed files with 458 additions and 0 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
__pycache__

21
LICENSE Normal file
View file

@ -0,0 +1,21 @@
The MIT License (MIT)
Copyright (c) 2015 Marcel Kapfer (mmk2410) <marcelmichaelkapfer@yahoo.co.nz>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

6
Makefile Normal file
View file

@ -0,0 +1,6 @@
all:
install:
install scorelib /usr/bin
uninstall:
rm /usr/bin/scorelib

46
README.md Normal file
View file

@ -0,0 +1,46 @@
# Scorelib
A program for managing your music score collection. It runs on Linux and is written for the holy command line.
## Usage
There are two different modes: the normal mode and the edit mode. When you start the program you start out in the normal mode with the prompt ` > ` and you can run the following commands:
| Command | Description |
|------------------|:--------------------------:|
| help, usage, ? | print the help text |
| add, new, a | add a new piece |
| list, l | list all pieces |
| edit, e | edit a piece |
| search, s | search for a piece |
| init | initialize the database |
| kill | destroy the database |
| quit, exit, x, q | close Scorelib |
If you switch to the editing mode your prompt will look like this: ` (edit) > ` and you can run these commands:
| Commands | Description |
|--------------------|:-------------------------------------------:|
| help, usage, ? | Print the help text |
| done, q | Done editing piece, back to normal mode |
| print, p | Print piece information |
| delete, d | Delete this item and go back to normal mode |
| edit, change, c, e | Change the values of the item |
## Contributing
The program is licensed under MIT license. If you want to contribute just follow these steps:
1. Fork it
2. Create your feature branch (`git chechout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create new pull request
You can also add yourself to the CONTRIBUTORS.md file. (Create it if it doesn't exist)
## Social
Twitter: [@mmk2410](https://twitter.com/mmk2410)
Google+: [+MarcelMichaelKapfer](https://plus.google.com/+MarcelMichaelKapfer)

384
scorelib Executable file
View file

@ -0,0 +1,384 @@
#!/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)