Ejemplo n.º 1
0
 def parse(cls, tokenized_query):
     parsed_query = {}
     for key, expressions in tokenized_query.items():
         field, _ = parse_field(key)
         parsed_query[key] = [
             cls.PARSERS_BY_FIELD[field](exp) for exp in expressions
         ]
     return parsed_query
Ejemplo n.º 2
0
 def build(cls, parsed_query: Dict[str, Iterable]) -> Dict[str, Iterable]:
     built_query = {}
     for key, operations in parsed_query.items():
         field, _ = parse_field(key)
         built_query[key] = [
             QueryCondSpec(cond=cls.CONDITIONS_BY_FIELD[field](
                 op=op_spec.op, negation=op_spec.negation),
                           params=op_spec.params) for op_spec in operations
         ]
     return built_query
Ejemplo n.º 3
0
 def tokenize(cls, query_spec: str) -> Dict[str, Iterable]:
     tokenized_query = tokenize_query(query_spec)
     for key in tokenized_query.keys():
         field, _ = parse_field(key)
         if field and (field not in cls.PARSERS_BY_FIELD
                       or field not in cls.CONDITIONS_BY_FIELD):
             raise QueryError(
                 'key `{}` is not supported by query manager `{}`.'.format(
                     key, cls.NAME))
     return tokenized_query
Ejemplo n.º 4
0
 def parse_field(cls, field: str) -> Tuple[str, Optional[str]]:
     return parse_field(field=field)
Ejemplo n.º 5
0
    def test_parse_field(self):
        with self.assertRaises(QueryParserException):
            parse_field('')

        with self.assertRaises(QueryParserException):
            parse_field('.')

        with self.assertRaises(QueryParserException):
            parse_field('sdf.sdf.sf')

        with self.assertRaises(QueryParserException):
            parse_field('foo.')

        assert parse_field('foo') == ('foo', None)
        assert parse_field('foo_bar') == ('foo_bar', None)
        assert parse_field('foo.bar') == ('foo', 'bar')
        assert parse_field('metric.foo_bar') == ('metric', 'foo_bar')
Ejemplo n.º 6
0
 def proxy_field(cls, field: str) -> str:
     field, suffix = parse_field(field)
     if field in cls.FIELDS_PROXY:
         field = cls.FIELDS_PROXY[field]
     return '{}__{}'.format(field, suffix) if suffix else field
Ejemplo n.º 7
0
    def test_parse_field(self):
        with self.assertRaises(QueryParserException):
            parse_field('')

        with self.assertRaises(QueryParserException):
            parse_field('__')

        with self.assertRaises(QueryParserException):
            parse_field('sdf__sdf__sf')

        with self.assertRaises(QueryParserException):
            parse_field('foo__')

        assert parse_field('foo') == ('foo', None)
        assert parse_field('foo_bar') == ('foo_bar', None)
        assert parse_field('foo__bar') == ('foo', 'bar')
        assert parse_field('metric__foo_bar') == ('metric', 'foo_bar')
Ejemplo n.º 8
0
 def parse_field(cls, field):
     return parse_field(field=field)