Skip to content

Commit b8a16fb

Browse files
author
Gerit Wagner
committed
add linter function
1 parent dac1c32 commit b8a16fb

File tree

1 file changed

+43
-2
lines changed

1 file changed

+43
-2
lines changed

search_query/linter.py

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@
88
import search_query.parser
99
from search_query.constants import ExitCodes
1010
from search_query.search_file import load_search_file
11+
from search_query.search_file import SearchFile
1112

1213
if typing.TYPE_CHECKING:
1314
from search_query.parser_base import QueryStringParser
1415
# pylint: disable=broad-except
1516

1617

17-
def get_parser(
18+
def _get_parser(
1819
search_string: str, *, platform: str, search_field_general: str
1920
) -> QueryStringParser:
2021
"""Run the linter on the search string"""
@@ -32,6 +33,46 @@ def get_parser(
3233
return parser
3334

3435

36+
def lint_file(search_file: SearchFile) -> dict:
37+
"""Lint a search file and return the messages."""
38+
# pylint: disable=too-many-locals
39+
platform = search_query.parser.get_platform(search_file.platform)
40+
if platform not in search_query.parser.PARSERS:
41+
raise ValueError(
42+
f"Unknown platform: {platform}. "
43+
f"Must be one of {search_query.parser.PARSERS}"
44+
)
45+
46+
return lint_query_string(
47+
search_file.search_string,
48+
platform=search_file.platform,
49+
search_field_general=search_file.search_field,
50+
)
51+
52+
53+
def lint_query_string(
54+
search_string: str,
55+
*,
56+
platform: str,
57+
search_field_general: str = "",
58+
) -> dict:
59+
"""Lint a query string and return the messages."""
60+
# pylint: disable=too-many-locals
61+
if platform not in search_query.parser.PARSERS:
62+
raise ValueError(
63+
f"Unknown platform: {platform}. "
64+
f"Must be one of {search_query.parser.PARSERS}"
65+
)
66+
67+
print(f"Linting query string for platform {platform}")
68+
69+
parser = _get_parser(
70+
search_string, platform=platform, search_field_general=search_field_general
71+
)
72+
73+
return parser.linter.messages # type: ignore
74+
75+
3576
# pylint: disable=too-many-locals
3677
def pre_commit_hook(file_path: str) -> int:
3778
"""Entrypoint for the query linter hook"""
@@ -52,7 +93,7 @@ def pre_commit_hook(file_path: str) -> int:
5293

5394
print(f"Lint: {Path(file_path).name} ({platform})")
5495

55-
parser = get_parser(
96+
parser = _get_parser(
5697
search_file.search_string,
5798
platform=search_file.platform,
5899
search_field_general=search_file.search_field,

0 commit comments

Comments
 (0)