From f88e5602a7c2200d6627640163459d96ef8b42fa Mon Sep 17 00:00:00 2001 From: boyska Date: Sat, 4 Feb 2017 18:54:06 +0100 Subject: [PATCH 1/2] more,better log messages for audiogens now audiogens will have their own logger and will log the "source" of what they are adding to playlist refs #36 --- larigira/audiogen_mostrecent.py | 5 ++++- larigira/audiogen_randomdir.py | 7 +++++-- larigira/audiogen_script.py | 2 +- larigira/audiogen_static.py | 6 +++++- 4 files changed, 15 insertions(+), 5 deletions(-) diff --git a/larigira/audiogen_mostrecent.py b/larigira/audiogen_mostrecent.py index 237b014..0fe0381 100644 --- a/larigira/audiogen_mostrecent.py +++ b/larigira/audiogen_mostrecent.py @@ -7,6 +7,7 @@ from tempfile import mkstemp from pytimeparse.timeparse import timeparse from larigira.fsutils import scan_dir +log = logging.getLogger(__name__) def get_mtime(fname): @@ -17,7 +18,7 @@ def recent_choose(paths, howmany, minepoch): found_files = {} for path in paths: if not os.path.exists(path): - logging.warning("Can't find requested path: %s", path) + log.warning("Can't find requested path: %s", path) continue if os.path.isfile(path): found_files[path] = get_mtime(path) @@ -66,6 +67,8 @@ def generate(spec): prefix='audiogen-randomdir-') os.close(tmp[0]) shutil.copy(path, tmp[1]) + log.info("copying %s -> %s", path, os.path.basename(tmp[1])) yield 'file://{}'.format(tmp[1]) + generate.description = 'Select most recent file inside a directory' diff --git a/larigira/audiogen_randomdir.py b/larigira/audiogen_randomdir.py index e263a4e..1c79365 100644 --- a/larigira/audiogen_randomdir.py +++ b/larigira/audiogen_randomdir.py @@ -5,6 +5,7 @@ import random from tempfile import mkstemp from larigira.fsutils import scan_dir +log = logging.getLogger(__name__) def generate(spec): @@ -23,7 +24,7 @@ def generate(spec): found_files = set() for path in spec['paths']: if not os.path.exists(path): - logging.warning("Can't find requested path: %s", path) + log.warning("Can't find requested path: %s", path) continue if os.path.isfile(path): found_files.add(path) @@ -34,9 +35,11 @@ def generate(spec): for path in picked: tmp = mkstemp(suffix=os.path.splitext(path)[-1], - prefix='audiogen-randomdir-') + prefix='randomdir-') os.close(tmp[0]) shutil.copy(path, tmp[1]) + log.info("copying %s -> %s", path, os.path.basename(tmp[1])) yield 'file://{}'.format(tmp[1]) + generate.description = 'Picks random files from a specified directory' diff --git a/larigira/audiogen_script.py b/larigira/audiogen_script.py index fdb9dc1..8e078dd 100644 --- a/larigira/audiogen_script.py +++ b/larigira/audiogen_script.py @@ -20,7 +20,7 @@ import os import subprocess from .config import get_conf -log = logging.getLogger('audioscript') +log = logging.getLogger(__name__) def generate(spec): diff --git a/larigira/audiogen_static.py b/larigira/audiogen_static.py index a495ac4..8bf88c1 100644 --- a/larigira/audiogen_static.py +++ b/larigira/audiogen_static.py @@ -2,6 +2,7 @@ import os import logging import shutil from tempfile import mkstemp +log = logging.getLogger(__name__) def generate(spec): @@ -15,11 +16,14 @@ def generate(spec): for path in spec['paths']: if not os.path.exists(path): - logging.warning("Can't find requested path: %s", path) + log.warning("Can't find requested path: %s", path) continue tmp = mkstemp(suffix=os.path.splitext(path)[-1], prefix='audiogen-static-') os.close(tmp[0]) + log.info("copying %s -> %s", path, os.path.basename(tmp[1])) shutil.copy(path, tmp[1]) yield 'file://{}'.format(tmp[1]) + + generate.description = 'Picks always the same specified file' From 0d13262229bf3c77358fb13488da22452c424703 Mon Sep 17 00:00:00 2001 From: boyska Date: Sat, 4 Feb 2017 19:06:10 +0100 Subject: [PATCH 2/2] audiogens have filename hint in temp file refs #36 --- larigira/audiogen_mostrecent.py | 4 ++-- larigira/audiogen_randomdir.py | 4 ++-- larigira/audiogen_static.py | 4 +++- larigira/fsutils.py | 7 +++++++ larigira/tests/test_fsutils.py | 16 ++++++++++++++++ 5 files changed, 30 insertions(+), 5 deletions(-) create mode 100644 larigira/tests/test_fsutils.py diff --git a/larigira/audiogen_mostrecent.py b/larigira/audiogen_mostrecent.py index 0fe0381..e35dc75 100644 --- a/larigira/audiogen_mostrecent.py +++ b/larigira/audiogen_mostrecent.py @@ -6,7 +6,7 @@ from tempfile import mkstemp from pytimeparse.timeparse import timeparse -from larigira.fsutils import scan_dir +from larigira.fsutils import scan_dir, shortname log = logging.getLogger(__name__) @@ -64,7 +64,7 @@ def generate(spec): for path in picked: tmp = mkstemp(suffix=os.path.splitext(path)[-1], - prefix='audiogen-randomdir-') + prefix='randomdir-%s-' % shortname(path)) os.close(tmp[0]) shutil.copy(path, tmp[1]) log.info("copying %s -> %s", path, os.path.basename(tmp[1])) diff --git a/larigira/audiogen_randomdir.py b/larigira/audiogen_randomdir.py index 1c79365..2453210 100644 --- a/larigira/audiogen_randomdir.py +++ b/larigira/audiogen_randomdir.py @@ -4,7 +4,7 @@ import shutil import random from tempfile import mkstemp -from larigira.fsutils import scan_dir +from larigira.fsutils import scan_dir, shortname log = logging.getLogger(__name__) @@ -35,7 +35,7 @@ def generate(spec): for path in picked: tmp = mkstemp(suffix=os.path.splitext(path)[-1], - prefix='randomdir-') + prefix='randomdir-%s-' % shortname(path)) os.close(tmp[0]) shutil.copy(path, tmp[1]) log.info("copying %s -> %s", path, os.path.basename(tmp[1])) diff --git a/larigira/audiogen_static.py b/larigira/audiogen_static.py index 8bf88c1..8d801ff 100644 --- a/larigira/audiogen_static.py +++ b/larigira/audiogen_static.py @@ -2,6 +2,8 @@ import os import logging import shutil from tempfile import mkstemp + +from larigira.fsutils import shortname log = logging.getLogger(__name__) @@ -19,7 +21,7 @@ def generate(spec): log.warning("Can't find requested path: %s", path) continue tmp = mkstemp(suffix=os.path.splitext(path)[-1], - prefix='audiogen-static-') + prefix='static-%s-' % shortname(path)) os.close(tmp[0]) log.info("copying %s -> %s", path, os.path.basename(tmp[1])) shutil.copy(path, tmp[1]) diff --git a/larigira/fsutils.py b/larigira/fsutils.py index 0b38c6f..6501e21 100644 --- a/larigira/fsutils.py +++ b/larigira/fsutils.py @@ -22,3 +22,10 @@ def scan_dir_audio(dirname, extensions=('mp3', 'oga', 'wav', 'ogg')): for fname in filenames: if multi_fnmatch(fname, extensions): yield os.path.join(root, fname) + + +def shortname(path): + name = os.path.basename(path) # filename + name = name.rsplit('.', 1)[0] # no extension + name = ''.join(c for c in name if c.isalnum()) # no strange chars + return name diff --git a/larigira/tests/test_fsutils.py b/larigira/tests/test_fsutils.py new file mode 100644 index 0000000..4a5d8bd --- /dev/null +++ b/larigira/tests/test_fsutils.py @@ -0,0 +1,16 @@ +from larigira.fsutils import shortname + + +def test_shortname_self(): + '''sometimes, shortname is just filename without extension''' + assert shortname('/tmp/asd/foo.bar') == 'foo' + + +def test_shortname_has_numbers(): + '''shortname will preserve numbers''' + assert shortname('/tmp/asd/foo1.bar') == 'foo1' + + +def test_shortname_has_no_hyphen(): + '''shortname will not preserve hyphens''' + assert shortname('/tmp/asd/foo-1.bar') == 'foo1'