-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOpenSong.py
executable file
·140 lines (119 loc) · 3.43 KB
/
OpenSong.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
#!/usr/bin/env python3
import os
from xml.etree import ElementTree as ET
from datetime import datetime
class Set():
name = ''
path = ''
songs = []
def __repr__(self):
return '{}, {}, {}'.format(self.name, self.path, self.songs)
def __init__(self, setpath):
# print('opening set:', setpath)
if not os.path.exists(setpath):
return
xml = ET.parse(setpath)
root = xml.getroot()
self.path = setpath
self.name = root.attrib['name']
self.songs = []
slide_groups = xml.find('slide_groups')
for slide_group in slide_groups:
songpath = ''
if slide_group.attrib['type'] == 'song':
name = slide_group.attrib['name']
try:
subdir = slide_group.attrib['path']
except:
subdir = ''
if subdir != '':
songpath = os.path.join(songsdir, subdir, name)
else:
songpath = os.path.join(songsdir, name)
if songpath != '':
# print(songpath)
song = Song(songpath)
# print(song.name)
if (song.name != ''):
self.songs.append(song)
# print(self.name)
# print(self.songs)
class Song():
path = ''
lyrics = ''
name = ''
ccli = ''
data = {}
def __init__(self, songpath):
# print('opening song:', songpath)
if not os.path.exists(songpath):
return
xml = ET.parse(songpath)
song = xml.getroot()
self.path = songpath
self.data = {}
for element in song:
# print(element.tag)
# print(element.text)
# print(element.attrib)
self.data[element.tag] = element.text
self.name = self.data['title']
self.lyrics = self.data['lyrics']
self.ccli = self.data['ccli']
class SongFolder():
path = ''
songs = []
subfolders = []
name = ''
def __init__(self, folderpath):
self.path = folderpath
self.name = os.path.basename(folderpath)
if not os.path.exists(folderpath):
return
for item in os.listdir(self.path):
if '.' == item[0]:
continue
fp = os.path.join(self.path, item)
if os.path.isdir(fp):
self.subfolders.append(SongFolder(fp))
else:
self.songs.append(Song(fp))
class Library():
sets = {}
songfolder = None
songstats = {}
def __init__(self, new_librarypath):
global setsdir
global songsdir
self.librarypath = new_librarypath
setsdir = os.path.join(self.librarypath, 'Sets')
songsdir = os.path.join(self.librarypath, 'Songs')
self.songfolder = SongFolder(songsdir)
# create sets listing
for root, dirs, files in os.walk(setsdir):
for basename in files:
pathname = os.path.join(root, basename)
try:
songset = Set(pathname)
except:
# print("could not parse", basename)
continue
name = songset.name
try:
datestamp = datetime.strptime(name, "%Y-%m-%d")
except:
continue
if datestamp < datetime.strptime("2012-01-01", "%Y-%m-%d"):
continue
self.sets[datestamp] = songset
for song in songset.songs:
if song.path == '':
continue
self.songs.append(song)
songpath = song.path
name = os.path.basename(songpath)
if name not in self.songstats:
self.songstats[name] = {
"uses": 1, 'author': song.data['author'], 'copyright': song.data['copyright']}
else:
self.songstats[name]['uses'] += 1