From 8266e012c5b473b136421c124d42f82b07892351 Mon Sep 17 00:00:00 2001 From: David Reiss Date: Wed, 21 Oct 2009 06:09:16 +0000 Subject: [PATCH] pynames git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@827893 13f79535-47bb-0310-9956-ffa450edef68 --- compiler/cpp/src/generate/t_py_generator.cc | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/compiler/cpp/src/generate/t_py_generator.cc b/compiler/cpp/src/generate/t_py_generator.cc index ec4095b0..8c545024 100644 --- a/compiler/cpp/src/generate/t_py_generator.cc +++ b/compiler/cpp/src/generate/t_py_generator.cc @@ -354,6 +354,8 @@ void t_py_generator::generate_typedef(t_typedef* ttypedef) {} * @param tenum The enumeration */ void t_py_generator::generate_enum(t_enum* tenum) { + std::ostringstream to_string_mapping, from_string_mapping; + f_types_ << "class " << tenum->get_name() << (gen_newstyle_ ? "(object)" : "") << @@ -361,6 +363,9 @@ void t_py_generator::generate_enum(t_enum* tenum) { indent_up(); generate_python_docstring(f_types_, tenum); + to_string_mapping << indent() << "_VALUES_TO_NAMES = {" << endl; + from_string_mapping << indent() << "_NAMES_TO_VALUES = {" << endl; + vector constants = tenum->get_constants(); vector::iterator c_iter; int value = -1; @@ -373,10 +378,21 @@ void t_py_generator::generate_enum(t_enum* tenum) { f_types_ << indent() << (*c_iter)->get_name() << " = " << value << endl; + + // Dictionaries to/from string names of enums + to_string_mapping << + indent() << indent() << value << ": \"" << + escape_string((*c_iter)->get_name()) << "\"," << endl; + from_string_mapping << + indent() << indent() << '"' << escape_string((*c_iter)->get_name()) << + "\": " << value << ',' << endl; } + to_string_mapping << indent() << "}" << endl; + from_string_mapping << indent() << "}" << endl; indent_down(); f_types_ << endl; + f_types_ << to_string_mapping.str() << endl << from_string_mapping.str() << endl; } /** -- 2.17.1