From b8ab1c7f5b9580891d43d66888880a95bf629deb Mon Sep 17 00:00:00 2001
From: Michael Hanselmann <hansmi@google.com>
Date: Wed, 14 Sep 2011 12:21:53 +0200
Subject: [PATCH] baserlib: Function to retrieve opcodes used by handler

This will be used to verify opcodes used by RAPI.

Signed-off-by: Michael Hanselmann <hansmi@google.com>
Reviewed-by: Iustin Pop <iustin@google.com>
---
 lib/rapi/baserlib.py | 26 +++++++++++++++++++++-----
 1 file changed, 21 insertions(+), 5 deletions(-)

diff --git a/lib/rapi/baserlib.py b/lib/rapi/baserlib.py
index c76ece259..314de121a 100644
--- a/lib/rapi/baserlib.py
+++ b/lib/rapi/baserlib.py
@@ -48,6 +48,18 @@ _SUPPORTED_METHODS = frozenset([
   ])
 
 
+def _BuildOpcodeAttributes():
+  """Builds list of attributes used for per-handler opcodes.
+
+  """
+  return [(method, "%s_OPCODE" % method, "%s_RENAME" % method,
+           "Get%sOpInput" % method.capitalize())
+          for method in _SUPPORTED_METHODS]
+
+
+_OPCODE_ATTRS = _BuildOpcodeAttributes()
+
+
 def BuildUriList(ids, uri_format, uri_fields=("name", "uri")):
   """Builds a URI list as used by index resources.
 
@@ -408,14 +420,18 @@ class ResourceBase(object):
                                     " daemon: %s" % err)
 
 
+def GetResourceOpcodes(cls):
+  """Returns all opcodes used by a resource.
+
+  """
+  return frozenset(filter(None, (getattr(cls, op_attr, None)
+                                 for (_, op_attr, _, _) in _OPCODE_ATTRS)))
+
+
 class _MetaOpcodeResource(type):
   """Meta class for RAPI resources.
 
   """
-  _ATTRS = [(method, "%s_OPCODE" % method, "%s_RENAME" % method,
-             "Get%sOpInput" % method.capitalize())
-            for method in _SUPPORTED_METHODS]
-
   def __call__(mcs, *args, **kwargs):
     """Instantiates class and patches it for use by the RAPI daemon.
 
@@ -423,7 +439,7 @@ class _MetaOpcodeResource(type):
     # Access to private attributes of a client class, pylint: disable=W0212
     obj = type.__call__(mcs, *args, **kwargs)
 
-    for (method, op_attr, rename_attr, fn_attr) in mcs._ATTRS:
+    for (method, op_attr, rename_attr, fn_attr) in _OPCODE_ATTRS:
       try:
         opcode = getattr(obj, op_attr)
       except AttributeError:
-- 
GitLab