From 40d55f9dac7c732e568a2d0a559d15d74f8fde6e Mon Sep 17 00:00:00 2001
From: Nathaniel Manista <nathaniel@google.com>
Date: Tue, 14 Mar 2017 00:01:35 +0000
Subject: [PATCH] Add import prefix to Python generator

This is needed by a customer with a nontraditional codebase.
---
 src/compiler/python_generator.cc | 22 ++++++++++++++--------
 src/compiler/python_generator.h  |  3 +++
 2 files changed, 17 insertions(+), 8 deletions(-)

diff --git a/src/compiler/python_generator.cc b/src/compiler/python_generator.cc
index 4841da8da8..242ce06a16 100644
--- a/src/compiler/python_generator.cc
+++ b/src/compiler/python_generator.cc
@@ -101,18 +101,20 @@ class IndentScope {
 // TODO(https://github.com/google/protobuf/issues/888):
 // Export `ModuleName` from protobuf's
 // `src/google/protobuf/compiler/python/python_generator.cc` file.
-grpc::string ModuleName(const grpc::string& filename) {
+grpc::string ModuleName(const grpc::string& filename,
+                        const grpc::string& import_prefix) {
   grpc::string basename = StripProto(filename);
   basename = StringReplace(basename, "-", "_");
   basename = StringReplace(basename, "/", ".");
-  return basename + "_pb2";
+  return import_prefix + basename + "_pb2";
 }
 
 // TODO(https://github.com/google/protobuf/issues/888):
 // Export `ModuleAlias` from protobuf's
 // `src/google/protobuf/compiler/python/python_generator.cc` file.
-grpc::string ModuleAlias(const grpc::string& filename) {
-  grpc::string module_name = ModuleName(filename);
+grpc::string ModuleAlias(const grpc::string& filename,
+                         const grpc::string& import_prefix) {
+  grpc::string module_name = ModuleName(filename, import_prefix);
   // We can't have dots in the module name, so we replace each with _dot_.
   // But that could lead to a collision between a.b and a_dot_b, so we also
   // duplicate each underscore.
@@ -189,7 +191,7 @@ bool PrivateGenerator::GetModuleAndMessagePath(const Descriptor* type,
   grpc::string generator_file_name = file->name();
   grpc::string module;
   if (generator_file_name != file_name || generate_in_pb2_grpc) {
-    module = ModuleAlias(file_name) + ".";
+    module = ModuleAlias(file_name, config.import_prefix) + ".";
   } else {
     module = "";
   }
@@ -666,8 +668,10 @@ bool PrivateGenerator::PrintPreamble() {
         for (int k = 0; k < 2; ++k) {
           const Descriptor* type = types[k];
           grpc::string type_file_name = type->file()->name();
-          grpc::string module_name = ModuleName(type_file_name);
-          grpc::string module_alias = ModuleAlias(type_file_name);
+          grpc::string module_name =
+              ModuleName(type_file_name, config.import_prefix);
+          grpc::string module_alias =
+              ModuleAlias(type_file_name, config.import_prefix);
           imports_set.insert(std::make_tuple(module_name, module_alias));
         }
       }
@@ -766,7 +770,9 @@ pair<bool, grpc::string> PrivateGenerator::GetGrpcServices() {
 }  // namespace
 
 GeneratorConfiguration::GeneratorConfiguration()
-    : grpc_package_root("grpc"), beta_package_root("grpc.beta") {}
+    : grpc_package_root("grpc"),
+      beta_package_root("grpc.beta"),
+      import_prefix("") {}
 
 PythonGrpcGenerator::PythonGrpcGenerator(const GeneratorConfiguration& config)
     : config_(config) {}
diff --git a/src/compiler/python_generator.h b/src/compiler/python_generator.h
index 6a95255d40..b91059fad7 100644
--- a/src/compiler/python_generator.h
+++ b/src/compiler/python_generator.h
@@ -45,7 +45,10 @@ namespace grpc_python_generator {
 struct GeneratorConfiguration {
   GeneratorConfiguration();
   grpc::string grpc_package_root;
+  // TODO(https://github.com/grpc/grpc/issues/8622): Drop this.
   grpc::string beta_package_root;
+  // TODO(https://github.com/google/protobuf/issues/888): Drop this.
+  grpc::string import_prefix;
 };
 
 class PythonGrpcGenerator : public grpc::protobuf::compiler::CodeGenerator {
-- 
GitLab