-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.py
173 lines (141 loc) · 4.5 KB
/
parser.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
from __future__ import unicode_literals
# !/usr/bin/env python
# -*- coding: utf-8 -*-
import glob
import os
from tags import tag_finder
import logging
from operator import itemgetter, attrgetter, methodcaller
def path_leaf(path):
"""
It gets the path final leaf
"""
head, tail = os.path.split(path)
return tail or os.path.basename(head)
class MusicDir:
"""
The MusicDir class indexes the Music directory and provides a
find_number(index) method
"""
def __init__(self, path):
# TODO
"""
"""
self.path = os.path.join(os.path.dirname(__file__), path)
os.chdir(self.path)
files = glob.glob("*")
# Music objects list
self.musics = []
# Music index list (A12, B1, ...)
self.indexes = []
for music_file in files:
# noinspection PyBroadException
try:
self.musics.append(Music(music_file))
l = len(self.musics)
self.indexes.append(self.musics[l - 1].index)
logging.info("Successfully added %s to library" % music_file)
except:
logging.warning("Unable to load %s to library" % music_file)
self._sort()
def _sort(self):
self.musics = sorted(self.musics, key=attrgetter('index.letter', 'index.number'))
self.indexes = sorted(self.indexes, key=attrgetter('letter', 'number'))
def print_music_dir(self):
# TODO
"""
"""
for music in self.musics:
music.print_music()
return
def find_index(self, index):
# TODO
"""
Returns the Music corresponding to the index
"""
for i in range(0, len(self.musics)):
condition = index.__eq__(self.musics[i].index)
if condition:
return self.musics[i]
return None
def filled_slots(self):
# TODO
"""
"""
dic = dict([(1, 'A'), (2, 'B'), (3, 'C'), (4, 'D')])
result = [[], [], [], []]
letter = 1
while letter <= 4:
number = 0
while number < 20:
number += 1
if self.find_index(Index(dic[letter], number)) != None:
# noinspection PyTypeChecker
result[letter - 1].append(True)
else:
# noinspection PyTypeChecker
result[letter - 1].append(False)
letter += 1
return result
class Music:
# TODO
"""
"""
def __init__(self, path):
#TODO
"""
"""
#file named : CODE-Name-Artist.format
self.path = path
self.file_name = path_leaf(self.path)
self.index, self.artist, self.name, self.format = self.find_tags()
def __str__(self):
return self.index.__str__() + " - " + self.name + " - " + self.artist + " - "+ self.format
def __repr__(self):
return self.__str__()
def find_tags(self):
#TODO
"""
"""
logging.debug("Executing tag_finder() method")
tags = tag_finder(self.path)
#Audio file mode
#index
index = self.file_name.split("-")[0]
index = Index(index[:1], int(index[1:]))
logging.debug(index)
#artiste
try:
artist = tags['artist']
except KeyError:
artist = "unknown"
#nom
logging.debug("Artist:" + artist)
try:
name = tags['title']
except KeyError:
name = "unknown"
#format
logging.debug("Title:" + name)
extension = self.file_name.split(".")[-1]
return index, artist, name, extension
def print_music(self):
#TODO
"""
"""
print self.index.__str__() + " - " + self.name + " - " + self.artist + " - " + self.format
def display(self):
#TODO
"""
"""
return "%s%s : %s by %s" % (self.index, self.name, self.artist)
class Index(object):
def __init__(self, letter, number):
self.letter = letter
self.number = number
def __str__(self):
return self.letter + unicode(self.number)
def __repr__(self):
return self.__str__()
def __eq__(self, other):
return self.letter == other.letter and self.number == other.number