CC-4092: Not checking for return type "None" on some function calls in media-monitor

-more fixes
This commit is contained in:
Martin Konecny 2012-07-11 16:48:46 -04:00
parent bed2a0731b
commit ba5a749073
3 changed files with 65 additions and 33 deletions

View file

@ -437,10 +437,10 @@ class AirTimeApiClient(ApiClientInterface):
return response return response
#returns a list of all db files for a given directory in JSON format: # returns a list of all db files for a given directory in JSON format:
#{"files":["path/to/file1", "path/to/file2"]} # ["path/to/file1", "path/to/file2"]
#Note that these are relative paths to the given directory. The full # Note that these are relative paths to the given directory. The full
#path is not returned. # path is not returned.
def list_all_db_files(self, dir_id): def list_all_db_files(self, dir_id):
logger = self.logger logger = self.logger
try: try:
@ -454,8 +454,12 @@ class AirTimeApiClient(ApiClientInterface):
except Exception, e: except Exception, e:
response = {} response = {}
logger.error("Exception: %s", e) logger.error("Exception: %s", e)
return response try:
return getattr(response, "files")
except AttributeError:
self.logger.error("Could not find index 'files' in dictionary: %s", str(response))
return []
def list_all_watched_dirs(self): def list_all_watched_dirs(self):
logger = self.logger logger = self.logger

View file

@ -43,8 +43,7 @@ class AirtimeMediaMonitorBootstrap():
went offline. went offline.
""" """
def scan(self): def scan(self):
directories = self.get_list_of_watched_dirs(); directories = self.get_list_of_watched_dirs()
self.logger.info("watched directories found: %s", directories) self.logger.info("watched directories found: %s", directories)
for id, dir in directories.iteritems(): for id, dir in directories.iteritems():
@ -60,12 +59,22 @@ class AirtimeMediaMonitorBootstrap():
return self.api_client.list_all_db_files(dir_id) return self.api_client.list_all_db_files(dir_id)
""" """
returns the path and the database row id for this path for all watched directories. Also returns the path and its corresponding database row idfor all watched directories. Also
returns the Stor directory, which can be identified by its row id (always has value of "1") returns the Stor directory, which can be identified by its row id (always has value of "1")
Return type is a dictionary similar to:
{"1":"/srv/airtime/stor/"}
""" """
def get_list_of_watched_dirs(self): def get_list_of_watched_dirs(self):
json = self.api_client.list_all_watched_dirs() json = self.api_client.list_all_watched_dirs()
return json["dirs"]
try:
dirs = getattr(json, "dirs")
except AttributeError:
self.logger.error("Could not find index 'dirs' in dictionary: %s", str(dirs))
return {}
return dirs
""" """
This function takes in a path name provided by the database (and its corresponding row id) This function takes in a path name provided by the database (and its corresponding row id)
@ -91,8 +100,9 @@ class AirtimeMediaMonitorBootstrap():
db_known_files_set = set() db_known_files_set = set()
files = self.list_db_files(dir_id) files = self.list_db_files(dir_id)
for file in files['files']:
db_known_files_set.add(file) for f in files:
db_known_files_set.add(f)
all_files = self.mmc.scan_dir_for_new_files(dir) all_files = self.mmc.scan_dir_for_new_files(dir)

View file

@ -11,8 +11,17 @@ from api_clients import api_client
""" """
list of supported easy tags in mutagen version 1.20 list of supported easy tags in mutagen version 1.20
['albumartistsort', 'musicbrainz_albumstatus', 'lyricist', 'releasecountry', 'date', 'performer', 'musicbrainz_albumartistid', 'composer', 'encodedby', 'tracknumber', 'musicbrainz_albumid', 'album', 'asin', 'musicbrainz_artistid', 'mood', 'copyright', 'author', 'media', 'length', 'version', 'artistsort', 'titlesort', 'discsubtitle', 'website', 'musicip_fingerprint', 'conductor', 'compilation', 'barcode', 'performer:*', 'composersort', 'musicbrainz_discid', 'musicbrainz_albumtype', 'genre', 'isrc', 'discnumber', 'musicbrainz_trmid', 'replaygain_*_gain', 'musicip_puid', 'artist', 'title', 'bpm', 'musicbrainz_trackid', 'arranger', 'albumsort', 'replaygain_*_peak', 'organization'] ['albumartistsort', 'musicbrainz_albumstatus', 'lyricist', 'releasecountry',
'date', 'performer', 'musicbrainz_albumartistid', 'composer', 'encodedby',
'tracknumber', 'musicbrainz_albumid', 'album', 'asin', 'musicbrainz_artistid',
'mood', 'copyright', 'author', 'media', 'length', 'version', 'artistsort',
'titlesort', 'discsubtitle', 'website', 'musicip_fingerprint', 'conductor',
'compilation', 'barcode', 'performer:*', 'composersort', 'musicbrainz_discid',
'musicbrainz_albumtype', 'genre', 'isrc', 'discnumber', 'musicbrainz_trmid',
'replaygain_*_gain', 'musicip_puid', 'artist', 'title', 'bpm', 'musicbrainz_trackid',
'arranger', 'albumsort', 'replaygain_*_peak', 'organization']
""" """
class AirtimeMetadata: class AirtimeMetadata:
def __init__(self): def __init__(self):
@ -56,10 +65,17 @@ class AirtimeMetadata:
self.logger = logging.getLogger() self.logger = logging.getLogger()
def get_md5(self, filepath): def get_md5(self, filepath):
f = open(filepath, 'rb') """
m = hashlib.md5() Returns an md5 of the file located at filepath. Returns an empty string
m.update(f.read()) if there was an error reading the file.
md5 = m.hexdigest() """
try:
f = open(filepath, 'rb')
m = hashlib.md5()
m.update(f.read())
md5 = m.hexdigest()
except Exception, e:
return ""
return md5 return md5
@ -120,6 +136,10 @@ class AirtimeMetadata:
return item return item
def get_md_from_file(self, filepath): def get_md_from_file(self, filepath):
"""
Returns None if error retrieving metadata. Otherwise returns a dictionary
representing the file's metadata
"""
self.logger.info("getting info from filepath %s", filepath) self.logger.info("getting info from filepath %s", filepath)
@ -129,7 +149,6 @@ class AirtimeMetadata:
md['MDATA_KEY_MD5'] = md5 md['MDATA_KEY_MD5'] = md5
file_info = mutagen.File(filepath, easy=True) file_info = mutagen.File(filepath, easy=True)
except Exception, e: except Exception, e:
self.logger.error("failed getting metadata from %s", filepath) self.logger.error("failed getting metadata from %s", filepath)
self.logger.error("Exception %s", e) self.logger.error("Exception %s", e)
@ -138,17 +157,16 @@ class AirtimeMetadata:
#check if file has any metadata #check if file has any metadata
if file_info is None: if file_info is None:
return None return None
#check if file has any metadata
if file_info is not None: for key in file_info.keys() :
for key in file_info.keys() : if key in self.mutagen2airtime:
if key in self.mutagen2airtime: val = file_info[key]
val = file_info[key] try:
try: if val is not None and len(val) > 0 and val[0] is not None and len(val[0]) > 0:
if val is not None and len(val) > 0 and val[0] is not None and len(val[0]) > 0: md[self.mutagen2airtime[key]] = val[0]
md[self.mutagen2airtime[key]] = val[0] except Exception, e:
except Exception, e: self.logger.error('Exception: %s', e)
self.logger.error('Exception: %s', e) self.logger.error("traceback: %s", traceback.format_exc())
self.logger.error("traceback: %s", traceback.format_exc())
if 'MDATA_KEY_TITLE' not in md: if 'MDATA_KEY_TITLE' not in md:
#get rid of file extension from original name, name might have more than 1 '.' in it. #get rid of file extension from original name, name might have more than 1 '.' in it.
original_name = os.path.basename(filepath) original_name = os.path.basename(filepath)
@ -225,11 +243,11 @@ class AirtimeMetadata:
md['MDATA_KEY_BITRATE'] = getattr(file_info.info, "bitrate", 0) md['MDATA_KEY_BITRATE'] = getattr(file_info.info, "bitrate", 0)
md['MDATA_KEY_SAMPLERATE'] = getattr(file_info.info, "sample_rate", 0) md['MDATA_KEY_SAMPLERATE'] = getattr(file_info.info, "sample_rate", 0)
#TODO: WHATS A GOOD DEFAULT VALUE??? md['MDATA_KEY_DURATION'] = self.format_length(getattr(file_info.info, "length", 0.0))
md['MDATA_KEY_DURATION'] = self.format_length(file_info.info.length)
#TODO: WHATS A GOOD DEFAULT VALUE??? md['MDATA_KEY_MIME'] = ""
md['MDATA_KEY_MIME'] = file_info.mime[0] if len(file_info.mime) > 0:
md['MDATA_KEY_MIME'] = file_info.mime[0]
except Exception as e: except Exception as e:
self.logger.warn(e) self.logger.warn(e)