1 # Protocol Buffers - Google's data interchange format 2 # Copyright 2008 Google Inc. All rights reserved. 3 # https://developers.google.com/protocol-buffers/ 4 # 5 # Redistribution and use in source and binary forms, with or without 6 # modification, are permitted provided that the following conditions are 7 # met: 8 # 9 # * Redistributions of source code must retain the above copyright 10 # notice, this list of conditions and the following disclaimer. 11 # * Redistributions in binary form must reproduce the above 12 # copyright notice, this list of conditions and the following disclaimer 13 # in the documentation and/or other materials provided with the 14 # distribution. 15 # * Neither the name of Google Inc. nor the names of its 16 # contributors may be used to endorse or promote products derived from 17 # this software without specific prior written permission. 18 # 19 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 31 """A simple wrapper around enum types to expose utility functions. 32 33 Instances are created as properties with the same name as the enum they wrap 34 on proto classes. For usage, see: 35 reflection_test.py 36 """ 37 38 __author__ = 'rabsatt (at] google.com (Kevin Rabsatt)' 39 40 41 class EnumTypeWrapper(object): 42 """A utility for finding the names of enum values.""" 43 44 DESCRIPTOR = None 45 46 def __init__(self, enum_type): 47 """Inits EnumTypeWrapper with an EnumDescriptor.""" 48 self._enum_type = enum_type 49 self.DESCRIPTOR = enum_type; 50 51 def Name(self, number): 52 """Returns a string containing the name of an enum value.""" 53 if number in self._enum_type.values_by_number: 54 return self._enum_type.values_by_number[number].name 55 raise ValueError('Enum %s has no name defined for value %d' % ( 56 self._enum_type.name, number)) 57 58 def Value(self, name): 59 """Returns the value coresponding to the given enum name.""" 60 if name in self._enum_type.values_by_name: 61 return self._enum_type.values_by_name[name].number 62 raise ValueError('Enum %s has no value defined for name %s' % ( 63 self._enum_type.name, name)) 64 65 def keys(self): 66 """Return a list of the string names in the enum. 67 68 These are returned in the order they were defined in the .proto file. 69 """ 70 71 return [value_descriptor.name 72 for value_descriptor in self._enum_type.values] 73 74 def values(self): 75 """Return a list of the integer values in the enum. 76 77 These are returned in the order they were defined in the .proto file. 78 """ 79 80 return [value_descriptor.number 81 for value_descriptor in self._enum_type.values] 82 83 def items(self): 84 """Return a list of the (name, value) pairs of the enum. 85 86 These are returned in the order they were defined in the .proto file. 87 """ 88 return [(value_descriptor.name, value_descriptor.number) 89 for value_descriptor in self._enum_type.values] 90