search_query.linter_base

Validator for search queries.

Classes

QueryListLinter(parser, string_parser_class)

Class for Query List Validation

QueryStringLinter(query_str)

Class for Query String Validation

class search_query.linter_base.QueryListLinter(parser: QueryListParser, string_parser_class: Type[QueryStringParser])

Bases: object

Class for Query List Validation

add_linter_message(error: QueryErrorCode, *, list_position: int, positions: List[tuple[int, int]], details: str = '') None

Add a linter message.

check_status() None

Check the output of the linter and report errors to the user

has_fatal_errors() bool

Check if there are any fatal errors.

print_messages() None

Print the latest linter messages.

class search_query.linter_base.QueryStringLinter(query_str: str)

Bases: object

Class for Query String Validation

FAULTY_OPERATOR_REGEX = '\\b(?:[aA][nN][dD]|[oO][rR]|[nN][oO][tT])\\b'
OPERATOR_PRECEDENCE = {'AND': 1, 'NEAR': 3, 'NOT': 2, 'OR': 0, 'WITHIN': 3}
PARENTHESIS_REGEX = '[\\(\\)]'
PLATFORM: PLATFORM = 'generic'
VALID_FIELDS_REGEX: re.Pattern
add_artificial_parentheses_for_operator_precedence(index: int = 0, output: list | None = None) tuple[int, list[Token]]

Adds artificial parentheses with position (-1, -1) to enforce operator precedence.

add_higher_value(output: list[Token], previous_value: int, value: int, art_par: int) tuple[list[Token], int]

Adds open parenthesis to higher value operators

add_linter_message(error: QueryErrorCode, *, positions: Sequence[tuple], details: str = '') None

Add a linter message.

check_boolean_operator_readability(*, faulty_operators: str = '|&') None

Check for readability of boolean operators.

check_invalid_characters_in_search_term(invalid_characters: str) None

Check a search term for invalid characters

check_invalid_characters_in_search_term_query(query: Query, invalid_characters: str) None

Check a search term for invalid characters

check_missing_tokens() None

Check missing tokens

check_near_distance_in_range(*, max_value: int) None

Check for NEAR with a specified distance out of range.

check_operator_capitalization() None

Check if operators are capitalized.

check_operators_with_fields(query: Query) None

Check for operators with fields

check_status() None

Check the output of the linter and report errors to the user

check_unbalanced_parentheses() None

Check query for unbalanced parentheses.

check_unbalanced_quotes_in_terms(query: Query) None

Recursively check for unbalanced quotes in quoted search terms.

check_unknown_token_types() None

Check for unknown token types.

check_unsupported_search_fields_in_query(query: Query) None

Check for the correct format of fields.

Note: compile valid_field_regex with/out flags=re.IGNORECASE

flatten_redundant_artificial_nesting(tokens: list[Token]) None

Flattens redundant artificial nesting: If two artificial open parens are followed eventually by two artificial close parens at the same level, removes the outer ones.

get_precedence(token: str) int

Returns operator precedence for logical and proximity operators.

get_query_with_fields_at_terms(query: Query) Query

Move the search field from the operator to the terms.

Note: utility function for validating search terms with efficient access to search fields (at the level of terms).

handle_fully_quoted_query_str(query_str: str) str

Handle fully quoted query string.

handle_nonstandard_quotes_in_query_str(query_str: str) str

Handle non-standard quotes in query string.

handle_prefix_in_query_str(query_str: str) str

Handle prefix in query string.

Removes tokens before a fully quoted query if they are not connected with a valid operator.

Only applies if quotes are balanced (even number of quotes).

handle_suffix_in_query_str(query_str: str) str

Handle suffix in query string.

Removes tokens after a fully quoted query if they are not connected with a valid operator.

Only applies if quotes are balanced (even number of quotes).

has_fatal_errors() bool

Check if there are any fatal errors.

print_messages() None

Print the latest linter messages.

abstract syntax_str_to_generic_search_field_set(field_value: str) set[Fields]

Translate a search field

abstract validate_query_tree(query: Query) None

Validate query tree

abstract validate_tokens(*, tokens: List[Token], query_str: str, search_field_general: str = '') List[Token]

Validate tokens