datablob 0.9.3__tar.gz → 0.11.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.
- {datablob-0.9.3 → datablob-0.11.0}/PKG-INFO +19 -2
- {datablob-0.9.3 → datablob-0.11.0}/README.md +12 -1
- {datablob-0.9.3 → datablob-0.11.0}/datablob/__init__.py +129 -80
- datablob-0.11.0/datablob/storage_client.py +77 -0
- {datablob-0.9.3 → datablob-0.11.0}/datablob.egg-info/PKG-INFO +19 -2
- {datablob-0.9.3 → datablob-0.11.0}/datablob.egg-info/SOURCES.txt +4 -1
- {datablob-0.9.3 → datablob-0.11.0}/datablob.egg-info/requires.txt +7 -0
- {datablob-0.9.3 → datablob-0.11.0}/setup.py +11 -6
- datablob-0.11.0/tests/test_client.py +28 -0
- datablob-0.11.0/tests/test_storage_client.py +103 -0
- {datablob-0.9.3 → datablob-0.11.0}/LICENSE +0 -0
- {datablob-0.9.3 → datablob-0.11.0}/datablob.egg-info/dependency_links.txt +0 -0
- {datablob-0.9.3 → datablob-0.11.0}/datablob.egg-info/top_level.txt +0 -0
- {datablob-0.9.3 → datablob-0.11.0}/setup.cfg +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: datablob
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.11.0
|
|
4
4
|
Summary: Client for Updating a Simple Data Warehouse on Blob Storage
|
|
5
5
|
Home-page: https://github.com/gocarta/datablob
|
|
6
6
|
Download-URL: https://github.com/gocarta/datablob/tarball/download
|
|
@@ -19,7 +19,12 @@ Requires-Dist: openpyxl
|
|
|
19
19
|
Requires-Dist: pandas
|
|
20
20
|
Requires-Dist: pyarrow
|
|
21
21
|
Requires-Dist: shapely
|
|
22
|
+
Requires-Dist: tylertoo
|
|
22
23
|
Requires-Dist: tzdata
|
|
24
|
+
Provides-Extra: gcs
|
|
25
|
+
Requires-Dist: google-cloud-storage; extra == "gcs"
|
|
26
|
+
Provides-Extra: dev
|
|
27
|
+
Requires-Dist: pytest; extra == "dev"
|
|
23
28
|
Dynamic: author
|
|
24
29
|
Dynamic: author-email
|
|
25
30
|
Dynamic: classifier
|
|
@@ -29,6 +34,7 @@ Dynamic: download-url
|
|
|
29
34
|
Dynamic: home-page
|
|
30
35
|
Dynamic: keywords
|
|
31
36
|
Dynamic: license-file
|
|
37
|
+
Dynamic: provides-extra
|
|
32
38
|
Dynamic: requires-dist
|
|
33
39
|
Dynamic: summary
|
|
34
40
|
|
|
@@ -49,6 +55,11 @@ Client for Updating a Simple Data Warehouse on Blob Storage
|
|
|
49
55
|
pip install datablob
|
|
50
56
|
```
|
|
51
57
|
|
|
58
|
+
To include Google Cloud Storage (GCS) support:
|
|
59
|
+
```sh
|
|
60
|
+
pip install datablob[gcs]
|
|
61
|
+
```
|
|
62
|
+
|
|
52
63
|
## supported formats
|
|
53
64
|
- csv
|
|
54
65
|
- [geojson (points and polygons)](https://geojson.org/)
|
|
@@ -63,10 +74,16 @@ pip install datablob
|
|
|
63
74
|
```py
|
|
64
75
|
from datablob import DataBlobClient
|
|
65
76
|
|
|
77
|
+
# AWS S3 (default)
|
|
66
78
|
client = DataBlobClient(
|
|
67
79
|
bucket_name="example-test-bucket-123", bucket_path="prefix/to/dataportal"
|
|
68
80
|
)
|
|
69
81
|
|
|
82
|
+
# Google Cloud Storage (GCS)
|
|
83
|
+
client = DataBlobClient(
|
|
84
|
+
bucket_name="example-gcs-bucket-123", bucket_path="prefix/to/dataportal", provider="gcs"
|
|
85
|
+
)
|
|
86
|
+
|
|
70
87
|
rows = [
|
|
71
88
|
{
|
|
72
89
|
"name": "Random Building",
|
|
@@ -81,7 +98,7 @@ client.update_dataset(
|
|
|
81
98
|
version="2", # version of the dataset. It's a string, so you can version as you like.
|
|
82
99
|
data=rows, # list of dictionaries. Each row is a dictionary.
|
|
83
100
|
latitude_key="@lat", # name of the latitude column
|
|
84
|
-
|
|
101
|
+
longitude_key="@lon", # name of the longitude column
|
|
85
102
|
polygon_key="@geom", # name of the column with a polygon geometry in it (if applicable)
|
|
86
103
|
xlsx=True # set to True to include an Excel file in output
|
|
87
104
|
)
|
|
@@ -15,6 +15,11 @@ Client for Updating a Simple Data Warehouse on Blob Storage
|
|
|
15
15
|
pip install datablob
|
|
16
16
|
```
|
|
17
17
|
|
|
18
|
+
To include Google Cloud Storage (GCS) support:
|
|
19
|
+
```sh
|
|
20
|
+
pip install datablob[gcs]
|
|
21
|
+
```
|
|
22
|
+
|
|
18
23
|
## supported formats
|
|
19
24
|
- csv
|
|
20
25
|
- [geojson (points and polygons)](https://geojson.org/)
|
|
@@ -29,10 +34,16 @@ pip install datablob
|
|
|
29
34
|
```py
|
|
30
35
|
from datablob import DataBlobClient
|
|
31
36
|
|
|
37
|
+
# AWS S3 (default)
|
|
32
38
|
client = DataBlobClient(
|
|
33
39
|
bucket_name="example-test-bucket-123", bucket_path="prefix/to/dataportal"
|
|
34
40
|
)
|
|
35
41
|
|
|
42
|
+
# Google Cloud Storage (GCS)
|
|
43
|
+
client = DataBlobClient(
|
|
44
|
+
bucket_name="example-gcs-bucket-123", bucket_path="prefix/to/dataportal", provider="gcs"
|
|
45
|
+
)
|
|
46
|
+
|
|
36
47
|
rows = [
|
|
37
48
|
{
|
|
38
49
|
"name": "Random Building",
|
|
@@ -47,7 +58,7 @@ client.update_dataset(
|
|
|
47
58
|
version="2", # version of the dataset. It's a string, so you can version as you like.
|
|
48
59
|
data=rows, # list of dictionaries. Each row is a dictionary.
|
|
49
60
|
latitude_key="@lat", # name of the latitude column
|
|
50
|
-
|
|
61
|
+
longitude_key="@lon", # name of the longitude column
|
|
51
62
|
polygon_key="@geom", # name of the column with a polygon geometry in it (if applicable)
|
|
52
63
|
xlsx=True # set to True to include an Excel file in output
|
|
53
64
|
)
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import boto3
|
|
2
1
|
import csv
|
|
3
2
|
from datetime import datetime
|
|
4
3
|
import geopandas as gpd
|
|
@@ -10,10 +9,17 @@ import os
|
|
|
10
9
|
import pandas as pd
|
|
11
10
|
from shapely.geometry import shape
|
|
12
11
|
import tempfile
|
|
13
|
-
import
|
|
12
|
+
import tylertoo
|
|
13
|
+
from typing import Literal, TypeAlias
|
|
14
14
|
import zipfile
|
|
15
15
|
from zoneinfo import ZoneInfo
|
|
16
16
|
|
|
17
|
+
from datablob.storage_client import (
|
|
18
|
+
GcsClient as GcsClient,
|
|
19
|
+
S3Client,
|
|
20
|
+
StorageClientProtocol,
|
|
21
|
+
)
|
|
22
|
+
|
|
17
23
|
POSSIBLE_LATITUDE_KEYS = [
|
|
18
24
|
"LATITUDE",
|
|
19
25
|
"Latitude",
|
|
@@ -37,12 +43,41 @@ POSSIBLE_LONGITUDE_KEYS = [
|
|
|
37
43
|
"@lon",
|
|
38
44
|
]
|
|
39
45
|
|
|
46
|
+
Provider: TypeAlias = Literal["s3", "gcs"]
|
|
47
|
+
VALID_PROVIDERS: tuple[Provider, ...] = ("s3", "gcs")
|
|
48
|
+
|
|
40
49
|
|
|
41
50
|
class DataBlobClient:
|
|
42
|
-
|
|
51
|
+
storage_client: StorageClientProtocol
|
|
52
|
+
|
|
53
|
+
def __init__(
|
|
54
|
+
self,
|
|
55
|
+
bucket_name,
|
|
56
|
+
bucket_path,
|
|
57
|
+
timezones=None,
|
|
58
|
+
provider: Provider = "s3",
|
|
59
|
+
storage_client: StorageClientProtocol | None = None,
|
|
60
|
+
):
|
|
61
|
+
if provider not in VALID_PROVIDERS:
|
|
62
|
+
raise ValueError(
|
|
63
|
+
f"Unsupported provider '{provider}'. Must be 's3' or 'gcs'."
|
|
64
|
+
)
|
|
65
|
+
|
|
43
66
|
self.bucket_name = bucket_name
|
|
44
67
|
self.bucket_path = bucket_path.rstrip("/")
|
|
45
68
|
self.timezones = timezones or ["UTC"]
|
|
69
|
+
self.provider = provider
|
|
70
|
+
|
|
71
|
+
if storage_client is not None:
|
|
72
|
+
self.storage_client = storage_client
|
|
73
|
+
elif self.provider == "s3":
|
|
74
|
+
self.storage_client = S3Client(self.bucket_name)
|
|
75
|
+
elif self.provider == "gcs":
|
|
76
|
+
self.storage_client = GcsClient(self.bucket_name)
|
|
77
|
+
else:
|
|
78
|
+
raise ValueError(
|
|
79
|
+
f"Unsupported provider '{self.provider}'. Must be 's3' or 'gcs'."
|
|
80
|
+
)
|
|
46
81
|
|
|
47
82
|
def _get_unique_keys(self, rows):
|
|
48
83
|
columns = set()
|
|
@@ -64,23 +99,21 @@ class DataBlobClient:
|
|
|
64
99
|
|
|
65
100
|
def get_filenames_by_dataset_and_version(self):
|
|
66
101
|
results = {}
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
if
|
|
75
|
-
|
|
76
|
-
if
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
results[dataset_id][version] = []
|
|
83
|
-
results[dataset_id][version].append(filename)
|
|
102
|
+
keys = self.storage_client.list_keys(prefix=self.bucket_path)
|
|
103
|
+
prefix_len = len(self.bucket_path) + 1 if self.bucket_path else 0
|
|
104
|
+
for full_key in keys:
|
|
105
|
+
key = full_key[prefix_len:] if prefix_len else full_key
|
|
106
|
+
|
|
107
|
+
if len(key.split("/")) == 3:
|
|
108
|
+
[dataset_id, version, filename] = key.split("/")
|
|
109
|
+
if version.startswith("v"):
|
|
110
|
+
version = version[1:]
|
|
111
|
+
if filename:
|
|
112
|
+
if dataset_id not in results:
|
|
113
|
+
results[dataset_id] = {}
|
|
114
|
+
if version not in results[dataset_id]:
|
|
115
|
+
results[dataset_id][version] = []
|
|
116
|
+
results[dataset_id][version].append(filename)
|
|
84
117
|
|
|
85
118
|
for dataset_id, subdict in results.items():
|
|
86
119
|
versions = list(subdict.keys())
|
|
@@ -96,48 +129,36 @@ class DataBlobClient:
|
|
|
96
129
|
key = (
|
|
97
130
|
self.bucket_path + "/" + dataset_name + "/v" + dataset_version + "/data.csv"
|
|
98
131
|
)
|
|
99
|
-
|
|
100
|
-
Bucket=self.bucket_name,
|
|
101
|
-
Key=key,
|
|
102
|
-
Body=data,
|
|
103
|
-
)
|
|
132
|
+
self.storage_client.put_object(key, data)
|
|
104
133
|
|
|
105
134
|
def upload_tsv(self, dataset_name, dataset_version, data):
|
|
106
135
|
key = (
|
|
107
136
|
self.bucket_path + "/" + dataset_name + "/v" + dataset_version + "/data.tsv"
|
|
108
137
|
)
|
|
109
|
-
|
|
110
|
-
Bucket=self.bucket_name,
|
|
111
|
-
Key=key,
|
|
112
|
-
Body=data,
|
|
113
|
-
)
|
|
138
|
+
self.storage_client.put_object(key, data)
|
|
114
139
|
|
|
115
140
|
def get_dataset_as_csv(self, name, version, remove_bom=True):
|
|
116
141
|
key = self.bucket_path + "/" + name + "/v" + version + "/data.csv"
|
|
117
|
-
|
|
118
|
-
object_content = response["Body"].read().decode("utf-8")
|
|
142
|
+
object_content = self.storage_client.get_object_bytes(key).decode("utf-8")
|
|
119
143
|
if remove_bom:
|
|
120
144
|
object_content = object_content.lstrip("\ufeff")
|
|
121
145
|
return object_content
|
|
122
146
|
|
|
123
147
|
def get_dataset_as_tsv(self, name, version, remove_bom=True):
|
|
124
148
|
key = self.bucket_path + "/" + name + "/v" + version + "/data.tsv"
|
|
125
|
-
|
|
126
|
-
object_content = response["Body"].read().decode("utf-8")
|
|
149
|
+
object_content = self.storage_client.get_object_bytes(key).decode("utf-8")
|
|
127
150
|
if remove_bom:
|
|
128
151
|
object_content = object_content.lstrip("\ufeff")
|
|
129
152
|
return object_content
|
|
130
153
|
|
|
131
154
|
def get_dataset_as_json(self, name, version):
|
|
132
155
|
key = self.bucket_path + "/" + name + "/v" + version + "/data.json"
|
|
133
|
-
|
|
134
|
-
object_content = response["Body"].read().decode("utf-8")
|
|
156
|
+
object_content = self.storage_client.get_object_bytes(key).decode("utf-8")
|
|
135
157
|
return json_loads(object_content)
|
|
136
158
|
|
|
137
159
|
def get_dataset_metadata(self, name: str, version: str):
|
|
138
160
|
key = self.bucket_path + "/" + name + "/v" + version + "/meta.json"
|
|
139
|
-
|
|
140
|
-
object_content = response["Body"].read().decode("utf-8")
|
|
161
|
+
object_content = self.storage_client.get_object_bytes(key).decode("utf-8")
|
|
141
162
|
return json_loads(object_content)
|
|
142
163
|
|
|
143
164
|
def upload_geojson_points(self, dataset_name, dataset_version, data):
|
|
@@ -149,10 +170,9 @@ class DataBlobClient:
|
|
|
149
170
|
+ dataset_version
|
|
150
171
|
+ "/data.points.geojson"
|
|
151
172
|
)
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
Body=data if isinstance(data, str) else json_dumps(data),
|
|
173
|
+
self.storage_client.put_object(
|
|
174
|
+
key,
|
|
175
|
+
data if isinstance(data, str) else json_dumps(data),
|
|
156
176
|
)
|
|
157
177
|
|
|
158
178
|
def upload_geojson_polygons(self, dataset_name, dataset_version, data):
|
|
@@ -164,10 +184,9 @@ class DataBlobClient:
|
|
|
164
184
|
+ dataset_version
|
|
165
185
|
+ "/data.polygons.geojson"
|
|
166
186
|
)
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
Body=data if isinstance(data, str) else json_dumps(data),
|
|
187
|
+
self.storage_client.put_object(
|
|
188
|
+
key,
|
|
189
|
+
data if isinstance(data, str) else json_dumps(data),
|
|
171
190
|
)
|
|
172
191
|
|
|
173
192
|
def upload_shapefile_points(self, dataset_name, dataset_version, blob):
|
|
@@ -179,11 +198,7 @@ class DataBlobClient:
|
|
|
179
198
|
+ dataset_version
|
|
180
199
|
+ "/data.points.shp.zip"
|
|
181
200
|
)
|
|
182
|
-
|
|
183
|
-
Bucket=self.bucket_name,
|
|
184
|
-
Key=key,
|
|
185
|
-
Body=blob,
|
|
186
|
-
)
|
|
201
|
+
self.storage_client.put_object(key, blob)
|
|
187
202
|
|
|
188
203
|
def upload_shapefile_polygons(self, dataset_name, dataset_version, blob):
|
|
189
204
|
key = (
|
|
@@ -194,11 +209,7 @@ class DataBlobClient:
|
|
|
194
209
|
+ dataset_version
|
|
195
210
|
+ "/data.polygons.shp.zip"
|
|
196
211
|
)
|
|
197
|
-
|
|
198
|
-
Bucket=self.bucket_name,
|
|
199
|
-
Key=key,
|
|
200
|
-
Body=blob,
|
|
201
|
-
)
|
|
212
|
+
self.storage_client.put_object(key, blob)
|
|
202
213
|
|
|
203
214
|
def upload_jsonl(self, dataset_name, dataset_version, data):
|
|
204
215
|
key = (
|
|
@@ -214,11 +225,7 @@ class DataBlobClient:
|
|
|
214
225
|
for row in data:
|
|
215
226
|
results += json_dumps(row) + "\n"
|
|
216
227
|
|
|
217
|
-
|
|
218
|
-
Bucket=self.bucket_name,
|
|
219
|
-
Key=key,
|
|
220
|
-
Body=results,
|
|
221
|
-
)
|
|
228
|
+
self.storage_client.put_object(key, results)
|
|
222
229
|
|
|
223
230
|
def upload_json(self, dataset_name, dataset_version, data):
|
|
224
231
|
key = (
|
|
@@ -229,10 +236,9 @@ class DataBlobClient:
|
|
|
229
236
|
+ dataset_version
|
|
230
237
|
+ "/data.json"
|
|
231
238
|
)
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
Body=data if isinstance(data, str) else json_dumps(data),
|
|
239
|
+
self.storage_client.put_object(
|
|
240
|
+
key,
|
|
241
|
+
data if isinstance(data, str) else json_dumps(data),
|
|
236
242
|
)
|
|
237
243
|
|
|
238
244
|
def upload_parquet(self, dataset_name, dataset_version, data):
|
|
@@ -244,11 +250,18 @@ class DataBlobClient:
|
|
|
244
250
|
+ dataset_version
|
|
245
251
|
+ "/data.parquet"
|
|
246
252
|
)
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
253
|
+
self.storage_client.put_object(key, data)
|
|
254
|
+
|
|
255
|
+
def upload_pmtiles(self, dataset_name, dataset_version, data):
|
|
256
|
+
key = (
|
|
257
|
+
self.bucket_path
|
|
258
|
+
+ "/"
|
|
259
|
+
+ dataset_name
|
|
260
|
+
+ "/v"
|
|
261
|
+
+ dataset_version
|
|
262
|
+
+ "/data.pmtiles"
|
|
251
263
|
)
|
|
264
|
+
self.storage_client.put_object(key, data)
|
|
252
265
|
|
|
253
266
|
def upload_xlsx(self, dataset_name, dataset_version, data):
|
|
254
267
|
key = (
|
|
@@ -259,11 +272,7 @@ class DataBlobClient:
|
|
|
259
272
|
+ dataset_version
|
|
260
273
|
+ "/data.xlsx"
|
|
261
274
|
)
|
|
262
|
-
|
|
263
|
-
Bucket=self.bucket_name,
|
|
264
|
-
Key=key,
|
|
265
|
-
Body=data,
|
|
266
|
-
)
|
|
275
|
+
self.storage_client.put_object(key, data)
|
|
267
276
|
|
|
268
277
|
def upload_metadata(self, dataset_name, dataset_version, data):
|
|
269
278
|
key = (
|
|
@@ -274,17 +283,16 @@ class DataBlobClient:
|
|
|
274
283
|
+ dataset_version
|
|
275
284
|
+ "/meta.json"
|
|
276
285
|
)
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
Body=data if isinstance(data, str) else json_dumps(data, indent=4),
|
|
286
|
+
self.storage_client.put_object(
|
|
287
|
+
key,
|
|
288
|
+
data if isinstance(data, str) else json_dumps(data, indent=4),
|
|
281
289
|
)
|
|
282
290
|
|
|
283
291
|
def convert_gdf_to_shapefile(self, gdf):
|
|
284
292
|
buf = io.BytesIO()
|
|
285
293
|
|
|
286
294
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
287
|
-
path = os.path.join(tmpdir,
|
|
295
|
+
path = os.path.join(tmpdir, "data.shp")
|
|
288
296
|
gdf.to_file(path, driver="ESRI Shapefile")
|
|
289
297
|
|
|
290
298
|
# copy temp files into in-memory zip file
|
|
@@ -425,6 +433,12 @@ class DataBlobClient:
|
|
|
425
433
|
jsonl: bool = True,
|
|
426
434
|
geojson: bool = True,
|
|
427
435
|
parquet: bool = True,
|
|
436
|
+
pmtiles: bool = False,
|
|
437
|
+
pmtiles_coalesce_lines: bool | None = None,
|
|
438
|
+
pmtiles_collapse_square: bool = False,
|
|
439
|
+
pmtiles_density_drop: bool | None = None,
|
|
440
|
+
pmtiles_simplify_factor: float | None = None,
|
|
441
|
+
pmtiles_max_zoom: int | Literal["auto"] = "auto",
|
|
428
442
|
xlsx: bool = False,
|
|
429
443
|
xlsx_data_types: list[str] | None = None,
|
|
430
444
|
):
|
|
@@ -447,8 +461,9 @@ class DataBlobClient:
|
|
|
447
461
|
data_as_csv = self.convert_rows_to_csv(flat_rows, fieldnames=columns)
|
|
448
462
|
data_as_tsv = self.convert_rows_to_tsv(flat_rows, fieldnames=columns)
|
|
449
463
|
|
|
450
|
-
df = pd.DataFrame(data)
|
|
464
|
+
df = pd.DataFrame(data, columns=columns)
|
|
451
465
|
|
|
466
|
+
gdf = None
|
|
452
467
|
data_as_geojson_points = None
|
|
453
468
|
data_as_geojson_polygons = None
|
|
454
469
|
shapefile_points_blob = None
|
|
@@ -520,6 +535,36 @@ class DataBlobClient:
|
|
|
520
535
|
self.upload_xlsx(name, version, data_as_xlsx)
|
|
521
536
|
meta["files"].append({"filename": "data.xlsx", "format": "Excel"})
|
|
522
537
|
|
|
538
|
+
if pmtiles:
|
|
539
|
+
if data_as_parquet_blob is False:
|
|
540
|
+
raise Exception("can't create PMTiles without geoparquet version")
|
|
541
|
+
|
|
542
|
+
with tempfile.TemporaryDirectory() as tmpdir:
|
|
543
|
+
parquet_path = os.path.join(tmpdir, "data.parquet")
|
|
544
|
+
overview_path = os.path.join(tmpdir, "overview.parquet")
|
|
545
|
+
pmtiles_path = os.path.join(tmpdir, "data.pmtiles")
|
|
546
|
+
|
|
547
|
+
with open(parquet_path, "wb") as f:
|
|
548
|
+
f.write(data_as_parquet_blob)
|
|
549
|
+
|
|
550
|
+
# create parquet file with overviews from parquet file without overviews
|
|
551
|
+
tylertoo.overview(
|
|
552
|
+
parquet_path,
|
|
553
|
+
overview_path,
|
|
554
|
+
max_zoom=pmtiles_max_zoom,
|
|
555
|
+
collapse_square=pmtiles_collapse_square,
|
|
556
|
+
density_drop=pmtiles_density_drop,
|
|
557
|
+
coalesce_lines=pmtiles_coalesce_lines,
|
|
558
|
+
simplify_factor=pmtiles_simplify_factor,
|
|
559
|
+
)
|
|
560
|
+
|
|
561
|
+
# convert parquet file with overviews to a pmtiles file
|
|
562
|
+
tylertoo.export_pmtiles(overview_path, pmtiles_path)
|
|
563
|
+
|
|
564
|
+
# read pmtiles into memory
|
|
565
|
+
with open(pmtiles_path, "rb") as f:
|
|
566
|
+
data_as_pmtiles_blob = f.read()
|
|
567
|
+
|
|
523
568
|
self.upload_csv(name, version, data_as_csv)
|
|
524
569
|
meta["files"].append({"filename": "data.csv", "format": "CSV"})
|
|
525
570
|
|
|
@@ -562,4 +607,8 @@ class DataBlobClient:
|
|
|
562
607
|
self.upload_parquet(name, version, data_as_parquet_blob)
|
|
563
608
|
meta["files"].append({"filename": "data.parquet", "format": "Parquet"})
|
|
564
609
|
|
|
610
|
+
if pmtiles and data_as_pmtiles_blob:
|
|
611
|
+
self.upload_pmtiles(name, version, data_as_pmtiles_blob)
|
|
612
|
+
meta["files"].append({"filename": "data.pmtiles", "format": "PMTiles"})
|
|
613
|
+
|
|
565
614
|
self.upload_metadata(name, version, meta)
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import io
|
|
2
|
+
from typing import Protocol
|
|
3
|
+
|
|
4
|
+
import boto3
|
|
5
|
+
|
|
6
|
+
BlobBody = str | bytes | io.BytesIO | io.StringIO
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class StorageClientProtocol(Protocol):
|
|
10
|
+
def put_object(self, key: str, body: BlobBody) -> None: ...
|
|
11
|
+
|
|
12
|
+
def get_object_bytes(self, key: str) -> bytes: ...
|
|
13
|
+
|
|
14
|
+
def list_keys(self, prefix: str) -> list[str]: ...
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class S3Client:
|
|
18
|
+
def __init__(self, bucket_name: str):
|
|
19
|
+
self.bucket_name = bucket_name
|
|
20
|
+
self.client = boto3.client("s3")
|
|
21
|
+
|
|
22
|
+
def put_object(self, key: str, body: BlobBody) -> None:
|
|
23
|
+
if isinstance(body, (io.BytesIO, io.StringIO)):
|
|
24
|
+
data = body.getvalue()
|
|
25
|
+
else:
|
|
26
|
+
data = body
|
|
27
|
+
self.client.put_object(
|
|
28
|
+
Bucket=self.bucket_name,
|
|
29
|
+
Key=key,
|
|
30
|
+
Body=data,
|
|
31
|
+
)
|
|
32
|
+
|
|
33
|
+
def get_object_bytes(self, key: str) -> bytes:
|
|
34
|
+
response = self.client.get_object(Bucket=self.bucket_name, Key=key)
|
|
35
|
+
return response["Body"].read()
|
|
36
|
+
|
|
37
|
+
def list_keys(self, prefix: str) -> list[str]:
|
|
38
|
+
keys = []
|
|
39
|
+
response = self.client.list_objects_v2(Bucket=self.bucket_name, Prefix=prefix)
|
|
40
|
+
if "Contents" in response:
|
|
41
|
+
for obj in response["Contents"]:
|
|
42
|
+
keys.append(obj["Key"])
|
|
43
|
+
return keys
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
class GcsClient:
|
|
47
|
+
def __init__(self, bucket_name: str):
|
|
48
|
+
self.bucket_name = bucket_name
|
|
49
|
+
try:
|
|
50
|
+
from google.cloud import storage
|
|
51
|
+
except ImportError:
|
|
52
|
+
raise ImportError(
|
|
53
|
+
"google-cloud-storage is required to use GCS with datablob. "
|
|
54
|
+
"Install it with 'pip install datablob[gcs]'"
|
|
55
|
+
)
|
|
56
|
+
self.client = storage.Client()
|
|
57
|
+
|
|
58
|
+
def put_object(self, key: str, body: BlobBody) -> None:
|
|
59
|
+
bucket = self.client.bucket(self.bucket_name)
|
|
60
|
+
blob = bucket.blob(key)
|
|
61
|
+
if isinstance(body, (io.BytesIO, io.StringIO)):
|
|
62
|
+
data = body.getvalue()
|
|
63
|
+
else:
|
|
64
|
+
data = body
|
|
65
|
+
blob.upload_from_string(data)
|
|
66
|
+
|
|
67
|
+
def get_object_bytes(self, key: str) -> bytes:
|
|
68
|
+
bucket = self.client.bucket(self.bucket_name)
|
|
69
|
+
blob = bucket.blob(key)
|
|
70
|
+
return blob.download_as_bytes()
|
|
71
|
+
|
|
72
|
+
def list_keys(self, prefix: str) -> list[str]:
|
|
73
|
+
keys = []
|
|
74
|
+
blobs = self.client.list_blobs(self.bucket_name, prefix=prefix)
|
|
75
|
+
for blob in blobs:
|
|
76
|
+
keys.append(blob.name)
|
|
77
|
+
return keys
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: datablob
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.11.0
|
|
4
4
|
Summary: Client for Updating a Simple Data Warehouse on Blob Storage
|
|
5
5
|
Home-page: https://github.com/gocarta/datablob
|
|
6
6
|
Download-URL: https://github.com/gocarta/datablob/tarball/download
|
|
@@ -19,7 +19,12 @@ Requires-Dist: openpyxl
|
|
|
19
19
|
Requires-Dist: pandas
|
|
20
20
|
Requires-Dist: pyarrow
|
|
21
21
|
Requires-Dist: shapely
|
|
22
|
+
Requires-Dist: tylertoo
|
|
22
23
|
Requires-Dist: tzdata
|
|
24
|
+
Provides-Extra: gcs
|
|
25
|
+
Requires-Dist: google-cloud-storage; extra == "gcs"
|
|
26
|
+
Provides-Extra: dev
|
|
27
|
+
Requires-Dist: pytest; extra == "dev"
|
|
23
28
|
Dynamic: author
|
|
24
29
|
Dynamic: author-email
|
|
25
30
|
Dynamic: classifier
|
|
@@ -29,6 +34,7 @@ Dynamic: download-url
|
|
|
29
34
|
Dynamic: home-page
|
|
30
35
|
Dynamic: keywords
|
|
31
36
|
Dynamic: license-file
|
|
37
|
+
Dynamic: provides-extra
|
|
32
38
|
Dynamic: requires-dist
|
|
33
39
|
Dynamic: summary
|
|
34
40
|
|
|
@@ -49,6 +55,11 @@ Client for Updating a Simple Data Warehouse on Blob Storage
|
|
|
49
55
|
pip install datablob
|
|
50
56
|
```
|
|
51
57
|
|
|
58
|
+
To include Google Cloud Storage (GCS) support:
|
|
59
|
+
```sh
|
|
60
|
+
pip install datablob[gcs]
|
|
61
|
+
```
|
|
62
|
+
|
|
52
63
|
## supported formats
|
|
53
64
|
- csv
|
|
54
65
|
- [geojson (points and polygons)](https://geojson.org/)
|
|
@@ -63,10 +74,16 @@ pip install datablob
|
|
|
63
74
|
```py
|
|
64
75
|
from datablob import DataBlobClient
|
|
65
76
|
|
|
77
|
+
# AWS S3 (default)
|
|
66
78
|
client = DataBlobClient(
|
|
67
79
|
bucket_name="example-test-bucket-123", bucket_path="prefix/to/dataportal"
|
|
68
80
|
)
|
|
69
81
|
|
|
82
|
+
# Google Cloud Storage (GCS)
|
|
83
|
+
client = DataBlobClient(
|
|
84
|
+
bucket_name="example-gcs-bucket-123", bucket_path="prefix/to/dataportal", provider="gcs"
|
|
85
|
+
)
|
|
86
|
+
|
|
70
87
|
rows = [
|
|
71
88
|
{
|
|
72
89
|
"name": "Random Building",
|
|
@@ -81,7 +98,7 @@ client.update_dataset(
|
|
|
81
98
|
version="2", # version of the dataset. It's a string, so you can version as you like.
|
|
82
99
|
data=rows, # list of dictionaries. Each row is a dictionary.
|
|
83
100
|
latitude_key="@lat", # name of the latitude column
|
|
84
|
-
|
|
101
|
+
longitude_key="@lon", # name of the longitude column
|
|
85
102
|
polygon_key="@geom", # name of the column with a polygon geometry in it (if applicable)
|
|
86
103
|
xlsx=True # set to True to include an Excel file in output
|
|
87
104
|
)
|
|
@@ -3,8 +3,11 @@ README.md
|
|
|
3
3
|
setup.cfg
|
|
4
4
|
setup.py
|
|
5
5
|
datablob/__init__.py
|
|
6
|
+
datablob/storage_client.py
|
|
6
7
|
datablob.egg-info/PKG-INFO
|
|
7
8
|
datablob.egg-info/SOURCES.txt
|
|
8
9
|
datablob.egg-info/dependency_links.txt
|
|
9
10
|
datablob.egg-info/requires.txt
|
|
10
|
-
datablob.egg-info/top_level.txt
|
|
11
|
+
datablob.egg-info/top_level.txt
|
|
12
|
+
tests/test_client.py
|
|
13
|
+
tests/test_storage_client.py
|
|
@@ -10,12 +10,8 @@ setup(
|
|
|
10
10
|
name="datablob",
|
|
11
11
|
packages=["datablob"],
|
|
12
12
|
package_dir={"datablob": "datablob"},
|
|
13
|
-
package_data={
|
|
14
|
-
|
|
15
|
-
"__init__.py",
|
|
16
|
-
]
|
|
17
|
-
},
|
|
18
|
-
version="0.9.3",
|
|
13
|
+
package_data={"datablob": ["__init__.py", "storage_client.py"]},
|
|
14
|
+
version="0.11.0",
|
|
19
15
|
description="Client for Updating a Simple Data Warehouse on Blob Storage",
|
|
20
16
|
long_description=long_description,
|
|
21
17
|
long_description_content_type="text/markdown",
|
|
@@ -37,6 +33,15 @@ setup(
|
|
|
37
33
|
"pandas",
|
|
38
34
|
"pyarrow",
|
|
39
35
|
"shapely",
|
|
36
|
+
"tylertoo",
|
|
40
37
|
"tzdata",
|
|
41
38
|
],
|
|
39
|
+
extras_require={
|
|
40
|
+
"gcs": [
|
|
41
|
+
"google-cloud-storage",
|
|
42
|
+
],
|
|
43
|
+
"dev": [
|
|
44
|
+
"pytest",
|
|
45
|
+
],
|
|
46
|
+
},
|
|
42
47
|
)
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import pytest
|
|
2
|
+
|
|
3
|
+
from datablob import DataBlobClient
|
|
4
|
+
from datablob.storage_client import GcsClient, S3Client
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def test_init_with_providers():
|
|
8
|
+
# s3 (default)
|
|
9
|
+
client = DataBlobClient(bucket_name="my-bucket", bucket_path="path/to/data")
|
|
10
|
+
assert client.bucket_name == "my-bucket"
|
|
11
|
+
assert client.bucket_path == "path/to/data"
|
|
12
|
+
assert client.provider == "s3"
|
|
13
|
+
assert isinstance(client.storage_client, S3Client)
|
|
14
|
+
|
|
15
|
+
# gcs provider
|
|
16
|
+
client = DataBlobClient(
|
|
17
|
+
bucket_name="my-gcs-bucket", bucket_path="path/to/data", provider="gcs"
|
|
18
|
+
)
|
|
19
|
+
assert client.bucket_name == "my-gcs-bucket"
|
|
20
|
+
assert client.provider == "gcs"
|
|
21
|
+
assert isinstance(client.storage_client, GcsClient)
|
|
22
|
+
|
|
23
|
+
# Invalid providers should be rejected explicitly
|
|
24
|
+
with pytest.raises(ValueError, match="Unsupported provider 'azure'"):
|
|
25
|
+
DataBlobClient(bucket_name="my-bucket", bucket_path="path", provider="azure")
|
|
26
|
+
|
|
27
|
+
with pytest.raises(ValueError, match="Unsupported provider 'GCS'"):
|
|
28
|
+
DataBlobClient(bucket_name="my-bucket", bucket_path="path", provider="GCS")
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import io
|
|
2
|
+
import pytest
|
|
3
|
+
from unittest.mock import MagicMock, patch
|
|
4
|
+
|
|
5
|
+
from datablob.storage_client import GcsClient, S3Client
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
@patch("boto3.client")
|
|
9
|
+
def test_s3_client_put_object(mock_boto3_client):
|
|
10
|
+
s3_client = S3Client("my-s3-bucket")
|
|
11
|
+
|
|
12
|
+
s3_client.put_object("path/to/file.txt", "hello world")
|
|
13
|
+
mock_boto3_client.return_value.put_object.assert_called_with(
|
|
14
|
+
Bucket="my-s3-bucket",
|
|
15
|
+
Key="path/to/file.txt",
|
|
16
|
+
Body="hello world",
|
|
17
|
+
)
|
|
18
|
+
|
|
19
|
+
bytes_io = io.BytesIO(b"binary data")
|
|
20
|
+
s3_client.put_object("path/to/binary.dat", bytes_io)
|
|
21
|
+
mock_boto3_client.return_value.put_object.assert_called_with(
|
|
22
|
+
Bucket="my-s3-bucket",
|
|
23
|
+
Key="path/to/binary.dat",
|
|
24
|
+
Body=b"binary data",
|
|
25
|
+
)
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
@patch("boto3.client")
|
|
29
|
+
def test_s3_client_get_object(mock_boto3_client):
|
|
30
|
+
mock_body = MagicMock()
|
|
31
|
+
mock_body.read.return_value = b"sample content"
|
|
32
|
+
mock_boto3_client.return_value.get_object.return_value = {"Body": mock_body}
|
|
33
|
+
|
|
34
|
+
s3_client = S3Client("my-s3-bucket")
|
|
35
|
+
content = s3_client.get_object_bytes("path/to/file.txt")
|
|
36
|
+
|
|
37
|
+
assert content == b"sample content"
|
|
38
|
+
mock_boto3_client.return_value.get_object.assert_called_once_with(
|
|
39
|
+
Bucket="my-s3-bucket",
|
|
40
|
+
Key="path/to/file.txt",
|
|
41
|
+
)
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
@patch("boto3.client")
|
|
45
|
+
def test_s3_client_list_keys(mock_boto3):
|
|
46
|
+
mock_boto3_client = mock_boto3.return_value
|
|
47
|
+
mock_boto3_client.list_objects_v2.return_value = {
|
|
48
|
+
"Contents": [
|
|
49
|
+
{"Key": "prefix/ds1/v1/meta.json"},
|
|
50
|
+
{"Key": "prefix/ds1/v1/data.csv"},
|
|
51
|
+
]
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
s3_client = S3Client("my-s3-bucket")
|
|
55
|
+
keys = s3_client.list_keys("prefix")
|
|
56
|
+
|
|
57
|
+
assert keys == ["prefix/ds1/v1/meta.json", "prefix/ds1/v1/data.csv"]
|
|
58
|
+
mock_boto3_client.list_objects_v2.assert_called_once_with(
|
|
59
|
+
Bucket="my-s3-bucket",
|
|
60
|
+
Prefix="prefix",
|
|
61
|
+
)
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
@patch.dict("sys.modules", {"google.cloud": None})
|
|
65
|
+
def test_gcs_client_missing_import():
|
|
66
|
+
with pytest.raises(ImportError, match="google-cloud-storage is required"):
|
|
67
|
+
GcsClient("my-gcs-bucket")
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
@patch("google.cloud.storage.Client")
|
|
71
|
+
def test_gcs_client_operations(mock_gcs_client_cls):
|
|
72
|
+
mock_client_instance = mock_gcs_client_cls.return_value
|
|
73
|
+
mock_bucket = MagicMock()
|
|
74
|
+
mock_blob = MagicMock()
|
|
75
|
+
mock_client_instance.bucket.return_value = mock_bucket
|
|
76
|
+
mock_bucket.blob.return_value = mock_blob
|
|
77
|
+
|
|
78
|
+
gcs_client = GcsClient("my-gcs-bucket")
|
|
79
|
+
|
|
80
|
+
gcs_client.put_object("path/to/file.txt", "hello gcs")
|
|
81
|
+
mock_client_instance.bucket.assert_called_with("my-gcs-bucket")
|
|
82
|
+
mock_bucket.blob.assert_called_with("path/to/file.txt")
|
|
83
|
+
mock_blob.upload_from_string.assert_called_with("hello gcs")
|
|
84
|
+
|
|
85
|
+
bytes_io = io.BytesIO(b"gcs binary")
|
|
86
|
+
gcs_client.put_object("path/to/binary.dat", bytes_io)
|
|
87
|
+
mock_blob.upload_from_string.assert_called_with(b"gcs binary")
|
|
88
|
+
|
|
89
|
+
mock_blob.download_as_bytes.return_value = b"retrieved gcs bytes"
|
|
90
|
+
content = gcs_client.get_object_bytes("path/to/file.txt")
|
|
91
|
+
assert content == b"retrieved gcs bytes"
|
|
92
|
+
|
|
93
|
+
blob1 = MagicMock()
|
|
94
|
+
blob1.name = "prefix/file1.csv"
|
|
95
|
+
blob2 = MagicMock()
|
|
96
|
+
blob2.name = "prefix/file2.csv"
|
|
97
|
+
mock_client_instance.list_blobs.return_value = [blob1, blob2]
|
|
98
|
+
|
|
99
|
+
keys = gcs_client.list_keys("prefix")
|
|
100
|
+
assert keys == ["prefix/file1.csv", "prefix/file2.csv"]
|
|
101
|
+
mock_client_instance.list_blobs.assert_called_once_with(
|
|
102
|
+
"my-gcs-bucket", prefix="prefix"
|
|
103
|
+
)
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|