Kaynağa Gözat

Merge branch 'ftr36-audiogen-filenames'

fixes #36
more information on what audiogens have produced
boyska 7 yıl önce
ebeveyn
işleme
e5a0352417

+ 6 - 3
larigira/audiogen_mostrecent.py

@@ -6,7 +6,8 @@ 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__)
 
 
 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)
@@ -63,9 +64,11 @@ 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]))
         yield 'file://{}'.format(tmp[1])
 
+
 generate.description = 'Select most recent file inside a directory'

+ 6 - 3
larigira/audiogen_randomdir.py

@@ -4,7 +4,8 @@ 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__)
 
 
 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-%s-' % shortname(path))
         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'

+ 1 - 1
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):

+ 8 - 2
larigira/audiogen_static.py

@@ -3,6 +3,9 @@ import logging
 import shutil
 from tempfile import mkstemp
 
+from larigira.fsutils import shortname
+log = logging.getLogger(__name__)
+
 
 def generate(spec):
     '''
@@ -15,11 +18,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-')
+                      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])
         yield 'file://{}'.format(tmp[1])
+
+
 generate.description = 'Picks always the same specified file'

+ 7 - 0
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

+ 16 - 0
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'