-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__init__.py
More file actions
68 lines (54 loc) · 2.26 KB
/
__init__.py
File metadata and controls
68 lines (54 loc) · 2.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from typing import List, Tuple, Union
from mediahaven.mediahaven import MediaHavenClient
# Records
from mediahaven.resources.records import Records
from mediahaven.resources.field_definitions import FieldDefinitions
from mediahaven.resources.organisations import Organisations
class MediaHaven(MediaHavenClient):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.records = Records(self)
self.field_definitions = FieldDefinitions(self)
self.organisations = Organisations(self)
class MediaHavenQuery:
"MediaHaven query object."
def __init__(self, ):
self._clauses = []
def add_clauses(self, query_params: Union[List[Tuple[str, str]], List[str]] , exclude: bool = False, is_and: bool = False):
"""Adds clauses to the query.
Args:
query_params: A list of tuples containing the query parameters (field and value), or a string containing free text.
exclude: If true the clauses will be excluded from the query.
is_and: If true the clauses will be combined with an AND operator, otherwise with an OR operator.
"""
self._clauses.append(
{
"query_params": query_params,
"operator": ("-" if exclude else "+"),
"is_and": is_and
}
)
def generate_query(self) -> str:
"""Generates the query string.
Returns:
The query string.
"""
query = " ".join(
[
f'{clause["operator"]}(' + ' '.join(
[f'{k_v[0]}:"{k_v[1]}"' for k_v in clause["query_params"]]
if isinstance(clause["query_params"][0], Tuple)
else [f'{v}' for v in clause["query_params"]]
) + ')'
if not clause["is_and"]
else " ".join(
[f'{clause["operator"]}({k_v[0]}:"{k_v[1]}")' for k_v in clause["query_params"]]
if isinstance(clause["query_params"][0], Tuple)
else [f'{clause["operator"]}({v})' for v in clause["query_params"]]
)
for clause in self._clauses
]
)
return query