From: W. Trevor King Date: Mon, 5 Dec 2011 14:26:32 +0000 (-0500) Subject: Add `--ignore REGEXP` option to mkogg.py. X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=0c7da3ce1a9e8b361e87c54987d7dd798f58458b;p=blog.git Add `--ignore REGEXP` option to mkogg.py. I store my music as FLAC files, but some of it originally came from other file formats (e.g. purchased mp3s). These non-FLAC originials get stored in '.orig' subdirectories. Now I can run: $ mkogg.py --ignore '.*/[.]orig/.*' ... to avoid converting these originals. --- diff --git a/posts/mkogg/mkogg.py b/posts/mkogg/mkogg.py index 1c96d0a..f5a10b0 100755 --- a/posts/mkogg/mkogg.py +++ b/posts/mkogg/mkogg.py @@ -45,6 +45,7 @@ External packages required for full functionality: from hashlib import sha256 as _hash import os import os.path +import re as _re import shutil from subprocess import Popen, PIPE from tempfile import mkstemp @@ -78,13 +79,14 @@ class Converter (object): .. _suggestions: http://www.xiph.org/vorbis/doc/v-comment.html """ def __init__(self, source_dir, target_dir, target_extension='ogg', - cache_file=None): + cache_file=None, ignore_function=None): self.source_dir = source_dir self.target_dir = target_dir self._source_extensions = ['flac', 'mp3', 'ogg', 'wav'] self._target_extension = target_extension self._cache_file = cache_file self._cache = self._read_cache() + self._ignore_function = ignore_function f,self._tempfile = mkstemp(prefix='mkogg-') def cleanup(self): @@ -134,6 +136,9 @@ class Converter (object): print 'skip', filename, ext continue source_path = os.path.join(dirpath, filename) + if (self._ignore_function is not None and + self._ignore_function(source_path)): + continue rel_path = os.path.relpath(dirpath, self.source_dir) target_path = os.path.join( self.target_dir, rel_path, @@ -469,6 +474,8 @@ if __name__ == '__main__': p.add_option('-c', '--cache', dest='cache', metavar='PATH', help=('Save conversion hashes in a cache file to avoid ' 'repeated previous conversions.')) + p.add_option('-i', '--ignore', dest='ignore', metavar='REGEXP', + help=('Ignore source paths matching REGEXP.')) p.add_option('--test', dest='test', action='store_true', default=False, help='Run internal tests and exit') @@ -477,9 +484,15 @@ if __name__ == '__main__': if options.test: sys.exit(test()) + if options.ignore is not None: + ignore_regexp = _re.compile(options.ignore) + ignore_function = ignore_regexp.match + else: + ignore_function = None + source_dir,target_dir = args c = Converter(source_dir, target_dir, target_extension=options.ext, - cache_file=options.cache) + cache_file=options.cache, ignore_function=ignore_function) try: c.run() finally: