forked from puddletag/puddletag
/
audio_filter.py
233 lines (184 loc) · 6.19 KB
/
audio_filter.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# -*- coding: utf-8 -*-
import logging
import re
from pyparsing import (CaselessLiteral, OpAssoc, Combine, Word,
alphanums, QuotedString, infix_notation)
from . import findfunc, audioinfo
from .puddleobjects import gettaglist
from .util import to_string
def str_cmp(a, b):
if not isinstance(a, str):
a = '\\'.join(a)
if not isinstance(b, str):
b = '\\'.join(b)
return a.lower() == b.lower()
FIELDS = set(z.lower() for z in gettaglist()).union(audioinfo.FILETAGS)
def parse_arg(audio, text):
if not isinstance(text, str):
return text
if text[0] == '%' and text[-1] == '%':
return to_string(audio.get(text[1:-1], ''))
elif text in FIELDS:
return to_string(audio.get(text, ''))
else:
if text[0] == '"' and text[-1] == '"':
text = text[1:-1]
return findfunc.parsefunc(text, audio)
return ""
def wrap_bool(original):
def __bool__(self):
if hasattr(self, 'args'):
self.args = [parse_arg(self.audio, z) for z in self.args]
else:
self.arg = parse_arg(self.audio, self.arg)
return original(self)
return __bool__
class BoolOperand(object):
def __init__(self, t):
self.args = t[0][0::2]
class BoolAnd(BoolOperand):
@wrap_bool
def __bool__(self):
logging.debug('and: ' + str(self.args))
for a in self.args:
if not bool(a):
return False
return True
class BoolOr(BoolOperand):
@wrap_bool
def __bool__(self):
logging.debug('or: ' + str(self.args))
for a in self.args:
if bool(a):
return True
return False
class BoolNot(BoolOperand):
def __init__(self, t):
self.arg = t[0][1]
@wrap_bool
def __bool__(self):
logging.debug('not: ' + str(self.arg))
if isinstance(self.arg, str):
arg = self.arg.lower()
for v in self.audio.values():
if isinstance(v, str):
v = [v]
v = '\\\\'.join(v).lower()
if arg in v:
return False
return True
return not bool(self.arg)
class Greater(BoolOperand):
@wrap_bool
def __bool__(self):
logging.debug('greater: ' + str(self.args))
try:
self.args = list(map(float, self.args))
except ValueError:
pass
return self.args[0] > self.args[1]
class Less(BoolOperand):
@wrap_bool
def __bool__(self):
logging.debug('less: ' + str(self.args))
try:
self.args = list(map(float, self.args))
except ValueError:
pass
return self.args[0] < self.args[1]
class Equal(BoolOperand):
@wrap_bool
def __bool__(self):
logging.debug('equal: ' + str(self.args))
return str_cmp(self.args[0], self.args[1])
class Missing(BoolOperand):
def __init__(self, t):
self.arg = t[0][1]
def __bool__(self):
logging.debug('missing: ' + str(self.arg))
if getattr(self, "audio", None):
return not (self.arg in self.audio)
return False
class Present(BoolOperand):
def __init__(self, t):
self.arg = t[0][1]
def __bool__(self):
logging.debug('present: ' + str(self.arg))
if getattr(self, "audio", None):
return (self.arg in self.audio)
return False
class BoolIs(BoolOperand):
@wrap_bool
def __bool__(self):
logging.debug('is: ' + str(self.args))
return str_cmp(self.args[0], self.args[1])
class Has(BoolOperand):
@wrap_bool
def __bool__(self):
logging.debug('has: ' + str(self.args))
return self.args[1].lower() in self.args[0].lower()
class Matches(BoolOperand):
@wrap_bool
def __bool__(self):
logging.debug('matches: ' + str(self.args))
return not re.search(self.args[1].lower(), self.args[0].lower()) is None
bool_exprs = [
(CaselessLiteral("missing"), 1, OpAssoc.RIGHT, Missing),
(CaselessLiteral("present"), 1, OpAssoc.RIGHT, Present),
(CaselessLiteral("greater"), 2, OpAssoc.LEFT, Greater),
(CaselessLiteral("less"), 2, OpAssoc.LEFT, Less),
(CaselessLiteral("equal"), 2, OpAssoc.LEFT, Equal),
(CaselessLiteral("has"), 2, OpAssoc.LEFT, Has),
(CaselessLiteral("matches"), 2, OpAssoc.LEFT, Matches),
(CaselessLiteral("is"), 2, OpAssoc.LEFT, BoolIs),
(CaselessLiteral("and"), 2, OpAssoc.LEFT, BoolAnd),
(CaselessLiteral("or"), 2, OpAssoc.LEFT, BoolOr),
(CaselessLiteral("not"), 1, OpAssoc.RIGHT, BoolNot),
]
field_expr = Combine('%' + Word(alphanums + '_') + '%')
tokens = QuotedString('"', unquote_results=False) \
| field_expr | Word(alphanums + '_')
bool_expr = infix_notation(tokens, bool_exprs)
bool_expr.enable_packrat()
def parse(audio, expr):
for i in bool_exprs:
i[3].audio = audio
try:
res = bool_expr.parse_string(expr)[0]
except ParseException as e:
res = expr
if isinstance(res, str):
res = res.lower()
for field, value in audio.items():
if isinstance(value, str):
value = [value]
elif isinstance(value, (int, float)):
value = [str(value)]
try:
logging.debug('simple filter: %s in %s', res, value)
if res in '\\\\'.join(value).lower():
return True
except TypeError as e:
continue
else:
return bool(res)
return False
if __name__ == '__main__':
audio = audioinfo.Tag('clen.mp3')
# parse(audio, "not p")
# parse(audio, 'not missing artist')
# parse(audio, '7 greater 6')
# parse(audio, '%track% greater 14')
# parse(audio, '%track% greater "$add($len(%artist%), 50)"')
# t = time.time()
# parse(audio, '(not missing artist) and (20 greater 19)')
# parse(audio, 'not (20 greater 19)')
# print time.time() - t
# parse(audio, 'not missing artist and 18 greater 19')
# parse(audio, 'artist is "Carl Douglas"')
# parse(audio, "artist has aarl")
# parse(audio, "artist has Carl")
import time
t = time.time()
print(audio.filepath)
print(parse(audio, '__filename has clen'))