| 39 | |
| 40 | |
| 41 | class Context(object): |
| 42 | def __init__( |
| 43 | self, |
| 44 | coordinator_endpoint, |
| 45 | flex, |
| 46 | name=None, |
| 47 | timestamp=time.time(), |
| 48 | context="global", |
| 49 | graph_name=None, |
| 50 | ): |
| 51 | if name is None: |
| 52 | name = "context_" + "".join(random.choices(ascii_letters, k=8)) |
| 53 | |
| 54 | self.name = name |
| 55 | self.flex = flex |
| 56 | self.coordinator_endpoint = coordinator_endpoint |
| 57 | # switch to specific graph after `using graph` |
| 58 | self.context = context |
| 59 | self.graph_name = graph_name |
| 60 | self.timestamp = timestamp |
| 61 | |
| 62 | def switch_context(self, context: str): |
| 63 | self.context = context |
| 64 | if self.context == "global": |
| 65 | self.graph_name = None |
| 66 | |
| 67 | def set_graph_name(self, graph_name: str): |
| 68 | self.graph_name = graph_name |
| 69 | |
| 70 | def to_dict(self) -> dict: |
| 71 | return { |
| 72 | "name": self.name, |
| 73 | "graph_name": str(self.graph_name), |
| 74 | "flex": self.flex, |
| 75 | "coordinator_endpoint": self.coordinator_endpoint, |
| 76 | "context": self.context, |
| 77 | "timestamp": self.timestamp, |
| 78 | } |
| 79 | |
| 80 | @classmethod |
| 81 | def from_dict(cls, dikt): |
| 82 | return Context( |
| 83 | coordinator_endpoint=dikt.get("coordinator_endpoint"), |
| 84 | flex=dikt.get("flex"), |
| 85 | name=dikt.get("name"), |
| 86 | timestamp=dikt.get("timestamp"), |
| 87 | context=dikt.get("context"), |
| 88 | graph_name=dikt.get("graph_name", None), |
| 89 | ) |
| 90 | |
| 91 | def is_expired(self, validity_period=86400) -> bool: |
| 92 | current_timestamp = time.time() |
| 93 | if current_timestamp - self.timestamp > validity_period: |
| 94 | return True |
| 95 | return False |
| 96 | |
| 97 | def reset_timestamp(self): |
| 98 | self.timestamp = time.time() |
no outgoing calls
no test coverage detected