|
| 1 | +from __future__ import annotations |
| 2 | +from abc import ABC |
| 3 | +from datetime import datetime |
| 4 | +from typing import Any, Iterable, List |
| 5 | + |
| 6 | + |
| 7 | +FieldName = str # TODO: validate field names? |
| 8 | +"""https://obelisk.pages.ilabt.imec.be/obelisk-core/query.html#available-data-point-fields""" |
| 9 | + |
| 10 | + |
| 11 | +class Constraint(ABC): |
| 12 | + pass |
| 13 | + |
| 14 | + |
| 15 | +class Comparison(): |
| 16 | + left: FieldName |
| 17 | + right: Any |
| 18 | + op: str |
| 19 | + |
| 20 | + def __init__(self, left: FieldName, right: Any, op: str): |
| 21 | + self.left = left |
| 22 | + self.right = right |
| 23 | + self.op = op |
| 24 | + |
| 25 | + def __str__(self) -> str: |
| 26 | + right = self._sstr(self.right) |
| 27 | + if not right.startswith('('): |
| 28 | + right = f"'{right}'" |
| 29 | + |
| 30 | + return f"('{self.left}'{self.op}{right})" |
| 31 | + |
| 32 | + @staticmethod |
| 33 | + def _sstr(item: Any): |
| 34 | + """Smart string conversion""" |
| 35 | + if isinstance(item, datetime): |
| 36 | + return item.isoformat() |
| 37 | + return str(item) |
| 38 | + |
| 39 | + @staticmethod |
| 40 | + def _iterable_to_group(iter: Iterable[Any]) -> str: |
| 41 | + """Produces a group of the form ("a","b")""" |
| 42 | + return str(tuple([Comparison._sstr(x) for x in iter])) |
| 43 | + |
| 44 | + @classmethod |
| 45 | + def equal(cls, left: FieldName, right: Any) -> Comparison: |
| 46 | + return Comparison(left, right, "==") |
| 47 | + |
| 48 | + @classmethod |
| 49 | + def not_equal(cls, left: FieldName, right: Any) -> Comparison: |
| 50 | + return Comparison(left, right, "!=") |
| 51 | + |
| 52 | + @classmethod |
| 53 | + def less(cls, left: FieldName, right: Any) -> Comparison: |
| 54 | + return Comparison(left, right, "<") |
| 55 | + |
| 56 | + @classmethod |
| 57 | + def less_equal(cls, left: FieldName, right: Any) -> Comparison: |
| 58 | + return Comparison(left, right, "<=") |
| 59 | + |
| 60 | + @classmethod |
| 61 | + def greater(cls, left: FieldName, right: Any) -> Comparison: |
| 62 | + return Comparison(left, right, ">") |
| 63 | + |
| 64 | + @classmethod |
| 65 | + def greater_equal(cls, left: FieldName, right: Any) -> Comparison: |
| 66 | + return Comparison(left, right, ">=") |
| 67 | + |
| 68 | + @classmethod |
| 69 | + def is_in(cls, left: FieldName, right: Iterable[Any]) -> Comparison: |
| 70 | + return Comparison(left, cls._iterable_to_group(right), "=in=") |
| 71 | + |
| 72 | + @classmethod |
| 73 | + def is_not_in(cls, left: FieldName, right: Iterable[Any]) -> Comparison: |
| 74 | + return Comparison(left, cls._iterable_to_group(right), "=out=") |
| 75 | + |
| 76 | + @classmethod |
| 77 | + def null(cls, left: FieldName) -> Comparison: |
| 78 | + return Comparison(left, "", "=null=") |
| 79 | + |
| 80 | + @classmethod |
| 81 | + def not_null(cls, left: FieldName) -> Comparison: |
| 82 | + return Comparison(left, "", "=notnull=") |
| 83 | + |
| 84 | + |
| 85 | +Item = Constraint | Comparison |
| 86 | + |
| 87 | + |
| 88 | +class And(Constraint): |
| 89 | + content: List[Item] |
| 90 | + |
| 91 | + def __init__(self, *args: Item): |
| 92 | + self.content = list(args) |
| 93 | + |
| 94 | + def __str__(self) -> str: |
| 95 | + return "(" + ";".join([str(x) for x in self.content]) + ")" |
| 96 | + |
| 97 | + |
| 98 | +class Or(Constraint): |
| 99 | + content: List[Item] |
| 100 | + |
| 101 | + def __init__(self, *args: Item): |
| 102 | + self.content = list(args) |
| 103 | + |
| 104 | + def __str__(self) -> str: |
| 105 | + return "(" + ",".join([str(x) for x in self.content]) + ")" |
| 106 | + |
| 107 | + |
| 108 | +class Filter(): |
| 109 | + content: Item | None = None |
| 110 | + |
| 111 | + def __init__(self, content: Constraint | None = None): |
| 112 | + self.content = content |
| 113 | + |
| 114 | + def __str__(self) -> str: |
| 115 | + return str(self.content) |
| 116 | + |
| 117 | + def add_and(self, *other: Item) -> Filter: |
| 118 | + if self.content is None: |
| 119 | + self.content = And(*other) |
| 120 | + else: |
| 121 | + self.content = And(self.content, *other) |
| 122 | + return self |
| 123 | + |
| 124 | + def add_or(self, *other: Item) -> Filter: |
| 125 | + if self.content is None: |
| 126 | + self.content = Or(*other) |
| 127 | + else: |
| 128 | + self.content = Or(self.content, *other) |
| 129 | + return self |
0 commit comments