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

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: Follow sdefresne suugestions. 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..95e0c7bdb0dac52b7057014c60af4f1f307d29eb
--- /dev/null
+++ b/mojo/public/python/mojo/bindings/reflection.py
@@ -0,0 +1,40 @@
+# 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,
+ }
+ current_enum_value = 0
+ for value in dictionary.pop('VALUES', []):
+ if isinstance(value, str):
+ key, enum_value = value, current_enum_value
+ elif isinstance(value, tuple):
+ key, enum_value = value
+ else:
+ raise ValueError('incorrect value: %r' % value)
+ if isinstance(key, str) and isinstance(enum_value, int):
+ class_members[key], current_enum_value = enum_value, enum_value + 1
pkl (ping after 24h if needed) 2014/09/04 18:07:20 This is going to silently overwrite previously def
qsr 2014/09/05 11:46:02 Not really, if that happens, that must be caught a
+ else:
+ raise ValueError('incorrect value: %r' % value)
+ return type.__new__(mcs, name, bases, class_members)
+
+ def __setattr__(mcs, key, value):
+ raise NotImplementedError
« no previous file with comments | « mojo/public/python/mojo/bindings/__init__.py ('k') | mojo/public/tools/bindings/generators/mojom_python_generator.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698