Coverage for /builds/BuildGrid/buildgrid/buildgrid/server/operations/filtering/parser.py: 100.00%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

22 statements  

1# Copyright (C) 2020 Bloomberg LP 

2# 

3# Licensed under the Apache License, Version 2.0 (the "License"); 

4# you may not use this file except in compliance with the License. 

5# You may obtain a copy of the License at 

6# 

7# <http://www.apache.org/licenses/LICENSE-2.0> 

8# 

9# Unless required by applicable law or agreed to in writing, software 

10# distributed under the License is distributed on an "AS IS" BASIS, 

11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 

12# See the License for the specific language governing permissions and 

13# limitations under the License. 

14 

15 

16from typing import List 

17 

18from lark import Lark 

19from lark.exceptions import LarkError 

20 

21from buildgrid._exceptions import InvalidArgumentError 

22 

23from buildgrid.server.operations.filtering import OperationFilter 

24from buildgrid.server.operations.filtering.interpreter import FilterTreeInterpreter 

25 

26 

27class FilterParser: 

28 """ 

29 Utility class primarily used to parse a filter string and return a list a OperationFilters. 

30 """ 

31 

32 lark_parser = Lark.open("filter_grammar.lark", rel_to=__file__, start="filter_phrase") 

33 

34 @staticmethod 

35 def parse_listoperations_filters(filter_string: str) -> List[OperationFilter]: 

36 """ Separate the lowercase filter string into individual components, and return a map containing 

37 the relevant filters to use. 

38 

39 Filter strings take the following form: key1=value1&key2=value2,value3&key3=value4 

40 All spaces in the input are ignored. 

41 

42 Filter options are separated with ampersands (&). 

43 If a key is repeated, the combined set of values are treated as a logical conjunction (AND). 

44 

45 """ 

46 operation_filters: List[OperationFilter] = [] 

47 

48 # Handle empty input separately to simplify grammar 

49 if not filter_string.strip(): 

50 return operation_filters 

51 

52 try: 

53 filter_phrase_tree = FilterParser.lark_parser.parse(filter_string) 

54 except LarkError as e: 

55 raise InvalidArgumentError(f"Error parsing filter string. See docs for correct filter string syntax. " 

56 f"Parser error follows: [{str(e)}]") 

57 

58 try: 

59 operation_filters = FilterTreeInterpreter().visit(filter_phrase_tree) 

60 except InvalidArgumentError as e: 

61 raise InvalidArgumentError( 

62 f"Invalid filter string [{filter_string}]. " 

63 "See docs for correct filter string syntax and values.") from e 

64 

65 return operation_filters