|
4 | 4 | import os |
5 | 5 | import shutil |
6 | 6 | import warnings |
| 7 | +from functools import wraps, cache |
7 | 8 | from typing import Optional |
| 9 | +from urllib.parse import urlparse |
8 | 10 |
|
9 | 11 | import dateutil.parser |
10 | 12 | import requests |
11 | 13 |
|
12 | 14 | from marble_client.constants import CACHE_FNAME, CACHE_META_FNAME, NODE_REGISTRY_URL |
13 | | -from marble_client.exceptions import UnknownNodeError |
| 15 | +from marble_client.exceptions import UnknownNodeError, JupyterEnvironmentError |
14 | 16 | from marble_client.node import MarbleNode |
15 | 17 |
|
16 | 18 | __all__ = ["MarbleClient"] |
17 | 19 |
|
18 | 20 |
|
| 21 | +def check_jupyterlab(f): |
| 22 | + """ |
| 23 | + Wraps the function f by first checking if the current script is running in a |
| 24 | + Marble Jupyterlab environment and raising a JupyterEnvironmentError if not. |
| 25 | +
|
| 26 | + This is used as a pre-check for functions that only work in a Marble Jupyterlab |
| 27 | + environment. |
| 28 | + """ |
| 29 | + @wraps(f) |
| 30 | + def wrapper(*args, **kwargs): |
| 31 | + if os.getenv("PAVICS_HOST_URL"): |
| 32 | + return f(*args, **kwargs) |
| 33 | + raise JupyterEnvironmentError("Not in a Marble jupyterlab environment") |
| 34 | + return wrapper |
| 35 | + |
| 36 | + |
19 | 37 | class MarbleClient: |
20 | 38 | def __init__(self, fallback: Optional[bool] = True) -> None: |
21 | 39 | """Constructor method |
@@ -51,6 +69,38 @@ def __init__(self, fallback: Optional[bool] = True) -> None: |
51 | 69 | def nodes(self) -> dict[str, MarbleNode]: |
52 | 70 | return self._nodes |
53 | 71 |
|
| 72 | + @property |
| 73 | + @cache |
| 74 | + @check_jupyterlab |
| 75 | + def this_node(self) -> MarbleNode: |
| 76 | + """ |
| 77 | + Return the node where this script is currently running. |
| 78 | +
|
| 79 | + Note that this function only works in a Marble Jupyterlab environment. |
| 80 | + """ |
| 81 | + host_url = urlparse(os.getenv("PAVICS_HOST_URL")) |
| 82 | + for node in self.nodes.values(): |
| 83 | + if urlparse(node.url).hostname == host_url.hostname: |
| 84 | + return node |
| 85 | + raise UnknownNodeError(f"No node found in the registry with the url {host_url}") |
| 86 | + |
| 87 | + @check_jupyterlab |
| 88 | + def this_session(self, session: Optional[requests.Session]) -> requests.Session: |
| 89 | + """ |
| 90 | + Add the login session cookies of the user who is currently logged in to the session object. |
| 91 | + If a session object is not passed as an argument to this function, create a new session |
| 92 | + object as well. |
| 93 | +
|
| 94 | + Note that this function only works in a Marble Jupyterlab environment. |
| 95 | + """ |
| 96 | + if session is None: |
| 97 | + session = requests.Session() |
| 98 | + r = requests.get(f"{os.getenv('JUPYTERHUB_API_URL')}/users/{os.getenv('JUPYTERHUB_USER')}", |
| 99 | + headers={"Authorization": f"token {os.getenv('JUPYTERHUB_API_TOKEN')}"}) |
| 100 | + for name, value in r.json().get("auth_state", {}).get("magpie_cookies", {}).items(): |
| 101 | + session.cookies.set(name, value) |
| 102 | + return session |
| 103 | + |
54 | 104 | def __getitem__(self, node: str) -> MarbleNode: |
55 | 105 | try: |
56 | 106 | return self.nodes[node] |
|
0 commit comments