Repository: thrift Updated Branches: refs/heads/master 8733bf4c1 -> 4fcc74478
THRIFT-3499 Add package_prefix to python generator Client: Python Patch: Eric Klaus <[email protected]> This closes #755 Project: http://git-wip-us.apache.org/repos/asf/thrift/repo Commit: http://git-wip-us.apache.org/repos/asf/thrift/commit/4fcc7447 Tree: http://git-wip-us.apache.org/repos/asf/thrift/tree/4fcc7447 Diff: http://git-wip-us.apache.org/repos/asf/thrift/diff/4fcc7447 Branch: refs/heads/master Commit: 4fcc74478ff62da677215eddcbaacacab76c7e41 Parents: 8733bf4 Author: Eric Klaus <[email protected]> Authored: Tue Dec 15 09:55:05 2015 -0600 Committer: Jens Geyer <[email protected]> Committed: Sat Jan 9 00:21:35 2016 +0100 ---------------------------------------------------------------------- compiler/cpp/src/generate/t_py_generator.cc | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/thrift/blob/4fcc7447/compiler/cpp/src/generate/t_py_generator.cc ---------------------------------------------------------------------- diff --git a/compiler/cpp/src/generate/t_py_generator.cc b/compiler/cpp/src/generate/t_py_generator.cc index a90bfca..928bc46 100644 --- a/compiler/cpp/src/generate/t_py_generator.cc +++ b/compiler/cpp/src/generate/t_py_generator.cc @@ -68,6 +68,12 @@ public: iter = parsed_options.find("slots"); gen_slots_ = (iter != parsed_options.end()); + iter = parsed_options.find("package_prefix"); + if (iter != parsed_options.end()) { + package_prefix_ = iter->second; + } + + iter = parsed_options.find("dynamic"); gen_dynamic_ = (iter != parsed_options.end()); @@ -242,7 +248,7 @@ public: return sub_namespace == "twisted"; } - static std::string get_real_py_module(const t_program* program, bool gen_twisted) { + static std::string get_real_py_module(const t_program* program, bool gen_twisted, std::string package_dir="") { if (gen_twisted) { std::string twisted_module = program->get_namespace("py.twisted"); if (!twisted_module.empty()) { @@ -254,7 +260,7 @@ public: if (real_module.empty()) { return program->get_name(); } - return real_module; + return package_dir + real_module; } static bool is_immutable(t_type* ttype) { @@ -305,6 +311,8 @@ private: */ string coding_; + string package_prefix_; + /** * File streams */ @@ -387,7 +395,7 @@ string t_py_generator::render_includes() { const vector<t_program*>& includes = program_->get_includes(); string result = ""; for (size_t i = 0; i < includes.size(); ++i) { - result += "import " + get_real_py_module(includes[i], gen_twisted_) + ".ttypes\n"; + result += "import " + get_real_py_module(includes[i], gen_twisted_, package_prefix_) + ".ttypes\n"; } return result; } @@ -1045,7 +1053,7 @@ void t_py_generator::generate_service(t_service* tservice) { if (tservice->get_extends() != NULL) { f_service_ << "import " - << get_real_py_module(tservice->get_extends()->get_program(), gen_twisted_) << "." + << get_real_py_module(tservice->get_extends()->get_program(), gen_twisted_, package_prefix_) << "." << tservice->get_extends()->get_name() << endl; } @@ -2498,10 +2506,10 @@ string t_py_generator::type_name(t_type* ttype) { t_program* program = ttype->get_program(); if (ttype->is_service()) { - return get_real_py_module(program, gen_twisted_) + "." + ttype->get_name(); + return get_real_py_module(program, gen_twisted_, package_prefix_) + "." + ttype->get_name(); } if (program != NULL && program != program_) { - return get_real_py_module(program, gen_twisted_) + ".ttypes." + ttype->get_name(); + return get_real_py_module(program, gen_twisted_, package_prefix_) + ".ttypes." + ttype->get_name(); } return ttype->get_name(); } @@ -2598,4 +2606,6 @@ THRIFT_REGISTER_GENERATOR( " dynfrozen=CLS Derive generated immutable classes from class CLS instead of TFrozenBase.\n" " dynexc=CLS Derive generated exceptions from CLS instead of TExceptionBase.\n" " dynimport='from foo.bar import CLS'\n" - " Add an import line to generated code to find the dynbase class.\n") + " Add an import line to generated code to find the dynbase class.\n" + " package_prefix='top.package.'\n" + " Package prefix for generated files.\n")
