limit randomdir to audio files

+ more testing
This commit is contained in:
boyska 2017-08-08 11:09:59 +02:00
parent 165c4aeced
commit 9e5c0335fa
No known key found for this signature in database
GPG key ID: 7395DCAE58289CA9
3 changed files with 65 additions and 24 deletions

View file

@ -3,11 +3,25 @@ import logging
import shutil import shutil
import random import random
from tempfile import mkstemp from tempfile import mkstemp
from pathlib import Path
from larigira.fsutils import scan_dir, shortname from larigira.fsutils import scan_dir_audio, shortname, is_audio
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
def candidates(paths):
c = set()
for path in paths:
if not path.exists():
log.warning("Can't find requested path: %s", path)
continue
if path.is_file() and is_audio(str(path)):
c.add(str(path))
elif path.is_dir():
c.update(scan_dir_audio(str(path)))
return c
def generate(spec): def generate(spec):
''' '''
resolves audiospec-randomdir resolves audiospec-randomdir
@ -21,18 +35,12 @@ def generate(spec):
if attr not in spec: if attr not in spec:
raise ValueError("Malformed audiospec: missing '%s'" % attr) raise ValueError("Malformed audiospec: missing '%s'" % attr)
found_files = set() found_files = candidates([Path(p) for p in spec['paths']])
for path in spec['paths']:
if not os.path.exists(path):
log.warning("Can't find requested path: %s", path)
continue
if os.path.isfile(path):
found_files.add(path)
elif os.path.isdir(path):
found_files.update(scan_dir(path))
picked = random.sample(found_files, int(spec['howmany'])) picked = random.sample(found_files, int(spec['howmany']))
# TODO: use specnick
nick = spec.get('nick', spec.eid)
for path in picked: for path in picked:
tmp = mkstemp(suffix=os.path.splitext(path)[-1], tmp = mkstemp(suffix=os.path.splitext(path)[-1],
prefix='randomdir-%s-' % shortname(path)) prefix='randomdir-%s-' % shortname(path))

View file

@ -1,5 +1,6 @@
import os import os
import fnmatch import fnmatch
import mimetypes
def scan_dir(dirname, extension=None): def scan_dir(dirname, extension=None):
@ -17,10 +18,15 @@ def multi_fnmatch(fname, extensions):
return False return False
def is_audio(fname):
mimetype = mimetypes.guess_type(fname)[0]
return mimetype.split('/')[0] == 'audio'
def scan_dir_audio(dirname, extensions=('mp3', 'oga', 'wav', 'ogg')): def scan_dir_audio(dirname, extensions=('mp3', 'oga', 'wav', 'ogg')):
for root, dirnames, filenames in os.walk(dirname): for root, dirnames, filenames in os.walk(dirname):
for fname in filenames: for fname in filenames:
if multi_fnmatch(fname, extensions): if is_audio(fname):
yield os.path.join(root, fname) yield os.path.join(root, fname)

View file

@ -1,18 +1,45 @@
from gevent import monkey from pathlib import Path
monkey.patch_all(subprocess=True)
import pytest from larigira.audiogen_randomdir import candidates
from larigira.audiogen_randomdir import generate
@pytest.fixture def P(pypathlocal):
def simplerandom(): return Path(str(pypathlocal))
return {
'paths': '/tmp/do/not/exist',
}
def test_accepted_syntax(simplerandom): def test_txt_files_are_excluded(tmpdir):
'''Check the minimal needed configuration for randomdir''' p = tmpdir.join("foo.txt")
generate(simplerandom) p.write('')
assert len(candidates([P(p)])) == 0
assert len(candidates([P(tmpdir)])) == 0
def test_nested_txt_files_are_excluded(tmpdir):
p = tmpdir.mkdir('one').mkdir('two').join("foo.txt")
p.write('')
assert len(candidates([P(p)])) == 0
assert len(candidates([P(tmpdir)])) == 0
def test_mp3_files_are_considered(tmpdir):
p = tmpdir.join("foo.mp3")
p.write('')
assert len(candidates([P(p)])) == 1
assert len(candidates([P(tmpdir)])) == 1
def test_nested_mp3_files_are_considered(tmpdir):
p = tmpdir.mkdir('one').mkdir('two').join("foo.mp3")
p.write('')
assert len(candidates([P(p)])) == 1
assert len(candidates([P(tmpdir)])) == 1
def test_same_name(tmpdir):
'''file with same name on different dir should not be confused'''
p = tmpdir.mkdir('one').mkdir('two').join("foo.mp3")
p.write('')
p = tmpdir.join("foo.mp3")
p.write('')
assert len(candidates([P(tmpdir)])) == 2