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 33Instances are created as properties with the same name as the enum they wrap 34on proto classes. For usage, see: 35 reflection_test.py 36""" 37 38__author__ = 'rabsatt@google.com (Kevin Rabsatt)' 39 40import six 41 42 43class EnumTypeWrapper(object): 44 """A utility for finding the names of enum values.""" 45 46 DESCRIPTOR = None 47 48 def __init__(self, enum_type): 49 """Inits EnumTypeWrapper with an EnumDescriptor.""" 50 self._enum_type = enum_type 51 self.DESCRIPTOR = enum_type # pylint: disable=invalid-name 52 53 def Name(self, number): # pylint: disable=invalid-name 54 """Returns a string containing the name of an enum value.""" 55 try: 56 return self._enum_type.values_by_number[number].name 57 except KeyError: 58 pass # fall out to break exception chaining 59 60 if not isinstance(number, six.integer_types): 61 raise TypeError( 62 'Enum value for {} must be an int, but got {} {!r}.'.format( 63 self._enum_type.name, type(number), number)) 64 else: 65 # repr here to handle the odd case when you pass in a boolean. 66 raise ValueError('Enum {} has no name defined for value {!r}'.format( 67 self._enum_type.name, number)) 68 69 def Value(self, name): # pylint: disable=invalid-name 70 """Returns the value corresponding to the given enum name.""" 71 try: 72 return self._enum_type.values_by_name[name].number 73 except KeyError: 74 pass # fall out to break exception chaining 75 raise ValueError('Enum {} has no value defined for name {!r}'.format( 76 self._enum_type.name, name)) 77 78 def keys(self): 79 """Return a list of the string names in the enum. 80 81 Returns: 82 A list of strs, in the order they were defined in the .proto file. 83 """ 84 85 return [value_descriptor.name 86 for value_descriptor in self._enum_type.values] 87 88 def values(self): 89 """Return a list of the integer values in the enum. 90 91 Returns: 92 A list of ints, in the order they were defined in the .proto file. 93 """ 94 95 return [value_descriptor.number 96 for value_descriptor in self._enum_type.values] 97 98 def items(self): 99 """Return a list of the (name, value) pairs of the enum. 100 101 Returns: 102 A list of (str, int) pairs, in the order they were defined 103 in the .proto file. 104 """ 105 return [(value_descriptor.name, value_descriptor.number) 106 for value_descriptor in self._enum_type.values] 107 108 def __getattr__(self, name): 109 """Returns the value corresponding to the given enum name.""" 110 try: 111 return self._enum_type.values_by_name[name].number 112 except KeyError: 113 pass # fall out to break exception chaining 114 raise AttributeError('Enum {} has no value defined for name {!r}'.format( 115 self._enum_type.name, name)) 116