Revision 286c0df2 lib/rapi/baserlib.py
b/lib/rapi/baserlib.py | ||
---|---|---|
56 | 56 |
|
57 | 57 |
""" |
58 | 58 |
return [(method, "%s_OPCODE" % method, "%s_RENAME" % method, |
59 |
"Get%sOpInput" % method.capitalize()) |
|
59 |
"%s_ALIASES" % method, "Get%sOpInput" % method.capitalize())
|
|
60 | 60 |
for method in _SUPPORTED_METHODS] |
61 | 61 |
|
62 | 62 |
|
... | ... | |
406 | 406 |
|
407 | 407 |
""" |
408 | 408 |
return frozenset(filter(None, (getattr(cls, op_attr, None) |
409 |
for (_, op_attr, _, _) in OPCODE_ATTRS))) |
|
409 |
for (_, op_attr, _, _, _) in OPCODE_ATTRS)))
|
|
410 | 410 |
|
411 | 411 |
|
412 | 412 |
def GetHandlerAccess(handler, method): |
... | ... | |
420 | 420 |
return getattr(handler, "%s_ACCESS" % method, None) |
421 | 421 |
|
422 | 422 |
|
423 |
def GetHandler(get_fn, aliases): |
|
424 |
result = get_fn() |
|
425 |
if not isinstance(result, dict) or aliases is None: |
|
426 |
return result |
|
427 |
|
|
428 |
for (param, alias) in aliases.items(): |
|
429 |
if param in result: |
|
430 |
if alias in result: |
|
431 |
raise http.HttpBadRequest("Parameter '%s' has an alias of '%s', but" |
|
432 |
" both values are present in response" % |
|
433 |
(param, alias)) |
|
434 |
result[alias] = result[param] |
|
435 |
|
|
436 |
return result |
|
437 |
|
|
438 |
|
|
423 | 439 |
class _MetaOpcodeResource(type): |
424 | 440 |
"""Meta class for RAPI resources. |
425 | 441 |
|
... | ... | |
431 | 447 |
# Access to private attributes of a client class, pylint: disable=W0212 |
432 | 448 |
obj = type.__call__(mcs, *args, **kwargs) |
433 | 449 |
|
434 |
for (method, op_attr, rename_attr, fn_attr) in OPCODE_ATTRS: |
|
450 |
for (method, op_attr, rename_attr, aliases_attr, fn_attr) in OPCODE_ATTRS:
|
|
435 | 451 |
if hasattr(obj, method): |
436 |
# If the method handler is already defined, "*_RENAME" or "Get*OpInput"
|
|
437 |
# shouldn't be (they're only used by the automatically generated
|
|
438 |
# handler) |
|
452 |
# If the method handler is already defined, "*_RENAME" or |
|
453 |
# "Get*OpInput" shouldn't be (they're only used by the automatically
|
|
454 |
# generated handler)
|
|
439 | 455 |
assert not hasattr(obj, rename_attr) |
440 | 456 |
assert not hasattr(obj, fn_attr) |
457 |
|
|
458 |
# The aliases are allowed only on GET calls |
|
459 |
assert not hasattr(obj, aliases_attr) or method == http.HTTP_GET |
|
460 |
|
|
461 |
# GET methods can add aliases of values they return under a different |
|
462 |
# name |
|
463 |
if method == http.HTTP_GET and hasattr(obj, aliases_attr): |
|
464 |
setattr(obj, method, |
|
465 |
compat.partial(GetHandler, getattr(obj, method), |
|
466 |
getattr(obj, aliases_attr))) |
|
441 | 467 |
else: |
442 | 468 |
# Try to generate handler method on handler instance |
443 | 469 |
try: |
... | ... | |
473 | 499 |
automatically generate a GET handler submitting the opcode |
474 | 500 |
@cvar GET_RENAME: Set this to rename parameters in the GET handler (see |
475 | 501 |
L{baserlib.FillOpcode}) |
502 |
@cvar GET_ALIASES: Set this to duplicate return values in GET results (see |
|
503 |
L{baserlib.GetHandler}) |
|
476 | 504 |
@ivar GetGetOpInput: Define this to override the default method for |
477 | 505 |
getting opcode parameters (see L{baserlib.OpcodeResource._GetDefaultData}) |
478 | 506 |
|
Also available in: Unified diff