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

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

Issue 545433002: mojo: Generate top level enums for python bindings. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Remove unhelpful conditional 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
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'

Powered by Google App Engine
This is Rietveld 408576698