first commit
This commit is contained in:
commit
c6ba0352f7
7 changed files with 202 additions and 0 deletions
4
.gitignore
vendored
Normal file
4
.gitignore
vendored
Normal file
|
@ -0,0 +1,4 @@
|
|||
.cache
|
||||
.eggs
|
||||
*.egg-info
|
||||
*.pyc
|
4
README.rst
Normal file
4
README.rst
Normal file
|
@ -0,0 +1,4 @@
|
|||
EventFilters for larigira
|
||||
===========================
|
||||
|
||||
wtf
|
0
larigira/__init__.py
Normal file
0
larigira/__init__.py
Normal file
0
larigira/filters/__init__.py
Normal file
0
larigira/filters/__init__.py
Normal file
41
larigira/filters/basic.py
Normal file
41
larigira/filters/basic.py
Normal file
|
@ -0,0 +1,41 @@
|
|||
import os.path
|
||||
|
||||
import mutagen
|
||||
|
||||
|
||||
def maxwait(songs, context, conf):
|
||||
wait = int(conf.get('EF_MAXWAIT_SEC', 0))
|
||||
if wait == 0:
|
||||
return True
|
||||
print(context)
|
||||
curpos, duration = map(int, context['status']['time'].split(':'))
|
||||
remaining = duration - curpos
|
||||
if remaining > wait:
|
||||
return False, 'remaining %d max allowed %d' % (remaining, wait)
|
||||
return True
|
||||
|
||||
|
||||
def get_duration(path):
|
||||
'''get track duration in seconds'''
|
||||
audio = mutagen.File(path)
|
||||
return int(audio.info.length)
|
||||
|
||||
|
||||
def percentwait(songs, context, conf, getdur=get_duration):
|
||||
percentwait = int(conf.get('EF_MAXWAIT_PERC', 0))
|
||||
if percentwait == 0:
|
||||
return True
|
||||
print(context)
|
||||
curpos, duration = map(int, context['status']['time'].split(':'))
|
||||
remaining = duration - curpos
|
||||
eventduration = 0
|
||||
for uri in songs['uris']:
|
||||
if not uri.startswith('file://'):
|
||||
return True, '%s is not a file' % uri
|
||||
path = uri[len('file://'):] # strips file://
|
||||
eventduration += getdur(path)
|
||||
|
||||
wait = eventduration * (percentwait/100.)
|
||||
if remaining > wait:
|
||||
return False, 'remaining %d max allowed %d' % (remaining, wait)
|
||||
return True
|
98
larigira/filters/tests/test_basic.py
Normal file
98
larigira/filters/tests/test_basic.py
Normal file
|
@ -0,0 +1,98 @@
|
|||
from larigira.filters.basic import maxwait, percentwait
|
||||
|
||||
|
||||
def matchval(d):
|
||||
def mocked(input_):
|
||||
if input_ in d:
|
||||
return d[input_]
|
||||
for k in d:
|
||||
if k in input_: # string matching
|
||||
return d[k]
|
||||
raise Exception("This test case is bugged! No value for %s" % input_)
|
||||
return mocked
|
||||
|
||||
|
||||
durations = dict(one=60, two=120, three=180, four=240, ten=600, twenty=1200,
|
||||
thirty=1800, nonexist=None)
|
||||
dur = matchval(durations)
|
||||
|
||||
|
||||
def normalize_ret(ret):
|
||||
if type(ret) is bool:
|
||||
return ret, ''
|
||||
return ret
|
||||
|
||||
|
||||
def mw(*args, **kwargs):
|
||||
return normalize_ret(maxwait(*args, **kwargs))
|
||||
|
||||
|
||||
def pw(*args, **kwargs):
|
||||
kwargs['getdur'] = dur
|
||||
return normalize_ret(percentwait(*args, **kwargs))
|
||||
|
||||
|
||||
def test_maxwait_nonpresent_disabled():
|
||||
ret = mw([], {}, {})
|
||||
assert ret[0] is True
|
||||
|
||||
|
||||
def test_maxwait_explicitly_disabled():
|
||||
ret = mw([], {}, {'EF_MAXWAIT_SEC': 0})
|
||||
assert ret[0] is True
|
||||
|
||||
|
||||
def test_maxwait_ok():
|
||||
ret = mw([], {'status': {'time': '250:300'}}, {'EF_MAXWAIT_SEC': 100})
|
||||
assert ret[0] is True
|
||||
|
||||
|
||||
def test_maxwait_exceeded():
|
||||
ret = mw([], {'status': {'time': '100:300'}}, {'EF_MAXWAIT_SEC': 100})
|
||||
assert ret[0] is False
|
||||
|
||||
|
||||
def test_maxwait_limit():
|
||||
ret = mw([], {'status': {'time': '199:300'}}, {'EF_MAXWAIT_SEC': 100})
|
||||
assert ret[0] is False
|
||||
ret = mw([], {'status': {'time': '200:300'}}, {'EF_MAXWAIT_SEC': 100})
|
||||
assert ret[0] is True
|
||||
ret = mw([], {'status': {'time': '201:300'}}, {'EF_MAXWAIT_SEC': 100})
|
||||
assert ret[0] is True
|
||||
|
||||
|
||||
def test_percentwait_nonpresent_disabled():
|
||||
ret = pw([], {}, {})
|
||||
assert ret[0] is True
|
||||
|
||||
|
||||
def test_percentwait_explicitly_disabled():
|
||||
ret = pw([], {}, {'EF_MAXWAIT_PERC': 0})
|
||||
assert ret[0] is True
|
||||
|
||||
|
||||
def test_percentwait_ok():
|
||||
# less than one minute missing
|
||||
ret = pw(dict(uris=['file:///oneminute.ogg']),
|
||||
{'status': {'time': '250:300'}},
|
||||
{'EF_MAXWAIT_PERC': 100})
|
||||
assert ret[0] is True
|
||||
|
||||
# more than one minute missing
|
||||
ret = pw(dict(uris=['file:///oneminute.ogg']),
|
||||
{'status': {'time': '220:300'}},
|
||||
{'EF_MAXWAIT_PERC': 100})
|
||||
assert ret[0] is False
|
||||
|
||||
|
||||
def test_percentwait_morethan100():
|
||||
# requiring 5*10 = 50mins = 3000sec
|
||||
ret = pw(dict(uris=['file:///tenminute.ogg']),
|
||||
{'status': {'time': '4800:6000'}},
|
||||
{'EF_MAXWAIT_PERC': 500})
|
||||
assert ret[0] is True
|
||||
|
||||
ret = pw(dict(uris=['file:///oneminute.ogg']),
|
||||
{'status': {'time': '2000:6000'}},
|
||||
{'EF_MAXWAIT_PERC': 500})
|
||||
assert ret[0] is False
|
55
setup.py
Normal file
55
setup.py
Normal file
|
@ -0,0 +1,55 @@
|
|||
import sys
|
||||
import os
|
||||
|
||||
from setuptools import setup
|
||||
from setuptools.command.test import test as TestCommand
|
||||
|
||||
|
||||
def read(fname):
|
||||
with open(os.path.join(os.path.dirname(__file__), fname)) as buf:
|
||||
return buf.read()
|
||||
|
||||
|
||||
class PyTest(TestCommand):
|
||||
user_options = [('pytest-args=', 'a', "Arguments to pass to py.test")]
|
||||
|
||||
def initialize_options(self):
|
||||
TestCommand.initialize_options(self)
|
||||
self.pytest_args = []
|
||||
|
||||
def finalize_options(self):
|
||||
TestCommand.finalize_options(self)
|
||||
self.test_args = []
|
||||
self.test_suite = True
|
||||
|
||||
def run_tests(self):
|
||||
# import here, cause outside the eggs aren't loaded
|
||||
import pytest
|
||||
errno = pytest.main(self.pytest_args)
|
||||
sys.exit(errno)
|
||||
|
||||
|
||||
setup(name='larigira-filters',
|
||||
version='1.1.0',
|
||||
description='some filters for larigira',
|
||||
long_description=read('README.rst'),
|
||||
author='boyska',
|
||||
author_email='piuttosto@logorroici.org',
|
||||
license='AGPL',
|
||||
packages=['larigira.filters'],
|
||||
install_requires=['mutagen'],
|
||||
tests_require=['pytest-timeout==1.0', 'py>=1.4.29', 'pytest==3.0', ],
|
||||
cmdclass={'test': PyTest},
|
||||
zip_safe=False,
|
||||
include_package_data=True,
|
||||
entry_points={
|
||||
'larigira.eventfilter': [
|
||||
'maxwait = larigira.filters.base:maxwait',
|
||||
'percentwait = larigira.filters.base:percentwait',
|
||||
],
|
||||
},
|
||||
classifiers=[
|
||||
"License :: OSI Approved :: GNU Affero General Public License v3",
|
||||
"Programming Language :: Python :: 3",
|
||||
]
|
||||
)
|
Loading…
Reference in a new issue