Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1294)

Unified Diff: mojo/public/python/mojo/bindings/reflection.py

Issue 538613005: mojo: Start generating structs for python bindings (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Make structs immutable. Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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 2b7a058aeb69f9c4ba756e2143334f729a640a94..1cc46dfc8be7c242d825bdc18a1cae41a7ef07f7 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,
@@ -41,3 +42,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__': [],
+ }
+ 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'

Powered by Google App Engine
This is Rietveld 408576698