From ba5a7490730dcbf9d050adba9deef1038d91e308 Mon Sep 17 00:00:00 2001 From: Martin Konecny Date: Wed, 11 Jul 2012 16:48:46 -0400 Subject: [PATCH] CC-4092: Not checking for return type "None" on some function calls in media-monitor -more fixes --- python_apps/api_clients/api_client.py | 16 +++-- .../airtimemediamonitorbootstrap.py | 22 +++++-- .../airtimefilemonitor/airtimemetadata.py | 60 ++++++++++++------- 3 files changed, 65 insertions(+), 33 deletions(-) diff --git a/python_apps/api_clients/api_client.py b/python_apps/api_clients/api_client.py index 2cf8a8c4c..97d054ed5 100644 --- a/python_apps/api_clients/api_client.py +++ b/python_apps/api_clients/api_client.py @@ -437,10 +437,10 @@ class AirTimeApiClient(ApiClientInterface): return response - #returns a list of all db files for a given directory in JSON format: - #{"files":["path/to/file1", "path/to/file2"]} - #Note that these are relative paths to the given directory. The full - #path is not returned. + # returns a list of all db files for a given directory in JSON format: + # ["path/to/file1", "path/to/file2"] + # Note that these are relative paths to the given directory. The full + # path is not returned. def list_all_db_files(self, dir_id): logger = self.logger try: @@ -454,8 +454,12 @@ class AirTimeApiClient(ApiClientInterface): except Exception, e: response = {} 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): logger = self.logger diff --git a/python_apps/media-monitor/airtimefilemonitor/airtimemediamonitorbootstrap.py b/python_apps/media-monitor/airtimefilemonitor/airtimemediamonitorbootstrap.py index 67574df63..5f0b9c5b8 100644 --- a/python_apps/media-monitor/airtimefilemonitor/airtimemediamonitorbootstrap.py +++ b/python_apps/media-monitor/airtimefilemonitor/airtimemediamonitorbootstrap.py @@ -43,8 +43,7 @@ class AirtimeMediaMonitorBootstrap(): went offline. """ 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) for id, dir in directories.iteritems(): @@ -60,12 +59,22 @@ class AirtimeMediaMonitorBootstrap(): 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") + + Return type is a dictionary similar to: + {"1":"/srv/airtime/stor/"} """ def get_list_of_watched_dirs(self): 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) @@ -91,8 +100,9 @@ class AirtimeMediaMonitorBootstrap(): db_known_files_set = set() 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) diff --git a/python_apps/media-monitor/airtimefilemonitor/airtimemetadata.py b/python_apps/media-monitor/airtimefilemonitor/airtimemetadata.py index 35b5135ab..01022034d 100644 --- a/python_apps/media-monitor/airtimefilemonitor/airtimemetadata.py +++ b/python_apps/media-monitor/airtimefilemonitor/airtimemetadata.py @@ -11,8 +11,17 @@ from api_clients import api_client """ 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: def __init__(self): @@ -56,10 +65,17 @@ class AirtimeMetadata: self.logger = logging.getLogger() def get_md5(self, filepath): - f = open(filepath, 'rb') - m = hashlib.md5() - m.update(f.read()) - md5 = m.hexdigest() + """ + Returns an md5 of the file located at filepath. Returns an empty string + if there was an error reading the file. + """ + try: + f = open(filepath, 'rb') + m = hashlib.md5() + m.update(f.read()) + md5 = m.hexdigest() + except Exception, e: + return "" return md5 @@ -120,6 +136,10 @@ class AirtimeMetadata: return item 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) @@ -129,7 +149,6 @@ class AirtimeMetadata: md['MDATA_KEY_MD5'] = md5 file_info = mutagen.File(filepath, easy=True) - except Exception, e: self.logger.error("failed getting metadata from %s", filepath) self.logger.error("Exception %s", e) @@ -138,17 +157,16 @@ class AirtimeMetadata: #check if file has any metadata if file_info is None: return None - #check if file has any metadata - if file_info is not None: - for key in file_info.keys() : - if key in self.mutagen2airtime: - val = file_info[key] - try: - 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] - except Exception, e: - self.logger.error('Exception: %s', e) - self.logger.error("traceback: %s", traceback.format_exc()) + + for key in file_info.keys() : + if key in self.mutagen2airtime: + val = file_info[key] + try: + 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] + except Exception, e: + self.logger.error('Exception: %s', e) + self.logger.error("traceback: %s", traceback.format_exc()) if 'MDATA_KEY_TITLE' not in md: #get rid of file extension from original name, name might have more than 1 '.' in it. 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_SAMPLERATE'] = getattr(file_info.info, "sample_rate", 0) - #TODO: WHATS A GOOD DEFAULT VALUE??? - md['MDATA_KEY_DURATION'] = self.format_length(file_info.info.length) + md['MDATA_KEY_DURATION'] = self.format_length(getattr(file_info.info, "length", 0.0)) - #TODO: WHATS A GOOD DEFAULT VALUE??? - md['MDATA_KEY_MIME'] = file_info.mime[0] + md['MDATA_KEY_MIME'] = "" + if len(file_info.mime) > 0: + md['MDATA_KEY_MIME'] = file_info.mime[0] except Exception as e: self.logger.warn(e)