Chromium Code Reviews| 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 |