diff --git a/python_apps/media-monitor2/media/monitor/organizer.py b/python_apps/media-monitor2/media/monitor/organizer.py index 011383c39..e5d0247de 100644 --- a/python_apps/media-monitor2/media/monitor/organizer.py +++ b/python_apps/media-monitor2/media/monitor/organizer.py @@ -18,8 +18,8 @@ class Organizer(ReportHandler,Loggable): _instance = None def __new__(cls, channel, target_path, recorded_path): if cls._instance: - cls._instance.channel = channel - cls._instance.target_path = target_path + cls._instance.channel = channel + cls._instance.target_path = target_path cls._instance.recorded_path = recorded_path else: cls._instance = super(Organizer, cls).__new__( cls, channel, @@ -27,8 +27,8 @@ class Organizer(ReportHandler,Loggable): return cls._instance def __init__(self, channel, target_path, recorded_path): - self.channel = channel - self.target_path = target_path + self.channel = channel + self.target_path = target_path self.recorded_path = recorded_path super(Organizer, self).__init__(signal=self.channel, weak=False) diff --git a/python_apps/media-monitor2/media/monitor/pure.py b/python_apps/media-monitor2/media/monitor/pure.py index 77756ce87..7160f18c0 100644 --- a/python_apps/media-monitor2/media/monitor/pure.py +++ b/python_apps/media-monitor2/media/monitor/pure.py @@ -166,15 +166,23 @@ def apply_rules_dict(d, rules): if k in d: new_d[k] = rule(d[k]) return new_d +def default_to_f(dictionary, keys, default, condition): + new_d = copy.deepcopy(dictionary) + for k in keys: + if condition(dictionary=new_d, key=k): new_d[k] = default + return new_d + def default_to(dictionary, keys, default): """ Checks if the list of keys 'keys' exists in 'dictionary'. If not then it returns a new dictionary with all those missing keys defaults to 'default' """ - new_d = copy.deepcopy(dictionary) - for k in keys: - if not (k in new_d): new_d[k] = default - return new_d + cnd = lambda dictionary, key: key not in dictionary + return default_to_f(dictionary, keys, default, cnd) + #new_d = copy.deepcopy(dictionary) + #for k in keys: + #if not (k in new_d): new_d[k] = default + #return new_d def remove_whitespace(dictionary): """ @@ -234,8 +242,9 @@ def normalized_metadata(md, original_path): new_md = apply_rules_dict(new_md, format_rules) new_md = default_to(dictionary=new_md, keys=['MDATA_KEY_TITLE'], default=no_extension_basename(original_path)) + new_md = remove_whitespace(new_md) new_md = default_to(dictionary=new_md, keys=path_md, - default=unicode_unknown) + default=u'') new_md = default_to(dictionary=new_md, keys=['MDATA_KEY_FTYPE'], default=u'audioclip') # In the case where the creator is 'Airtime Show Recorder' we would like to @@ -252,13 +261,13 @@ def normalized_metadata(md, original_path): # be set to the original path of the file for airtime recorded shows # (before it was "organized"). We will skip this procedure for now # because it's not clear why it was done - return remove_whitespace(new_md) + return new_md -def organized_path(old_path, root_path, normal_md): +def organized_path(old_path, root_path, orig_md): """ old_path - path where file is store at the moment <= maybe not necessary? root_path - the parent directory where all organized files go - normal_md - original meta data of the file as given by mutagen AFTER being + orig_md - original meta data of the file as given by mutagen AFTER being normalized return value: new file path """ @@ -266,6 +275,8 @@ def organized_path(old_path, root_path, normal_md): ext = extension(old_path) # The blocks for each if statement look awfully similar. Perhaps there is a # way to simplify this code + normal_md = default_to_f(orig_md, path_md, unicode_unknown, + lambda dictionary, key: len(dictionary[key]) == 0) if is_airtime_recorded(normal_md): fname = u'%s-%s-%s.%s' % ( normal_md['MDATA_KEY_YEAR'], normal_md['MDATA_KEY_TITLE'], @@ -322,8 +333,7 @@ def get_system_locale(locale_path='/etc/default/locale'): try: config = ConfigObj(locale_path) return config - except Exception as e: - raise FailedToSetLocale(locale_path,cause=e) + except Exception as e: raise FailedToSetLocale(locale_path,cause=e) else: raise ValueError("locale path '%s' does not exist. \ permissions issue?" % locale_path) @@ -337,8 +347,7 @@ def configure_locale(config): if default_locale[1] is None: lang = config.get('LANG') new_locale = lang - else: - new_locale = default_locale + else: new_locale = default_locale locale.setlocale(locale.LC_ALL, new_locale) reload(sys) sys.setdefaultencoding("UTF-8") @@ -395,7 +404,7 @@ def sub_path(directory,f): the paths. """ normalized = normpath(directory) - common = os.path.commonprefix([ directory, normpath(f) ]) + common = os.path.commonprefix([ normalized, normpath(f) ]) return common == normalized if __name__ == '__main__': diff --git a/python_apps/media-monitor2/tests/test_pure.py b/python_apps/media-monitor2/tests/test_pure.py index 02e753c8d..c3b08f874 100644 --- a/python_apps/media-monitor2/tests/test_pure.py +++ b/python_apps/media-monitor2/tests/test_pure.py @@ -60,4 +60,11 @@ class TestMMP(unittest.TestCase): self.assertRaises( ValueError, lambda : mmp.file_md5('/bull/shit/path') ) self.assertTrue( m1 == mmp.file_md5(p) ) + def test_sub_path(self): + f1 = "/home/testing/123.mp3" + d1 = "/home/testing" + d2 = "/home/testing/" + self.assertTrue( mmp.sub_path(d1, f1) ) + self.assertTrue( mmp.sub_path(d2, f1) ) + if __name__ == '__main__': unittest.main()