aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar Nathaniel Manista <nathaniel@google.com>2017-03-14 09:33:28 -0700
committerGravatar GitHub <noreply@github.com>2017-03-14 09:33:28 -0700
commitf0fb7413ea0a185ca29b0102bc12e9a39382d6d9 (patch)
tree995aea1f729575272acce5cce1e39b3646034ca0
parentfce0f049039dad7e65b883c686a620be7ff1fb98 (diff)
parent40d55f9dac7c732e568a2d0a559d15d74f8fde6e (diff)
Merge pull request #10141 from nathanielmanistaatgoogle/import-prefix
Add import prefix to Python code generator.
-rw-r--r--src/compiler/python_generator.cc22
-rw-r--r--src/compiler/python_generator.h3
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 {