forked from shiweihappy/GEE-Python-API
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path_cloud_api_utils.py
More file actions
executable file
·757 lines (634 loc) · 24.5 KB
/
_cloud_api_utils.py
File metadata and controls
executable file
·757 lines (634 loc) · 24.5 KB
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
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
#!/usr/bin/env python
"""Earth Engine helper functions for working with the Cloud API.
Many of the functions defined here are for mapping legacy calls in ee.data into
their new Cloud API equivalents. This generally requires remapping call
parameters and result values.
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import calendar
import datetime
import re
import warnings
from . import ee_exception
from apiclient import discovery
from apiclient import http
from apiclient import model
from google_auth_httplib2 import AuthorizedHttp
import httplib2
import six
# The default user project to use when making Cloud API calls.
_cloud_api_user_project = None
def _wrap_request(headers_supplier, response_inspector):
"""Builds a callable that wraps an API request.
Args:
headers_supplier: If not None, this will be called for each request and the
resulting dict incorporated into that request's HTTP headers.
response_inspector: If not None, this will be called with an
httplib2.Response containing the HTTP response and body content.
The call happens no matter what the HTTP response status was.
Returns:
Something that can be called in place of the http.HttpRequest constructor
to build an HttpRequest.
"""
if headers_supplier is None and response_inspector is None:
return http.HttpRequest
# pylint: disable=invalid-name
def builder(http_transport,
postproc,
uri,
method='GET',
body=None,
headers=None,
methodId=None,
resumable=None):
"""Builds an HttpRequest, adding headers and response inspection."""
additional_headers = headers_supplier()
if additional_headers:
headers = headers.copy() if headers else {}
headers.update(additional_headers)
request = http.HttpRequest(
http_transport,
postproc,
uri,
method=method,
body=body,
headers=headers,
methodId=methodId,
resumable=resumable)
if response_inspector:
request.add_response_callback(response_inspector)
return request
return builder
def set_cloud_api_user_project(cloud_api_user_project):
global _cloud_api_user_project
_cloud_api_user_project = cloud_api_user_project
def build_cloud_resource(api_base_url,
api_key=None,
credentials=None,
timeout=None,
headers_supplier=None,
response_inspector=None,
http_transport=None,
raw=False):
"""Builds an Earth Engine Cloud API resource.
Args:
api_base_url: The base URL of the cloud endpoints.
api_key: An API key that's enabled for use with the Earth Engine Cloud API.
credentials: OAuth2 credentials to use when authenticating to the API.
timeout: How long a timeout to set on requests, in seconds.
headers_supplier: A callable that will return a set of headers to be applied
to a request. Will be called once for each request.
response_inspector: A callable that will be invoked with the raw
httplib2.Response responses.
http_transport: An optional custom http_transport to use.
raw: Whether or not to return raw bytes when making method requests.
Returns:
A resource object to use to call the Cloud API.
"""
discovery_service_url = (
'{}/$discovery/rest?version=v1alpha&prettyPrint=false'
.format(api_base_url))
if http_transport is None:
http_transport = httplib2.Http(timeout=timeout)
if credentials is not None:
http_transport = AuthorizedHttp(credentials, http=http_transport)
request_builder = _wrap_request(headers_supplier, response_inspector)
# Discovery uses json by default.
if raw:
alt_model = model.RawModel()
else:
alt_model = None
resource = discovery.build(
'earthengine',
'v1alpha',
discoveryServiceUrl=discovery_service_url,
developerKey=api_key,
http=http_transport,
requestBuilder=request_builder,
model=alt_model)
return resource
def build_cloud_resource_from_document(discovery_document,
http_transport=None,
headers_supplier=None,
response_inspector=None):
"""Builds an Earth Engine Cloud API resource from a description of the API.
This version is intended for use in tests.
Args:
discovery_document: The description of the API.
http_transport: An HTTP transport object to use for the call.
headers_supplier: A callable that will return a set of headers to be applied
to a request. Will be called once for each request.
response_inspector: A callable that will be invoked with the raw
httplib2.Response responses.
Returns:
A resource object to use to call the Cloud API.
"""
request_builder = _wrap_request(headers_supplier, response_inspector)
return discovery.build_from_document(
discovery_document,
http=http_transport,
requestBuilder=request_builder)
def _convert_dict(to_convert,
conversions,
defaults=None,
key_warnings=False,
retain_keys=False):
"""Applies a set of conversion rules to a dict.
Args:
to_convert: A dictionary of key/value pairs to convert.
conversions: A dictionary giving the mapping from key names in "to_convert"
to how those keys and their values will be handled. Key/value pairs in
"to_convert" will be modified in a way that depends on how the key
appears in "conversions". If "to_convert" contains a key/value mapping
of "k"->"v", then:
- If "conversions" contains "k"->"X" then the result will contain
"X"->"v".
- If "conversions" contains "k"->None then the result will not contain an
entry for "k".
- If "conversions" contains "k"->("X", f) then the result will contain
"X"->f("v")
- If "conversions" does not contain an entry for "k" then the result
will not contain an entry for "k" unless retain_keys is true;
if key_warnings is True then a warning will be printed.
- If two or more distinct input keys are converted to the same output key,
one of the resulting values will appear in the result, the others
will be dropped, and a warning will be printed.
defaults: Values to insert in the result if the result of conversion does
not contain these keys.
key_warnings: Whether to print warnings for input keys that are not mapped
to anything in the output.
retain_keys: Whether or not to retain the state of dict. If false, any keys
that don't show up in the conversions dict will be dropped from result.
Returns:
The "to_convert" dict with keys renamed, values converted, and defaults
added.
"""
result = {}
for key, value in six.iteritems(to_convert):
if key in conversions:
conversion = conversions[key]
if conversion is not None:
if isinstance(conversion, tuple):
key = conversion[0]
value = conversion[1](value)
else:
key = conversion
if key in result:
warnings.warn(
'Multiple request parameters converted to {}'.format(key))
result[key] = value
elif retain_keys:
result[key] = value
elif key_warnings:
warnings.warn('Unrecognized key {} ignored'.format(key))
if defaults:
for default_key, default_value in six.iteritems(defaults):
if default_key not in result:
result[default_key] = default_value
return result
def _convert_value(value, conversions, default):
"""Converts a value using a set of value mappings.
Args:
value: The value to convert.
conversions: A dict giving the desired output for each of a set of possible
input values.
default: The value to return if the input value is not one of the ones
listed in "conversions".
Returns:
The converted value.
"""
return conversions.get(value, default)
def _convert_msec_to_timestamp(time_msec):
"""Converts a time value to a google.protobuf.Timestamp's string form.
Args:
time_msec: A time in msec since the Unix epoch.
Returns:
A string formatted like '2003-09-07T19:30:12.345Z', which is the expected
form of google.protobuf.Timestamp values.
"""
return datetime.datetime.utcfromtimestamp(
time_msec / 1000.0).isoformat() + 'Z'
def _convert_timestamp_to_msec(timestamp):
"""Converts a google.protobuf.Timestamp's string form to a time in msec.
Args:
timestamp: A string formatted like '2003-09-07T19:30:12.345Z', which is the
expected form of google.protobuf.Timestamp values.
Returns:
A time in msec since the Unix epoch.
"""
# The fractional second part is optional. Sigh.
if '.' in timestamp:
parsed_timestamp = datetime.datetime.strptime(
timestamp, '%Y-%m-%dT%H:%M:%S.%fZ')
else:
parsed_timestamp = datetime.datetime.strptime(
timestamp, '%Y-%m-%dT%H:%M:%SZ')
return (calendar.timegm(parsed_timestamp.utctimetuple()) * 1000 +
int(parsed_timestamp.microsecond / 1000))
def _convert_bounding_box_to_geo_json(bbox):
"""Converts a lng/lat bounding box to a GeoJSON string."""
lng_min = bbox[0]
lat_min = bbox[1]
lng_max = bbox[2]
lat_max = bbox[3]
return ('{{"type":"Polygon","coordinates":'
'[[[{0},{1}],[{2},{1}],[{2},{3}],[{0},{3}],[{0},{1}]]]}}'.format(
lng_min, lat_min, lng_max, lat_max))
def convert_get_list_params_to_list_assets_params(params):
"""Converts a getList params dict to something usable with listAssets."""
return _convert_dict(
params, {
'id': ('parent', convert_asset_id_to_asset_name),
'num': 'pageSize'
}, key_warnings=True)
def convert_list_assets_result_to_get_list_result(result):
"""Converts a listAssets result to something getList can return."""
if 'assets' not in result:
return []
return [_convert_asset_for_get_list_result(i) for i in result['assets']]
def convert_get_list_params_to_list_images_params(params):
"""Converts a getList params dict to something usable with listImages."""
params = _convert_dict(
params, {
'id': ('parent', convert_asset_id_to_asset_name),
'num': 'pageSize',
'starttime': ('startTime', _convert_msec_to_timestamp),
'endtime': ('endTime', _convert_msec_to_timestamp),
'bbox': ('region', _convert_bounding_box_to_geo_json),
'region': 'region'
},
key_warnings=True)
# getList returns minimal information; we can filter unneeded stuff out
# server-side.
params['fields'] = 'images(name)'
return params
def convert_list_images_result_to_get_list_result(result):
"""Converts a listImages result to something getList can return."""
if 'images' not in result:
return []
return [_convert_image_for_get_list_result(i) for i in result['images']]
def _convert_asset_for_get_list_result(asset):
"""Converts an EarthEngineAsset to the format returned by getList."""
result = _convert_dict(
asset, {
'name': 'id',
'type': ('type', _convert_asset_type_for_get_list_result)
},
defaults={'type': 'Unknown'})
return result
def _convert_image_for_get_list_result(asset):
"""Converts an Image to the format returned by getList."""
result = _convert_dict(
asset, {
'name': 'id',
},
defaults={'type': 'Image'})
return result
def _convert_asset_type_for_get_list_result(asset_type):
"""Converts an EarthEngineAsset.Type to the format returned by getList."""
return _convert_value(
asset_type, {
'IMAGE': 'Image',
'IMAGE_COLLECTION': 'ImageCollection',
'TABLE': 'Table',
'FOLDER': 'Folder'
}, 'Unknown')
def convert_asset_type_for_create_asset(asset_type):
"""Converts a createAsset asset type to an EarthEngineAsset.Type."""
return _convert_value(
asset_type, {
'ImageCollection': 'IMAGE_COLLECTION',
'Folder': 'FOLDER'
}, asset_type)
def convert_asset_id_to_asset_name(asset_id):
"""Converts an internal asset ID to a Cloud API asset name.
If asset_id already matches the format 'projects/*/assets/**', it is returned
as-is.
Args:
asset_id: The asset ID to convert.
Returns:
An asset name string in the format 'projects/*/assets/**'.
"""
# r'[a-z][a-z0-9\-]{4,28}[a-z0-9]' matches a valid Cloud project ID.
if re.match(r'projects/[a-z][a-z0-9\-]{4,28}[a-z0-9]/assets/.*', asset_id):
return asset_id
elif asset_id.split('/')[0] in ['users', 'projects']:
return 'projects/earthengine-legacy/assets/{}'.format(asset_id)
else:
return 'projects/earthengine-public/assets/{}'.format(asset_id)
def split_asset_name(asset_name):
"""Splits an asset name into the parent and ID parts.
Args:
asset_name: The asset ID to split, in the form 'projects/*/assets/**'.
Returns:
The parent ('projects/*') and ID ('**') parts of the name.
"""
projects, parent, _, remainder = asset_name.split('/', 3)
return projects + '/' + parent, remainder
def convert_operation_name_to_task_id(operation_name):
"""Converts an Operation name to a task ID."""
return re.search('operations/(.*)', operation_name).group(1)
def convert_task_id_to_operation_name(task_id):
"""Converts a task ID to an Operation name."""
return 'projects/{}/operations/{}'.format(_cloud_api_user_project, task_id)
def convert_params_to_image_manifest(params):
"""Converts params to an ImageManifest for ingestion."""
return _convert_dict(
params, {
'id': ('name', convert_asset_id_to_asset_name),
'tilesets': ('tilesets', convert_tilesets_to_one_platform_tilesets)
},
retain_keys=True)
def convert_params_to_table_manifest(params):
"""Converts params to a TableManifest for ingestion."""
return _convert_dict(
params, {
'id': ('name', convert_asset_id_to_asset_name),
'sources': ('sources', convert_sources_to_one_platform_sources),
},
retain_keys=True)
def convert_tilesets_to_one_platform_tilesets(tilesets):
"""Converts a tileset to a one platform representation of a tileset."""
converted_tilesets = []
for tileset in tilesets:
converted_tileset = _convert_dict(
tileset,
{'sources': ('sources', convert_sources_to_one_platform_sources)},
retain_keys=True)
converted_tilesets.append(converted_tileset)
return converted_tilesets
def convert_sources_to_one_platform_sources(sources):
"""Converts the sources to one platform representation of sources."""
converted_sources = []
for source in sources:
if 'primaryPath' in source:
converted_source = {'uris': [source['primaryPath']]}
if 'additionalPaths' in source:
for additional_path in source['additionalPaths']:
converted_source['uris'].append(additional_path)
converted_sources.append(converted_source)
else:
converted_sources.append(source)
return converted_sources
def encode_number_as_cloud_value(number):
# Numeric values in constantValue-style nodes end up stored in doubles. If the
# input is an integer that loses precision as a double, use the int64 slot
# ("integerValue") in ValueNode.
if (isinstance(number, six.integer_types) and float(number) != number):
return {'integerValue': str(number)}
else:
return {'constantValue': number}
def convert_algorithms(algorithms):
"""Converts a ListAlgorithmsResult to the internal format.
The internal code expects a dict mapping each algorithm's name to a dict
containing:
- description: string
- returns: string
- arguments: list of dicts, each containing
- name: argument name
- type: argument type
- description: argument description (optional)
- optional: bool (optional)
- default: default value (optional)
- hidden: bool (optional)
- deprecated: string containing deprecation reason (optional)
Args:
algorithms: A ListAlgorithmResult.
Returns:
A version of that algorithms list that can be interpreted by
apifunction.initialize().
"""
return dict(
_convert_algorithm(algorithm) for algorithm in algorithms['algorithms'])
def _convert_algorithm(algorithm):
"""Converts an Algorithm to the internal format."""
# Strip leading 'algorithms/' from the name.
algorithm_name = algorithm['name'][11:]
converted_algorithm = _convert_dict(
algorithm, {
'description': 'description',
'returnType': 'returns',
'arguments': ('args', _convert_algorithm_arguments),
'hidden': 'hidden'
},
defaults={
'description': '',
'returns': '',
'args': []
})
if algorithm.get('deprecated'):
converted_algorithm['deprecated'] = algorithm.get('deprecationReason', '')
return algorithm_name, converted_algorithm
def _convert_algorithm_arguments(args):
return [_convert_algorithm_argument(arg) for arg in args]
def _convert_algorithm_argument(arg):
return _convert_dict(
arg, {
'argumentName': 'name',
'type': 'type',
'description': 'description',
'optional': 'optional',
'defaultValue': 'default'
},
defaults={
'description': '',
'type': ''
})
def convert_to_image_file_format(format_str):
"""Converts a legacy file format string to an ImageFileFormat enum value.
Args:
format_str: A string describing an image file format that was passed to
one of the functions in ee.data that takes image file formats.
Returns:
A best guess at the corresponding ImageFileFormat enum name.
"""
if format_str is None:
return 'AUTO_JPEG_PNG'
format_str = format_str.upper()
if format_str == 'JPG':
return 'JPEG'
elif format_str == 'AUTO':
return 'AUTO_JPEG_PNG'
elif format_str == 'GEOTIFF':
return 'GEO_TIFF'
elif format_str == 'TFRECORD':
return 'TF_RECORD_IMAGE'
else:
# It's probably "JPEG" or "PNG", but might be some other supported format.
# Let the server validate it.
return format_str
def convert_to_table_file_format(format_str):
"""Converts a legacy file format string to a TableFileFormat enum value.
Args:
format_str: A string describing a table file format that was passed to
one of the functions in ee.data that takes table file formats.
Returns:
A best guess at the corresponding TableFileFormat enum name.
"""
format_str = format_str.upper()
if format_str == 'GEOJSON':
return 'GEO_JSON'
elif format_str == 'TFRECORD':
return 'TF_RECORD_TABLE'
else:
# It's probably "CSV" or "KML" or one of the others.
# Let the server validate it.
return format_str
def convert_to_band_list(bands):
"""Converts a band list, possibly as CSV, to a real list of bands.
Args:
bands: A list of strings containing band names, or a string containing
a comma-separated list of band names, or None.
Returns:
A list of band names.
"""
if bands is None:
return []
elif isinstance(bands, six.string_types):
return bands.split(',')
elif isinstance(bands, list):
return bands
else:
raise ee_exception.EEException('Invalid band list ' + bands)
def convert_to_visualization_options(params):
"""Extracts a VisualizationOptions from a param dict.
Args:
params: See ee.data.getMapId() for the description of the keys and values
that might appear here.
Returns:
A VisualizationOptions proto, in dict form.
"""
result = {}
if 'palette' in params:
palette = params['palette']
if isinstance(palette, six.string_types):
palette = palette.split(',')
result['paletteColors'] = palette
value_range = len(palette) - 1
else:
value_range = 255
ranges = []
if 'gain' in params or 'bias' in params:
if 'min' in params or 'max' in params:
raise ee_exception.EEException(
'Gain and bias can\'t be specified together with min and max')
# The Cloud API doesn't support gain/bias, only min/max. Extract and
# convert.
gains = _convert_csv_numbers_to_list(params.get('gain'))
biases = _convert_csv_numbers_to_list(params.get('bias'))
if not gains:
gains = [1.0] * len(biases)
elif not biases:
biases = [0.0] * len(gains)
elif len(gains) != len(biases):
raise ee_exception.EEException('Length of gain and bias must match.')
for gain, bias in zip(gains, biases):
# The transformation equations are
# x -> x * gain + bias
# x -> range * (x - min) / (max - min)
# Solving for (min, max) given (gain, bias) gives:
range_min = -bias / gain
range_max = value_range / gain + range_min
ranges.append({'min': range_min, 'max': range_max})
elif 'min' in params or 'max' in params:
mins = _convert_csv_numbers_to_list(params.get('min'))
maxes = _convert_csv_numbers_to_list(params.get('max'))
if not mins:
mins = [0.0] * len(maxes)
elif not maxes:
maxes = [1.0] * len(mins)
elif len(mins) != len(maxes):
raise ee_exception.EEException('Length of min and max must match.')
for range_min, range_max in zip(mins, maxes):
ranges.append({'min': range_min, 'max': range_max})
if ranges:
result['ranges'] = ranges
gammas = _convert_csv_numbers_to_list(params.get('gamma'))
if len(gammas) > 1:
raise ee_exception.EEException('Only one gamma value is supported.')
elif gammas:
result['gamma'] = {'value': gammas[0]}
return result
def _convert_csv_numbers_to_list(value):
"""Converts a string containing CSV numbers to a list."""
if not value:
return []
return [float(x) for x in value.split(',')]
def convert_operation_to_task(operation):
"""Converts an Operation to a legacy Task."""
result = _convert_dict(
operation['metadata'], {
'createTime': ('creation_timestamp_ms', _convert_timestamp_to_msec),
'updateTime': ('update_timestamp_ms', _convert_timestamp_to_msec),
'startTime': ('start_timestamp_ms', _convert_timestamp_to_msec),
'state': ('state', _convert_operation_state_to_task_state),
'description': 'description',
'type': 'task_type',
'destinationUris': 'destination_uris',
})
if operation.get('done'):
if 'error' in operation:
result['error_message'] = operation['error']['message']
result['id'] = convert_operation_name_to_task_id(operation['name'])
result['name'] = operation['name']
return result
def _convert_operation_state_to_task_state(state):
"""Converts a state string from an Operation to the Task equivalent."""
return _convert_value(
state, {
'PENDING': 'READY',
'RUNNING': 'RUNNING',
'CANCELLING': 'CANCEL_REQUESTED',
'SUCCEEDED': 'COMPLETED',
'CANCELLED': 'CANCELLED',
'FAILED': 'FAILED'
}, 'UNKNOWN')
def convert_iam_policy_to_acl(policy):
"""Converts an IAM Policy proto to the legacy ACL format."""
bindings = {
binding['role']: binding.get('members', [])
for binding in policy.get('bindings', [])
}
owners = bindings.get('roles/owner', [])
readers = bindings.get('roles/viewer', [])
writers = bindings.get('roles/editor', [])
if 'allUsers' in readers:
all_users_can_read = True
readers.remove('allUsers')
else:
all_users_can_read = False
result = {'owners': owners, 'readers': readers, 'writers': writers}
if all_users_can_read:
result['all_users_can_read'] = True
return result
def convert_acl_to_iam_policy(acl):
"""Converts the legacy ACL format to an IAM Policy proto."""
owners = acl.get('owners', [])
readers = acl.get('readers', [])
if acl.get('all_users_can_read', False):
readers.append('allUsers')
writers = acl.get('writers', [])
bindings = []
if owners:
bindings.append({'role': 'roles/owner', 'members': owners})
if readers:
bindings.append({'role': 'roles/viewer', 'members': readers})
if writers:
bindings.append({'role': 'roles/editor', 'members': writers})
return {'bindings': bindings}
def convert_to_grid_dimensions(dimensions):
"""Converts an input value to GridDimensions.
Args:
dimensions: May specify a single number to indicate a square shape,
or a tuple of two dimensions to indicate (width,height).
Returns:
A GridDimensions as a dict.
"""
if isinstance(dimensions, six.integer_types):
return {'width': dimensions, 'height': dimensions}
elif len(dimensions) == 1:
return {'width': dimensions[0], 'height': dimensions[0]}
else:
return {'width': dimensions[0], 'height': dimensions[1]}