From a887a3a3d222793119148ea91b62f8b2aad22da1 Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Mon, 31 Dec 2012 06:52:18 -0500 Subject: [PATCH] mkogg.py: Prefix imports with underscores for a clean namespace --- posts/mkogg/mkogg.py | 88 ++++++++++++++++++++++---------------------- 1 file changed, 45 insertions(+), 43 deletions(-) diff --git a/posts/mkogg/mkogg.py b/posts/mkogg/mkogg.py index a9bfdc6..581631a 100755 --- a/posts/mkogg/mkogg.py +++ b/posts/mkogg/mkogg.py @@ -44,21 +44,21 @@ External packages required for full functionality: """ from hashlib import sha256 as _hash -import os -import os.path +import os as _os import re as _re -import shutil -from subprocess import Popen, PIPE -from tempfile import mkstemp +import shutil as _shutil +import subprocess as _subprocess +import tempfile as _tempfile try: - import mutagen.flac - import mutagen.id3 - import mutagen.m4a - import mutagen.mp3 - import mutagen.oggvorbis -except ImportError, _mutagen_import_error: - mutagen = None + import mutagen as _mutagen + import mutagen.flac as _mutagen_flag + import mutagen.id3 as _mutagen_id3 + import mutagen.m4a as _mutagen_m4a + import mutagen.mp3 as _mutagen_mp3 + import mutagen.oggvorbis as _mutagen_oggvorbis +except ImportError as _mutagen_import_error: + _mutagen = None __version__ = '0.4' @@ -66,7 +66,9 @@ __version__ = '0.4' def invoke(args, stdin=None, expect=(0,)): print(' {}'.format(args)) - p = Popen(args, stdin=PIPE, stdout=PIPE, stderr=PIPE) + p = _subprocess.Popen( + args, stdin=_subprocess.PIPE, stdout=_subprocess.PIPE, + stderr=_subprocess.PIPE) stdout,stderr = p.communicate(stdin) status = p.wait() assert status in expect, 'invalid status {} from {}'.format(status, args) @@ -129,10 +131,10 @@ class Converter (object): self._cache = self._read_cache() self._hash = hash self._ignore_function = ignore_function - f,self._tempfile = mkstemp(prefix='mkogg-') + f,self._tempfile = _tempfile.mkstemp(prefix='mkogg-') def cleanup(self): - os.remove(self._tempfile) + _os.remove(self._tempfile) self._save_cache() def _read_cache(self): @@ -168,30 +170,30 @@ class Converter (object): def run(self): self._makedirs(self.target_dir) - for dirpath,dirnames,filenames in os.walk(self.source_dir): + for dirpath,dirnames,filenames in _os.walk(self.source_dir): for filename in filenames: - root,ext = os.path.splitext(filename) + root,ext = _os.path.splitext(filename) ext = ext.lower() if ext.startswith('.'): ext = ext[1:] if ext not in self._source_extensions: print('skip', filename, ext) continue - source_path = os.path.join(dirpath, filename) + 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( + rel_path = _os.path.relpath(dirpath, self.source_dir) + target_path = _os.path.join( self.target_dir, rel_path, '{}.{}'.format(root, self._target_extension)) - target_dir = os.path.dirname(target_path) + target_dir = _os.path.dirname(target_path) self._makedirs(target_dir) self._convert(source_path, target_path, ext) def _makedirs(self, target_dir): - if not os.path.exists(target_dir): - os.makedirs(target_dir) + if not _os.path.exists(target_dir): + _os.makedirs(target_dir) def _convert(self, source, target, ext): if self._hash: @@ -201,12 +203,12 @@ class Converter (object): old_cache_value == self._cache_value(target)): print('already cached {} to {}'.format(source, target)) return - elif os.path.exists(target): + elif _os.path.exists(target): print('target {} already exists'.format(target)) return print('convert {} to {}'.format(source, target)) if ext == self._target_extension: - shutil.copy(source, target) + _shutil.copy(source, target) return try: convert = getattr(self, 'convert_{}_to_{}'.format( @@ -295,7 +297,7 @@ class Converter (object): if 'tracktotal' in metadata: value = [] for i,v in enumerate(metadata['tracknumber']): - value.append(u'{}/{}'.format( + value.append('{}/{}'.format( v, metadata['tracktotal'][i])) else: value = metadata['tracknumber'] @@ -342,14 +344,14 @@ class Converter (object): self.convert_flac_to_ogg(source, target) def get_flac_metadata(self, source): - if mutagen is None: + if _mutagen is None: raise _mutagen_import_error - return mutagen.flac.FLAC(source) + return _mutagen_flac.FLAC(source) def get_m4a_metadata(self, source): - if mutagen is None: + if _mutagen is None: raise _mutagen_import_error - m4a = mutagen.m4a.M4A(source) + m4a = _mutagen_m4a.M4A(source) metadata = {} for key,value in m4a.items(): try: @@ -370,9 +372,9 @@ class Converter (object): return metadata def get_mp3_metadata(self, source): - if mutagen is None: + if _mutagen is None: raise _mutagen_import_error - mp3 = mutagen.mp3.MP3(source) + mp3 = _mutagen_mp3.MP3(source) metadata = {} for key,value in mp3.items(): try: @@ -382,34 +384,34 @@ class Converter (object): v = value.text if vorbis_key == 'tracknumber': for i,v_entry in enumerate(v): - if u'/' in v_entry: - tracknumber,tracktotal = v_entry.split(u'/', 1) + if '/' in v_entry: + tracknumber,tracktotal = v_entry.split('/', 1) v[i] = tracknumber metadata['tracktotal'] = ['tracktotal'] metadata[vorbis_key] = v return metadata def get_ogg_metadata(self, source): - if mutagen is None: + if _mutagen is None: raise _mutagen_import_error - return mutagen.oggvorbis.OggVorbis(source) + return _mutagen_oggvorbis.OggVorbis(source) def get_wav_metadata(self, source): return {} def set_flac_metadata(self, target, metadata): - if mutagen is None: + if _mutagen is None: raise _mutagen_import_error - flac = mutagen.flac.FLAC(target) + flac = _mutagen_flac.FLAC(target) self._set_vorbis_comments(flac, metadata) flac.save() def set_mp3_metadata(self, target, metadata): vorbis_keys_to_id3 = dict( (v,k) for k,v in self.id3_to_vorbis_keys.items()) - if mutagen is None: + if _mutagen is None: raise _mutagen_import_error - mp3 = mutagen.mp3.MP3(target) + mp3 = _mutagen_mp3.MP3(target) if mp3.tags is not None: mp3.tags.delete() handled_trck = False @@ -430,7 +432,7 @@ class Converter (object): frame_name = vorbis_keys_to_id3[key].upper() except KeyError: continue - frame = getattr(mutagen.id3, frame_name) + frame = getattr(_mutagen_id3, frame_name) id3_encoding = self._guess_id3_encoding(value) max_encoding = max(max_encoding, id3_encoding) mp3[frame_name] = frame(encoding=id3_encoding, text=value) @@ -443,9 +445,9 @@ class Converter (object): mp3.save(v1=v1) def set_ogg_metadata(self, target, metadata): - if mutagen is None: + if _mutagen is None: raise _mutagen_import_error - ogg = mutagen.oggvorbis.OggVorbis(target) + ogg = _mutagen_oggvorbis.OggVorbis(target) self._set_vorbis_comments(ogg, metadata) ogg.save() -- 2.26.2