Changeset View
Changeset View
Standalone View
Standalone View
swh/search/translator.py
# Copyright (C) 2021 The Software Heritage developers | |||||
# See the AUTHORS file at the top-level directory of this distribution | |||||
# License: GNU General Public License version 3, or any later version | |||||
# See top-level LICENSE file for more information | |||||
import logging | |||||
import os | import os | ||||
import tempfile | |||||
from pkg_resources import resource_filename | from pkg_resources import resource_filename | ||||
from tree_sitter import Language, Parser | from tree_sitter import Language, Parser | ||||
from swh.search.utils import get_expansion, unescape | from swh.search.utils import get_expansion, unescape | ||||
logger = logging.getLogger(__name__) | |||||
class Translator: | class Translator: | ||||
RANGE_OPERATOR_MAP = { | RANGE_OPERATOR_MAP = { | ||||
">": "gt", | ">": "gt", | ||||
"<": "lt", | "<": "lt", | ||||
">=": "gte", | ">=": "gte", | ||||
"<=": "lte", | "<=": "lte", | ||||
} | } | ||||
def __init__(self): | def __init__(self): | ||||
ql_rel_paths = [ | ql_path = resource_filename("swh.search", "static/swh_ql.so") | ||||
"static/swh_ql.so", # installed | if not os.path.exists(ql_path): | ||||
"../../query_language/swh_ql.so", # development | logging.info("%s does not exist, building in temporary directory", ql_path) | ||||
] | self._build_dir = tempfile.TemporaryDirectory(prefix="swh.search-build") | ||||
for ql_rel_path in ql_rel_paths: | source_path = resource_filename("swh.search", "query_language") | ||||
ql_path = resource_filename("swh.search", ql_rel_path) | ql_path = os.path.join(self._build_dir.name, "swh_ql.so") | ||||
if os.path.exists(ql_path): | Language.build_library(ql_path, [source_path]) | ||||
break | |||||
else: | |||||
assert False, "swh_ql.so was not found in any of the expected paths" | |||||
search_ql = Language(ql_path, "swh_search_ql") | search_ql = Language(ql_path, "swh_search_ql") | ||||
self.parser = Parser() | self.parser = Parser() | ||||
self.parser.set_language(search_ql) | self.parser.set_language(search_ql) | ||||
self.query = "" | self.query = "" | ||||
def parse_query(self, query): | def parse_query(self, query): | ||||
▲ Show 20 Lines • Show All 265 Lines • Show Last 20 Lines |