Refactored code

This commit is contained in:
Rudi Grinberg 2012-10-16 14:48:16 -04:00
parent 7255241e6c
commit 0638d21473
2 changed files with 32 additions and 1 deletions

View File

@ -2,9 +2,28 @@
from contextlib import contextmanager
from media.monitor.pure import truncate_to_length, toposort
from os.path import normpath
from media.monitor.exceptions import BadSongFile
from media.monitor.log import Loggable
import media.monitor.pure as mmp
from collections import namedtuple
import mutagen
class FakeMutagen(dict):
"""
Need this fake mutagen object so that airtime_special functions
return a proper default value instead of throwing an exceptions for
files that mutagen doesn't recognize
"""
FakeInfo = namedtuple('FakeInfo','length bitrate')
def __init__(self,path):
self.path = path
self.mime = ['audio/wav']
self.info = FakeMutagen.FakeInfo(0.0, '')
dict.__init__(self)
def set_length(self,l):
old_bitrate = self.info.bitrate
self.info = FakeMutagen.FakeInfo(l, old_bitrate)
class MetadataAbsent(Exception):
def __init__(self, name): self.name = name
@ -129,7 +148,14 @@ def normalize_mutagen(path):
Consumes a path and reads the metadata using mutagen. normalizes some of
the metadata that isn't read through the mutagen hash
"""
m = mutagen.File(path, easy=True)
if not mmp.file_playable(path): raise BadSongFile(path)
try : m = mutagen.File(path, easy=True)
except Exception : raise BadSongFile(path)
if m is None: m = FakeMutagen(path)
try:
if mmp.extension(path) == 'wav':
m.set_length(mmp.read_wave_duration(path))
except Exception: raise BadSongFile(path)
md = {}
for k,v in m.iteritems():
if type(v) is list:

View File

@ -48,6 +48,8 @@ airtime2mutagen = {
"MDATA_KEY_COPYRIGHT" : "copyright",
}
# TODO :Remove FakeMutagen class. Moved to media.metadata.process
class FakeMutagen(dict):
"""
Need this fake mutagen object so that airtime_special functions
@ -172,6 +174,9 @@ class Metadata(Loggable):
for e in exceptions: raise e
def __init__(self, fpath):
# Forcing the unicode through
try : fpath = fpath.decode("utf-8")
except : pass
self.__metadata = global_reader.read_mutagen(fpath)
def __init__2(self, fpath):