mkogg.py: don't require explicit methods for conversion via wav.
authorW. Trevor King <wking@tremily.us>
Thu, 12 Jul 2012 03:40:44 +0000 (23:40 -0400)
committerW. Trevor King <wking@tremily.us>
Thu, 12 Jul 2012 03:40:44 +0000 (23:40 -0400)
Only require explit conversion methords for * -> wav and wav -> *.
With this and metadata getters/setters, conversion will work fine, and
we the number of methods will scale better with an increasing number
of file types.

You should still write a direct conversion method if you can avoid the
wav intermediate.

posts/mkogg/mkogg.py

index 7690de619d77f616f8ea1683f062b07fff31a846..174e71260825d5f8e0894e1f4753a932e9f9baf3 100755 (executable)
@@ -1,6 +1,6 @@
 #!/usr/bin/env python
 #
-# Copyright (C) 2009-2011 W. Trevor King <wking@drexel.edu>
+# Copyright (C) 2009-2012 W. Trevor King <wking@drexel.edu>
 #
 # This program is free software: you can redistribute it and/or modify
 # it under the terms of the GNU Lesser General Public License as
@@ -189,8 +189,16 @@ class Converter (object):
         if ext == self._target_extension:
             shutil.copy(source, target)
             return
-        convert = getattr(self, 'convert_{}_to_{}'.format(
-                ext, self._target_extension))
+        try:
+            convert = getattr(self, 'convert_{}_to_{}'.format(
+                    ext, self._target_extension))
+        except AttributeError:
+            to_wav = getattr(self, 'convert_{}_to_wav'.format(ext))
+            from_wav = getattr(self, 'convert_wav_to_{}'.format(
+                    self._target_extension))
+            def convert(source, target):
+                to_wav(source, self._tempfile)
+                from_wav(self._tempfile, target)
         convert(source, target)
         if not getattr(convert, 'handles_metadata', False):
             get_metadata = getattr(self, 'get_{}_metadata'.format(ext))
@@ -289,10 +297,6 @@ class Converter (object):
                 return id3_encoding
         raise ValueError(text_list)
 
-    def convert_flac_to_mp3(self, source, target):
-        self.convert_flac_to_wav(source, self._tempfile)
-        self.convert_wav_to_mp3(self._tempfile, target)
-
     def convert_flac_to_wav(self, source, target):
         invoke(['ogg123', '-d', 'wav', '-f', target, source])
 
@@ -300,24 +304,10 @@ class Converter (object):
         invoke(['oggenc', '--quiet', '-q', '3', source, '-o', target])
     convert_flac_to_ogg.handles_metadata = True
 
-    def convert_mp3_to_flac(self, source, target):
-        self.convert_mp3_to_wav(source, self._tempfile)
-        self.convert_wav_to_flac(self._tempfile, target)
-
-    def convert_mp3_to_ogg(self, source, target):
-        self.convert_mp3_to_wav(source, self._tempfile)
-        self.convert_wav_to_ogg(self._tempfile, target)
 
     def convert_mp3_to_wav(self, source, target):
         invoke(['mpg123',  '-w', target, source])
 
-    def convert_ogg_to_flac(self, source, target):
-        self.convert_ogg_to_wav(source, self._tempfile)
-        self.convert_wav_to_flac(self._tempfile, target)
-
-    def convert_ogg_to_mp3(self, source, target):
-        self.convert_flac_to_mp3(source, target)
-
     def convert_ogg_to_wav(self, source, target):
         self.convert_flac_to_wav(source, target)