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 |
new file mode 100644 |
index 0000000000000000000000000000000000000000..d7ba5241122a51dc79a48577ae68c215eac12bf7 |
--- /dev/null |
+++ b/mojo/public/python/mojo/bindings/reflection.py |
@@ -0,0 +1,39 @@ |
+# Copyright 2014 The Chromium Authors. All rights reserved. |
+# Use of this source code is governed by a BSD-style license that can be |
+# found in the LICENSE file. |
+ |
+"""The meta classes used by the mojo python bindings.""" |
+ |
+class MojoEnumType(type): |
+ """Meta class for enumerations. |
+ |
+ Usage: |
+ class MyEnum(object): |
+ __metaclass__ = MojoEnumType |
+ VALUES = [ |
+ ('A', 0), |
+ 'B', |
+ ('C', 5), |
+ ] |
+ |
+ 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, |
sdefresne
2014/09/09 08:46:10
nit: you probably want to keep __module__ defined
|
+ } |
+ for value in dictionary.pop('VALUES', []): |
+ if not isinstance(value, tuple): |
+ raise ValueError('incorrect value: %r' % value) |
+ key, enum_value = value |
+ if isinstance(key, str) and isinstance(enum_value, int): |
+ class_members[key] = enum_value |
+ else: |
+ raise ValueError('incorrect value: %r' % value) |
+ 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' |