| OLD | NEW |
| 1 // Protocol Buffers - Google's data interchange format | 1 // Protocol Buffers - Google's data interchange format |
| 2 // Copyright 2008 Google Inc. All rights reserved. | 2 // Copyright 2008 Google Inc. All rights reserved. |
| 3 // http://code.google.com/p/protobuf/ | 3 // https://developers.google.com/protocol-buffers/ |
| 4 // | 4 // |
| 5 // Redistribution and use in source and binary forms, with or without | 5 // Redistribution and use in source and binary forms, with or without |
| 6 // modification, are permitted provided that the following conditions are | 6 // modification, are permitted provided that the following conditions are |
| 7 // met: | 7 // met: |
| 8 // | 8 // |
| 9 // * Redistributions of source code must retain the above copyright | 9 // * Redistributions of source code must retain the above copyright |
| 10 // notice, this list of conditions and the following disclaimer. | 10 // notice, this list of conditions and the following disclaimer. |
| 11 // * Redistributions in binary form must reproduce the above | 11 // * Redistributions in binary form must reproduce the above |
| 12 // copyright notice, this list of conditions and the following disclaimer | 12 // copyright notice, this list of conditions and the following disclaimer |
| 13 // in the documentation and/or other materials provided with the | 13 // in the documentation and/or other materials provided with the |
| 14 // distribution. | 14 // distribution. |
| 15 // * Neither the name of Google Inc. nor the names of its | 15 // * Neither the name of Google Inc. nor the names of its |
| 16 // contributors may be used to endorse or promote products derived from | 16 // contributors may be used to endorse or promote products derived from |
| 17 // this software without specific prior written permission. | 17 // this software without specific prior written permission. |
| 18 // | 18 // |
| 19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | 30 |
| 31 //#PY25 compatible generated code for GAE. |
| 32 // Copyright 2007 Google Inc. All Rights Reserved. |
| 31 // Author: robinson@google.com (Will Robinson) | 33 // Author: robinson@google.com (Will Robinson) |
| 32 // | 34 // |
| 33 // This module outputs pure-Python protocol message classes that will | 35 // This module outputs pure-Python protocol message classes that will |
| 34 // largely be constructed at runtime via the metaclass in reflection.py. | 36 // largely be constructed at runtime via the metaclass in reflection.py. |
| 35 // In other words, our job is basically to output a Python equivalent | 37 // In other words, our job is basically to output a Python equivalent |
| 36 // of the C++ *Descriptor objects, and fix up all circular references | 38 // of the C++ *Descriptor objects, and fix up all circular references |
| 37 // within these objects. | 39 // within these objects. |
| 38 // | 40 // |
| 39 // Note that the runtime performance of protocol message classes created in | 41 // Note that the runtime performance of protocol message classes created in |
| 40 // this way is expected to be lousy. The plan is to create an alternate | 42 // this way is expected to be lousy. The plan is to create an alternate |
| 41 // generator that outputs a Python/C extension module that lets | 43 // generator that outputs a Python/C extension module that lets |
| 42 // performance-minded Python code leverage the fast C++ implementation | 44 // performance-minded Python code leverage the fast C++ implementation |
| 43 // directly. | 45 // directly. |
| 44 | 46 |
| 47 #include <google/protobuf/stubs/hash.h> |
| 45 #include <limits> | 48 #include <limits> |
| 46 #include <map> | 49 #include <map> |
| 50 #include <memory> |
| 51 #ifndef _SHARED_PTR_H |
| 52 #include <google/protobuf/stubs/shared_ptr.h> |
| 53 #endif |
| 54 #include <string> |
| 47 #include <utility> | 55 #include <utility> |
| 48 #include <string> | |
| 49 #include <vector> | 56 #include <vector> |
| 50 | 57 |
| 51 #include <google/protobuf/compiler/python/python_generator.h> | 58 #include <google/protobuf/compiler/python/python_generator.h> |
| 52 #include <google/protobuf/descriptor.pb.h> | 59 #include <google/protobuf/descriptor.pb.h> |
| 53 | 60 |
| 61 #include <google/protobuf/stubs/logging.h> |
| 54 #include <google/protobuf/stubs/common.h> | 62 #include <google/protobuf/stubs/common.h> |
| 55 #include <google/protobuf/stubs/stringprintf.h> | 63 #include <google/protobuf/stubs/stringprintf.h> |
| 56 #include <google/protobuf/io/printer.h> | 64 #include <google/protobuf/io/printer.h> |
| 57 #include <google/protobuf/descriptor.h> | 65 #include <google/protobuf/descriptor.h> |
| 58 #include <google/protobuf/io/zero_copy_stream.h> | 66 #include <google/protobuf/io/zero_copy_stream.h> |
| 59 #include <google/protobuf/stubs/strutil.h> | 67 #include <google/protobuf/stubs/strutil.h> |
| 60 #include <google/protobuf/stubs/substitute.h> | 68 #include <google/protobuf/stubs/substitute.h> |
| 61 | 69 |
| 62 namespace google { | 70 namespace google { |
| 63 namespace protobuf { | 71 namespace protobuf { |
| (...skipping 14 matching lines...) Expand all Loading... |
| 78 | 86 |
| 79 // Returns the Python module name expected for a given .proto filename. | 87 // Returns the Python module name expected for a given .proto filename. |
| 80 string ModuleName(const string& filename) { | 88 string ModuleName(const string& filename) { |
| 81 string basename = StripProto(filename); | 89 string basename = StripProto(filename); |
| 82 StripString(&basename, "-", '_'); | 90 StripString(&basename, "-", '_'); |
| 83 StripString(&basename, "/", '.'); | 91 StripString(&basename, "/", '.'); |
| 84 return basename + "_pb2"; | 92 return basename + "_pb2"; |
| 85 } | 93 } |
| 86 | 94 |
| 87 | 95 |
| 96 // Returns the alias we assign to the module of the given .proto filename |
| 97 // when importing. See testPackageInitializationImport in |
| 98 // google/protobuf/python/reflection_test.py |
| 99 // to see why we need the alias. |
| 100 string ModuleAlias(const string& filename) { |
| 101 string module_name = ModuleName(filename); |
| 102 // We can't have dots in the module name, so we replace each with _dot_. |
| 103 // But that could lead to a collision between a.b and a_dot_b, so we also |
| 104 // duplicate each underscore. |
| 105 GlobalReplaceSubstring("_", "__", &module_name); |
| 106 GlobalReplaceSubstring(".", "_dot_", &module_name); |
| 107 return module_name; |
| 108 } |
| 109 |
| 110 |
| 111 // Returns an import statement of form "from X.Y.Z import T" for the given |
| 112 // .proto filename. |
| 113 string ModuleImportStatement(const string& filename) { |
| 114 string module_name = ModuleName(filename); |
| 115 int last_dot_pos = module_name.rfind('.'); |
| 116 if (last_dot_pos == string::npos) { |
| 117 // NOTE(petya): this is not tested as it would require a protocol buffer |
| 118 // outside of any package, and I don't think that is easily achievable. |
| 119 return "import " + module_name; |
| 120 } else { |
| 121 return "from " + module_name.substr(0, last_dot_pos) + " import " + |
| 122 module_name.substr(last_dot_pos + 1); |
| 123 } |
| 124 } |
| 125 |
| 126 |
| 88 // Returns the name of all containing types for descriptor, | 127 // Returns the name of all containing types for descriptor, |
| 89 // in order from outermost to innermost, followed by descriptor's | 128 // in order from outermost to innermost, followed by descriptor's |
| 90 // own name. Each name is separated by |separator|. | 129 // own name. Each name is separated by |separator|. |
| 91 template <typename DescriptorT> | 130 template <typename DescriptorT> |
| 92 string NamePrefixedWithNestedTypes(const DescriptorT& descriptor, | 131 string NamePrefixedWithNestedTypes(const DescriptorT& descriptor, |
| 93 const string& separator) { | 132 const string& separator) { |
| 94 string name = descriptor.name(); | 133 string name = descriptor.name(); |
| 95 for (const Descriptor* current = descriptor.containing_type(); | 134 for (const Descriptor* current = descriptor.containing_type(); |
| 96 current != NULL; current = current->containing_type()) { | 135 current != NULL; current = current->containing_type()) { |
| 97 name = current->name() + separator + name; | 136 name = current->name() + separator + name; |
| (...skipping 23 matching lines...) Expand all Loading... |
| 121 | 160 |
| 122 | 161 |
| 123 // Prints the common boilerplate needed at the top of every .py | 162 // Prints the common boilerplate needed at the top of every .py |
| 124 // file output by this generator. | 163 // file output by this generator. |
| 125 void PrintTopBoilerplate( | 164 void PrintTopBoilerplate( |
| 126 io::Printer* printer, const FileDescriptor* file, bool descriptor_proto) { | 165 io::Printer* printer, const FileDescriptor* file, bool descriptor_proto) { |
| 127 // TODO(robinson): Allow parameterization of Python version? | 166 // TODO(robinson): Allow parameterization of Python version? |
| 128 printer->Print( | 167 printer->Print( |
| 129 "# Generated by the protocol buffer compiler. DO NOT EDIT!\n" | 168 "# Generated by the protocol buffer compiler. DO NOT EDIT!\n" |
| 130 "# source: $filename$\n" | 169 "# source: $filename$\n" |
| 170 "\nimport sys\n_b=sys.version_info[0]<3 and (lambda x:x) or (lambda x:x.en
code('latin1'))" //##PY25 |
| 131 "\n", | 171 "\n", |
| 132 "filename", file->name()); | 172 "filename", file->name()); |
| 133 if (HasTopLevelEnums(file)) { | 173 if (HasTopLevelEnums(file)) { |
| 134 printer->Print( | 174 printer->Print( |
| 135 "from google.protobuf.internal import enum_type_wrapper\n"); | 175 "from google.protobuf.internal import enum_type_wrapper\n"); |
| 136 } | 176 } |
| 137 printer->Print( | 177 printer->Print( |
| 138 "from google.protobuf import descriptor as _descriptor\n" | 178 "from google.protobuf import descriptor as _descriptor\n" |
| 139 "from google.protobuf import message as _message\n" | 179 "from google.protobuf import message as _message\n" |
| 140 "from google.protobuf import reflection as _reflection\n" | 180 "from google.protobuf import reflection as _reflection\n" |
| 141 ); | 181 "from google.protobuf import symbol_database as " |
| 182 "_symbol_database\n"); |
| 142 if (HasGenericServices(file)) { | 183 if (HasGenericServices(file)) { |
| 143 printer->Print( | 184 printer->Print( |
| 144 "from google.protobuf import service as _service\n" | 185 "from google.protobuf import service as _service\n" |
| 145 "from google.protobuf import service_reflection\n"); | 186 "from google.protobuf import service_reflection\n"); |
| 146 } | 187 } |
| 147 | 188 |
| 148 // Avoid circular imports if this module is descriptor_pb2. | 189 // Avoid circular imports if this module is descriptor_pb2. |
| 149 if (!descriptor_proto) { | 190 if (!descriptor_proto) { |
| 150 printer->Print( | 191 printer->Print( |
| 151 "from google.protobuf import descriptor_pb2\n"); | 192 "from google.protobuf import descriptor_pb2\n"); |
| 152 } | 193 } |
| 153 printer->Print( | 194 printer->Print( |
| 154 "# @@protoc_insertion_point(imports)\n"); | 195 "# @@protoc_insertion_point(imports)\n\n" |
| 196 "_sym_db = _symbol_database.Default()\n"); |
| 155 printer->Print("\n\n"); | 197 printer->Print("\n\n"); |
| 156 } | 198 } |
| 157 | 199 |
| 158 | 200 |
| 159 // Returns a Python literal giving the default value for a field. | 201 // Returns a Python literal giving the default value for a field. |
| 160 // If the field specifies no explicit default value, we'll return | 202 // If the field specifies no explicit default value, we'll return |
| 161 // the default default value for the field type (zero for numbers, | 203 // the default default value for the field type (zero for numbers, |
| 162 // empty string for strings, empty list for repeated fields, and | 204 // empty string for strings, empty list for repeated fields, and |
| 163 // None for non-repeated, composite fields). | 205 // None for non-repeated, composite fields). |
| 164 // | 206 // |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 210 return "(1e10000 * 0)"; | 252 return "(1e10000 * 0)"; |
| 211 } else { | 253 } else { |
| 212 return SimpleFtoa(value); | 254 return SimpleFtoa(value); |
| 213 } | 255 } |
| 214 } | 256 } |
| 215 case FieldDescriptor::CPPTYPE_BOOL: | 257 case FieldDescriptor::CPPTYPE_BOOL: |
| 216 return field.default_value_bool() ? "True" : "False"; | 258 return field.default_value_bool() ? "True" : "False"; |
| 217 case FieldDescriptor::CPPTYPE_ENUM: | 259 case FieldDescriptor::CPPTYPE_ENUM: |
| 218 return SimpleItoa(field.default_value_enum()->number()); | 260 return SimpleItoa(field.default_value_enum()->number()); |
| 219 case FieldDescriptor::CPPTYPE_STRING: | 261 case FieldDescriptor::CPPTYPE_STRING: |
| 220 if (field.type() == FieldDescriptor::TYPE_STRING) { | 262 //##!PY25 return "b\"" + CEscape(field.default_value_string()) + |
| 221 return "unicode(\"" + CEscape(field.default_value_string()) + | 263 //##!PY25 (field.type() != FieldDescriptor::TYPE_STRING ? "\"" : |
| 222 "\", \"utf-8\")"; | 264 //##!PY25 "\".decode('utf-8')"); |
| 223 } else { | 265 return "_b(\"" + CEscape(field.default_value_string()) + //##PY25 |
| 224 return "\"" + CEscape(field.default_value_string()) + "\""; | 266 (field.type() != FieldDescriptor::TYPE_STRING ? "\")" : //##PY25 |
| 225 } | 267 "\").decode('utf-8')"); //##PY25 |
| 226 case FieldDescriptor::CPPTYPE_MESSAGE: | 268 case FieldDescriptor::CPPTYPE_MESSAGE: |
| 227 return "None"; | 269 return "None"; |
| 228 } | 270 } |
| 229 // (We could add a default case above but then we wouldn't get the nice | 271 // (We could add a default case above but then we wouldn't get the nice |
| 230 // compiler warning when a new type is added.) | 272 // compiler warning when a new type is added.) |
| 231 GOOGLE_LOG(FATAL) << "Not reached."; | 273 GOOGLE_LOG(FATAL) << "Not reached."; |
| 232 return ""; | 274 return ""; |
| 233 } | 275 } |
| 234 | 276 |
| 277 string StringifySyntax(FileDescriptor::Syntax syntax) { |
| 278 switch (syntax) { |
| 279 case FileDescriptor::SYNTAX_PROTO2: |
| 280 return "proto2"; |
| 281 case FileDescriptor::SYNTAX_PROTO3: |
| 282 return "proto3"; |
| 283 case FileDescriptor::SYNTAX_UNKNOWN: |
| 284 default: |
| 285 GOOGLE_LOG(FATAL) << "Unsupported syntax; this generator only supports pro
to2 " |
| 286 "and proto3 syntax."; |
| 287 return ""; |
| 288 } |
| 289 } |
| 235 | 290 |
| 236 | 291 |
| 237 } // namespace | 292 } // namespace |
| 238 | 293 |
| 239 | 294 |
| 240 Generator::Generator() : file_(NULL) { | 295 Generator::Generator() : file_(NULL) { |
| 241 } | 296 } |
| 242 | 297 |
| 243 Generator::~Generator() { | 298 Generator::~Generator() { |
| 244 } | 299 } |
| (...skipping 15 matching lines...) Expand all Loading... |
| 260 string module_name = ModuleName(file->name()); | 315 string module_name = ModuleName(file->name()); |
| 261 string filename = module_name; | 316 string filename = module_name; |
| 262 StripString(&filename, ".", '/'); | 317 StripString(&filename, ".", '/'); |
| 263 filename += ".py"; | 318 filename += ".py"; |
| 264 | 319 |
| 265 FileDescriptorProto fdp; | 320 FileDescriptorProto fdp; |
| 266 file_->CopyTo(&fdp); | 321 file_->CopyTo(&fdp); |
| 267 fdp.SerializeToString(&file_descriptor_serialized_); | 322 fdp.SerializeToString(&file_descriptor_serialized_); |
| 268 | 323 |
| 269 | 324 |
| 270 scoped_ptr<io::ZeroCopyOutputStream> output(context->Open(filename)); | 325 google::protobuf::scoped_ptr<io::ZeroCopyOutputStream> output(context->Open(fi
lename)); |
| 271 GOOGLE_CHECK(output.get()); | 326 GOOGLE_CHECK(output.get()); |
| 272 io::Printer printer(output.get(), '$'); | 327 io::Printer printer(output.get(), '$'); |
| 273 printer_ = &printer; | 328 printer_ = &printer; |
| 274 | 329 |
| 275 PrintTopBoilerplate(printer_, file_, GeneratingDescriptorProto()); | 330 PrintTopBoilerplate(printer_, file_, GeneratingDescriptorProto()); |
| 276 PrintImports(); | 331 PrintImports(); |
| 277 PrintFileDescriptor(); | 332 PrintFileDescriptor(); |
| 278 PrintTopLevelEnums(); | 333 PrintTopLevelEnums(); |
| 279 PrintTopLevelExtensions(); | 334 PrintTopLevelExtensions(); |
| 280 PrintAllNestedEnumsInFile(); | 335 PrintAllNestedEnumsInFile(); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 296 | 351 |
| 297 printer.Print( | 352 printer.Print( |
| 298 "# @@protoc_insertion_point(module_scope)\n"); | 353 "# @@protoc_insertion_point(module_scope)\n"); |
| 299 | 354 |
| 300 return !printer.failed(); | 355 return !printer.failed(); |
| 301 } | 356 } |
| 302 | 357 |
| 303 // Prints Python imports for all modules imported by |file|. | 358 // Prints Python imports for all modules imported by |file|. |
| 304 void Generator::PrintImports() const { | 359 void Generator::PrintImports() const { |
| 305 for (int i = 0; i < file_->dependency_count(); ++i) { | 360 for (int i = 0; i < file_->dependency_count(); ++i) { |
| 306 string module_name = ModuleName(file_->dependency(i)->name()); | 361 const string& filename = file_->dependency(i)->name(); |
| 307 printer_->Print("import $module$\n", "module", | 362 string import_statement = ModuleImportStatement(filename); |
| 308 module_name); | 363 string module_alias = ModuleAlias(filename); |
| 364 printer_->Print("$statement$ as $alias$\n", "statement", |
| 365 import_statement, "alias", module_alias); |
| 366 CopyPublicDependenciesAliases(module_alias, file_->dependency(i)); |
| 309 } | 367 } |
| 310 printer_->Print("\n"); | 368 printer_->Print("\n"); |
| 311 | 369 |
| 312 // Print public imports. | 370 // Print public imports. |
| 313 for (int i = 0; i < file_->public_dependency_count(); ++i) { | 371 for (int i = 0; i < file_->public_dependency_count(); ++i) { |
| 314 string module_name = ModuleName(file_->public_dependency(i)->name()); | 372 string module_name = ModuleName(file_->public_dependency(i)->name()); |
| 315 printer_->Print("from $module$ import *\n", "module", module_name); | 373 printer_->Print("from $module$ import *\n", "module", module_name); |
| 316 } | 374 } |
| 317 printer_->Print("\n"); | 375 printer_->Print("\n"); |
| 318 } | 376 } |
| 319 | 377 |
| 320 // Prints the single file descriptor for this file. | 378 // Prints the single file descriptor for this file. |
| 321 void Generator::PrintFileDescriptor() const { | 379 void Generator::PrintFileDescriptor() const { |
| 322 map<string, string> m; | 380 map<string, string> m; |
| 323 m["descriptor_name"] = kDescriptorKey; | 381 m["descriptor_name"] = kDescriptorKey; |
| 324 m["name"] = file_->name(); | 382 m["name"] = file_->name(); |
| 325 m["package"] = file_->package(); | 383 m["package"] = file_->package(); |
| 384 m["syntax"] = StringifySyntax(file_->syntax()); |
| 326 const char file_descriptor_template[] = | 385 const char file_descriptor_template[] = |
| 327 "$descriptor_name$ = _descriptor.FileDescriptor(\n" | 386 "$descriptor_name$ = _descriptor.FileDescriptor(\n" |
| 328 " name='$name$',\n" | 387 " name='$name$',\n" |
| 329 " package='$package$',\n"; | 388 " package='$package$',\n" |
| 389 " syntax='$syntax$',\n"; |
| 330 printer_->Print(m, file_descriptor_template); | 390 printer_->Print(m, file_descriptor_template); |
| 331 printer_->Indent(); | 391 printer_->Indent(); |
| 332 printer_->Print( | 392 printer_->Print( |
| 333 "serialized_pb='$value$'", | 393 //##!PY25 "serialized_pb=b'$value$'\n", |
| 394 "serialized_pb=_b('$value$')\n", //##PY25 |
| 334 "value", strings::CHexEscape(file_descriptor_serialized_)); | 395 "value", strings::CHexEscape(file_descriptor_serialized_)); |
| 396 if (file_->dependency_count() != 0) { |
| 397 printer_->Print(",\ndependencies=["); |
| 398 for (int i = 0; i < file_->dependency_count(); ++i) { |
| 399 string module_alias = ModuleAlias(file_->dependency(i)->name()); |
| 400 printer_->Print("$module_alias$.DESCRIPTOR,", "module_alias", |
| 401 module_alias); |
| 402 } |
| 403 printer_->Print("]"); |
| 404 } |
| 335 | 405 |
| 336 // TODO(falk): Also print options and fix the message_type, enum_type, | 406 // TODO(falk): Also print options and fix the message_type, enum_type, |
| 337 // service and extension later in the generation. | 407 // service and extension later in the generation. |
| 338 | 408 |
| 339 printer_->Outdent(); | 409 printer_->Outdent(); |
| 340 printer_->Print(")\n"); | 410 printer_->Print(")\n"); |
| 411 printer_->Print("_sym_db.RegisterFileDescriptor($name$)\n", "name", |
| 412 kDescriptorKey); |
| 341 printer_->Print("\n"); | 413 printer_->Print("\n"); |
| 342 } | 414 } |
| 343 | 415 |
| 344 // Prints descriptors and module-level constants for all top-level | 416 // Prints descriptors and module-level constants for all top-level |
| 345 // enums defined in |file|. | 417 // enums defined in |file|. |
| 346 void Generator::PrintTopLevelEnums() const { | 418 void Generator::PrintTopLevelEnums() const { |
| 347 vector<pair<string, int> > top_level_enum_values; | 419 vector<pair<string, int> > top_level_enum_values; |
| 348 for (int i = 0; i < file_->enum_type_count(); ++i) { | 420 for (int i = 0; i < file_->enum_type_count(); ++i) { |
| 349 const EnumDescriptor& enum_descriptor = *file_->enum_type(i); | 421 const EnumDescriptor& enum_descriptor = *file_->enum_type(i); |
| 350 PrintEnum(enum_descriptor); | 422 PrintEnum(enum_descriptor); |
| 351 printer_->Print("$name$ = " | 423 printer_->Print("$name$ = " |
| 352 "enum_type_wrapper.EnumTypeWrapper($descriptor_name$)", | 424 "enum_type_wrapper.EnumTypeWrapper($descriptor_name$)", |
| 353 "name", enum_descriptor.name(), | 425 "name", enum_descriptor.name(), |
| 354 "descriptor_name", | 426 "descriptor_name", |
| 355 ModuleLevelDescriptorName(enum_descriptor)); | 427 ModuleLevelDescriptorName(enum_descriptor)); |
| 356 printer_->Print("\n"); | 428 printer_->Print("\n"); |
| 357 | 429 |
| 358 for (int j = 0; j < enum_descriptor.value_count(); ++j) { | 430 for (int j = 0; j < enum_descriptor.value_count(); ++j) { |
| 359 const EnumValueDescriptor& value_descriptor = *enum_descriptor.value(j); | 431 const EnumValueDescriptor& value_descriptor = *enum_descriptor.value(j); |
| 360 top_level_enum_values.push_back( | 432 top_level_enum_values.push_back( |
| 361 make_pair(value_descriptor.name(), value_descriptor.number())); | 433 std::make_pair(value_descriptor.name(), value_descriptor.number())); |
| 362 } | 434 } |
| 363 } | 435 } |
| 364 | 436 |
| 365 for (int i = 0; i < top_level_enum_values.size(); ++i) { | 437 for (int i = 0; i < top_level_enum_values.size(); ++i) { |
| 366 printer_->Print("$name$ = $value$\n", | 438 printer_->Print("$name$ = $value$\n", |
| 367 "name", top_level_enum_values[i].first, | 439 "name", top_level_enum_values[i].first, |
| 368 "value", SimpleItoa(top_level_enum_values[i].second)); | 440 "value", SimpleItoa(top_level_enum_values[i].second)); |
| 369 } | 441 } |
| 370 printer_->Print("\n"); | 442 printer_->Print("\n"); |
| 371 } | 443 } |
| 372 | 444 |
| 373 // Prints all enums contained in all message types in |file|. | 445 // Prints all enums contained in all message types in |file|. |
| 374 void Generator::PrintAllNestedEnumsInFile() const { | 446 void Generator::PrintAllNestedEnumsInFile() const { |
| 375 for (int i = 0; i < file_->message_type_count(); ++i) { | 447 for (int i = 0; i < file_->message_type_count(); ++i) { |
| 376 PrintNestedEnums(*file_->message_type(i)); | 448 PrintNestedEnums(*file_->message_type(i)); |
| 377 } | 449 } |
| 378 } | 450 } |
| 379 | 451 |
| 380 // Prints a Python statement assigning the appropriate module-level | 452 // Prints a Python statement assigning the appropriate module-level |
| 381 // enum name to a Python EnumDescriptor object equivalent to | 453 // enum name to a Python EnumDescriptor object equivalent to |
| 382 // enum_descriptor. | 454 // enum_descriptor. |
| 383 void Generator::PrintEnum(const EnumDescriptor& enum_descriptor) const { | 455 void Generator::PrintEnum(const EnumDescriptor& enum_descriptor) const { |
| 384 map<string, string> m; | 456 map<string, string> m; |
| 385 m["descriptor_name"] = ModuleLevelDescriptorName(enum_descriptor); | 457 string module_level_descriptor_name = |
| 458 ModuleLevelDescriptorName(enum_descriptor); |
| 459 m["descriptor_name"] = module_level_descriptor_name; |
| 386 m["name"] = enum_descriptor.name(); | 460 m["name"] = enum_descriptor.name(); |
| 387 m["full_name"] = enum_descriptor.full_name(); | 461 m["full_name"] = enum_descriptor.full_name(); |
| 388 m["file"] = kDescriptorKey; | 462 m["file"] = kDescriptorKey; |
| 389 const char enum_descriptor_template[] = | 463 const char enum_descriptor_template[] = |
| 390 "$descriptor_name$ = _descriptor.EnumDescriptor(\n" | 464 "$descriptor_name$ = _descriptor.EnumDescriptor(\n" |
| 391 " name='$name$',\n" | 465 " name='$name$',\n" |
| 392 " full_name='$full_name$',\n" | 466 " full_name='$full_name$',\n" |
| 393 " filename=None,\n" | 467 " filename=None,\n" |
| 394 " file=$file$,\n" | 468 " file=$file$,\n" |
| 395 " values=[\n"; | 469 " values=[\n"; |
| 396 string options_string; | 470 string options_string; |
| 397 enum_descriptor.options().SerializeToString(&options_string); | 471 enum_descriptor.options().SerializeToString(&options_string); |
| 398 printer_->Print(m, enum_descriptor_template); | 472 printer_->Print(m, enum_descriptor_template); |
| 399 printer_->Indent(); | 473 printer_->Indent(); |
| 400 printer_->Indent(); | 474 printer_->Indent(); |
| 401 for (int i = 0; i < enum_descriptor.value_count(); ++i) { | 475 for (int i = 0; i < enum_descriptor.value_count(); ++i) { |
| 402 PrintEnumValueDescriptor(*enum_descriptor.value(i)); | 476 PrintEnumValueDescriptor(*enum_descriptor.value(i)); |
| 403 printer_->Print(",\n"); | 477 printer_->Print(",\n"); |
| 404 } | 478 } |
| 405 printer_->Outdent(); | 479 printer_->Outdent(); |
| 406 printer_->Print("],\n"); | 480 printer_->Print("],\n"); |
| 407 printer_->Print("containing_type=None,\n"); | 481 printer_->Print("containing_type=None,\n"); |
| 408 printer_->Print("options=$options_value$,\n", | 482 printer_->Print("options=$options_value$,\n", |
| 409 "options_value", | 483 "options_value", |
| 410 OptionsValue("EnumOptions", options_string)); | 484 OptionsValue("EnumOptions", options_string)); |
| 411 EnumDescriptorProto edp; | 485 EnumDescriptorProto edp; |
| 412 PrintSerializedPbInterval(enum_descriptor, edp); | 486 PrintSerializedPbInterval(enum_descriptor, edp); |
| 413 printer_->Outdent(); | 487 printer_->Outdent(); |
| 414 printer_->Print(")\n"); | 488 printer_->Print(")\n"); |
| 489 printer_->Print("_sym_db.RegisterEnumDescriptor($name$)\n", "name", |
| 490 module_level_descriptor_name); |
| 415 printer_->Print("\n"); | 491 printer_->Print("\n"); |
| 416 } | 492 } |
| 417 | 493 |
| 418 // Recursively prints enums in nested types within descriptor, then | 494 // Recursively prints enums in nested types within descriptor, then |
| 419 // prints enums contained at the top level in descriptor. | 495 // prints enums contained at the top level in descriptor. |
| 420 void Generator::PrintNestedEnums(const Descriptor& descriptor) const { | 496 void Generator::PrintNestedEnums(const Descriptor& descriptor) const { |
| 421 for (int i = 0; i < descriptor.nested_type_count(); ++i) { | 497 for (int i = 0; i < descriptor.nested_type_count(); ++i) { |
| 422 PrintNestedEnums(*descriptor.nested_type(i)); | 498 PrintNestedEnums(*descriptor.nested_type(i)); |
| 423 } | 499 } |
| 424 | 500 |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 513 "output_type=$output_type$,\n" | 589 "output_type=$output_type$,\n" |
| 514 "options=$options_value$,\n"); | 590 "options=$options_value$,\n"); |
| 515 printer_->Outdent(); | 591 printer_->Outdent(); |
| 516 printer_->Print("),\n"); | 592 printer_->Print("),\n"); |
| 517 } | 593 } |
| 518 | 594 |
| 519 printer_->Outdent(); | 595 printer_->Outdent(); |
| 520 printer_->Print("])\n\n"); | 596 printer_->Print("])\n\n"); |
| 521 } | 597 } |
| 522 | 598 |
| 599 |
| 600 void Generator::PrintDescriptorKeyAndModuleName( |
| 601 const ServiceDescriptor& descriptor) const { |
| 602 printer_->Print( |
| 603 "$descriptor_key$ = $descriptor_name$,\n", |
| 604 "descriptor_key", kDescriptorKey, |
| 605 "descriptor_name", ModuleLevelServiceDescriptorName(descriptor)); |
| 606 printer_->Print( |
| 607 "__module__ = '$module_name$'\n", |
| 608 "module_name", ModuleName(file_->name())); |
| 609 } |
| 610 |
| 523 void Generator::PrintServiceClass(const ServiceDescriptor& descriptor) const { | 611 void Generator::PrintServiceClass(const ServiceDescriptor& descriptor) const { |
| 524 // Print the service. | 612 // Print the service. |
| 525 printer_->Print("class $class_name$(_service.Service):\n", | 613 printer_->Print("$class_name$ = service_reflection.GeneratedServiceType(" |
| 614 "'$class_name$', (_service.Service,), dict(\n", |
| 526 "class_name", descriptor.name()); | 615 "class_name", descriptor.name()); |
| 527 printer_->Indent(); | 616 printer_->Indent(); |
| 528 printer_->Print( | 617 Generator::PrintDescriptorKeyAndModuleName(descriptor); |
| 529 "__metaclass__ = service_reflection.GeneratedServiceType\n" | 618 printer_->Print("))\n\n"); |
| 530 "$descriptor_key$ = $descriptor_name$\n", | |
| 531 "descriptor_key", kDescriptorKey, | |
| 532 "descriptor_name", ModuleLevelServiceDescriptorName(descriptor)); | |
| 533 printer_->Outdent(); | 619 printer_->Outdent(); |
| 534 } | 620 } |
| 535 | 621 |
| 536 void Generator::PrintServiceStub(const ServiceDescriptor& descriptor) const { | 622 void Generator::PrintServiceStub(const ServiceDescriptor& descriptor) const { |
| 537 // Print the service stub. | 623 // Print the service stub. |
| 538 printer_->Print("class $class_name$_Stub($class_name$):\n", | 624 printer_->Print("$class_name$_Stub = " |
| 625 "service_reflection.GeneratedServiceStubType(" |
| 626 "'$class_name$_Stub', ($class_name$,), dict(\n", |
| 539 "class_name", descriptor.name()); | 627 "class_name", descriptor.name()); |
| 540 printer_->Indent(); | 628 printer_->Indent(); |
| 541 printer_->Print( | 629 Generator::PrintDescriptorKeyAndModuleName(descriptor); |
| 542 "__metaclass__ = service_reflection.GeneratedServiceStubType\n" | 630 printer_->Print("))\n\n"); |
| 543 "$descriptor_key$ = $descriptor_name$\n", | |
| 544 "descriptor_key", kDescriptorKey, | |
| 545 "descriptor_name", ModuleLevelServiceDescriptorName(descriptor)); | |
| 546 printer_->Outdent(); | 631 printer_->Outdent(); |
| 547 } | 632 } |
| 548 | 633 |
| 549 // Prints statement assigning ModuleLevelDescriptorName(message_descriptor) | 634 // Prints statement assigning ModuleLevelDescriptorName(message_descriptor) |
| 550 // to a Python Descriptor object for message_descriptor. | 635 // to a Python Descriptor object for message_descriptor. |
| 551 // | 636 // |
| 552 // Mutually recursive with PrintNestedDescriptors(). | 637 // Mutually recursive with PrintNestedDescriptors(). |
| 553 void Generator::PrintDescriptor(const Descriptor& message_descriptor) const { | 638 void Generator::PrintDescriptor(const Descriptor& message_descriptor) const { |
| 554 PrintNestedDescriptors(message_descriptor); | 639 PrintNestedDescriptors(message_descriptor); |
| 555 | 640 |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 589 *message_descriptor.enum_type(i)); | 674 *message_descriptor.enum_type(i)); |
| 590 printer_->Print(descriptor_name.c_str()); | 675 printer_->Print(descriptor_name.c_str()); |
| 591 printer_->Print(",\n"); | 676 printer_->Print(",\n"); |
| 592 } | 677 } |
| 593 printer_->Outdent(); | 678 printer_->Outdent(); |
| 594 printer_->Print("],\n"); | 679 printer_->Print("],\n"); |
| 595 string options_string; | 680 string options_string; |
| 596 message_descriptor.options().SerializeToString(&options_string); | 681 message_descriptor.options().SerializeToString(&options_string); |
| 597 printer_->Print( | 682 printer_->Print( |
| 598 "options=$options_value$,\n" | 683 "options=$options_value$,\n" |
| 599 "is_extendable=$extendable$", | 684 "is_extendable=$extendable$,\n" |
| 685 "syntax='$syntax$'", |
| 600 "options_value", OptionsValue("MessageOptions", options_string), | 686 "options_value", OptionsValue("MessageOptions", options_string), |
| 601 "extendable", message_descriptor.extension_range_count() > 0 ? | 687 "extendable", message_descriptor.extension_range_count() > 0 ? |
| 602 "True" : "False"); | 688 "True" : "False", |
| 689 "syntax", StringifySyntax(message_descriptor.file()->syntax())); |
| 603 printer_->Print(",\n"); | 690 printer_->Print(",\n"); |
| 604 | 691 |
| 605 // Extension ranges | 692 // Extension ranges |
| 606 printer_->Print("extension_ranges=["); | 693 printer_->Print("extension_ranges=["); |
| 607 for (int i = 0; i < message_descriptor.extension_range_count(); ++i) { | 694 for (int i = 0; i < message_descriptor.extension_range_count(); ++i) { |
| 608 const Descriptor::ExtensionRange* range = | 695 const Descriptor::ExtensionRange* range = |
| 609 message_descriptor.extension_range(i); | 696 message_descriptor.extension_range(i); |
| 610 printer_->Print("($start$, $end$), ", | 697 printer_->Print("($start$, $end$), ", |
| 611 "start", SimpleItoa(range->start), | 698 "start", SimpleItoa(range->start), |
| 612 "end", SimpleItoa(range->end)); | 699 "end", SimpleItoa(range->end)); |
| 613 } | 700 } |
| 614 printer_->Print("],\n"); | 701 printer_->Print("],\n"); |
| 615 | 702 printer_->Print("oneofs=[\n"); |
| 703 printer_->Indent(); |
| 704 for (int i = 0; i < message_descriptor.oneof_decl_count(); ++i) { |
| 705 const OneofDescriptor* desc = message_descriptor.oneof_decl(i); |
| 706 map<string, string> m; |
| 707 m["name"] = desc->name(); |
| 708 m["full_name"] = desc->full_name(); |
| 709 m["index"] = SimpleItoa(desc->index()); |
| 710 printer_->Print( |
| 711 m, |
| 712 "_descriptor.OneofDescriptor(\n" |
| 713 " name='$name$', full_name='$full_name$',\n" |
| 714 " index=$index$, containing_type=None, fields=[]),\n"); |
| 715 } |
| 716 printer_->Outdent(); |
| 717 printer_->Print("],\n"); |
| 616 // Serialization of proto | 718 // Serialization of proto |
| 617 DescriptorProto edp; | 719 DescriptorProto edp; |
| 618 PrintSerializedPbInterval(message_descriptor, edp); | 720 PrintSerializedPbInterval(message_descriptor, edp); |
| 619 | 721 |
| 620 printer_->Outdent(); | 722 printer_->Outdent(); |
| 621 printer_->Print(")\n"); | 723 printer_->Print(")\n"); |
| 622 } | 724 } |
| 623 | 725 |
| 624 // Prints Python Descriptor objects for all nested types contained in | 726 // Prints Python Descriptor objects for all nested types contained in |
| 625 // message_descriptor. | 727 // message_descriptor. |
| 626 // | 728 // |
| 627 // Mutually recursive with PrintDescriptor(). | 729 // Mutually recursive with PrintDescriptor(). |
| 628 void Generator::PrintNestedDescriptors( | 730 void Generator::PrintNestedDescriptors( |
| 629 const Descriptor& containing_descriptor) const { | 731 const Descriptor& containing_descriptor) const { |
| 630 for (int i = 0; i < containing_descriptor.nested_type_count(); ++i) { | 732 for (int i = 0; i < containing_descriptor.nested_type_count(); ++i) { |
| 631 PrintDescriptor(*containing_descriptor.nested_type(i)); | 733 PrintDescriptor(*containing_descriptor.nested_type(i)); |
| 632 } | 734 } |
| 633 } | 735 } |
| 634 | 736 |
| 635 // Prints all messages in |file|. | 737 // Prints all messages in |file|. |
| 636 void Generator::PrintMessages() const { | 738 void Generator::PrintMessages() const { |
| 637 for (int i = 0; i < file_->message_type_count(); ++i) { | 739 for (int i = 0; i < file_->message_type_count(); ++i) { |
| 638 PrintMessage(*file_->message_type(i)); | 740 vector<string> to_register; |
| 741 PrintMessage(*file_->message_type(i), "", &to_register); |
| 742 for (int j = 0; j < to_register.size(); ++j) { |
| 743 printer_->Print("_sym_db.RegisterMessage($name$)\n", "name", |
| 744 to_register[j]); |
| 745 } |
| 639 printer_->Print("\n"); | 746 printer_->Print("\n"); |
| 640 } | 747 } |
| 641 } | 748 } |
| 642 | 749 |
| 643 // Prints a Python class for the given message descriptor. We defer to the | 750 // Prints a Python class for the given message descriptor. We defer to the |
| 644 // metaclass to do almost all of the work of actually creating a useful class. | 751 // metaclass to do almost all of the work of actually creating a useful class. |
| 645 // The purpose of this function and its many helper functions above is merely | 752 // The purpose of this function and its many helper functions above is merely |
| 646 // to output a Python version of the descriptors, which the metaclass in | 753 // to output a Python version of the descriptors, which the metaclass in |
| 647 // reflection.py will use to construct the meat of the class itself. | 754 // reflection.py will use to construct the meat of the class itself. |
| 648 // | 755 // |
| 649 // Mutually recursive with PrintNestedMessages(). | 756 // Mutually recursive with PrintNestedMessages(). |
| 650 void Generator::PrintMessage( | 757 // Collect nested message names to_register for the symbol_database. |
| 651 const Descriptor& message_descriptor) const { | 758 void Generator::PrintMessage(const Descriptor& message_descriptor, |
| 652 printer_->Print("class $name$(_message.Message):\n", "name", | 759 const string& prefix, |
| 653 message_descriptor.name()); | 760 vector<string>* to_register) const { |
| 761 string qualified_name(prefix + message_descriptor.name()); |
| 762 to_register->push_back(qualified_name); |
| 763 printer_->Print( |
| 764 "$name$ = _reflection.GeneratedProtocolMessageType('$name$', " |
| 765 "(_message.Message,), dict(\n", |
| 766 "name", message_descriptor.name()); |
| 654 printer_->Indent(); | 767 printer_->Indent(); |
| 655 printer_->Print("__metaclass__ = _reflection.GeneratedProtocolMessageType\n"); | 768 |
| 656 PrintNestedMessages(message_descriptor); | 769 PrintNestedMessages(message_descriptor, qualified_name + ".", to_register); |
| 657 map<string, string> m; | 770 map<string, string> m; |
| 658 m["descriptor_key"] = kDescriptorKey; | 771 m["descriptor_key"] = kDescriptorKey; |
| 659 m["descriptor_name"] = ModuleLevelDescriptorName(message_descriptor); | 772 m["descriptor_name"] = ModuleLevelDescriptorName(message_descriptor); |
| 660 printer_->Print(m, "$descriptor_key$ = $descriptor_name$\n"); | 773 printer_->Print(m, "$descriptor_key$ = $descriptor_name$,\n"); |
| 661 | 774 printer_->Print("__module__ = '$module_name$'\n", |
| 662 printer_->Print( | 775 "module_name", ModuleName(file_->name())); |
| 663 "\n" | 776 printer_->Print("# @@protoc_insertion_point(class_scope:$full_name$)\n", |
| 664 "# @@protoc_insertion_point(class_scope:$full_name$)\n", | 777 "full_name", message_descriptor.full_name()); |
| 665 "full_name", message_descriptor.full_name()); | 778 printer_->Print("))\n"); |
| 666 | |
| 667 printer_->Outdent(); | 779 printer_->Outdent(); |
| 668 } | 780 } |
| 669 | 781 |
| 670 // Prints all nested messages within |containing_descriptor|. | 782 // Prints all nested messages within |containing_descriptor|. |
| 671 // Mutually recursive with PrintMessage(). | 783 // Mutually recursive with PrintMessage(). |
| 672 void Generator::PrintNestedMessages( | 784 void Generator::PrintNestedMessages(const Descriptor& containing_descriptor, |
| 673 const Descriptor& containing_descriptor) const { | 785 const string& prefix, |
| 786 vector<string>* to_register) const { |
| 674 for (int i = 0; i < containing_descriptor.nested_type_count(); ++i) { | 787 for (int i = 0; i < containing_descriptor.nested_type_count(); ++i) { |
| 675 printer_->Print("\n"); | 788 printer_->Print("\n"); |
| 676 PrintMessage(*containing_descriptor.nested_type(i)); | 789 PrintMessage(*containing_descriptor.nested_type(i), prefix, to_register); |
| 790 printer_->Print(",\n"); |
| 677 } | 791 } |
| 678 } | 792 } |
| 679 | 793 |
| 680 // Recursively fixes foreign fields in all nested types in |descriptor|, then | 794 // Recursively fixes foreign fields in all nested types in |descriptor|, then |
| 681 // sets the message_type and enum_type of all message and enum fields to point | 795 // sets the message_type and enum_type of all message and enum fields to point |
| 682 // to their respective descriptors. | 796 // to their respective descriptors. |
| 683 // Args: | 797 // Args: |
| 684 // descriptor: descriptor to print fields for. | 798 // descriptor: descriptor to print fields for. |
| 685 // containing_descriptor: if descriptor is a nested type, this is its | 799 // containing_descriptor: if descriptor is a nested type, this is its |
| 686 // containing type, or NULL if this is a root/top-level type. | 800 // containing type, or NULL if this is a root/top-level type. |
| 687 void Generator::FixForeignFieldsInDescriptor( | 801 void Generator::FixForeignFieldsInDescriptor( |
| 688 const Descriptor& descriptor, | 802 const Descriptor& descriptor, |
| 689 const Descriptor* containing_descriptor) const { | 803 const Descriptor* containing_descriptor) const { |
| 690 for (int i = 0; i < descriptor.nested_type_count(); ++i) { | 804 for (int i = 0; i < descriptor.nested_type_count(); ++i) { |
| 691 FixForeignFieldsInDescriptor(*descriptor.nested_type(i), &descriptor); | 805 FixForeignFieldsInDescriptor(*descriptor.nested_type(i), &descriptor); |
| 692 } | 806 } |
| 693 | 807 |
| 694 for (int i = 0; i < descriptor.field_count(); ++i) { | 808 for (int i = 0; i < descriptor.field_count(); ++i) { |
| 695 const FieldDescriptor& field_descriptor = *descriptor.field(i); | 809 const FieldDescriptor& field_descriptor = *descriptor.field(i); |
| 696 FixForeignFieldsInField(&descriptor, field_descriptor, "fields_by_name"); | 810 FixForeignFieldsInField(&descriptor, field_descriptor, "fields_by_name"); |
| 697 } | 811 } |
| 698 | 812 |
| 699 FixContainingTypeInDescriptor(descriptor, containing_descriptor); | 813 FixContainingTypeInDescriptor(descriptor, containing_descriptor); |
| 700 for (int i = 0; i < descriptor.enum_type_count(); ++i) { | 814 for (int i = 0; i < descriptor.enum_type_count(); ++i) { |
| 701 const EnumDescriptor& enum_descriptor = *descriptor.enum_type(i); | 815 const EnumDescriptor& enum_descriptor = *descriptor.enum_type(i); |
| 702 FixContainingTypeInDescriptor(enum_descriptor, &descriptor); | 816 FixContainingTypeInDescriptor(enum_descriptor, &descriptor); |
| 703 } | 817 } |
| 818 for (int i = 0; i < descriptor.oneof_decl_count(); ++i) { |
| 819 map<string, string> m; |
| 820 const OneofDescriptor* oneof = descriptor.oneof_decl(i); |
| 821 m["descriptor_name"] = ModuleLevelDescriptorName(descriptor); |
| 822 m["oneof_name"] = oneof->name(); |
| 823 for (int j = 0; j < oneof->field_count(); ++j) { |
| 824 m["field_name"] = oneof->field(j)->name(); |
| 825 printer_->Print( |
| 826 m, |
| 827 "$descriptor_name$.oneofs_by_name['$oneof_name$'].fields.append(\n" |
| 828 " $descriptor_name$.fields_by_name['$field_name$'])\n"); |
| 829 printer_->Print( |
| 830 m, |
| 831 "$descriptor_name$.fields_by_name['$field_name$'].containing_oneof = " |
| 832 "$descriptor_name$.oneofs_by_name['$oneof_name$']\n"); |
| 833 } |
| 834 } |
| 704 } | 835 } |
| 705 | 836 |
| 706 void Generator::AddMessageToFileDescriptor(const Descriptor& descriptor) const { | 837 void Generator::AddMessageToFileDescriptor(const Descriptor& descriptor) const { |
| 707 map<string, string> m; | 838 map<string, string> m; |
| 708 m["descriptor_name"] = kDescriptorKey; | 839 m["descriptor_name"] = kDescriptorKey; |
| 709 m["message_name"] = descriptor.name(); | 840 m["message_name"] = descriptor.name(); |
| 710 m["message_descriptor_name"] = ModuleLevelDescriptorName(descriptor); | 841 m["message_descriptor_name"] = ModuleLevelDescriptorName(descriptor); |
| 711 const char file_descriptor_template[] = | 842 const char file_descriptor_template[] = |
| 712 "$descriptor_name$.message_types_by_name['$message_name$'] = " | 843 "$descriptor_name$.message_types_by_name['$message_name$'] = " |
| 713 "$message_descriptor_name$\n"; | 844 "$message_descriptor_name$\n"; |
| 714 printer_->Print(m, file_descriptor_template); | 845 printer_->Print(m, file_descriptor_template); |
| 715 } | 846 } |
| 716 | 847 |
| 848 void Generator::AddEnumToFileDescriptor( |
| 849 const EnumDescriptor& descriptor) const { |
| 850 map<string, string> m; |
| 851 m["descriptor_name"] = kDescriptorKey; |
| 852 m["enum_name"] = descriptor.name(); |
| 853 m["enum_descriptor_name"] = ModuleLevelDescriptorName(descriptor); |
| 854 const char file_descriptor_template[] = |
| 855 "$descriptor_name$.enum_types_by_name['$enum_name$'] = " |
| 856 "$enum_descriptor_name$\n"; |
| 857 printer_->Print(m, file_descriptor_template); |
| 858 } |
| 859 |
| 860 void Generator::AddExtensionToFileDescriptor( |
| 861 const FieldDescriptor& descriptor) const { |
| 862 map<string, string> m; |
| 863 m["descriptor_name"] = kDescriptorKey; |
| 864 m["field_name"] = descriptor.name(); |
| 865 const char file_descriptor_template[] = |
| 866 "$descriptor_name$.extensions_by_name['$field_name$'] = " |
| 867 "$field_name$\n"; |
| 868 printer_->Print(m, file_descriptor_template); |
| 869 } |
| 870 |
| 717 // Sets any necessary message_type and enum_type attributes | 871 // Sets any necessary message_type and enum_type attributes |
| 718 // for the Python version of |field|. | 872 // for the Python version of |field|. |
| 719 // | 873 // |
| 720 // containing_type may be NULL, in which case this is a module-level field. | 874 // containing_type may be NULL, in which case this is a module-level field. |
| 721 // | 875 // |
| 722 // python_dict_name is the name of the Python dict where we should | 876 // python_dict_name is the name of the Python dict where we should |
| 723 // look the field up in the containing type. (e.g., fields_by_name | 877 // look the field up in the containing type. (e.g., fields_by_name |
| 724 // or extensions_by_name). We ignore python_dict_name if containing_type | 878 // or extensions_by_name). We ignore python_dict_name if containing_type |
| 725 // is NULL. | 879 // is NULL. |
| 726 void Generator::FixForeignFieldsInField(const Descriptor* containing_type, | 880 void Generator::FixForeignFieldsInField(const Descriptor* containing_type, |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 771 // Prints containing_type for nested descriptors or enum descriptors. | 925 // Prints containing_type for nested descriptors or enum descriptors. |
| 772 template <typename DescriptorT> | 926 template <typename DescriptorT> |
| 773 void Generator::FixContainingTypeInDescriptor( | 927 void Generator::FixContainingTypeInDescriptor( |
| 774 const DescriptorT& descriptor, | 928 const DescriptorT& descriptor, |
| 775 const Descriptor* containing_descriptor) const { | 929 const Descriptor* containing_descriptor) const { |
| 776 if (containing_descriptor != NULL) { | 930 if (containing_descriptor != NULL) { |
| 777 const string nested_name = ModuleLevelDescriptorName(descriptor); | 931 const string nested_name = ModuleLevelDescriptorName(descriptor); |
| 778 const string parent_name = ModuleLevelDescriptorName( | 932 const string parent_name = ModuleLevelDescriptorName( |
| 779 *containing_descriptor); | 933 *containing_descriptor); |
| 780 printer_->Print( | 934 printer_->Print( |
| 781 "$nested_name$.containing_type = $parent_name$;\n", | 935 "$nested_name$.containing_type = $parent_name$\n", |
| 782 "nested_name", nested_name, | 936 "nested_name", nested_name, |
| 783 "parent_name", parent_name); | 937 "parent_name", parent_name); |
| 784 } | 938 } |
| 785 } | 939 } |
| 786 | 940 |
| 787 // Prints statements setting the message_type and enum_type fields in the | 941 // Prints statements setting the message_type and enum_type fields in the |
| 788 // Python descriptor objects we've already output in ths file. We must | 942 // Python descriptor objects we've already output in ths file. We must |
| 789 // do this in a separate step due to circular references (otherwise, we'd | 943 // do this in a separate step due to circular references (otherwise, we'd |
| 790 // just set everything in the initial assignment statements). | 944 // just set everything in the initial assignment statements). |
| 791 void Generator::FixForeignFieldsInDescriptors() const { | 945 void Generator::FixForeignFieldsInDescriptors() const { |
| 792 for (int i = 0; i < file_->message_type_count(); ++i) { | 946 for (int i = 0; i < file_->message_type_count(); ++i) { |
| 793 FixForeignFieldsInDescriptor(*file_->message_type(i), NULL); | 947 FixForeignFieldsInDescriptor(*file_->message_type(i), NULL); |
| 794 } | 948 } |
| 795 for (int i = 0; i < file_->message_type_count(); ++i) { | 949 for (int i = 0; i < file_->message_type_count(); ++i) { |
| 796 AddMessageToFileDescriptor(*file_->message_type(i)); | 950 AddMessageToFileDescriptor(*file_->message_type(i)); |
| 797 } | 951 } |
| 952 for (int i = 0; i < file_->enum_type_count(); ++i) { |
| 953 AddEnumToFileDescriptor(*file_->enum_type(i)); |
| 954 } |
| 955 for (int i = 0; i < file_->extension_count(); ++i) { |
| 956 AddExtensionToFileDescriptor(*file_->extension(i)); |
| 957 } |
| 798 printer_->Print("\n"); | 958 printer_->Print("\n"); |
| 799 } | 959 } |
| 800 | 960 |
| 801 // We need to not only set any necessary message_type fields, but | 961 // We need to not only set any necessary message_type fields, but |
| 802 // also need to call RegisterExtension() on each message we're | 962 // also need to call RegisterExtension() on each message we're |
| 803 // extending. | 963 // extending. |
| 804 void Generator::FixForeignFieldsInExtensions() const { | 964 void Generator::FixForeignFieldsInExtensions() const { |
| 805 // Top-level extensions. | 965 // Top-level extensions. |
| 806 for (int i = 0; i < file_->extension_count(); ++i) { | 966 for (int i = 0; i < file_->extension_count(); ++i) { |
| 807 FixForeignFieldsInExtension(*file_->extension(i)); | 967 FixForeignFieldsInExtension(*file_->extension(i)); |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 868 } | 1028 } |
| 869 | 1029 |
| 870 // Returns a Python expression that calls descriptor._ParseOptions using | 1030 // Returns a Python expression that calls descriptor._ParseOptions using |
| 871 // the given descriptor class name and serialized options protobuf string. | 1031 // the given descriptor class name and serialized options protobuf string. |
| 872 string Generator::OptionsValue( | 1032 string Generator::OptionsValue( |
| 873 const string& class_name, const string& serialized_options) const { | 1033 const string& class_name, const string& serialized_options) const { |
| 874 if (serialized_options.length() == 0 || GeneratingDescriptorProto()) { | 1034 if (serialized_options.length() == 0 || GeneratingDescriptorProto()) { |
| 875 return "None"; | 1035 return "None"; |
| 876 } else { | 1036 } else { |
| 877 string full_class_name = "descriptor_pb2." + class_name; | 1037 string full_class_name = "descriptor_pb2." + class_name; |
| 878 return "_descriptor._ParseOptions(" + full_class_name + "(), '" | 1038 //##!PY25 return "_descriptor._ParseOptions(" + full_class_name + "(), b'" |
| 879 + CEscape(serialized_options)+ "')"; | 1039 //##!PY25 + CEscape(serialized_options)+ "')"; |
| 1040 return "_descriptor._ParseOptions(" + full_class_name + "(), _b('" //##PY25 |
| 1041 + CEscape(serialized_options)+ "'))"; //##PY25 |
| 880 } | 1042 } |
| 881 } | 1043 } |
| 882 | 1044 |
| 883 // Prints an expression for a Python FieldDescriptor for |field|. | 1045 // Prints an expression for a Python FieldDescriptor for |field|. |
| 884 void Generator::PrintFieldDescriptor( | 1046 void Generator::PrintFieldDescriptor( |
| 885 const FieldDescriptor& field, bool is_extension) const { | 1047 const FieldDescriptor& field, bool is_extension) const { |
| 886 string options_string; | 1048 string options_string; |
| 887 field.options().SerializeToString(&options_string); | 1049 field.options().SerializeToString(&options_string); |
| 888 map<string, string> m; | 1050 map<string, string> m; |
| 889 m["name"] = field.name(); | 1051 m["name"] = field.name(); |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 972 // The C++ implementation doesn't guard against this either. Leaving | 1134 // The C++ implementation doesn't guard against this either. Leaving |
| 973 // it for now... | 1135 // it for now... |
| 974 string name = NamePrefixedWithNestedTypes(descriptor, "_"); | 1136 string name = NamePrefixedWithNestedTypes(descriptor, "_"); |
| 975 UpperString(&name); | 1137 UpperString(&name); |
| 976 // Module-private for now. Easy to make public later; almost impossible | 1138 // Module-private for now. Easy to make public later; almost impossible |
| 977 // to make private later. | 1139 // to make private later. |
| 978 name = "_" + name; | 1140 name = "_" + name; |
| 979 // We now have the name relative to its own module. Also qualify with | 1141 // We now have the name relative to its own module. Also qualify with |
| 980 // the module name iff this descriptor is from a different .proto file. | 1142 // the module name iff this descriptor is from a different .proto file. |
| 981 if (descriptor.file() != file_) { | 1143 if (descriptor.file() != file_) { |
| 982 name = ModuleName(descriptor.file()->name()) + "." + name; | 1144 name = ModuleAlias(descriptor.file()->name()) + "." + name; |
| 983 } | 1145 } |
| 984 return name; | 1146 return name; |
| 985 } | 1147 } |
| 986 | 1148 |
| 987 // Returns the name of the message class itself, not the descriptor. | 1149 // Returns the name of the message class itself, not the descriptor. |
| 988 // Like ModuleLevelDescriptorName(), module-qualifies the name iff | 1150 // Like ModuleLevelDescriptorName(), module-qualifies the name iff |
| 989 // the given descriptor describes an entity that doesn't come from | 1151 // the given descriptor describes an entity that doesn't come from |
| 990 // the current file. | 1152 // the current file. |
| 991 string Generator::ModuleLevelMessageName(const Descriptor& descriptor) const { | 1153 string Generator::ModuleLevelMessageName(const Descriptor& descriptor) const { |
| 992 string name = NamePrefixedWithNestedTypes(descriptor, "."); | 1154 string name = NamePrefixedWithNestedTypes(descriptor, "."); |
| 993 if (descriptor.file() != file_) { | 1155 if (descriptor.file() != file_) { |
| 994 name = ModuleName(descriptor.file()->name()) + "." + name; | 1156 name = ModuleAlias(descriptor.file()->name()) + "." + name; |
| 995 } | 1157 } |
| 996 return name; | 1158 return name; |
| 997 } | 1159 } |
| 998 | 1160 |
| 999 // Returns the unique Python module-level identifier given to a service | 1161 // Returns the unique Python module-level identifier given to a service |
| 1000 // descriptor. | 1162 // descriptor. |
| 1001 string Generator::ModuleLevelServiceDescriptorName( | 1163 string Generator::ModuleLevelServiceDescriptorName( |
| 1002 const ServiceDescriptor& descriptor) const { | 1164 const ServiceDescriptor& descriptor) const { |
| 1003 string name = descriptor.name(); | 1165 string name = descriptor.name(); |
| 1004 UpperString(&name); | 1166 UpperString(&name); |
| 1005 name = "_" + name; | 1167 name = "_" + name; |
| 1006 if (descriptor.file() != file_) { | 1168 if (descriptor.file() != file_) { |
| 1007 name = ModuleName(descriptor.file()->name()) + "." + name; | 1169 name = ModuleAlias(descriptor.file()->name()) + "." + name; |
| 1008 } | 1170 } |
| 1009 return name; | 1171 return name; |
| 1010 } | 1172 } |
| 1011 | 1173 |
| 1012 // Prints standard constructor arguments serialized_start and serialized_end. | 1174 // Prints standard constructor arguments serialized_start and serialized_end. |
| 1013 // Args: | 1175 // Args: |
| 1014 // descriptor: The cpp descriptor to have a serialized reference. | 1176 // descriptor: The cpp descriptor to have a serialized reference. |
| 1015 // proto: A proto | 1177 // proto: A proto |
| 1016 // Example printer output: | 1178 // Example printer output: |
| 1017 // serialized_start=41, | 1179 // serialized_start=41, |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1144 string message_options = OptionsValue( | 1306 string message_options = OptionsValue( |
| 1145 "MessageOptions", descriptor.options().SerializeAsString()); | 1307 "MessageOptions", descriptor.options().SerializeAsString()); |
| 1146 if (message_options != "None") { | 1308 if (message_options != "None") { |
| 1147 string descriptor_name = ModuleLevelDescriptorName(descriptor); | 1309 string descriptor_name = ModuleLevelDescriptorName(descriptor); |
| 1148 PrintDescriptorOptionsFixingCode(descriptor_name, | 1310 PrintDescriptorOptionsFixingCode(descriptor_name, |
| 1149 message_options, | 1311 message_options, |
| 1150 printer_); | 1312 printer_); |
| 1151 } | 1313 } |
| 1152 } | 1314 } |
| 1153 | 1315 |
| 1316 // If a dependency forwards other files through public dependencies, let's |
| 1317 // copy over the corresponding module aliases. |
| 1318 void Generator::CopyPublicDependenciesAliases( |
| 1319 const string& copy_from, const FileDescriptor* file) const { |
| 1320 for (int i = 0; i < file->public_dependency_count(); ++i) { |
| 1321 string module_alias = ModuleAlias(file->public_dependency(i)->name()); |
| 1322 printer_->Print("$alias$ = $copy_from$.$alias$\n", "alias", module_alias, |
| 1323 "copy_from", copy_from); |
| 1324 CopyPublicDependenciesAliases(copy_from, file->public_dependency(i)); |
| 1325 } |
| 1326 } |
| 1327 |
| 1154 } // namespace python | 1328 } // namespace python |
| 1155 } // namespace compiler | 1329 } // namespace compiler |
| 1156 } // namespace protobuf | 1330 } // namespace protobuf |
| 1157 } // namespace google | 1331 } // namespace google |
| OLD | NEW |