def __cmp__(self, other): """ Compares 2 items based on their appearance order Arguments: other : MediaItem - The item to compare to Returns: * -1 : If the item is lower than the current one * 0 : If the item is order is equal * 1 : If the item is higher than the current one The comparison is done base on: * the type of the item. Non-playable items appear first. * the defined sorting algorithm. This is a Add-on setting retrieved using the AddonSettings method. Options are: Name or Timestamp. """ if other is None: return -1 if self.type == other.type: # Logger.Debug("Comparing :: same types") sortMethod = AddonSettings.GetSortAlgorithm() # date sorting if sortMethod == "date": # Logger.Debug("Comparing :: Settings: Sorting by date") # at this point both have timestamps or dates, so we can compare if self.__timestamp == other.__timestamp: # same timestamps, compare names return cmp(self.name, other.name) else: # compare timestamps return cmp(other.__timestamp, self.__timestamp) # name sorting elif sortMethod == "name": # Logger.Debug("Comparing :: Settings: Sorting by name") return cmp(self.name, other.name) else: return 0 else: # one is folder other one is playable. Folders are always sorted first # Logger.Debug("Comparing :: different types, none playable first") if self.IsPlayable(): return -1 else: return 1
def __AddSortMethodToHandle(self, handle, items=None): """ Add a sort method to the plugin output. It takes the Add-On settings into account. But if none of the items have a date, it is forced to sort by name. Arguments: handle : int - The handle to add the sortmethod to items : MediaItems - The items that need to be sorted """ sortAlgorthim = AddonSettings.GetSortAlgorithm() if sortAlgorthim == "date": # if we had a list, check it for dates. Else assume that there are no dates! if items is not None: hasDates = len(filter(lambda i: i.HasDate(), items)) > 0 else: hasDates = True if hasDates: Logger.Debug("Sorting method default: Dates") xbmcplugin.addSortMethod(handle=handle, sortMethod=xbmcplugin.SORT_METHOD_DATE) xbmcplugin.addSortMethod(handle=handle, sortMethod=xbmcplugin.SORT_METHOD_LABEL) xbmcplugin.addSortMethod(handle=handle, sortMethod=xbmcplugin.SORT_METHOD_UNSORTED) else: Logger.Debug("Sorting method default: Dates, but no dates are available, sorting by name") xbmcplugin.addSortMethod(handle=handle, sortMethod=xbmcplugin.SORT_METHOD_LABEL) xbmcplugin.addSortMethod(handle=handle, sortMethod=xbmcplugin.SORT_METHOD_DATE) xbmcplugin.addSortMethod(handle=handle, sortMethod=xbmcplugin.SORT_METHOD_UNSORTED) elif sortAlgorthim == "name": Logger.Debug("Sorting method default: Names") xbmcplugin.addSortMethod(handle=handle, sortMethod=xbmcplugin.SORT_METHOD_LABEL) xbmcplugin.addSortMethod(handle=handle, sortMethod=xbmcplugin.SORT_METHOD_DATE) xbmcplugin.addSortMethod(handle=handle, sortMethod=xbmcplugin.SORT_METHOD_UNSORTED) else: Logger.Debug("Sorting method default: None") xbmcplugin.addSortMethod(handle=handle, sortMethod=xbmcplugin.SORT_METHOD_UNSORTED) xbmcplugin.addSortMethod(handle=handle, sortMethod=xbmcplugin.SORT_METHOD_LABEL) xbmcplugin.addSortMethod(handle=handle, sortMethod=xbmcplugin.SORT_METHOD_DATE) return