Index: mojo/public/python/mojo/bindings/reflection.py |
diff --git a/mojo/public/python/mojo/bindings/reflection.py b/mojo/public/python/mojo/bindings/reflection.py |
index d7ba5241122a51dc79a48577ae68c215eac12bf7..5ee2c1dd0eec03afea0198265ce73692baa4c7ca 100644 |
--- a/mojo/public/python/mojo/bindings/reflection.py |
+++ b/mojo/public/python/mojo/bindings/reflection.py |
@@ -18,6 +18,7 @@ class MojoEnumType(type): |
This will define a enum with 3 values, A = 0, B = 1 and C = 5. |
""" |
+ |
def __new__(mcs, name, bases, dictionary): |
class_members = { |
'__new__': None, |
@@ -37,3 +38,55 @@ class MojoEnumType(type): |
def __delattr__(mcs, key): |
raise AttributeError, 'can\'t delete attribute' |
+ |
+ |
+class MojoStructType(type): |
+ """Meta class for structs. |
+ |
+ Usage: |
+ class MyStruct(object): |
+ __metaclass__ = MojoStructType |
+ DESCRIPTOR = { |
+ 'constants': { |
+ 'C1': 1, |
+ 'C2': 2, |
+ }, |
+ 'enums': { |
+ 'ENUM1': [ |
+ ('V1', 1), |
+ 'V2', |
+ ], |
+ 'ENUM2': [ |
+ ('V1', 1), |
+ 'V2', |
+ ], |
+ }, |
+ } |
+ |
+ This will define an struct, with 2 constants C1 and C2, and 2 enums ENUM1 |
+ and ENUM2, each of those having 2 values, V1 and V2. |
+ """ |
+ |
+ def __new__(mcs, name, bases, dictionary): |
+ class_members = { |
+ '__slots__': [], |
sdefresne
2014/09/09 08:51:57
nit: maybe you want to keep the __module__ here fr
qsr
2014/09/09 08:55:10
I fixed all of this in the following CL (https://c
sdefresne
2014/09/09 09:30:36
Ok, no need to backport.
|
+ } |
+ descriptor = dictionary.pop('DESCRIPTOR', {}) |
+ |
+ # Add constants |
+ class_members.update(descriptor.get('constants', {})) |
+ |
+ # Add enums |
+ enums = descriptor.get('enums', {}) |
+ for key in enums: |
+ class_members[key] = MojoEnumType(key, |
+ (object,), |
+ { 'VALUES': enums[key] }) |
+ |
+ return type.__new__(mcs, name, bases, class_members) |
+ |
+ def __setattr__(mcs, key, value): |
+ raise AttributeError, 'can\'t set attribute' |
+ |
+ def __delattr__(mcs, key): |
+ raise AttributeError, 'can\'t delete attribute' |