marble-client 1.0.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- marble_client-1.0.0/LICENSE +21 -0
- marble_client-1.0.0/PKG-INFO +145 -0
- marble_client-1.0.0/README.md +108 -0
- marble_client-1.0.0/marble_client/__init__.py +4 -0
- marble_client-1.0.0/marble_client/client.py +126 -0
- marble_client-1.0.0/marble_client/constants.py +19 -0
- marble_client-1.0.0/marble_client/exceptions.py +10 -0
- marble_client-1.0.0/marble_client/node.py +107 -0
- marble_client-1.0.0/marble_client/services.py +73 -0
- marble_client-1.0.0/marble_client.egg-info/PKG-INFO +145 -0
- marble_client-1.0.0/marble_client.egg-info/SOURCES.txt +15 -0
- marble_client-1.0.0/marble_client.egg-info/dependency_links.txt +1 -0
- marble_client-1.0.0/marble_client.egg-info/requires.txt +3 -0
- marble_client-1.0.0/marble_client.egg-info/top_level.txt +1 -0
- marble_client-1.0.0/pyproject.toml +33 -0
- marble_client-1.0.0/requirements.txt +3 -0
- marble_client-1.0.0/setup.cfg +4 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 Data Analytics for Canadian Climate Services
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: marble_client
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: A python client to access information about the Marble climate infomatics network
|
|
5
|
+
Author-email: Deepak Chandan <dchandan@cs.toronto.edu>
|
|
6
|
+
License: MIT License
|
|
7
|
+
|
|
8
|
+
Copyright (c) 2023 Data Analytics for Canadian Climate Services
|
|
9
|
+
|
|
10
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
11
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
12
|
+
in the Software without restriction, including without limitation the rights
|
|
13
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
14
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
15
|
+
furnished to do so, subject to the following conditions:
|
|
16
|
+
|
|
17
|
+
The above copyright notice and this permission notice shall be included in all
|
|
18
|
+
copies or substantial portions of the Software.
|
|
19
|
+
|
|
20
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
21
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
22
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
23
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
24
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
25
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
26
|
+
SOFTWARE.
|
|
27
|
+
|
|
28
|
+
Project-URL: Bug Tracker, https://github.com/DACCS-Climate/marble_client_python/issues
|
|
29
|
+
Project-URL: Homepage, https://github.com/DACCS-Climate/marble_client_python
|
|
30
|
+
Keywords: climate,climate infomatics,climate data,DACCS,Marble
|
|
31
|
+
Classifier: Programming Language :: Python :: 3
|
|
32
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
33
|
+
Classifier: Operating System :: OS Independent
|
|
34
|
+
Requires-Python: >=3.9
|
|
35
|
+
Description-Content-Type: text/markdown
|
|
36
|
+
License-File: LICENSE
|
|
37
|
+
|
|
38
|
+
# Marble python client
|
|
39
|
+
A python library to access information about the Marble climate infomatics network. The library provides a pythonic interface to the Marble network's [central registry](https://github.com/DACCS-Climate/Marble-node-registry). Users of the network are encouraged to use this library to access the network information and avoid hardcoding URLs to various nodes or services.
|
|
40
|
+
|
|
41
|
+
## Installation
|
|
42
|
+
|
|
43
|
+
To install `marble_client` issue this command:
|
|
44
|
+
```shell
|
|
45
|
+
pip install marble_client
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## Basic usage
|
|
49
|
+
|
|
50
|
+
The first thing to do is to get a `client` object:
|
|
51
|
+
|
|
52
|
+
```python
|
|
53
|
+
>>> from marble_client import MarbleClient
|
|
54
|
+
|
|
55
|
+
>>> client = MarbleClient()
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
All the information about the network can now be retrieved from the `client` object. E.g. the nodes available in the network can be accessed as:
|
|
59
|
+
```python
|
|
60
|
+
>>> client.nodes
|
|
61
|
+
{'UofT': <marble_client.node.MarbleNode at 0x10c129990>,
|
|
62
|
+
'PAVICS': <marble_client.node.MarbleNode at 0x10c6dd690>,
|
|
63
|
+
'Hirondelle': <marble_client.node.MarbleNode at 0x10c6dd890>}
|
|
64
|
+
```
|
|
65
|
+
The returned object is a python `dict` with node names for keys and `MarbleNode` objects as values. A particular node can be accessed as:
|
|
66
|
+
|
|
67
|
+
```python
|
|
68
|
+
>>> mynode = client['UofT']
|
|
69
|
+
>>> type(mynode)
|
|
70
|
+
marble_client.node.MarbleNode
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
Now that one has a Marble node of interest, a useful operation would be to check if that node is online in realtime, this can be done as:
|
|
74
|
+
|
|
75
|
+
```python
|
|
76
|
+
>>> mynode.is_online()
|
|
77
|
+
True
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
The URL for the node can be retrieved as:
|
|
81
|
+
```python
|
|
82
|
+
>>> mynode.url
|
|
83
|
+
'https://daccs.cs.toronto.edu'
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
Various other qualities about the node can be accessed as shown below (see [implementation](https://github.com/DACCS-Climate/marble_client_python/blob/main/marble_client/node.py) for the full list of available attributes).
|
|
87
|
+
|
|
88
|
+
```python
|
|
89
|
+
>>> mynode.affiliation
|
|
90
|
+
'University of Toronto'
|
|
91
|
+
>>> mynode.contact
|
|
92
|
+
'daccs-info@cs.toronto.edu'
|
|
93
|
+
>>> mynode.marble_version # The version of the software stack available on this node
|
|
94
|
+
'1.27.0'
|
|
95
|
+
>>> mynode.location
|
|
96
|
+
{'longitude': -79.39, 'latitude': 43.65}
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
The "services" that a Marble node offers can differ from one node to another. A list of which services are offered at a given node can be inquired as follows:
|
|
100
|
+
```python
|
|
101
|
+
>>> mynode.services
|
|
102
|
+
['geoserver',
|
|
103
|
+
'flyingpigeon',
|
|
104
|
+
'finch',
|
|
105
|
+
'raven',
|
|
106
|
+
'hummingbird',
|
|
107
|
+
'thredds',
|
|
108
|
+
'jupyterhub',
|
|
109
|
+
'weaver']
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
To get further information on one of the services, first retrieve that service. This can be done in one of two ways:
|
|
113
|
+
```python
|
|
114
|
+
>>> service = mynode['thredds']
|
|
115
|
+
>>> type(service)
|
|
116
|
+
marble_client.services.MarbleService
|
|
117
|
+
>>>
|
|
118
|
+
>>> service = mynode.thredds
|
|
119
|
+
>>> type(service)
|
|
120
|
+
marble_client.services.MarbleService
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
The most important thing one needs from the service is the endpoint at which the service is located:
|
|
124
|
+
```python
|
|
125
|
+
>>> service.url
|
|
126
|
+
'https://daccs.cs.toronto.edu/thredds/'
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
The service URL can also be accessed directly using the service object's name:
|
|
130
|
+
```python
|
|
131
|
+
>>> service
|
|
132
|
+
'https://daccs.cs.toronto.edu/thredds/'
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
Various attributes that can be accessed on the `MarbleService` object can be found by consulting the [implementation](https://github.com/DACCS-Climate/marble_client_python/blob/main/marble_client/services.py).
|
|
136
|
+
|
|
137
|
+
Of course, all operations can be chained, so if you don't need `MarbleClient`, `MarbleNode` or `MarbleService` objects for future operations, then to get, for example, the weaver service endpoint for the "PAVICS" node, one can do:
|
|
138
|
+
```python
|
|
139
|
+
>>> url = MarbleClient()["PAVICS"].weaver.url # returns a string object
|
|
140
|
+
>>> print(f"Weaver URL is {url}")
|
|
141
|
+
Weaver URL is https://pavics.ouranos.ca/weaver/
|
|
142
|
+
>>> # A MarbleService object is returned that can be used wherever a string can be used
|
|
143
|
+
>>> print(f"Weaver URL is {MarbleClient()['PAVICS'].weaver}")
|
|
144
|
+
Weaver URL is https://pavics.ouranos.ca/weaver/
|
|
145
|
+
```
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
# Marble python client
|
|
2
|
+
A python library to access information about the Marble climate infomatics network. The library provides a pythonic interface to the Marble network's [central registry](https://github.com/DACCS-Climate/Marble-node-registry). Users of the network are encouraged to use this library to access the network information and avoid hardcoding URLs to various nodes or services.
|
|
3
|
+
|
|
4
|
+
## Installation
|
|
5
|
+
|
|
6
|
+
To install `marble_client` issue this command:
|
|
7
|
+
```shell
|
|
8
|
+
pip install marble_client
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Basic usage
|
|
12
|
+
|
|
13
|
+
The first thing to do is to get a `client` object:
|
|
14
|
+
|
|
15
|
+
```python
|
|
16
|
+
>>> from marble_client import MarbleClient
|
|
17
|
+
|
|
18
|
+
>>> client = MarbleClient()
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
All the information about the network can now be retrieved from the `client` object. E.g. the nodes available in the network can be accessed as:
|
|
22
|
+
```python
|
|
23
|
+
>>> client.nodes
|
|
24
|
+
{'UofT': <marble_client.node.MarbleNode at 0x10c129990>,
|
|
25
|
+
'PAVICS': <marble_client.node.MarbleNode at 0x10c6dd690>,
|
|
26
|
+
'Hirondelle': <marble_client.node.MarbleNode at 0x10c6dd890>}
|
|
27
|
+
```
|
|
28
|
+
The returned object is a python `dict` with node names for keys and `MarbleNode` objects as values. A particular node can be accessed as:
|
|
29
|
+
|
|
30
|
+
```python
|
|
31
|
+
>>> mynode = client['UofT']
|
|
32
|
+
>>> type(mynode)
|
|
33
|
+
marble_client.node.MarbleNode
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
Now that one has a Marble node of interest, a useful operation would be to check if that node is online in realtime, this can be done as:
|
|
37
|
+
|
|
38
|
+
```python
|
|
39
|
+
>>> mynode.is_online()
|
|
40
|
+
True
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
The URL for the node can be retrieved as:
|
|
44
|
+
```python
|
|
45
|
+
>>> mynode.url
|
|
46
|
+
'https://daccs.cs.toronto.edu'
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
Various other qualities about the node can be accessed as shown below (see [implementation](https://github.com/DACCS-Climate/marble_client_python/blob/main/marble_client/node.py) for the full list of available attributes).
|
|
50
|
+
|
|
51
|
+
```python
|
|
52
|
+
>>> mynode.affiliation
|
|
53
|
+
'University of Toronto'
|
|
54
|
+
>>> mynode.contact
|
|
55
|
+
'daccs-info@cs.toronto.edu'
|
|
56
|
+
>>> mynode.marble_version # The version of the software stack available on this node
|
|
57
|
+
'1.27.0'
|
|
58
|
+
>>> mynode.location
|
|
59
|
+
{'longitude': -79.39, 'latitude': 43.65}
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
The "services" that a Marble node offers can differ from one node to another. A list of which services are offered at a given node can be inquired as follows:
|
|
63
|
+
```python
|
|
64
|
+
>>> mynode.services
|
|
65
|
+
['geoserver',
|
|
66
|
+
'flyingpigeon',
|
|
67
|
+
'finch',
|
|
68
|
+
'raven',
|
|
69
|
+
'hummingbird',
|
|
70
|
+
'thredds',
|
|
71
|
+
'jupyterhub',
|
|
72
|
+
'weaver']
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
To get further information on one of the services, first retrieve that service. This can be done in one of two ways:
|
|
76
|
+
```python
|
|
77
|
+
>>> service = mynode['thredds']
|
|
78
|
+
>>> type(service)
|
|
79
|
+
marble_client.services.MarbleService
|
|
80
|
+
>>>
|
|
81
|
+
>>> service = mynode.thredds
|
|
82
|
+
>>> type(service)
|
|
83
|
+
marble_client.services.MarbleService
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
The most important thing one needs from the service is the endpoint at which the service is located:
|
|
87
|
+
```python
|
|
88
|
+
>>> service.url
|
|
89
|
+
'https://daccs.cs.toronto.edu/thredds/'
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
The service URL can also be accessed directly using the service object's name:
|
|
93
|
+
```python
|
|
94
|
+
>>> service
|
|
95
|
+
'https://daccs.cs.toronto.edu/thredds/'
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
Various attributes that can be accessed on the `MarbleService` object can be found by consulting the [implementation](https://github.com/DACCS-Climate/marble_client_python/blob/main/marble_client/services.py).
|
|
99
|
+
|
|
100
|
+
Of course, all operations can be chained, so if you don't need `MarbleClient`, `MarbleNode` or `MarbleService` objects for future operations, then to get, for example, the weaver service endpoint for the "PAVICS" node, one can do:
|
|
101
|
+
```python
|
|
102
|
+
>>> url = MarbleClient()["PAVICS"].weaver.url # returns a string object
|
|
103
|
+
>>> print(f"Weaver URL is {url}")
|
|
104
|
+
Weaver URL is https://pavics.ouranos.ca/weaver/
|
|
105
|
+
>>> # A MarbleService object is returned that can be used wherever a string can be used
|
|
106
|
+
>>> print(f"Weaver URL is {MarbleClient()['PAVICS'].weaver}")
|
|
107
|
+
Weaver URL is https://pavics.ouranos.ca/weaver/
|
|
108
|
+
```
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import contextlib
|
|
2
|
+
import datetime
|
|
3
|
+
import json
|
|
4
|
+
import os
|
|
5
|
+
import shutil
|
|
6
|
+
import warnings
|
|
7
|
+
from typing import Optional
|
|
8
|
+
|
|
9
|
+
import dateutil.parser
|
|
10
|
+
import requests
|
|
11
|
+
|
|
12
|
+
from marble_client.constants import CACHE_FNAME, CACHE_META_FNAME, NODE_REGISTRY_URL
|
|
13
|
+
from marble_client.exceptions import UnknownNodeError
|
|
14
|
+
from marble_client.node import MarbleNode
|
|
15
|
+
|
|
16
|
+
__all__ = ["MarbleClient"]
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class MarbleClient:
|
|
20
|
+
def __init__(self, fallback: Optional[bool] = True) -> None:
|
|
21
|
+
"""Constructor method
|
|
22
|
+
|
|
23
|
+
:param fallback: If True, then fall back to a cached version of the registry
|
|
24
|
+
if the cloud registry cannot be accessed, defaults to True
|
|
25
|
+
:type fallback: Optional[bool], optional
|
|
26
|
+
:raises requests.exceptions.RequestException: Raised when there is an issue
|
|
27
|
+
connecting to the cloud registry and `fallback` is False
|
|
28
|
+
:raises UserWarning: Raised when there is an issue connecting to the cloud registry
|
|
29
|
+
and `fallback` is True
|
|
30
|
+
:raise RuntimeError: If cached registry needs to be read but there is no cache
|
|
31
|
+
"""
|
|
32
|
+
self._fallback = fallback
|
|
33
|
+
self._nodes: dict[str, MarbleNode] = {}
|
|
34
|
+
self._registry: dict = {}
|
|
35
|
+
try:
|
|
36
|
+
registry = requests.get(NODE_REGISTRY_URL)
|
|
37
|
+
registry.raise_for_status()
|
|
38
|
+
except (requests.exceptions.RequestException, requests.exceptions.ConnectionError):
|
|
39
|
+
if self._fallback:
|
|
40
|
+
warnings.warn("Cannot retrieve cloud registry. Falling back to cached version")
|
|
41
|
+
self._load_registry_from_cache()
|
|
42
|
+
else:
|
|
43
|
+
raise
|
|
44
|
+
else:
|
|
45
|
+
self._load_registry_from_cloud(registry)
|
|
46
|
+
|
|
47
|
+
for node, node_details in self._registry.items():
|
|
48
|
+
self._nodes[node] = MarbleNode(node, node_details)
|
|
49
|
+
|
|
50
|
+
@property
|
|
51
|
+
def nodes(self) -> dict[str, MarbleNode]:
|
|
52
|
+
return self._nodes
|
|
53
|
+
|
|
54
|
+
def __getitem__(self, node: str) -> MarbleNode:
|
|
55
|
+
try:
|
|
56
|
+
return self.nodes[node]
|
|
57
|
+
except KeyError:
|
|
58
|
+
raise UnknownNodeError(f"No node named '{node}' in the Marble network.") from None
|
|
59
|
+
|
|
60
|
+
def _load_registry_from_cloud(self, registry_response: requests.Response) -> None:
|
|
61
|
+
try:
|
|
62
|
+
self._registry = registry_response.json()
|
|
63
|
+
except json.JSONDecodeError:
|
|
64
|
+
raise RuntimeError(
|
|
65
|
+
"Could not parse JSON returned from the cloud registry. "
|
|
66
|
+
f"Consider re-trying with 'fallback' set to True when instantiating the {self.__class__.__name__} "
|
|
67
|
+
"object."
|
|
68
|
+
)
|
|
69
|
+
self._save_registry_as_cache()
|
|
70
|
+
|
|
71
|
+
def _load_registry_from_cache(self):
|
|
72
|
+
try:
|
|
73
|
+
with open(CACHE_FNAME, "r") as f:
|
|
74
|
+
self._registry = json.load(f)
|
|
75
|
+
except FileNotFoundError:
|
|
76
|
+
raise RuntimeError(f"Local registry cache not found. No file named {CACHE_FNAME}.") from None
|
|
77
|
+
|
|
78
|
+
try:
|
|
79
|
+
with open(CACHE_META_FNAME, "r") as f:
|
|
80
|
+
data = json.load(f)
|
|
81
|
+
date = dateutil.parser.isoparse(data["last_cache_date"])
|
|
82
|
+
except (FileNotFoundError, ValueError):
|
|
83
|
+
date = "Unknown"
|
|
84
|
+
|
|
85
|
+
print(f"Registry loaded from cache dating: {date}")
|
|
86
|
+
|
|
87
|
+
def _save_registry_as_cache(self):
|
|
88
|
+
cache_backup = CACHE_FNAME + ".backup"
|
|
89
|
+
cache_meta_backup = CACHE_META_FNAME + ".backup"
|
|
90
|
+
|
|
91
|
+
# Create cache parent directories if they don't exist
|
|
92
|
+
for cache_dir in {os.path.dirname(CACHE_FNAME), os.path.dirname(CACHE_META_FNAME)}:
|
|
93
|
+
os.makedirs(cache_dir, exist_ok=True)
|
|
94
|
+
|
|
95
|
+
# Suppressing a FileNotFoundError error for the first use case where a cached registry file
|
|
96
|
+
# does not exist
|
|
97
|
+
with contextlib.suppress(FileNotFoundError):
|
|
98
|
+
shutil.copy(CACHE_FNAME, cache_backup)
|
|
99
|
+
shutil.copy(CACHE_META_FNAME, cache_meta_backup)
|
|
100
|
+
|
|
101
|
+
try:
|
|
102
|
+
metadata = {"last_cache_date": datetime.datetime.now(tz=datetime.timezone.utc).isoformat()}
|
|
103
|
+
|
|
104
|
+
# Write the metadata
|
|
105
|
+
with open(CACHE_META_FNAME, "w") as f:
|
|
106
|
+
json.dump(metadata, f)
|
|
107
|
+
|
|
108
|
+
# write the registry
|
|
109
|
+
with open(CACHE_FNAME, "w") as f:
|
|
110
|
+
json.dump(self._registry, f)
|
|
111
|
+
|
|
112
|
+
except OSError as e:
|
|
113
|
+
# If either the cache file or the metadata file could not be written, then restore from backup files
|
|
114
|
+
shutil.copy(cache_backup, CACHE_FNAME)
|
|
115
|
+
shutil.copy(cache_meta_backup, CACHE_META_FNAME)
|
|
116
|
+
|
|
117
|
+
finally:
|
|
118
|
+
# Similarly, suppressing an error that I don't need to catch
|
|
119
|
+
with contextlib.suppress(FileNotFoundError):
|
|
120
|
+
os.remove(cache_backup)
|
|
121
|
+
os.remove(cache_meta_backup)
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
if __name__ == "__main__":
|
|
125
|
+
d = MarbleClient()
|
|
126
|
+
print(d.nodes)
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import os
|
|
2
|
+
|
|
3
|
+
from platformdirs import user_cache_dir
|
|
4
|
+
|
|
5
|
+
__all__ = ("NODE_REGISTRY_URL", "CACHE_FNAME", "CACHE_META_FNAME")
|
|
6
|
+
|
|
7
|
+
# Marble node registry URL
|
|
8
|
+
NODE_REGISTRY_URL: str = os.getenv(
|
|
9
|
+
"MARBLE_NODE_REGISTRY_URL",
|
|
10
|
+
"https://raw.githubusercontent.com/DACCS-Climate/DACCS-node-registry/current-registry/node_registry.json",
|
|
11
|
+
)
|
|
12
|
+
|
|
13
|
+
_CACHE_DIR: str = os.getenv("MARBLE_CACHE_DIR", user_cache_dir("marble_client_python"))
|
|
14
|
+
|
|
15
|
+
# location to write registry cache
|
|
16
|
+
CACHE_FNAME: str = os.path.join(_CACHE_DIR, "registry.cached.json")
|
|
17
|
+
|
|
18
|
+
# location to write metadata about the registry cache
|
|
19
|
+
CACHE_META_FNAME: str = os.path.join(_CACHE_DIR, "cache_metadata.json")
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import json
|
|
2
|
+
from datetime import datetime
|
|
3
|
+
|
|
4
|
+
import dateutil.parser
|
|
5
|
+
import requests
|
|
6
|
+
|
|
7
|
+
from marble_client.exceptions import ServiceNotAvailableError
|
|
8
|
+
from marble_client.services import MarbleService
|
|
9
|
+
|
|
10
|
+
__all__ = ["MarbleNode"]
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class MarbleNode:
|
|
14
|
+
def __init__(self, nodename: str, jsondata: dict[str]) -> None:
|
|
15
|
+
self._nodedata = jsondata
|
|
16
|
+
self._name = nodename
|
|
17
|
+
|
|
18
|
+
for item in jsondata["links"]:
|
|
19
|
+
setattr(self, "_links_" + item["rel"].replace("-", "_"), item["href"])
|
|
20
|
+
|
|
21
|
+
self._services: list[str] = []
|
|
22
|
+
|
|
23
|
+
for service in jsondata["services"]:
|
|
24
|
+
s = MarbleService(service)
|
|
25
|
+
setattr(self, s.name, s)
|
|
26
|
+
self._services.append(s.name)
|
|
27
|
+
|
|
28
|
+
def is_online(self) -> bool:
|
|
29
|
+
try:
|
|
30
|
+
registry = requests.get(self.url)
|
|
31
|
+
registry.raise_for_status()
|
|
32
|
+
return True
|
|
33
|
+
except (requests.exceptions.RequestException, requests.exceptions.ConnectionError):
|
|
34
|
+
return False
|
|
35
|
+
|
|
36
|
+
@property
|
|
37
|
+
def name(self) -> str:
|
|
38
|
+
return self._name
|
|
39
|
+
|
|
40
|
+
@property
|
|
41
|
+
def description(self) -> str:
|
|
42
|
+
return self._nodedata["description"]
|
|
43
|
+
|
|
44
|
+
@property
|
|
45
|
+
def url(self) -> str:
|
|
46
|
+
return self._links_service
|
|
47
|
+
|
|
48
|
+
@property
|
|
49
|
+
def collection_url(self) -> str:
|
|
50
|
+
return self._links_collection
|
|
51
|
+
|
|
52
|
+
@property
|
|
53
|
+
def version_url(self) -> str:
|
|
54
|
+
return self._links_version
|
|
55
|
+
|
|
56
|
+
@property
|
|
57
|
+
def date_added(self) -> datetime:
|
|
58
|
+
return dateutil.parser.isoparse(self._nodedata["date_added"])
|
|
59
|
+
|
|
60
|
+
@property
|
|
61
|
+
def affiliation(self) -> str:
|
|
62
|
+
return self._nodedata["affiliation"]
|
|
63
|
+
|
|
64
|
+
@property
|
|
65
|
+
def location(self) -> dict[str, float]:
|
|
66
|
+
return self._nodedata["location"]
|
|
67
|
+
|
|
68
|
+
@property
|
|
69
|
+
def contact(self) -> str:
|
|
70
|
+
return self._nodedata["contact"]
|
|
71
|
+
|
|
72
|
+
@property
|
|
73
|
+
def last_updated(self) -> datetime:
|
|
74
|
+
return dateutil.parser.isoparse(self._nodedata["last_updated"])
|
|
75
|
+
|
|
76
|
+
@property
|
|
77
|
+
def marble_version(self) -> str:
|
|
78
|
+
return self._nodedata["version"]
|
|
79
|
+
|
|
80
|
+
@property
|
|
81
|
+
def services(self) -> list[str]:
|
|
82
|
+
return self._services
|
|
83
|
+
|
|
84
|
+
def __getitem__(self, service: str) -> MarbleService:
|
|
85
|
+
"""Get a service at a node by specifying its name.
|
|
86
|
+
|
|
87
|
+
:param service: Name of the Marble service
|
|
88
|
+
:type service: str
|
|
89
|
+
:raises ServiceNotAvailable: This exception is raised if the service is not available at the node
|
|
90
|
+
:return: _description_
|
|
91
|
+
:rtype: Marbleservice
|
|
92
|
+
"""
|
|
93
|
+
try:
|
|
94
|
+
s = getattr(self, service)
|
|
95
|
+
return s
|
|
96
|
+
except AttributeError:
|
|
97
|
+
raise ServiceNotAvailableError() from None
|
|
98
|
+
|
|
99
|
+
def __contains__(self, service: str) -> bool:
|
|
100
|
+
"""Check if a service is available at a node
|
|
101
|
+
|
|
102
|
+
:param service: Name of the Marble service
|
|
103
|
+
:type service: str
|
|
104
|
+
:return: True if the service is available, False otherwise
|
|
105
|
+
:rtype: bool
|
|
106
|
+
"""
|
|
107
|
+
return service in self._services
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
from typing import Any
|
|
2
|
+
|
|
3
|
+
__all__ = ["MarbleService"]
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class MarbleService:
|
|
7
|
+
def __init__(self, servicejson: dict[str, Any]) -> None:
|
|
8
|
+
"""Constructor method
|
|
9
|
+
|
|
10
|
+
:param servicejson: A JSON representing the service according to the schema defined for the Marble node registry
|
|
11
|
+
:type servicejson: dict[str, Any]
|
|
12
|
+
"""
|
|
13
|
+
self._name = servicejson["name"]
|
|
14
|
+
self._keywords = servicejson["keywords"]
|
|
15
|
+
self._description = servicejson["description"]
|
|
16
|
+
|
|
17
|
+
self._service = None
|
|
18
|
+
self._service_doc = None
|
|
19
|
+
|
|
20
|
+
for item in servicejson["links"]:
|
|
21
|
+
setattr(self, "_" + item["rel"].replace("-", "_"), item["href"])
|
|
22
|
+
|
|
23
|
+
@property
|
|
24
|
+
def name(self) -> str:
|
|
25
|
+
"""Name of the service
|
|
26
|
+
|
|
27
|
+
:return: Name of the service
|
|
28
|
+
:rtype: str
|
|
29
|
+
"""
|
|
30
|
+
return self._name
|
|
31
|
+
|
|
32
|
+
@property
|
|
33
|
+
def keywords(self) -> list[str]:
|
|
34
|
+
"""Keywords associated with this service
|
|
35
|
+
|
|
36
|
+
:return: Keywords associated with this service
|
|
37
|
+
:rtype: list[str]
|
|
38
|
+
"""
|
|
39
|
+
return self._keywords
|
|
40
|
+
|
|
41
|
+
@property
|
|
42
|
+
def description(self) -> str:
|
|
43
|
+
"""A short description of this service
|
|
44
|
+
|
|
45
|
+
:return: A short description of this service
|
|
46
|
+
:rtype: str
|
|
47
|
+
"""
|
|
48
|
+
return self._description
|
|
49
|
+
|
|
50
|
+
@property
|
|
51
|
+
def url(self) -> str:
|
|
52
|
+
"""Access the URL for the service itself. Note: the preferred approach to access the service
|
|
53
|
+
URL is via just using the name of the MarbleService object.
|
|
54
|
+
|
|
55
|
+
E.g.::
|
|
56
|
+
|
|
57
|
+
s = MarbleService(jsondata)
|
|
58
|
+
s # this prints http://url-of-service
|
|
59
|
+
|
|
60
|
+
:return: Service URL
|
|
61
|
+
:rtype: str
|
|
62
|
+
"""
|
|
63
|
+
return self._service
|
|
64
|
+
|
|
65
|
+
@property
|
|
66
|
+
def doc_url(self) -> str:
|
|
67
|
+
return self._service_doc
|
|
68
|
+
|
|
69
|
+
def __str__(self) -> str:
|
|
70
|
+
return f"Marble service: {self.name}\n"
|
|
71
|
+
|
|
72
|
+
def __repr__(self) -> str:
|
|
73
|
+
return self._service
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: marble-client
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: A python client to access information about the Marble climate infomatics network
|
|
5
|
+
Author-email: Deepak Chandan <dchandan@cs.toronto.edu>
|
|
6
|
+
License: MIT License
|
|
7
|
+
|
|
8
|
+
Copyright (c) 2023 Data Analytics for Canadian Climate Services
|
|
9
|
+
|
|
10
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
11
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
12
|
+
in the Software without restriction, including without limitation the rights
|
|
13
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
14
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
15
|
+
furnished to do so, subject to the following conditions:
|
|
16
|
+
|
|
17
|
+
The above copyright notice and this permission notice shall be included in all
|
|
18
|
+
copies or substantial portions of the Software.
|
|
19
|
+
|
|
20
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
21
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
22
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
23
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
24
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
25
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
26
|
+
SOFTWARE.
|
|
27
|
+
|
|
28
|
+
Project-URL: Bug Tracker, https://github.com/DACCS-Climate/marble_client_python/issues
|
|
29
|
+
Project-URL: Homepage, https://github.com/DACCS-Climate/marble_client_python
|
|
30
|
+
Keywords: climate,climate infomatics,climate data,DACCS,Marble
|
|
31
|
+
Classifier: Programming Language :: Python :: 3
|
|
32
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
33
|
+
Classifier: Operating System :: OS Independent
|
|
34
|
+
Requires-Python: >=3.9
|
|
35
|
+
Description-Content-Type: text/markdown
|
|
36
|
+
License-File: LICENSE
|
|
37
|
+
|
|
38
|
+
# Marble python client
|
|
39
|
+
A python library to access information about the Marble climate infomatics network. The library provides a pythonic interface to the Marble network's [central registry](https://github.com/DACCS-Climate/Marble-node-registry). Users of the network are encouraged to use this library to access the network information and avoid hardcoding URLs to various nodes or services.
|
|
40
|
+
|
|
41
|
+
## Installation
|
|
42
|
+
|
|
43
|
+
To install `marble_client` issue this command:
|
|
44
|
+
```shell
|
|
45
|
+
pip install marble_client
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## Basic usage
|
|
49
|
+
|
|
50
|
+
The first thing to do is to get a `client` object:
|
|
51
|
+
|
|
52
|
+
```python
|
|
53
|
+
>>> from marble_client import MarbleClient
|
|
54
|
+
|
|
55
|
+
>>> client = MarbleClient()
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
All the information about the network can now be retrieved from the `client` object. E.g. the nodes available in the network can be accessed as:
|
|
59
|
+
```python
|
|
60
|
+
>>> client.nodes
|
|
61
|
+
{'UofT': <marble_client.node.MarbleNode at 0x10c129990>,
|
|
62
|
+
'PAVICS': <marble_client.node.MarbleNode at 0x10c6dd690>,
|
|
63
|
+
'Hirondelle': <marble_client.node.MarbleNode at 0x10c6dd890>}
|
|
64
|
+
```
|
|
65
|
+
The returned object is a python `dict` with node names for keys and `MarbleNode` objects as values. A particular node can be accessed as:
|
|
66
|
+
|
|
67
|
+
```python
|
|
68
|
+
>>> mynode = client['UofT']
|
|
69
|
+
>>> type(mynode)
|
|
70
|
+
marble_client.node.MarbleNode
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
Now that one has a Marble node of interest, a useful operation would be to check if that node is online in realtime, this can be done as:
|
|
74
|
+
|
|
75
|
+
```python
|
|
76
|
+
>>> mynode.is_online()
|
|
77
|
+
True
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
The URL for the node can be retrieved as:
|
|
81
|
+
```python
|
|
82
|
+
>>> mynode.url
|
|
83
|
+
'https://daccs.cs.toronto.edu'
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
Various other qualities about the node can be accessed as shown below (see [implementation](https://github.com/DACCS-Climate/marble_client_python/blob/main/marble_client/node.py) for the full list of available attributes).
|
|
87
|
+
|
|
88
|
+
```python
|
|
89
|
+
>>> mynode.affiliation
|
|
90
|
+
'University of Toronto'
|
|
91
|
+
>>> mynode.contact
|
|
92
|
+
'daccs-info@cs.toronto.edu'
|
|
93
|
+
>>> mynode.marble_version # The version of the software stack available on this node
|
|
94
|
+
'1.27.0'
|
|
95
|
+
>>> mynode.location
|
|
96
|
+
{'longitude': -79.39, 'latitude': 43.65}
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
The "services" that a Marble node offers can differ from one node to another. A list of which services are offered at a given node can be inquired as follows:
|
|
100
|
+
```python
|
|
101
|
+
>>> mynode.services
|
|
102
|
+
['geoserver',
|
|
103
|
+
'flyingpigeon',
|
|
104
|
+
'finch',
|
|
105
|
+
'raven',
|
|
106
|
+
'hummingbird',
|
|
107
|
+
'thredds',
|
|
108
|
+
'jupyterhub',
|
|
109
|
+
'weaver']
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
To get further information on one of the services, first retrieve that service. This can be done in one of two ways:
|
|
113
|
+
```python
|
|
114
|
+
>>> service = mynode['thredds']
|
|
115
|
+
>>> type(service)
|
|
116
|
+
marble_client.services.MarbleService
|
|
117
|
+
>>>
|
|
118
|
+
>>> service = mynode.thredds
|
|
119
|
+
>>> type(service)
|
|
120
|
+
marble_client.services.MarbleService
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
The most important thing one needs from the service is the endpoint at which the service is located:
|
|
124
|
+
```python
|
|
125
|
+
>>> service.url
|
|
126
|
+
'https://daccs.cs.toronto.edu/thredds/'
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
The service URL can also be accessed directly using the service object's name:
|
|
130
|
+
```python
|
|
131
|
+
>>> service
|
|
132
|
+
'https://daccs.cs.toronto.edu/thredds/'
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
Various attributes that can be accessed on the `MarbleService` object can be found by consulting the [implementation](https://github.com/DACCS-Climate/marble_client_python/blob/main/marble_client/services.py).
|
|
136
|
+
|
|
137
|
+
Of course, all operations can be chained, so if you don't need `MarbleClient`, `MarbleNode` or `MarbleService` objects for future operations, then to get, for example, the weaver service endpoint for the "PAVICS" node, one can do:
|
|
138
|
+
```python
|
|
139
|
+
>>> url = MarbleClient()["PAVICS"].weaver.url # returns a string object
|
|
140
|
+
>>> print(f"Weaver URL is {url}")
|
|
141
|
+
Weaver URL is https://pavics.ouranos.ca/weaver/
|
|
142
|
+
>>> # A MarbleService object is returned that can be used wherever a string can be used
|
|
143
|
+
>>> print(f"Weaver URL is {MarbleClient()['PAVICS'].weaver}")
|
|
144
|
+
Weaver URL is https://pavics.ouranos.ca/weaver/
|
|
145
|
+
```
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
README.md
|
|
3
|
+
pyproject.toml
|
|
4
|
+
requirements.txt
|
|
5
|
+
marble_client/__init__.py
|
|
6
|
+
marble_client/client.py
|
|
7
|
+
marble_client/constants.py
|
|
8
|
+
marble_client/exceptions.py
|
|
9
|
+
marble_client/node.py
|
|
10
|
+
marble_client/services.py
|
|
11
|
+
marble_client.egg-info/PKG-INFO
|
|
12
|
+
marble_client.egg-info/SOURCES.txt
|
|
13
|
+
marble_client.egg-info/dependency_links.txt
|
|
14
|
+
marble_client.egg-info/requires.txt
|
|
15
|
+
marble_client.egg-info/top_level.txt
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
marble_client
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
build-backend = "setuptools.build_meta"
|
|
3
|
+
requires = ["setuptools>=61.0"]
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
authors = [{name = "Deepak Chandan", email = "dchandan@cs.toronto.edu"}]
|
|
7
|
+
classifiers = [
|
|
8
|
+
"Programming Language :: Python :: 3",
|
|
9
|
+
"License :: OSI Approved :: MIT License",
|
|
10
|
+
"Operating System :: OS Independent",
|
|
11
|
+
]
|
|
12
|
+
description = "A python client to access information about the Marble climate infomatics network"
|
|
13
|
+
dynamic = ["dependencies"]
|
|
14
|
+
keywords = ["climate", "climate infomatics", "climate data", "DACCS", "Marble"]
|
|
15
|
+
license = {file = "LICENSE"}
|
|
16
|
+
name = "marble_client"
|
|
17
|
+
readme = "README.md"
|
|
18
|
+
requires-python = ">=3.9"
|
|
19
|
+
version = "1.0.0"
|
|
20
|
+
|
|
21
|
+
[project.urls]
|
|
22
|
+
# Homepage will change to Marble homepage when that goes live
|
|
23
|
+
"Bug Tracker" = "https://github.com/DACCS-Climate/marble_client_python/issues"
|
|
24
|
+
"Homepage" = "https://github.com/DACCS-Climate/marble_client_python"
|
|
25
|
+
|
|
26
|
+
[tool.setuptools]
|
|
27
|
+
packages = ["marble_client"]
|
|
28
|
+
|
|
29
|
+
[tool.setuptools.dynamic]
|
|
30
|
+
dependencies = {file = ["requirements.txt"]}
|
|
31
|
+
|
|
32
|
+
[tool.black]
|
|
33
|
+
line-length = 120
|