-
Notifications
You must be signed in to change notification settings - Fork 25
/
cli.py
639 lines (552 loc) · 20.3 KB
/
cli.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
"""Tilesets command line interface"""
import json
import tempfile
import click
import cligj
from requests_toolbelt import MultipartEncoder, MultipartEncoderMonitor
import mapbox_tilesets
from mapbox_tilesets import utils, errors
@click.version_option(version=mapbox_tilesets.__version__, message="%(version)s")
@click.group()
def cli():
"""This is the command line interface for the Mapbox Tilesets API.
Thanks for joining us.
This CLI requires a Mapbox access token. You can either set it in your environment as
"MAPBOX_ACCESS_TOKEN" or "MapboxAccessToken" or pass it to each command with the --token flag.
"""
@cli.command("create")
@click.argument("tileset", required=True, type=str)
@click.option(
"--recipe",
"-r",
required=True,
type=click.Path(exists=True),
help="path to a Recipe JSON document",
)
@click.option("--name", "-n", required=True, type=str, help="name of the tileset")
@click.option(
"--description", "-d", required=False, type=str, help="description of the tileset"
)
@click.option(
"--privacy",
"-p",
required=False,
type=click.Choice(["public", "private"]),
help="set the tileset privacy options",
)
@click.option(
"--attribution",
required=False,
type=str,
help="attribution for the tileset in the form of a JSON string - Array<Object<text,link>>",
)
@click.option("--token", "-t", required=False, type=str, help="Mapbox access token")
@click.option("--indent", type=int, default=None, help="Indent for JSON output")
def create(
tileset,
recipe,
name=None,
description=None,
privacy=None,
attribution=None,
token=None,
indent=None,
):
"""Create a new tileset with a recipe.
$ tilesets create <tileset_id>
<tileset_id> is in the form of username.handle - for example "mapbox.neat-tileset".
The handle may only include "-" or "_" special characters and must be 32 characters or fewer.
"""
mapbox_api = utils._get_api()
mapbox_token = utils._get_token(token)
s = utils._get_session()
url = "{0}/tilesets/v1/{1}?access_token={2}".format(
mapbox_api, tileset, mapbox_token
)
body = {}
body["name"] = name or ""
body["description"] = description or ""
if privacy:
body["private"] = True if privacy == "private" else False
if not utils.validate_tileset_id(tileset):
raise errors.TilesetNameError(tileset)
if recipe:
with open(recipe) as json_recipe:
body["recipe"] = json.load(json_recipe)
if attribution:
try:
body["attribution"] = json.loads(attribution)
except:
click.echo("Unable to parse attribution JSON")
click.exit(1)
r = s.post(url, json=body)
click.echo(json.dumps(r.json(), indent=indent))
@cli.command("publish")
@click.argument("tileset", required=True, type=str)
@click.option("--token", "-t", required=False, type=str, help="Mapbox access token")
@click.option("--indent", type=int, default=None, help="Indent for JSON output")
def publish(tileset, token=None, indent=None):
"""Publish your tileset.
Only supports tilesets created with the Mapbox Tiling Service.
tilesets publish <tileset_id>
"""
mapbox_api = utils._get_api()
mapbox_token = utils._get_token(token)
s = utils._get_session()
url = "{0}/tilesets/v1/{1}/publish?access_token={2}".format(
mapbox_api, tileset, mapbox_token
)
r = s.post(url)
if r.status_code == 200:
click.echo(json.dumps(r.json(), indent=indent))
click.echo(
f"You can view the status of your tileset with the `tilesets status {tileset}` command.",
err=True,
)
else:
raise errors.TilesetsError(r.text)
@cli.command("update")
@click.argument("tileset", required=True, type=str)
@click.option("--token", "-t", required=False, type=str, help="Mapbox access token")
@click.option("--indent", type=int, default=None, help="Indent for JSON output")
@click.option("--name", "-n", required=False, type=str, help="name of the tileset")
@click.option(
"--description", "-d", required=False, type=str, help="description of the tileset"
)
@click.option(
"--privacy",
"-p",
required=False,
type=click.Choice(["public", "private"]),
help="set the tileset privacy options",
)
@click.option(
"--attribution",
required=False,
type=str,
help="attribution for the tileset in the form of a JSON string - Array<Object<text,link>>",
)
def update(
tileset,
token=None,
indent=None,
name=None,
description=None,
privacy=None,
attribution=None,
):
"""Update a tileset's information.
tilesets update <tileset_id>
"""
mapbox_api = utils._get_api()
mapbox_token = utils._get_token(token)
s = utils._get_session()
url = "{0}/tilesets/v1/{1}?access_token={2}".format(
mapbox_api, tileset, mapbox_token
)
body = {}
if name:
body["name"] = name
if description:
body["description"] = description
if privacy:
body["private"] = True if privacy == "private" else False
if attribution:
try:
body["attribution"] = json.loads(attribution)
except:
click.echo("Unable to parse attribution JSON")
click.exit(1)
r = s.patch(url, json=body)
if r.status_code != 204:
raise errors.TilesetsError(r.text)
@cli.command("delete")
@click.argument("tileset", required=True, type=str)
@click.option("--force", "-f", is_flag=True, help="Circumvents confirmation prompt")
@click.option("--token", "-t", required=False, type=str, help="Mapbox access token")
@click.option("--indent", type=int, default=None, help="Indent for JSON output")
def delete(tileset, token=None, indent=None, force=None):
"""Delete your tileset.
tilesets delete <tileset_id>
"""
mapbox_api = utils._get_api()
mapbox_token = utils._get_token(token)
s = utils._get_session()
if not force:
val = click.prompt(
'To confirm tileset deletion please enter the full tileset id "{0}"'.format(
tileset
),
type=str,
)
if val != tileset:
raise click.ClickException(f"{val} does not match {tileset}. Aborted!")
url = "{0}/tilesets/v1/{1}?access_token={2}".format(
mapbox_api, tileset, mapbox_token
)
r = s.delete(url)
if r.status_code == 200 or r.status_code == 204:
click.echo("Tileset deleted.")
else:
raise errors.TilesetsError(r.text)
@cli.command("status")
@click.argument("tileset", required=True, type=str)
@click.option("--token", "-t", required=False, type=str, help="Mapbox access token")
@click.option("--indent", type=int, default=None, help="Indent for JSON output")
def status(tileset, token=None, indent=None):
"""View the current queue/processing/complete status of your tileset.
tilesets status <tileset_id>
"""
mapbox_api = utils._get_api()
mapbox_token = utils._get_token(token)
s = utils._get_session()
url = "{0}/tilesets/v1/{1}/status?access_token={2}".format(
mapbox_api, tileset, mapbox_token
)
r = s.get(url)
click.echo(json.dumps(r.json(), indent=indent))
@cli.command("tilejson")
@click.argument("tileset", required=True, type=str)
@click.option("--token", "-t", required=False, type=str, help="Mapbox access token")
@click.option("--indent", type=int, default=None, help="Indent for JSON output")
@click.option(
"--secure", required=False, is_flag=True, help="receive HTTPS resource URLs"
)
def tilejson(tileset, token=None, indent=None, secure=False):
"""View the TileJSON of a particular tileset.
Can take a comma-separated list of tilesets for a composited TileJSON.
tilesets tilejson <tileset_id>,<tileset_id>
"""
mapbox_api = utils._get_api()
mapbox_token = utils._get_token(token)
s = utils._get_session()
# validate tilesets by splitting comma-delimted string
# and rejoining it
for t in tileset.split(","):
if not utils.validate_tileset_id(t):
raise errors.TilesetNameError(t)
url = "{0}/v4/{1}.json?access_token={2}".format(mapbox_api, tileset, mapbox_token)
if secure:
url = url + "&secure"
r = s.get(url)
if r.status_code == 200:
click.echo(json.dumps(r.json(), indent=indent))
else:
raise errors.TilesetsError(r.text)
@cli.command("jobs")
@click.argument("tileset", required=True, type=str)
@click.option("--stage", "-s", required=False, type=str, help="job stage")
@click.option(
"--limit",
required=False,
type=click.IntRange(1, 500),
default=100,
help="The maximum number of results to return, from 1 to 500 (default 100)",
)
@click.option("--token", "-t", required=False, type=str, help="Mapbox access token")
@click.option("--indent", type=int, default=None, help="Indent for JSON output")
def jobs(tileset, stage=None, limit=None, token=None, indent=None):
"""View all jobs for a particular tileset.
Only supports tilesets created with the Mapbox Tiling Service.
tilesets jobs <tileset_id>
"""
mapbox_api = utils._get_api()
mapbox_token = utils._get_token(token)
s = utils._get_session()
url = "{0}/tilesets/v1/{1}/jobs?access_token={2}".format(
mapbox_api, tileset, mapbox_token
)
url = "{0}&limit={1}".format(url, limit) if limit else url
url = "{0}&stage={1}".format(url, stage) if stage else url
r = s.get(url)
click.echo(json.dumps(r.json(), indent=indent))
@cli.command("job")
@click.argument("tileset", required=True, type=str)
@click.argument("job_id", required=True, type=str)
@click.option("--token", "-t", required=False, type=str, help="Mapbox access token")
@click.option("--indent", type=int, default=None, help="Indent for JSON output")
def job(tileset, job_id, token=None, indent=None):
"""View a single job for a particular tileset.
Only supports tilesets created with the Mapbox Tiling Service.
tilesets job <tileset_id> <job_id>
"""
mapbox_api = utils._get_api()
mapbox_token = utils._get_token(token)
s = utils._get_session()
url = "{0}/tilesets/v1/{1}/jobs/{2}?access_token={3}".format(
mapbox_api, tileset, job_id, mapbox_token
)
r = s.get(url)
click.echo(json.dumps(r.json(), indent=indent))
@cli.command("list")
@click.argument("username", required=True, type=str)
@click.option(
"--verbose",
"-v",
required=False,
is_flag=True,
help="Will print all tileset information",
)
@click.option(
"--type",
required=False,
type=click.Choice(["vector", "raster"]),
help="Filter results by tileset type",
)
@click.option(
"--visibility",
required=False,
type=click.Choice(["public", "private"]),
help="Filter results by visibility",
)
@click.option(
"--sortby",
required=False,
type=click.Choice(["created", "modified"]),
help="Sort the results by their created or modified timestamps",
)
@click.option(
"--limit",
required=False,
type=click.IntRange(1, 500),
default=100,
help="The maximum number of results to return, from 1 to 500 (default 100)",
)
@click.option("--token", "-t", required=False, type=str, help="Mapbox access token")
@click.option("--indent", type=int, default=None, help="Indent for JSON output")
def list(
username,
verbose,
type=None,
visibility=None,
sortby=None,
limit=None,
token=None,
indent=None,
):
"""List all tilesets for an account.
By default the response is a simple list of tileset IDs.
If you would like an array of all tileset's information,
use the --versbose flag.
tilests list <username>
"""
mapbox_api = utils._get_api()
mapbox_token = utils._get_token(token)
s = utils._get_session()
url = "{0}/tilesets/v1/{1}?access_token={2}".format(
mapbox_api, username, mapbox_token
)
url = "{0}&limit={1}".format(url, limit) if limit else url
url = "{0}&type={1}".format(url, type) if type else url
url = "{0}&visibility={1}".format(url, visibility) if visibility else url
url = "{0}&sortby={1}".format(url, sortby) if sortby else url
r = s.get(url)
if r.status_code == 200:
if verbose:
for tileset in r.json():
click.echo(json.dumps(tileset, indent=indent))
else:
for tileset in r.json():
click.echo(tileset["id"])
else:
raise errors.TilesetsError(r.text)
@cli.command("validate-recipe")
@click.argument("recipe", required=True, type=click.Path(exists=True))
@click.option("--token", "-t", required=False, type=str, help="Mapbox access token")
@click.option("--indent", type=int, default=None, help="Indent for JSON output")
def validate_recipe(recipe, token=None, indent=None):
"""Validate a Recipe JSON document
tilesets validate-recipe <path_to_recipe>
"""
mapbox_api = utils._get_api()
mapbox_token = utils._get_token(token)
s = utils._get_session()
url = "{0}/tilesets/v1/validateRecipe?access_token={1}".format(
mapbox_api, mapbox_token
)
with open(recipe) as json_recipe:
recipe_json = json.load(json_recipe)
r = s.put(url, json=recipe_json)
click.echo(json.dumps(r.json(), indent=indent))
@cli.command("view-recipe")
@click.argument("tileset", required=True, type=str)
@click.option("--token", "-t", required=False, type=str, help="Mapbox access token")
@click.option("--indent", type=int, default=None, help="Indent for JSON output")
def view_recipe(tileset, token=None, indent=None):
"""View a tileset's recipe JSON
tilesets view-recipe <tileset_id>
"""
mapbox_api = utils._get_api()
mapbox_token = utils._get_token(token)
s = utils._get_session()
url = "{0}/tilesets/v1/{1}/recipe?access_token={2}".format(
mapbox_api, tileset, mapbox_token
)
r = s.get(url)
if r.status_code == 200:
click.echo(json.dumps(r.json(), indent=indent))
else:
raise errors.TilesetsError(r.text)
@cli.command("update-recipe")
@click.argument("tileset", required=True, type=str)
@click.argument("recipe", required=True, type=click.Path(exists=True))
@click.option("--token", "-t", required=False, type=str, help="Mapbox access token")
@click.option("--indent", type=int, default=None, help="Indent for JSON output")
def update_recipe(tileset, recipe, token=None, indent=None):
"""Update a Recipe JSON document for a particular tileset
Only supports tilesets created with the Mapbox Tiling Service.
tilesets update-recipe <tileset_id> <path_to_recipe>
"""
mapbox_api = utils._get_api()
mapbox_token = utils._get_token(token)
s = utils._get_session()
url = "{0}/tilesets/v1/{1}/recipe?access_token={2}".format(
mapbox_api, tileset, mapbox_token
)
with open(recipe) as json_recipe:
recipe_json = json.load(json_recipe)
r = s.patch(url, json=recipe_json)
if r.status_code == 201 or r.status_code == 204:
click.echo("Updated recipe.", err=True)
else:
raise errors.TilesetsError(r.text)
@cli.command("validate-source")
@cligj.features_in_arg
def validate_source(features):
"""Validate your source file.
$ tilesets validate-source <path/to/your/src/file>
"""
click.echo("Validating features", err=True)
for feature in features:
utils.validate_geojson(feature)
click.echo("✔ valid")
@cli.command("add-source")
@click.argument("username", required=True, type=str)
@click.argument("id", required=True, type=str)
@cligj.features_in_arg
@click.option("--no-validation", is_flag=True, help="Bypass source file validation")
@click.option("--quiet", is_flag=True, help="Don't show progress bar")
@click.option("--token", "-t", required=False, type=str, help="Mapbox access token")
@click.option("--indent", type=int, default=None, help="Indent for JSON output")
@click.pass_context
def add_source(
ctx, username, id, features, no_validation, quiet, token=None, indent=None
):
"""Create/add a tileset source
tilesets add-source <username> <id> <path/to/source/data>
"""
mapbox_api = utils._get_api()
mapbox_token = utils._get_token(token)
s = utils._get_session()
url = (
f"{mapbox_api}/tilesets/v1/sources/{username}/{id}?access_token={mapbox_token}"
)
with tempfile.TemporaryFile() as file:
for feature in features:
if not no_validation:
utils.validate_geojson(feature)
file.write(
(json.dumps(feature, separators=(",", ":")) + "\n").encode("utf-8")
)
file.seek(0)
m = MultipartEncoder(fields={"file": ("file", file)})
if quiet:
resp = s.post(
url,
data=m,
headers={
"Content-Disposition": "multipart/form-data",
"Content-type": m.content_type,
},
)
else:
prog = click.progressbar(
length=m.len, fill_char="=", width=0, label="upload progress"
)
with prog:
def callback(m):
prog.pos = m.bytes_read
prog.update(0) # Step is 0 because we set pos above
monitor = MultipartEncoderMonitor(m, callback)
resp = s.post(
url,
data=monitor,
headers={
"Content-Disposition": "multipart/form-data",
"Content-type": monitor.content_type,
},
)
if resp.status_code == 200:
click.echo(json.dumps(resp.json(), indent=indent))
else:
raise errors.TilesetsError(resp.text)
@cli.command("view-source")
@click.argument("username", required=True, type=str)
@click.argument("id", required=True, type=str)
@click.option("--token", "-t", required=False, type=str, help="Mapbox access token")
@click.option("--indent", type=int, default=None, help="Indent for JSON output")
def view_source(username, id, token=None, indent=None):
"""View a Tileset Source's information
tilesets view-source <username> <id>
"""
mapbox_api = utils._get_api()
mapbox_token = utils._get_token(token)
s = utils._get_session()
url = "{0}/tilesets/v1/sources/{1}/{2}?access_token={3}".format(
mapbox_api, username, id, mapbox_token
)
r = s.get(url)
if r.status_code == 200:
click.echo(json.dumps(r.json(), indent=indent))
else:
raise errors.TilesetsError(r.text)
@cli.command("delete-source")
@click.argument("username", required=True, type=str)
@click.argument("id", required=True, type=str)
@click.option("--force", "-f", is_flag=True, help="Circumvents confirmation prompt")
@click.option("--token", "-t", required=False, type=str, help="Mapbox access token")
def delete_source(username, id, force, token=None):
"""Delete a Tileset Source + all of its files.
tilesets delete-source <username> <id>
"""
if not force:
val = click.prompt(
'To confirm source deletion please enter the full source id "{0}/{1}"'.format(
username, id
),
type=str,
)
if val != f"{username}/{id}":
raise click.ClickException(
f"{val} does not match {username}/{id}. Aborted!"
)
mapbox_api = utils._get_api()
mapbox_token = utils._get_token(token)
s = utils._get_session()
url = "{0}/tilesets/v1/sources/{1}/{2}?access_token={3}".format(
mapbox_api, username, id, mapbox_token
)
r = s.delete(url)
if r.status_code == 204:
click.echo("Source deleted.")
else:
raise errors.TilesetsError(r.text)
@cli.command("list-sources")
@click.argument("username", required=True, type=str)
@click.option("--token", "-t", required=False, type=str, help="Mapbox access token")
def list_sources(username, token=None):
"""List all Tileset Sources for an account. Response is an un-ordered array of sources.
tilesets list-sources <username>
"""
mapbox_api = utils._get_api()
mapbox_token = utils._get_token(token)
s = utils._get_session()
url = "{0}/tilesets/v1/sources/{1}?access_token={2}".format(
mapbox_api, username, mapbox_token
)
r = s.get(url)
if r.status_code == 200:
for source in r.json():
click.echo(source["id"])
else:
raise errors.TilesetsError(r.text)