-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathbase.py
More file actions
463 lines (343 loc) · 11.5 KB
/
Copy pathbase.py
File metadata and controls
463 lines (343 loc) · 11.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
#
# This file is part of the CloudBlue Connect Python OpenAPI Client.
#
# Copyright (c) 2025 CloudBlue. All Rights Reserved.
#
from connect.client.models.mixins import (
ActionMixin,
AsyncActionMixin,
AsyncCollectionMixin,
AsyncResourceMixin,
CollectionMixin,
ResourceMixin,
)
from connect.client.models.resourceset import AsyncResourceSet, ResourceSet
from connect.client.rql import R
class _NSBase:
def __init__(self, client, path):
self._client = client
self._path = path
@property
def path(self) -> str:
return self._path
def __getattr__(self, name):
if '_' in name:
name = name.replace('_', '-')
return self.collection(name)
def __iter__(self):
raise TypeError('A Namespace object is not iterable.')
def __call__(self, name):
return self.ns(name)
def collection(self, name: str):
"""
Returns a `[Async]Collection` object nested under this namespace object
identified by its name.
Usage:
```py3
devops_ns = client.ns('devops')
services = devops_ns.collection('products')
```
Concise form:
```py3
services = client('devops').services
```
Args:
name (str): The name of the collection to access.
Returns:
(Union[Collection, AsyncCollection]): Returns an object nested under this namespace
object identified by its name.
"""
if not isinstance(name, str):
raise TypeError('`name` must be a string.')
if not name:
raise ValueError('`name` must not be blank.')
return self._get_collection_class()(
self._client,
f'{self._path}/{name}',
)
def ns(self, name: str):
"""
Returns a `[Async]Namespace` object nested under this namespace
identified by its name.
Usage:
```py3
subscriptions_ns = client.ns('subscriptions')
nested_ns = subcriptions_ns.ns('nested')
```
Concise form:
```py3
nested_ns = client('subscriptions')('nested')
```
Args:
name (str): The name of the namespace to access.
Returns:
(Union[NS, AsyncNS]): Returns an object nested under this namespace
identified by its name.
"""
if not isinstance(name, str):
raise TypeError('`name` must be a string.')
if not name:
raise ValueError('`name` must not be blank.')
return self._get_namespace_class()(
self._client,
f'{self._path}/{name}',
)
def help(self):
self._client.print_help(self)
return self
def _get_collection_class(self):
raise NotImplementedError()
def _get_namespace_class(self):
raise NotImplementedError()
class NS(_NSBase):
def _get_collection_class(self):
return Collection
def _get_namespace_class(self):
return NS
class AsyncNS(_NSBase):
def _get_collection_class(self):
return AsyncCollection
def _get_namespace_class(self):
return AsyncNS
class _CollectionBase:
def __init__(self, client, path):
self._client = client
self._path = path
@property
def path(self):
return self._path
def __iter__(self):
raise TypeError('A Collection object is not iterable.')
def __getitem__(self, resource_id):
return self.resource(resource_id)
def __call__(self, name):
return self.action(name)
def all(self):
"""
Returns a `[Async]ResourceSet` object that that allow to access all the resources that
belong to this collection.
Returns:
(Union[ResourceSet, AsyncResourceSet]): Returns an object that that allow to access
all the resources that belong to this collection.
"""
return self._get_resourceset_class()(
self._client,
self._path,
)
def filter(self, *args, **kwargs):
"""
Returns a `[Async]ResourceSet` object.
The returned ResourceSet object will be filtered based on
the arguments and keyword arguments.
Arguments can be RQL filter expressions as strings
or R objects.
Usage:
```py3
rs = collection.filter('eq(field,value)', 'eq(another.field,value2)')
rs = collection.filter(R().field.eq('value'), R().another.field.eq('value2'))
```
All the arguments will be combined with logical **and**.
Filters can be also specified as keyword argument using the **__** (double underscore)
notation.
Usage:
```py3
rs = collection.filter(
field=value,
another__field=value,
field2__in=('a', 'b'),
field3__null=True,
)
```
Also keyword arguments will be combined with logical **and**.
Returns:
(Union[ResourceSet, AsyncResourceSet]): The returned ResourceSet object will be
filtered based on the arguments and keyword arguments.
"""
query = R()
for arg in args:
if isinstance(arg, str):
query &= R(_expr=arg)
continue
if isinstance(arg, R):
query &= arg
continue
raise TypeError(f'arguments must be string or R not {type(arg)}')
if kwargs:
query &= R(**kwargs)
return self._get_resourceset_class()(
self._client,
self._path,
query=query,
)
def resource(self, resource_id: str):
"""
Returns a `[Async]Resource` object that represent a resource that belong to
this collection identified by its unique identifier.
Usage:
```py3
resource = client.collection('products').resource('PRD-000-111-222')
```
Concise form:
```py3
resource = client.products['PRD-000-111-222']
```
Args:
resource_id (str): The unique identifier of the resource.
Returns:
(Union[Resource, AsyncResource]): Returns an object that represent a resource that
belong to this collection identified by its unique identifier.
"""
if not isinstance(resource_id, (str, int)):
raise TypeError('`resource_id` must be a string or int.')
if not resource_id:
raise ValueError('`resource_id` must not be blank.')
return self._get_resource_class()(
self._client,
f'{self._path}/{resource_id}',
)
def action(self, name: str):
"""
Returns an `[Async]Action` object that represent an action to perform
on this collection identified by its name.
Args:
name (str): The name of the action to perform.
Returns:
(Union[Action, AsyncAction]): Returns an object that represent an action to perform
on this collection identified by its name.
"""
if not isinstance(name, str):
raise TypeError('`name` must be a string.')
if not name:
raise ValueError('`name` must not be blank.')
return self._get_action_class()(
self._client,
f'{self._path}/{name}',
)
def help(self):
self._client.print_help(self)
return self
def _get_resource_class(self):
return NotImplementedError() # pragma: no cover
def _get_resourceset_class(self):
return NotImplementedError() # pragma: no cover
def _get_action_class(self):
raise NotImplementedError() # pragma: no cover
class Collection(_CollectionBase, CollectionMixin):
def _get_resource_class(self):
return Resource
def _get_resourceset_class(self):
return ResourceSet
def _get_action_class(self):
return Action
class AsyncCollection(_CollectionBase, AsyncCollectionMixin):
def _get_resource_class(self):
return AsyncResource
def _get_resourceset_class(self):
return AsyncResourceSet
def _get_action_class(self):
return AsyncAction
class _ResourceBase:
def __init__(self, client, path):
self._client = client
self._path = path
@property
def path(self):
return self._path
def __getattr__(self, name):
if '_' in name:
name = name.replace('_', '-')
return self.collection(name)
def __call__(self, name):
return self.action(name)
def collection(self, name: str):
"""
Returns a `[Async]Collection` object nested under this resource object
identified by its name.
Usage:
```py3
environments = (
client.ns("devops")
.collection("services")
.resource("SRVC-0000-1111")
.collection("environments")
)
```
Concise form:
```py3
services = client('devops').services['SRVC-0000-1111'].environments
```
Args:
name (str): The name of the collection to access.
Returns:
(Union[Collection, AsyncCollection]): Returns an object nested under this resource
object identified by its name.
""" # noqa: E501
if not isinstance(name, str):
raise TypeError('`name` must be a string.')
if not name:
raise ValueError('`name` must not be blank.')
return self._get_collection_class()(
self._client,
f'{self._path}/{name}',
)
def action(self, name: str):
"""
Returns an `[Async]Action` object that can be performed on this this resource object
identified by its name.
Usage:
```py3
approve_action = (
client.collection('requests')
.resource('PR-000-111-222')
.action('approve')
)
```
Concise form:
```py3
approve_action = client.requests[''PR-000-111-222']('approve')
```
Args:
name (str): The name of the action to perform.
Returns:
(Union[Action, AsyncAction]): Returns an object that can be performed on this this
resource object identified by its name.
"""
if not isinstance(name, str):
raise TypeError('`name` must be a string.')
if not name:
raise ValueError('`name` must not be blank.')
return self._get_action_class()(
self._client,
f'{self._path}/{name}',
)
def help(self):
self._client.print_help(self)
return self
def _get_collection_class(self):
raise NotImplementedError()
def _get_action_class(self):
raise NotImplementedError()
class Resource(_ResourceBase, ResourceMixin):
def _get_collection_class(self):
return Collection
def _get_action_class(self):
return Action
class AsyncResource(_ResourceBase, AsyncResourceMixin):
def _get_collection_class(self):
return AsyncCollection
def _get_action_class(self):
return AsyncAction
class _ActionBase:
def __init__(self, client, path):
self._client = client
self._path = path
@property
def path(self):
return self._path
def help(self):
self._client.print_help(self)
return self
class Action(_ActionBase, ActionMixin):
pass
class AsyncAction(_ActionBase, AsyncActionMixin):
pass