MDB/mdb.py

214 lines
7.6 KiB
Python
Raw Permalink Normal View History

2024-02-20 17:04:01 +01:00
import PySimpleGUI as sg
2024-02-21 00:01:34 +01:00
import ui
2024-02-20 17:04:01 +01:00
class Helper:
def __init__(self):
self.song_file = "C:\\Users\\Wirezat\\Downloads\\MDB\\song.txt"
self.dance_file = "C:\\Users\\Wirezat\\Downloads\\MDB\\dance.txt"
def get_song(self, *args):
'''
Retrieves all Songs Containing the Input.
Parameters:
*args (str): Parameters to search for.
Returns:
list: List of songs containing all specified substrings.
'''
result_list = []
try:
with open(self.song_file, 'r') as f:
for line in f:
if all(arg in line for arg in args):
song= line.split(';')
for i, item in enumerate(song):
if '[' in item and ']' in item:
dance_parts = item.strip("[]'").split(":")
song[i] = (dance_parts[0], dance_parts[1])
result_list.append(song)
except FileNotFoundError:
sg.popup_error('Datei wurde nicht gefunden.')
return 0
except IsADirectoryError:
sg.popup_error('The given Filepath is a directory')
return 0
except PermissionError:
sg.popup_error('The program has no permission to acces file')
return 0
return result_list
def get_song_beautified(self, length, *args):
'''
Retrieves all Songs Containing the Input. Gets the dance only as Long Form
Parameters:
length (str): long for the full Dance Name short for the shortened name
*args (str): Parameters to search for.
Returns:
list: List of songs containing all specified substrings. Formatted easy for user read
'''
base_songs = self.get_song(*args)
beautiful_songs = []
2024-02-21 00:01:34 +01:00
if not base_songs:
return 0
2024-02-20 17:04:01 +01:00
for song in base_songs:
2024-02-21 00:01:34 +01:00
if length:
2024-02-20 17:04:01 +01:00
beautiful_dance = [song[4][0]]
2024-02-21 00:01:34 +01:00
else:
2024-02-20 17:04:01 +01:00
beautiful_dance = [song[4][1]]
beautiful_song = song[:4] + (beautiful_dance) + song[5:]
beautiful_songs.append(beautiful_song)
return beautiful_songs
def write_song(self, song, artist, album, duration, dance, comment):
"""
Writes multiple values as a single line to the song file: "{Song}{Artist}{Album}{Duration}{Dance}{comment}"
"""
#sets default duration to "00:00:00"
if duration == "":
duration = "00:00:00"
validation_result = self.validate_song(song, artist, album, duration, dance)
match validation_result:
case 0:
return validation_result
case 1:
return validation_result
case 2:
return validation_result
case 3:
return validation_result
print(f"{validation_result}")
try:
with open(self.song_file, "a") as f:
f.write(f"{song};{artist};{album};{duration};{validation_result};{comment}\n")
return None
except FileNotFoundError:
sg.popup_error('Datei wurde nicht gefunden.')
return 0
except IsADirectoryError:
sg.popup_error('The given Filepath is a directory')
return 0
except PermissionError:
sg.popup_error('The program has no permission to acces file')
return 0
def get_dance(self, input):
"""
Retrieves All Dances containing the Input
Parameters:
input (str): Substring to search for in the dance file.
Returns:
list: List of tuples containing matching short and long dances.
Empty list if no matches found.
"""
result_list = []
try:
with open(self.dance_file, 'r') as f:
for line in f:
short, long = line.strip().split(':')
if input == short or input == long:
result_list.append(f"{short}:{long}") # Append "short:long"
return result_list
except FileNotFoundError:
sg.popup_error('Datei wurde nicht gefunden.')
return 0
except IsADirectoryError:
sg.popup_error('The given Filepath is a directory')
return 0
except PermissionError:
sg.popup_error('The program has no permission to acces file')
return 0
def add_dance(self, short, long):
"""
Appends a new line to the dance file in the following format: "short:long"
Parameters:
short (str): Short name of the dance.
long (str): Full name of the dance.
"""
try:
with open(self.dance_file, "a") as f:
f.write(f"{short}:{long}\n")
except FileNotFoundError:
sg.popup_error('Datei wurde nicht gefunden.')
return 0
except IsADirectoryError:
sg.popup_error('The given Filepath is a directory')
return 0
except PermissionError:
sg.popup_error('The program has no permission to acces file')
return 0
def validate_time_format(self, string, delimiter=":"):
"""
Checks if string is in "hh:mm:ss" Format or in "mm:ss" format
Parameters:
string (str): the string to be checked.
delimiter (str): the delimiter between the numbers.
Returns:
bool: True, wenn der String gültig ist, False sonst.
"""
parts = string.split(delimiter)
if len(parts) == 2:
minute, second = parts
hour = "00"
elif len(parts) == 3:
hour, minute, second = parts
else:
return False
if not hour.isdigit() and minute.isdigit() and second.isdigit():
return False
hour = int(hour)
minute = int(minute)
second = int(second)
if not (0 <= hour < 24) or not (0 <= minute < 60) or not (0 <= second < 60):
return False
return True
def validate_song(self, song, artist, album, duration, dance):
"""
Validates a song based on its attributes.
Parameters:
song (str): The title of the song.
artist (str): The artist of the song.
album (str): The album of the song.
duration (int): The duration of the song.
dance (str): The dance style of the song.
Returns:
1: if song already is in the database
2: if the duration is invalid
3: if the dance is invalid
"""
# Check if the song already exists in the database
exists = self.get_song(song, artist, album, duration, dance)
dance_entry = self.get_dance(dance)
if exists and exists != 0:
sg.popup_error('This Song already exists')
return 1
if not self.validate_time_format(duration):
sg.popup_error('The Time Format is not Valid. Please use "00:00:00"')
return 2
# Check if the dance style is invalid
if dance_entry == 0:
return
if not dance_entry:
sg.popup_error('The Dance is not valid')
return 3
# Otherwise, return the dance style found in the database
return dance_entry
2024-02-21 00:01:34 +01:00
class Settings:
def __init__(self) -> None:
pass
2024-02-20 17:04:01 +01:00
if __name__ == "__main__":
2024-02-21 00:01:34 +01:00
app = ui.Main()
2024-02-20 17:04:01 +01:00
app.run()