def airtime_dict(d): """ Converts mutagen dictionary 'd' into airtime dictionary """ temp_dict = {} for m_key, m_val in d.iteritems(): # TODO : some files have multiple fields for the same metadata. # genre is one example. In that case mutagen will return a list # of values if isinstance(m_val, list): # TODO : does it make more sense to just skip the element in # this case? if len(m_val) == 0: assign_val = '' else: assign_val = m_val[0] else: assign_val = m_val temp_dict[ m_key ] = assign_val airtime_dictionary = {} for muta_k, muta_v in temp_dict.iteritems(): # We must check if we can actually translate the mutagen key into # an airtime key before doing the conversion if muta_k in mutagen2airtime: airtime_key = mutagen2airtime[muta_k] # Apply truncation in the case where airtime_key is in our # truncation table muta_v = \ truncate_to_length(muta_v, truncate_table[airtime_key])\ if airtime_key in truncate_table else muta_v airtime_dictionary[ airtime_key ] = muta_v return airtime_dictionary
def read_value(self, path, original, running={}): # If value is present and normalized then we only check if it's # normalized or not. We normalize if it's not normalized already if self.name in original: v = original[self.name] if self.__is_normalized(v): return v else: return self.__normalizer(v) # We slice out only the dependencies that are required for the metadata # element. dep_slice_orig = self.__slice_deps(original) dep_slice_running = self.__slice_deps(running) # TODO : remove this later dep_slice_special = self.__slice_deps({'path': path}) # We combine all required dependencies into a single dictionary # that we will pass to the translator full_deps = dict(dep_slice_orig.items() + dep_slice_running.items() + dep_slice_special.items()) # check if any dependencies are absent # note: there is no point checking the case that len(full_deps) > # len(self.__deps) because we make sure to "slice out" any supefluous # dependencies above. if len(full_deps) != len(self.dependencies()) or \ len(self.dependencies()) == 0: # If we have a default value then use that. Otherwise throw an # exception if self.has_default(): return self.get_default() else: raise MetadataAbsent(self.name) # We have all dependencies. Now for actual for parsing def def_translate(dep): def wrap(k): e = [x for x in dep][0] return k[e] return wrap # Only case where we can select a default translator if self.__translator is None: self.translate(def_translate(self.dependencies())) if len(self.dependencies()) > 2: # dependencies include themselves self.logger.info("Ignoring some dependencies in translate %s" % self.name) self.logger.info(self.dependencies()) r = self.__normalizer(self.__translator(full_deps)) if self.__max_length != -1: r = truncate_to_length(r, self.__max_length) if self.__max_value != -1: try: r = truncate_to_value(r, self.__max_value) except ValueError, e: r = ''
def read_value(self, path, original, running={}): # If value is present and normalized then we only check if it's # normalized or not. We normalize if it's not normalized already if self.name in original: v = original[self.name] if self.__is_normalized(v): return v else: return self.__normalizer(v) # We slice out only the dependencies that are required for the metadata # element. dep_slice_orig = self.__slice_deps(original) dep_slice_running = self.__slice_deps(running) # TODO : remove this later dep_slice_special = self.__slice_deps({'path' : path}) # We combine all required dependencies into a single dictionary # that we will pass to the translator full_deps = dict( dep_slice_orig.items() + dep_slice_running.items() + dep_slice_special.items()) # check if any dependencies are absent # note: there is no point checking the case that len(full_deps) > # len(self.__deps) because we make sure to "slice out" any supefluous # dependencies above. if len(full_deps) != len(self.dependencies()) or \ len(self.dependencies()) == 0: # If we have a default value then use that. Otherwise throw an # exception if self.has_default(): return self.get_default() else: raise MetadataAbsent(self.name) # We have all dependencies. Now for actual for parsing def def_translate(dep): def wrap(k): e = [ x for x in dep ][0] return k[e] return wrap # Only case where we can select a default translator if self.__translator is None: self.translate(def_translate(self.dependencies())) if len(self.dependencies()) > 2: # dependencies include themselves self.logger.info("Ignoring some dependencies in translate %s" % self.name) self.logger.info(self.dependencies()) r = self.__normalizer( self.__translator(full_deps) ) if self.__max_length != -1: r = truncate_to_length(r, self.__max_length) if self.__max_value != -1: try: r = truncate_to_value(r, self.__max_value) except ValueError, e: r = '' return r
def read_value(self, path, original, running={}): # If value is present and normalized then we don't touch it if self.name in original: v = original[self.name] if self.__is_normalized(v): return v else: return self.__normalizer(v) # A dictionary slice with all the dependencies and their values dep_slice_orig = self.__slice_deps(original) dep_slice_running = self.__slice_deps(running) full_deps = dict( dep_slice_orig.items() + dep_slice_running.items() ) # check if any dependencies are absent if len(full_deps) != len(self.__deps) or len(self.__deps) == 0: # If we have a default value then use that. Otherwise throw an # exception if self.has_default(): return self.get_default() else: raise MetadataAbsent(self.name) # We have all dependencies. Now for actual for parsing r = self.__normalizer( self.__translator(full_deps) ) if self.__max_length != -1: r = truncate_to_length(r, self.__max_length) return r
def test_truncate_to_length(self): s1 = "testing with non string literal" s2 = u"testing with unicode literal" self.assertEqual(len(mmp.truncate_to_length(s1, 5)), 5) self.assertEqual(len(mmp.truncate_to_length(s2, 8)), 8)
def test_truncate_to_length(self): s1 = "testing with non string literal" s2 = u"testing with unicode literal" self.assertEqual( len(mmp.truncate_to_length(s1, 5)), 5) self.assertEqual( len(mmp.truncate_to_length(s2, 8)), 8)