def __init__(self, search_string, case_sensitive=True, keyword_tags=None): """ :param search_string: a string describing the match conditions :param case_sensitive: whether to search in a case-sensitive manner. :param keyword_tags: a list of tags to match search keywords in. """ self.case_sensitive = case_sensitive self.keyword_tags = keyword_tags or [] search_string = shave_marks(search_string) tokens = self.__tokenize_query(search_string) tokens = self.__red(tokens) tokens = self.__optimize_tokens(tokens) self.matchers = self.__tokens_to_matchers(tokens)
def get_tag_search( self, tag, format=True, artist_compilations=False, extend_title=True ): """ Get a tag value suitable for passing to the search system. This includes quoting and list joining. :param format: pre-format into a search query. :param artist_compilations: If True, automatically handle albumartist and other compilations detections when tag=="albumartist". :param extend_title: If the title tag is unknown, try to add some identifying information to it. :returns: unicode string that is used for searching """ extraformat = "" if tag == "albumartist": if artist_compilations and self.__tags.get('__compilation'): value = self.__tags.get('albumartist', None) tag = 'albumartist' extraformat += " ! __compilation==__null__" else: value = self.__tags.get('artist') elif tag in ('tracknumber', 'discnumber'): value = self.split_numerical(self.__tags.get(tag))[0] elif tag in ( '__length', '__playcount', '__rating', '__startoffset', '__stopoffset', ): value = self.__tags.get(tag, 0) elif tag == '__bitrate': try: value = int(self.__tags['__bitrate']) // 1000 if value != -1: # TRANSLATORS: Bitrate (k here is short for kbps). value = [_("%dk") % value, self.__tags['__bitrate']] except (KeyError, ValueError): value = -1 elif tag == '__basename': value = self.get_basename_display() else: value = self.__tags.get(tag) # Quote arguments if value is None: value = '__null__' if tag == 'title': extraformat += ' __loc==\"%s\"' % self.__tags['__loc'] elif format: if isinstance(value, list): value = ['"%s"' % self.quoter(val) for val in value] else: value = '"%s"' % self.quoter(value) # Join lists if format: if isinstance(value, list): value = " ".join(['%s==%s' % (tag, v) for v in value]) else: value = '%s==%s' % (tag, value) if extraformat: value += extraformat # Convert all return values to unicode if isinstance(value, list): value = map(shave_marks, value) else: value = shave_marks(value) return value
def get_tag_search( self, tag, format=True, artist_compilations=False, extend_title=True ): """ Get a tag value suitable for passing to the search system. This includes quoting and list joining. :param format: pre-format into a search query. :param artist_compilations: If True, automatically handle albumartist and other compilations detections when tag=="albumartist". :param extend_title: If the title tag is unknown, try to add some identifying information to it. :returns: unicode string that is used for searching """ extraformat = "" if tag == "albumartist": if artist_compilations and self.__tags.get('__compilation'): value = self.__tags.get('albumartist', None) tag = 'albumartist' extraformat += " ! __compilation==__null__" else: value = self.__tags.get('albumartist') if value is None: value = self.__tags.get('artist') elif tag in ('tracknumber', 'discnumber'): value = self.split_numerical(self.__tags.get(tag))[0] elif tag in ( '__length', '__playcount', '__rating', '__startoffset', '__stopoffset', ): value = self.__tags.get(tag, 0) elif tag == '__bitrate': try: value = int(self.__tags['__bitrate']) // 1000 if value != -1: # TRANSLATORS: Bitrate (k here is short for kbps). value = [_("%dk") % value, self.__tags['__bitrate']] except (KeyError, ValueError): value = -1 elif tag == '__basename': value = self.get_basename_display() else: value = self.__tags.get(tag) # Quote arguments if value is None: value = '__null__' if tag == 'title': extraformat += ' __loc==\"%s\"' % self.__tags['__loc'] elif format: if isinstance(value, list): value = ['"%s"' % self.quoter(val) for val in value] else: value = '"%s"' % self.quoter(value) # Join lists if format: if isinstance(value, list): value = " ".join(['%s==%s' % (tag, v) for v in value]) else: value = '%s==%s' % (tag, value) if extraformat: value += extraformat # Convert all return values to unicode if isinstance(value, list): value = map(shave_marks, value) else: value = shave_marks(value) return value