-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.py
185 lines (150 loc) · 6.14 KB
/
database.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#!/usr/bin/python3
import json
import yaml
import sqlite3
from datetime import date
class PlaylistItem():
def __repr__(self):
return str(self.songId)
class Song():
@classmethod
def from_yaml(cls, loader, node):
s = cls()
attrs = ['id', 'name', 'file', 'store', 'notes', 'bpm', 'instruments', 'visual', 'band']
[setattr(s, x, node[x]) for x in attrs if x in node]
[setattr(s, x, None) for x in attrs if x not in node]
# Temporary conversion
#if 'Tempo' in node: s.bpm = node['Tempo']
#if 'Notes' in node: s.notes = node['Notes']
s.played = 0
return s
@classmethod
def to_yaml(cls, dumper, data):
node = data.__dict__.copy()
exclude = ['played', 'filename', '_format']
for i in data.__dict__:
if node[i] == None or i in exclude:
del node[i]
return node
class JSONEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, Song):
return {x: getattr(obj, x) for x in ['name', 'file', 'filename', 'store', 'notes', 'bpm', 'played']}
if isinstance(obj, PlaylistItem):
return {x: getattr(obj, x) for x in ['id', 'playlistId', 'songId']}
return json.JSONEncoder.default(self, obj)
class Database():
def __init__(self):
self.pli_counter = 1
self.config = yaml.load(open('config.yaml', 'r').read(), yaml.Loader)
songs = yaml.load(open('songlist_new.yaml', 'r').read(), yaml.Loader)
#songs = yaml.load(open('songs.yaml', 'r').read(), yaml.Loader)
self.songs = {int(s['id']):Song.from_yaml(None, s) for s in songs}
for s in self.songs:
# FIXME: HOTFIX
if self.songs[s].band == None:
self.songs[s].band = 1
for song in self.songs.values():
store = (self.config['stores'][song.store]) if song.store != None else (self.config['stores'][self.config['defaultStore']])
song.filename = self.config['prefixes'][store['prefix']] + store['path'] + song.file + store['suffix'] if song.file != None else None
if 'format' in store:
song.prefix = self.config['prefixes'][store['prefix']]
song._format = store['format']
#song._format = song.format + store['path'] + song.file + store['suffix'] if song.file != None else None
self.playlist = yaml.load(open('playlist.yaml', 'r').read(), yaml.Loader)
for p in self.playlist.values():
if not hasattr(p, 'currentItemId'):
p['currentItemId'] = None
for p in self.playlist.values():
for i in range(len(p['items'])):
pli = p['items'][i]
self.songs[pli.songId].played += 1
p['items'][i].id = self.pli_counter
#self.songs[i.songId].played += 1
self.pli_counter += 1
self.save()
def save(self):
with open('playlist.yaml', 'w') as yaml_file:
yaml.dump(self.playlist, yaml_file, default_flow_style=False)
def saveConfig(self):
with open('config.yaml', 'w') as yaml_file:
yaml.dump(self.config, yaml_file, default_flow_style=False)
def saveSonglist(self):
with open('songlist_new.yaml', 'w') as yaml_file:
yaml.dump([Song.to_yaml(None, self.songs[s]) for s in self.songs], yaml_file, default_flow_style=False, allow_unicode=True)
def newPlaylist(self, band, name):
index = len(self.playlist) + 1
assert index not in self.playlist
p = self.playlist[index] = {}
p['id'] = index
p['band'] = band
p['currentItemId'] = None
p['date'] = str(date.today().strftime("%Y-%m-%d"))
p['items'] = []
p['note'] = name
self.save()
return index
def newPlaylistItem(self, pl, song):
item = PlaylistItem()
item.id = self.pli_counter
item.playlistId = pl
item.songId = song.id
item.played = False
#item.pos = len(self.playlist[pl]['items'])
self.pli_counter += 1
#self.playlist[pl]['items'][item.id] = item
self.playlist[pl]['items'].append(item)
self.save()
return item
def newSong(self, band, name):
pass
def deletePlaylistItem(self, pli):
i = self.playlist[pli.playlistId]['items'].index(pli)
del self.playlist[pli.playlistId]['items'][i]
self.save()
def playlistItemMove(self, item, new_index, relative=False):
pl = self.playlist[item.playlistId]['items']
index = pl.index(item) # item.pos
if relative:
if index + new_index not in range(0, len(pl) + 1):
return False
pl.pop(index)
pl.insert(index + (-1 if new_index < 0 else 1), item)
else:
i = pl.pop(index)
pl.insert(new_index, item)
self.save()
return True
def get_currentPlaylistItem(self, playlistId):
playlist = self.playlist[playlistId]
pl = playlist['items']
ci = playlist['currentItemId']
if ci == None:
return None
pli = [x for x in pl if x.id == ci]
# Can be deleted
if pli:
return pli[0]
return None
def set_currentPlaylistItem(self, playlistId, item):
playlist = self.playlist[playlistId]
pl = playlist['items']
playlist['currentItemId'] = item.id
def get_playlistItemNeighbour(self, playlistId, pli, offset):
playlist = self.playlist[playlistId]
if pli == None:
#i = len(playlist['items']) if offset < 0 else -offset
i = -1 if offset < 0 else -offset
else:
i = playlist['items'].index(pli)
if i + offset not in range(len(playlist['items'])):
return None
else:
return playlist['items'][i + offset]
def setActivePlaylist(self, band, playlistId):
self.config['bands'][band]['activePlaylist'] = playlistId
self.saveConfig()
pass
#def get_currentPlaylistItem(self, playlistId):
# playlist = db.playlist[playlistId]
# ci = pli['currentItemId']