view env/lib/python3.9/site-packages/galaxy/util/search.py @ 0:4f3585e2f14b draft default tip

"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
author shellac
date Mon, 22 Mar 2021 18:12:50 +0000
parents
children
line wrap: on
line source

import re


def parse_filters(search_term, filters):
    """Support github-like filters for narrowing the results.

    Order of chunks does not matter, only recognized filter names are allowed.

    :param search_term: the original search str from user input

    :returns allow_query: whoosh Query object used for filtering
        results of searching in index
    :returns search_term_without_filters: str that represents user's
        search phrase without the filters
    """
    allow_terms = []
    search_term_without_filters = None
    search_space = search_term.replace('"', "'")
    filter_keys = "|".join(list(filters.keys()))
    pattern = fr"({filter_keys}):(\w+|\'.*?\')"
    reserved = re.compile(pattern)
    while True:
        match = reserved.search(search_space)
        if match is None:
            search_term_without_filters = ' '.join(search_space.split())
            break
        first_group = match.groups()[0]
        if first_group in filters:
            filter_as = filters[first_group]
            allow_terms.append((filter_as, match.groups()[1].strip().replace("'", "")))
        search_space = search_space[0:match.start()] + search_space[match.end():]
    allow_query = allow_terms if len(allow_terms) > 0 else None
    return allow_query, search_term_without_filters