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 // https://developers.google.com/protocol-buffers/ | 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. |
(...skipping 26 matching lines...) Expand all Loading... |
37 // 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 |
38 // of the C++ *Descriptor objects, and fix up all circular references | 38 // of the C++ *Descriptor objects, and fix up all circular references |
39 // within these objects. | 39 // within these objects. |
40 // | 40 // |
41 // Note that the runtime performance of protocol message classes created in | 41 // Note that the runtime performance of protocol message classes created in |
42 // 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 |
43 // generator that outputs a Python/C extension module that lets | 43 // generator that outputs a Python/C extension module that lets |
44 // performance-minded Python code leverage the fast C++ implementation | 44 // performance-minded Python code leverage the fast C++ implementation |
45 // directly. | 45 // directly. |
46 | 46 |
47 #include <algorithm> | |
48 #include <google/protobuf/stubs/hash.h> | 47 #include <google/protobuf/stubs/hash.h> |
49 #include <limits> | 48 #include <limits> |
50 #include <map> | 49 #include <map> |
51 #include <memory> | 50 #include <memory> |
52 #ifndef _SHARED_PTR_H | 51 #ifndef _SHARED_PTR_H |
53 #include <google/protobuf/stubs/shared_ptr.h> | 52 #include <google/protobuf/stubs/shared_ptr.h> |
54 #endif | 53 #endif |
55 #include <string> | 54 #include <string> |
56 #include <utility> | 55 #include <utility> |
57 #include <vector> | 56 #include <vector> |
(...skipping 23 matching lines...) Expand all Loading... |
81 string StripProto(const string& filename) { | 80 string StripProto(const string& filename) { |
82 const char* suffix = HasSuffixString(filename, ".protodevel") | 81 const char* suffix = HasSuffixString(filename, ".protodevel") |
83 ? ".protodevel" : ".proto"; | 82 ? ".protodevel" : ".proto"; |
84 return StripSuffixString(filename, suffix); | 83 return StripSuffixString(filename, suffix); |
85 } | 84 } |
86 | 85 |
87 | 86 |
88 // Returns the Python module name expected for a given .proto filename. | 87 // Returns the Python module name expected for a given .proto filename. |
89 string ModuleName(const string& filename) { | 88 string ModuleName(const string& filename) { |
90 string basename = StripProto(filename); | 89 string basename = StripProto(filename); |
91 ReplaceCharacters(&basename, "-", '_'); | 90 StripString(&basename, "-", '_'); |
92 ReplaceCharacters(&basename, "/", '.'); | 91 StripString(&basename, "/", '.'); |
93 return basename + "_pb2"; | 92 return basename + "_pb2"; |
94 } | 93 } |
95 | 94 |
96 | 95 |
97 // Returns the alias we assign to the module of the given .proto filename | 96 // Returns the alias we assign to the module of the given .proto filename |
98 // when importing. See testPackageInitializationImport in | 97 // when importing. See testPackageInitializationImport in |
99 // google/protobuf/python/reflection_test.py | 98 // google/protobuf/python/reflection_test.py |
100 // to see why we need the alias. | 99 // to see why we need the alias. |
101 string ModuleAlias(const string& filename) { | 100 string ModuleAlias(const string& filename) { |
102 string module_name = ModuleName(filename); | 101 string module_name = ModuleName(filename); |
103 // We can't have dots in the module name, so we replace each with _dot_. | 102 // We can't have dots in the module name, so we replace each with _dot_. |
104 // But that could lead to a collision between a.b and a_dot_b, so we also | 103 // But that could lead to a collision between a.b and a_dot_b, so we also |
105 // duplicate each underscore. | 104 // duplicate each underscore. |
106 GlobalReplaceSubstring("_", "__", &module_name); | 105 GlobalReplaceSubstring("_", "__", &module_name); |
107 GlobalReplaceSubstring(".", "_dot_", &module_name); | 106 GlobalReplaceSubstring(".", "_dot_", &module_name); |
108 return module_name; | 107 return module_name; |
109 } | 108 } |
110 | 109 |
111 // Keywords reserved by the Python language. | |
112 const char* const kKeywords[] = { | |
113 "False", "None", "True", "and", "as", "assert", "break", | |
114 "class", "continue", "def", "del", "elif", "else", "except", | |
115 "finally", "for", "from", "global", "if", "import", "in", | |
116 "is", "lambda", "nonlocal", "not", "or", "pass", "raise", | |
117 "return", "try", "while", "with", "yield", | |
118 }; | |
119 const char* const* kKeywordsEnd = | |
120 kKeywords + (sizeof(kKeywords) / sizeof(kKeywords[0])); | |
121 | 110 |
122 bool ContainsPythonKeyword(const string& module_name) { | 111 // Returns an import statement of form "from X.Y.Z import T" for the given |
123 std::vector<string> tokens = Split(module_name, "."); | 112 // .proto filename. |
124 for (int i = 0; i < tokens.size(); ++i) { | 113 string ModuleImportStatement(const string& filename) { |
125 if (std::find(kKeywords, kKeywordsEnd, tokens[i]) != kKeywordsEnd) { | 114 string module_name = ModuleName(filename); |
126 return true; | 115 int last_dot_pos = module_name.rfind('.'); |
127 } | 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); |
128 } | 123 } |
129 return false; | |
130 } | 124 } |
131 | 125 |
132 | 126 |
133 // Returns the name of all containing types for descriptor, | 127 // Returns the name of all containing types for descriptor, |
134 // in order from outermost to innermost, followed by descriptor's | 128 // in order from outermost to innermost, followed by descriptor's |
135 // own name. Each name is separated by |separator|. | 129 // own name. Each name is separated by |separator|. |
136 template <typename DescriptorT> | 130 template <typename DescriptorT> |
137 string NamePrefixedWithNestedTypes(const DescriptorT& descriptor, | 131 string NamePrefixedWithNestedTypes(const DescriptorT& descriptor, |
138 const string& separator) { | 132 const string& separator) { |
139 string name = descriptor.name(); | 133 string name = descriptor.name(); |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
223 case FieldDescriptor::CPPTYPE_INT32: | 217 case FieldDescriptor::CPPTYPE_INT32: |
224 return SimpleItoa(field.default_value_int32()); | 218 return SimpleItoa(field.default_value_int32()); |
225 case FieldDescriptor::CPPTYPE_UINT32: | 219 case FieldDescriptor::CPPTYPE_UINT32: |
226 return SimpleItoa(field.default_value_uint32()); | 220 return SimpleItoa(field.default_value_uint32()); |
227 case FieldDescriptor::CPPTYPE_INT64: | 221 case FieldDescriptor::CPPTYPE_INT64: |
228 return SimpleItoa(field.default_value_int64()); | 222 return SimpleItoa(field.default_value_int64()); |
229 case FieldDescriptor::CPPTYPE_UINT64: | 223 case FieldDescriptor::CPPTYPE_UINT64: |
230 return SimpleItoa(field.default_value_uint64()); | 224 return SimpleItoa(field.default_value_uint64()); |
231 case FieldDescriptor::CPPTYPE_DOUBLE: { | 225 case FieldDescriptor::CPPTYPE_DOUBLE: { |
232 double value = field.default_value_double(); | 226 double value = field.default_value_double(); |
233 if (value == std::numeric_limits<double>::infinity()) { | 227 if (value == numeric_limits<double>::infinity()) { |
234 // Python pre-2.6 on Windows does not parse "inf" correctly. However, | 228 // Python pre-2.6 on Windows does not parse "inf" correctly. However, |
235 // a numeric literal that is too big for a double will become infinity. | 229 // a numeric literal that is too big for a double will become infinity. |
236 return "1e10000"; | 230 return "1e10000"; |
237 } else if (value == -std::numeric_limits<double>::infinity()) { | 231 } else if (value == -numeric_limits<double>::infinity()) { |
238 // See above. | 232 // See above. |
239 return "-1e10000"; | 233 return "-1e10000"; |
240 } else if (value != value) { | 234 } else if (value != value) { |
241 // infinity * 0 = nan | 235 // infinity * 0 = nan |
242 return "(1e10000 * 0)"; | 236 return "(1e10000 * 0)"; |
243 } else { | 237 } else { |
244 return "float(" + SimpleDtoa(value) + ")"; | 238 return "float(" + SimpleDtoa(value) + ")"; |
245 } | 239 } |
246 } | 240 } |
247 case FieldDescriptor::CPPTYPE_FLOAT: { | 241 case FieldDescriptor::CPPTYPE_FLOAT: { |
248 float value = field.default_value_float(); | 242 float value = field.default_value_float(); |
249 if (value == std::numeric_limits<float>::infinity()) { | 243 if (value == numeric_limits<float>::infinity()) { |
250 // Python pre-2.6 on Windows does not parse "inf" correctly. However, | 244 // Python pre-2.6 on Windows does not parse "inf" correctly. However, |
251 // a numeric literal that is too big for a double will become infinity. | 245 // a numeric literal that is too big for a double will become infinity. |
252 return "1e10000"; | 246 return "1e10000"; |
253 } else if (value == -std::numeric_limits<float>::infinity()) { | 247 } else if (value == -numeric_limits<float>::infinity()) { |
254 // See above. | 248 // See above. |
255 return "-1e10000"; | 249 return "-1e10000"; |
256 } else if (value != value) { | 250 } else if (value != value) { |
257 // infinity - infinity = nan | 251 // infinity - infinity = nan |
258 return "(1e10000 * 0)"; | 252 return "(1e10000 * 0)"; |
259 } else { | 253 } else { |
260 return "float(" + SimpleFtoa(value) + ")"; | 254 return "float(" + SimpleFtoa(value) + ")"; |
261 } | 255 } |
262 } | 256 } |
263 case FieldDescriptor::CPPTYPE_BOOL: | 257 case FieldDescriptor::CPPTYPE_BOOL: |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
313 // thread-safety constraints of the CodeGenerator interface aren't clear so | 307 // thread-safety constraints of the CodeGenerator interface aren't clear so |
314 // just be as conservative as possible. It's easier to relax this later if | 308 // just be as conservative as possible. It's easier to relax this later if |
315 // we need to, but I doubt it will be an issue. | 309 // we need to, but I doubt it will be an issue. |
316 // TODO(kenton): The proper thing to do would be to allocate any state on | 310 // TODO(kenton): The proper thing to do would be to allocate any state on |
317 // the stack and use that, so that the Generator class itself does not need | 311 // the stack and use that, so that the Generator class itself does not need |
318 // to have any mutable members. Then it is implicitly thread-safe. | 312 // to have any mutable members. Then it is implicitly thread-safe. |
319 MutexLock lock(&mutex_); | 313 MutexLock lock(&mutex_); |
320 file_ = file; | 314 file_ = file; |
321 string module_name = ModuleName(file->name()); | 315 string module_name = ModuleName(file->name()); |
322 string filename = module_name; | 316 string filename = module_name; |
323 ReplaceCharacters(&filename, ".", '/'); | 317 StripString(&filename, ".", '/'); |
324 filename += ".py"; | 318 filename += ".py"; |
325 | 319 |
326 FileDescriptorProto fdp; | 320 FileDescriptorProto fdp; |
327 file_->CopyTo(&fdp); | 321 file_->CopyTo(&fdp); |
328 fdp.SerializeToString(&file_descriptor_serialized_); | 322 fdp.SerializeToString(&file_descriptor_serialized_); |
329 | 323 |
330 | 324 |
331 google::protobuf::scoped_ptr<io::ZeroCopyOutputStream> output(context->Open(fi
lename)); | 325 google::protobuf::scoped_ptr<io::ZeroCopyOutputStream> output(context->Open(fi
lename)); |
332 GOOGLE_CHECK(output.get()); | 326 GOOGLE_CHECK(output.get()); |
333 io::Printer printer(output.get(), '$'); | 327 io::Printer printer(output.get(), '$'); |
(...skipping 24 matching lines...) Expand all Loading... |
358 printer.Print( | 352 printer.Print( |
359 "# @@protoc_insertion_point(module_scope)\n"); | 353 "# @@protoc_insertion_point(module_scope)\n"); |
360 | 354 |
361 return !printer.failed(); | 355 return !printer.failed(); |
362 } | 356 } |
363 | 357 |
364 // Prints Python imports for all modules imported by |file|. | 358 // Prints Python imports for all modules imported by |file|. |
365 void Generator::PrintImports() const { | 359 void Generator::PrintImports() const { |
366 for (int i = 0; i < file_->dependency_count(); ++i) { | 360 for (int i = 0; i < file_->dependency_count(); ++i) { |
367 const string& filename = file_->dependency(i)->name(); | 361 const string& filename = file_->dependency(i)->name(); |
368 | 362 string import_statement = ModuleImportStatement(filename); |
369 string module_name = ModuleName(filename); | |
370 string module_alias = ModuleAlias(filename); | 363 string module_alias = ModuleAlias(filename); |
371 if (ContainsPythonKeyword(module_name)) { | 364 printer_->Print("$statement$ as $alias$\n", "statement", |
372 // If the module path contains a Python keyword, we have to quote the | 365 import_statement, "alias", module_alias); |
373 // module name and import it using importlib. Otherwise the usual kind of | |
374 // import statement would result in a syntax error from the presence of | |
375 // the keyword. | |
376 printer_->Print("import importlib\n"); | |
377 printer_->Print("$alias$ = importlib.import_module('$name$')\n", "alias", | |
378 module_alias, "name", module_name); | |
379 } else { | |
380 int last_dot_pos = module_name.rfind('.'); | |
381 string import_statement; | |
382 if (last_dot_pos == string::npos) { | |
383 // NOTE(petya): this is not tested as it would require a protocol buffer | |
384 // outside of any package, and I don't think that is easily achievable. | |
385 import_statement = "import " + module_name; | |
386 } else { | |
387 import_statement = "from " + module_name.substr(0, last_dot_pos) + | |
388 " import " + module_name.substr(last_dot_pos + 1); | |
389 } | |
390 printer_->Print("$statement$ as $alias$\n", "statement", import_statement, | |
391 "alias", module_alias); | |
392 } | |
393 | |
394 CopyPublicDependenciesAliases(module_alias, file_->dependency(i)); | 366 CopyPublicDependenciesAliases(module_alias, file_->dependency(i)); |
395 } | 367 } |
396 printer_->Print("\n"); | 368 printer_->Print("\n"); |
397 | 369 |
398 // Print public imports. | 370 // Print public imports. |
399 for (int i = 0; i < file_->public_dependency_count(); ++i) { | 371 for (int i = 0; i < file_->public_dependency_count(); ++i) { |
400 string module_name = ModuleName(file_->public_dependency(i)->name()); | 372 string module_name = ModuleName(file_->public_dependency(i)->name()); |
401 printer_->Print("from $module$ import *\n", "module", module_name); | 373 printer_->Print("from $module$ import *\n", "module", module_name); |
402 } | 374 } |
403 printer_->Print("\n"); | 375 printer_->Print("\n"); |
404 } | 376 } |
405 | 377 |
406 // Prints the single file descriptor for this file. | 378 // Prints the single file descriptor for this file. |
407 void Generator::PrintFileDescriptor() const { | 379 void Generator::PrintFileDescriptor() const { |
408 std::map<string, string> m; | 380 map<string, string> m; |
409 m["descriptor_name"] = kDescriptorKey; | 381 m["descriptor_name"] = kDescriptorKey; |
410 m["name"] = file_->name(); | 382 m["name"] = file_->name(); |
411 m["package"] = file_->package(); | 383 m["package"] = file_->package(); |
412 m["syntax"] = StringifySyntax(file_->syntax()); | 384 m["syntax"] = StringifySyntax(file_->syntax()); |
413 const char file_descriptor_template[] = | 385 const char file_descriptor_template[] = |
414 "$descriptor_name$ = _descriptor.FileDescriptor(\n" | 386 "$descriptor_name$ = _descriptor.FileDescriptor(\n" |
415 " name='$name$',\n" | 387 " name='$name$',\n" |
416 " package='$package$',\n" | 388 " package='$package$',\n" |
417 " syntax='$syntax$',\n"; | 389 " syntax='$syntax$',\n"; |
418 printer_->Print(m, file_descriptor_template); | 390 printer_->Print(m, file_descriptor_template); |
419 printer_->Indent(); | 391 printer_->Indent(); |
420 printer_->Print( | 392 printer_->Print( |
421 //##!PY25 "serialized_pb=b'$value$'\n", | 393 //##!PY25 "serialized_pb=b'$value$'\n", |
422 "serialized_pb=_b('$value$')\n", //##PY25 | 394 "serialized_pb=_b('$value$')\n", //##PY25 |
423 "value", strings::CHexEscape(file_descriptor_serialized_)); | 395 "value", strings::CHexEscape(file_descriptor_serialized_)); |
424 if (file_->dependency_count() != 0) { | 396 if (file_->dependency_count() != 0) { |
425 printer_->Print(",\ndependencies=["); | 397 printer_->Print(",\ndependencies=["); |
426 for (int i = 0; i < file_->dependency_count(); ++i) { | 398 for (int i = 0; i < file_->dependency_count(); ++i) { |
427 string module_alias = ModuleAlias(file_->dependency(i)->name()); | 399 string module_alias = ModuleAlias(file_->dependency(i)->name()); |
428 printer_->Print("$module_alias$.DESCRIPTOR,", "module_alias", | 400 printer_->Print("$module_alias$.DESCRIPTOR,", "module_alias", |
429 module_alias); | 401 module_alias); |
430 } | 402 } |
431 printer_->Print("]"); | 403 printer_->Print("]"); |
432 } | 404 } |
433 if (file_->public_dependency_count() > 0) { | |
434 printer_->Print(",\npublic_dependencies=["); | |
435 for (int i = 0; i < file_->public_dependency_count(); ++i) { | |
436 string module_alias = ModuleAlias(file_->public_dependency(i)->name()); | |
437 printer_->Print("$module_alias$.DESCRIPTOR,", "module_alias", | |
438 module_alias); | |
439 } | |
440 printer_->Print("]"); | |
441 } | |
442 | 405 |
443 // 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, |
444 // service and extension later in the generation. | 407 // service and extension later in the generation. |
445 | 408 |
446 printer_->Outdent(); | 409 printer_->Outdent(); |
447 printer_->Print(")\n"); | 410 printer_->Print(")\n"); |
448 printer_->Print("_sym_db.RegisterFileDescriptor($name$)\n", "name", | 411 printer_->Print("_sym_db.RegisterFileDescriptor($name$)\n", "name", |
449 kDescriptorKey); | 412 kDescriptorKey); |
450 printer_->Print("\n"); | 413 printer_->Print("\n"); |
451 } | 414 } |
452 | 415 |
453 // Prints descriptors and module-level constants for all top-level | 416 // Prints descriptors and module-level constants for all top-level |
454 // enums defined in |file|. | 417 // enums defined in |file|. |
455 void Generator::PrintTopLevelEnums() const { | 418 void Generator::PrintTopLevelEnums() const { |
456 std::vector<std::pair<string, int> > top_level_enum_values; | 419 vector<pair<string, int> > top_level_enum_values; |
457 for (int i = 0; i < file_->enum_type_count(); ++i) { | 420 for (int i = 0; i < file_->enum_type_count(); ++i) { |
458 const EnumDescriptor& enum_descriptor = *file_->enum_type(i); | 421 const EnumDescriptor& enum_descriptor = *file_->enum_type(i); |
459 PrintEnum(enum_descriptor); | 422 PrintEnum(enum_descriptor); |
460 printer_->Print("$name$ = " | 423 printer_->Print("$name$ = " |
461 "enum_type_wrapper.EnumTypeWrapper($descriptor_name$)", | 424 "enum_type_wrapper.EnumTypeWrapper($descriptor_name$)", |
462 "name", enum_descriptor.name(), | 425 "name", enum_descriptor.name(), |
463 "descriptor_name", | 426 "descriptor_name", |
464 ModuleLevelDescriptorName(enum_descriptor)); | 427 ModuleLevelDescriptorName(enum_descriptor)); |
465 printer_->Print("\n"); | 428 printer_->Print("\n"); |
466 | 429 |
(...skipping 16 matching lines...) Expand all Loading... |
483 void Generator::PrintAllNestedEnumsInFile() const { | 446 void Generator::PrintAllNestedEnumsInFile() const { |
484 for (int i = 0; i < file_->message_type_count(); ++i) { | 447 for (int i = 0; i < file_->message_type_count(); ++i) { |
485 PrintNestedEnums(*file_->message_type(i)); | 448 PrintNestedEnums(*file_->message_type(i)); |
486 } | 449 } |
487 } | 450 } |
488 | 451 |
489 // Prints a Python statement assigning the appropriate module-level | 452 // Prints a Python statement assigning the appropriate module-level |
490 // enum name to a Python EnumDescriptor object equivalent to | 453 // enum name to a Python EnumDescriptor object equivalent to |
491 // enum_descriptor. | 454 // enum_descriptor. |
492 void Generator::PrintEnum(const EnumDescriptor& enum_descriptor) const { | 455 void Generator::PrintEnum(const EnumDescriptor& enum_descriptor) const { |
493 std::map<string, string> m; | 456 map<string, string> m; |
494 string module_level_descriptor_name = | 457 string module_level_descriptor_name = |
495 ModuleLevelDescriptorName(enum_descriptor); | 458 ModuleLevelDescriptorName(enum_descriptor); |
496 m["descriptor_name"] = module_level_descriptor_name; | 459 m["descriptor_name"] = module_level_descriptor_name; |
497 m["name"] = enum_descriptor.name(); | 460 m["name"] = enum_descriptor.name(); |
498 m["full_name"] = enum_descriptor.full_name(); | 461 m["full_name"] = enum_descriptor.full_name(); |
499 m["file"] = kDescriptorKey; | 462 m["file"] = kDescriptorKey; |
500 const char enum_descriptor_template[] = | 463 const char enum_descriptor_template[] = |
501 "$descriptor_name$ = _descriptor.EnumDescriptor(\n" | 464 "$descriptor_name$ = _descriptor.EnumDescriptor(\n" |
502 " name='$name$',\n" | 465 " name='$name$',\n" |
503 " full_name='$full_name$',\n" | 466 " full_name='$full_name$',\n" |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
577 const ServiceDescriptor& descriptor) const { | 540 const ServiceDescriptor& descriptor) const { |
578 printer_->Print("\n"); | 541 printer_->Print("\n"); |
579 string service_name = ModuleLevelServiceDescriptorName(descriptor); | 542 string service_name = ModuleLevelServiceDescriptorName(descriptor); |
580 string options_string; | 543 string options_string; |
581 descriptor.options().SerializeToString(&options_string); | 544 descriptor.options().SerializeToString(&options_string); |
582 | 545 |
583 printer_->Print( | 546 printer_->Print( |
584 "$service_name$ = _descriptor.ServiceDescriptor(\n", | 547 "$service_name$ = _descriptor.ServiceDescriptor(\n", |
585 "service_name", service_name); | 548 "service_name", service_name); |
586 printer_->Indent(); | 549 printer_->Indent(); |
587 std::map<string, string> m; | 550 map<string, string> m; |
588 m["name"] = descriptor.name(); | 551 m["name"] = descriptor.name(); |
589 m["full_name"] = descriptor.full_name(); | 552 m["full_name"] = descriptor.full_name(); |
590 m["file"] = kDescriptorKey; | 553 m["file"] = kDescriptorKey; |
591 m["index"] = SimpleItoa(descriptor.index()); | 554 m["index"] = SimpleItoa(descriptor.index()); |
592 m["options_value"] = OptionsValue("ServiceOptions", options_string); | 555 m["options_value"] = OptionsValue("ServiceOptions", options_string); |
593 const char required_function_arguments[] = | 556 const char required_function_arguments[] = |
594 "name='$name$',\n" | 557 "name='$name$',\n" |
595 "full_name='$full_name$',\n" | 558 "full_name='$full_name$',\n" |
596 "file=$file$,\n" | 559 "file=$file$,\n" |
597 "index=$index$,\n" | 560 "index=$index$,\n" |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
673 // | 636 // |
674 // Mutually recursive with PrintNestedDescriptors(). | 637 // Mutually recursive with PrintNestedDescriptors(). |
675 void Generator::PrintDescriptor(const Descriptor& message_descriptor) const { | 638 void Generator::PrintDescriptor(const Descriptor& message_descriptor) const { |
676 PrintNestedDescriptors(message_descriptor); | 639 PrintNestedDescriptors(message_descriptor); |
677 | 640 |
678 printer_->Print("\n"); | 641 printer_->Print("\n"); |
679 printer_->Print("$descriptor_name$ = _descriptor.Descriptor(\n", | 642 printer_->Print("$descriptor_name$ = _descriptor.Descriptor(\n", |
680 "descriptor_name", | 643 "descriptor_name", |
681 ModuleLevelDescriptorName(message_descriptor)); | 644 ModuleLevelDescriptorName(message_descriptor)); |
682 printer_->Indent(); | 645 printer_->Indent(); |
683 std::map<string, string> m; | 646 map<string, string> m; |
684 m["name"] = message_descriptor.name(); | 647 m["name"] = message_descriptor.name(); |
685 m["full_name"] = message_descriptor.full_name(); | 648 m["full_name"] = message_descriptor.full_name(); |
686 m["file"] = kDescriptorKey; | 649 m["file"] = kDescriptorKey; |
687 const char required_function_arguments[] = | 650 const char required_function_arguments[] = |
688 "name='$name$',\n" | 651 "name='$name$',\n" |
689 "full_name='$full_name$',\n" | 652 "full_name='$full_name$',\n" |
690 "filename=None,\n" | 653 "filename=None,\n" |
691 "file=$file$,\n" | 654 "file=$file$,\n" |
692 "containing_type=None,\n"; | 655 "containing_type=None,\n"; |
693 printer_->Print(m, required_function_arguments); | 656 printer_->Print(m, required_function_arguments); |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
733 message_descriptor.extension_range(i); | 696 message_descriptor.extension_range(i); |
734 printer_->Print("($start$, $end$), ", | 697 printer_->Print("($start$, $end$), ", |
735 "start", SimpleItoa(range->start), | 698 "start", SimpleItoa(range->start), |
736 "end", SimpleItoa(range->end)); | 699 "end", SimpleItoa(range->end)); |
737 } | 700 } |
738 printer_->Print("],\n"); | 701 printer_->Print("],\n"); |
739 printer_->Print("oneofs=[\n"); | 702 printer_->Print("oneofs=[\n"); |
740 printer_->Indent(); | 703 printer_->Indent(); |
741 for (int i = 0; i < message_descriptor.oneof_decl_count(); ++i) { | 704 for (int i = 0; i < message_descriptor.oneof_decl_count(); ++i) { |
742 const OneofDescriptor* desc = message_descriptor.oneof_decl(i); | 705 const OneofDescriptor* desc = message_descriptor.oneof_decl(i); |
743 std::map<string, string> m; | 706 map<string, string> m; |
744 m["name"] = desc->name(); | 707 m["name"] = desc->name(); |
745 m["full_name"] = desc->full_name(); | 708 m["full_name"] = desc->full_name(); |
746 m["index"] = SimpleItoa(desc->index()); | 709 m["index"] = SimpleItoa(desc->index()); |
747 string options_string = | |
748 OptionsValue("OneofOptions", desc->options().SerializeAsString()); | |
749 if (options_string == "None") { | |
750 m["options"] = ""; | |
751 } else { | |
752 m["options"] = ", options=" + options_string; | |
753 } | |
754 printer_->Print( | 710 printer_->Print( |
755 m, | 711 m, |
756 "_descriptor.OneofDescriptor(\n" | 712 "_descriptor.OneofDescriptor(\n" |
757 " name='$name$', full_name='$full_name$',\n" | 713 " name='$name$', full_name='$full_name$',\n" |
758 " index=$index$, containing_type=None, fields=[]$options$),\n"); | 714 " index=$index$, containing_type=None, fields=[]),\n"); |
759 } | 715 } |
760 printer_->Outdent(); | 716 printer_->Outdent(); |
761 printer_->Print("],\n"); | 717 printer_->Print("],\n"); |
762 // Serialization of proto | 718 // Serialization of proto |
763 DescriptorProto edp; | 719 DescriptorProto edp; |
764 PrintSerializedPbInterval(message_descriptor, edp); | 720 PrintSerializedPbInterval(message_descriptor, edp); |
765 | 721 |
766 printer_->Outdent(); | 722 printer_->Outdent(); |
767 printer_->Print(")\n"); | 723 printer_->Print(")\n"); |
768 } | 724 } |
769 | 725 |
770 // Prints Python Descriptor objects for all nested types contained in | 726 // Prints Python Descriptor objects for all nested types contained in |
771 // message_descriptor. | 727 // message_descriptor. |
772 // | 728 // |
773 // Mutually recursive with PrintDescriptor(). | 729 // Mutually recursive with PrintDescriptor(). |
774 void Generator::PrintNestedDescriptors( | 730 void Generator::PrintNestedDescriptors( |
775 const Descriptor& containing_descriptor) const { | 731 const Descriptor& containing_descriptor) const { |
776 for (int i = 0; i < containing_descriptor.nested_type_count(); ++i) { | 732 for (int i = 0; i < containing_descriptor.nested_type_count(); ++i) { |
777 PrintDescriptor(*containing_descriptor.nested_type(i)); | 733 PrintDescriptor(*containing_descriptor.nested_type(i)); |
778 } | 734 } |
779 } | 735 } |
780 | 736 |
781 // Prints all messages in |file|. | 737 // Prints all messages in |file|. |
782 void Generator::PrintMessages() const { | 738 void Generator::PrintMessages() const { |
783 for (int i = 0; i < file_->message_type_count(); ++i) { | 739 for (int i = 0; i < file_->message_type_count(); ++i) { |
784 std::vector<string> to_register; | 740 vector<string> to_register; |
785 PrintMessage(*file_->message_type(i), "", &to_register); | 741 PrintMessage(*file_->message_type(i), "", &to_register); |
786 for (int j = 0; j < to_register.size(); ++j) { | 742 for (int j = 0; j < to_register.size(); ++j) { |
787 printer_->Print("_sym_db.RegisterMessage($name$)\n", "name", | 743 printer_->Print("_sym_db.RegisterMessage($name$)\n", "name", |
788 to_register[j]); | 744 to_register[j]); |
789 } | 745 } |
790 printer_->Print("\n"); | 746 printer_->Print("\n"); |
791 } | 747 } |
792 } | 748 } |
793 | 749 |
794 // 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 |
795 // 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. |
796 // 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 |
797 // 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 |
798 // 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. |
799 // | 755 // |
800 // Mutually recursive with PrintNestedMessages(). | 756 // Mutually recursive with PrintNestedMessages(). |
801 // Collect nested message names to_register for the symbol_database. | 757 // Collect nested message names to_register for the symbol_database. |
802 void Generator::PrintMessage(const Descriptor& message_descriptor, | 758 void Generator::PrintMessage(const Descriptor& message_descriptor, |
803 const string& prefix, | 759 const string& prefix, |
804 std::vector<string>* to_register) const { | 760 vector<string>* to_register) const { |
805 string qualified_name(prefix + message_descriptor.name()); | 761 string qualified_name(prefix + message_descriptor.name()); |
806 to_register->push_back(qualified_name); | 762 to_register->push_back(qualified_name); |
807 printer_->Print( | 763 printer_->Print( |
808 "$name$ = _reflection.GeneratedProtocolMessageType('$name$', " | 764 "$name$ = _reflection.GeneratedProtocolMessageType('$name$', " |
809 "(_message.Message,), dict(\n", | 765 "(_message.Message,), dict(\n", |
810 "name", message_descriptor.name()); | 766 "name", message_descriptor.name()); |
811 printer_->Indent(); | 767 printer_->Indent(); |
812 | 768 |
813 PrintNestedMessages(message_descriptor, qualified_name + ".", to_register); | 769 PrintNestedMessages(message_descriptor, qualified_name + ".", to_register); |
814 std::map<string, string> m; | 770 map<string, string> m; |
815 m["descriptor_key"] = kDescriptorKey; | 771 m["descriptor_key"] = kDescriptorKey; |
816 m["descriptor_name"] = ModuleLevelDescriptorName(message_descriptor); | 772 m["descriptor_name"] = ModuleLevelDescriptorName(message_descriptor); |
817 printer_->Print(m, "$descriptor_key$ = $descriptor_name$,\n"); | 773 printer_->Print(m, "$descriptor_key$ = $descriptor_name$,\n"); |
818 printer_->Print("__module__ = '$module_name$'\n", | 774 printer_->Print("__module__ = '$module_name$'\n", |
819 "module_name", ModuleName(file_->name())); | 775 "module_name", ModuleName(file_->name())); |
820 printer_->Print("# @@protoc_insertion_point(class_scope:$full_name$)\n", | 776 printer_->Print("# @@protoc_insertion_point(class_scope:$full_name$)\n", |
821 "full_name", message_descriptor.full_name()); | 777 "full_name", message_descriptor.full_name()); |
822 printer_->Print("))\n"); | 778 printer_->Print("))\n"); |
823 printer_->Outdent(); | 779 printer_->Outdent(); |
824 } | 780 } |
825 | 781 |
826 // Prints all nested messages within |containing_descriptor|. | 782 // Prints all nested messages within |containing_descriptor|. |
827 // Mutually recursive with PrintMessage(). | 783 // Mutually recursive with PrintMessage(). |
828 void Generator::PrintNestedMessages(const Descriptor& containing_descriptor, | 784 void Generator::PrintNestedMessages(const Descriptor& containing_descriptor, |
829 const string& prefix, | 785 const string& prefix, |
830 std::vector<string>* to_register) const { | 786 vector<string>* to_register) const { |
831 for (int i = 0; i < containing_descriptor.nested_type_count(); ++i) { | 787 for (int i = 0; i < containing_descriptor.nested_type_count(); ++i) { |
832 printer_->Print("\n"); | 788 printer_->Print("\n"); |
833 PrintMessage(*containing_descriptor.nested_type(i), prefix, to_register); | 789 PrintMessage(*containing_descriptor.nested_type(i), prefix, to_register); |
834 printer_->Print(",\n"); | 790 printer_->Print(",\n"); |
835 } | 791 } |
836 } | 792 } |
837 | 793 |
838 // Recursively fixes foreign fields in all nested types in |descriptor|, then | 794 // Recursively fixes foreign fields in all nested types in |descriptor|, then |
839 // 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 |
840 // to their respective descriptors. | 796 // to their respective descriptors. |
(...skipping 12 matching lines...) Expand all Loading... |
853 const FieldDescriptor& field_descriptor = *descriptor.field(i); | 809 const FieldDescriptor& field_descriptor = *descriptor.field(i); |
854 FixForeignFieldsInField(&descriptor, field_descriptor, "fields_by_name"); | 810 FixForeignFieldsInField(&descriptor, field_descriptor, "fields_by_name"); |
855 } | 811 } |
856 | 812 |
857 FixContainingTypeInDescriptor(descriptor, containing_descriptor); | 813 FixContainingTypeInDescriptor(descriptor, containing_descriptor); |
858 for (int i = 0; i < descriptor.enum_type_count(); ++i) { | 814 for (int i = 0; i < descriptor.enum_type_count(); ++i) { |
859 const EnumDescriptor& enum_descriptor = *descriptor.enum_type(i); | 815 const EnumDescriptor& enum_descriptor = *descriptor.enum_type(i); |
860 FixContainingTypeInDescriptor(enum_descriptor, &descriptor); | 816 FixContainingTypeInDescriptor(enum_descriptor, &descriptor); |
861 } | 817 } |
862 for (int i = 0; i < descriptor.oneof_decl_count(); ++i) { | 818 for (int i = 0; i < descriptor.oneof_decl_count(); ++i) { |
863 std::map<string, string> m; | 819 map<string, string> m; |
864 const OneofDescriptor* oneof = descriptor.oneof_decl(i); | 820 const OneofDescriptor* oneof = descriptor.oneof_decl(i); |
865 m["descriptor_name"] = ModuleLevelDescriptorName(descriptor); | 821 m["descriptor_name"] = ModuleLevelDescriptorName(descriptor); |
866 m["oneof_name"] = oneof->name(); | 822 m["oneof_name"] = oneof->name(); |
867 for (int j = 0; j < oneof->field_count(); ++j) { | 823 for (int j = 0; j < oneof->field_count(); ++j) { |
868 m["field_name"] = oneof->field(j)->name(); | 824 m["field_name"] = oneof->field(j)->name(); |
869 printer_->Print( | 825 printer_->Print( |
870 m, | 826 m, |
871 "$descriptor_name$.oneofs_by_name['$oneof_name$'].fields.append(\n" | 827 "$descriptor_name$.oneofs_by_name['$oneof_name$'].fields.append(\n" |
872 " $descriptor_name$.fields_by_name['$field_name$'])\n"); | 828 " $descriptor_name$.fields_by_name['$field_name$'])\n"); |
873 printer_->Print( | 829 printer_->Print( |
874 m, | 830 m, |
875 "$descriptor_name$.fields_by_name['$field_name$'].containing_oneof = " | 831 "$descriptor_name$.fields_by_name['$field_name$'].containing_oneof = " |
876 "$descriptor_name$.oneofs_by_name['$oneof_name$']\n"); | 832 "$descriptor_name$.oneofs_by_name['$oneof_name$']\n"); |
877 } | 833 } |
878 } | 834 } |
879 } | 835 } |
880 | 836 |
881 void Generator::AddMessageToFileDescriptor(const Descriptor& descriptor) const { | 837 void Generator::AddMessageToFileDescriptor(const Descriptor& descriptor) const { |
882 std::map<string, string> m; | 838 map<string, string> m; |
883 m["descriptor_name"] = kDescriptorKey; | 839 m["descriptor_name"] = kDescriptorKey; |
884 m["message_name"] = descriptor.name(); | 840 m["message_name"] = descriptor.name(); |
885 m["message_descriptor_name"] = ModuleLevelDescriptorName(descriptor); | 841 m["message_descriptor_name"] = ModuleLevelDescriptorName(descriptor); |
886 const char file_descriptor_template[] = | 842 const char file_descriptor_template[] = |
887 "$descriptor_name$.message_types_by_name['$message_name$'] = " | 843 "$descriptor_name$.message_types_by_name['$message_name$'] = " |
888 "$message_descriptor_name$\n"; | 844 "$message_descriptor_name$\n"; |
889 printer_->Print(m, file_descriptor_template); | 845 printer_->Print(m, file_descriptor_template); |
890 } | 846 } |
891 | 847 |
892 void Generator::AddEnumToFileDescriptor( | 848 void Generator::AddEnumToFileDescriptor( |
893 const EnumDescriptor& descriptor) const { | 849 const EnumDescriptor& descriptor) const { |
894 std::map<string, string> m; | 850 map<string, string> m; |
895 m["descriptor_name"] = kDescriptorKey; | 851 m["descriptor_name"] = kDescriptorKey; |
896 m["enum_name"] = descriptor.name(); | 852 m["enum_name"] = descriptor.name(); |
897 m["enum_descriptor_name"] = ModuleLevelDescriptorName(descriptor); | 853 m["enum_descriptor_name"] = ModuleLevelDescriptorName(descriptor); |
898 const char file_descriptor_template[] = | 854 const char file_descriptor_template[] = |
899 "$descriptor_name$.enum_types_by_name['$enum_name$'] = " | 855 "$descriptor_name$.enum_types_by_name['$enum_name$'] = " |
900 "$enum_descriptor_name$\n"; | 856 "$enum_descriptor_name$\n"; |
901 printer_->Print(m, file_descriptor_template); | 857 printer_->Print(m, file_descriptor_template); |
902 } | 858 } |
903 | 859 |
904 void Generator::AddExtensionToFileDescriptor( | 860 void Generator::AddExtensionToFileDescriptor( |
905 const FieldDescriptor& descriptor) const { | 861 const FieldDescriptor& descriptor) const { |
906 std::map<string, string> m; | 862 map<string, string> m; |
907 m["descriptor_name"] = kDescriptorKey; | 863 m["descriptor_name"] = kDescriptorKey; |
908 m["field_name"] = descriptor.name(); | 864 m["field_name"] = descriptor.name(); |
909 const char file_descriptor_template[] = | 865 const char file_descriptor_template[] = |
910 "$descriptor_name$.extensions_by_name['$field_name$'] = " | 866 "$descriptor_name$.extensions_by_name['$field_name$'] = " |
911 "$field_name$\n"; | 867 "$field_name$\n"; |
912 printer_->Print(m, file_descriptor_template); | 868 printer_->Print(m, file_descriptor_template); |
913 } | 869 } |
914 | 870 |
915 // Sets any necessary message_type and enum_type attributes | 871 // Sets any necessary message_type and enum_type attributes |
916 // for the Python version of |field|. | 872 // for the Python version of |field|. |
917 // | 873 // |
918 // 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. |
919 // | 875 // |
920 // 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 |
921 // 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 |
922 // 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 |
923 // is NULL. | 879 // is NULL. |
924 void Generator::FixForeignFieldsInField(const Descriptor* containing_type, | 880 void Generator::FixForeignFieldsInField(const Descriptor* containing_type, |
925 const FieldDescriptor& field, | 881 const FieldDescriptor& field, |
926 const string& python_dict_name) const { | 882 const string& python_dict_name) const { |
927 const string field_referencing_expression = FieldReferencingExpression( | 883 const string field_referencing_expression = FieldReferencingExpression( |
928 containing_type, field, python_dict_name); | 884 containing_type, field, python_dict_name); |
929 std::map<string, string> m; | 885 map<string, string> m; |
930 m["field_ref"] = field_referencing_expression; | 886 m["field_ref"] = field_referencing_expression; |
931 const Descriptor* foreign_message_type = field.message_type(); | 887 const Descriptor* foreign_message_type = field.message_type(); |
932 if (foreign_message_type) { | 888 if (foreign_message_type) { |
933 m["foreign_type"] = ModuleLevelDescriptorName(*foreign_message_type); | 889 m["foreign_type"] = ModuleLevelDescriptorName(*foreign_message_type); |
934 printer_->Print(m, "$field_ref$.message_type = $foreign_type$\n"); | 890 printer_->Print(m, "$field_ref$.message_type = $foreign_type$\n"); |
935 } | 891 } |
936 const EnumDescriptor* enum_type = field.enum_type(); | 892 const EnumDescriptor* enum_type = field.enum_type(); |
937 if (enum_type) { | 893 if (enum_type) { |
938 m["enum_type"] = ModuleLevelDescriptorName(*enum_type); | 894 m["enum_type"] = ModuleLevelDescriptorName(*enum_type); |
939 printer_->Print(m, "$field_ref$.enum_type = $enum_type$\n"); | 895 printer_->Print(m, "$field_ref$.enum_type = $enum_type$\n"); |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1018 } | 974 } |
1019 | 975 |
1020 void Generator::FixForeignFieldsInExtension( | 976 void Generator::FixForeignFieldsInExtension( |
1021 const FieldDescriptor& extension_field) const { | 977 const FieldDescriptor& extension_field) const { |
1022 GOOGLE_CHECK(extension_field.is_extension()); | 978 GOOGLE_CHECK(extension_field.is_extension()); |
1023 // extension_scope() will be NULL for top-level extensions, which is | 979 // extension_scope() will be NULL for top-level extensions, which is |
1024 // exactly what FixForeignFieldsInField() wants. | 980 // exactly what FixForeignFieldsInField() wants. |
1025 FixForeignFieldsInField(extension_field.extension_scope(), extension_field, | 981 FixForeignFieldsInField(extension_field.extension_scope(), extension_field, |
1026 "extensions_by_name"); | 982 "extensions_by_name"); |
1027 | 983 |
1028 std::map<string, string> m; | 984 map<string, string> m; |
1029 // Confusingly, for FieldDescriptors that happen to be extensions, | 985 // Confusingly, for FieldDescriptors that happen to be extensions, |
1030 // containing_type() means "extended type." | 986 // containing_type() means "extended type." |
1031 // On the other hand, extension_scope() will give us what we normally | 987 // On the other hand, extension_scope() will give us what we normally |
1032 // mean by containing_type(). | 988 // mean by containing_type(). |
1033 m["extended_message_class"] = ModuleLevelMessageName( | 989 m["extended_message_class"] = ModuleLevelMessageName( |
1034 *extension_field.containing_type()); | 990 *extension_field.containing_type()); |
1035 m["field"] = FieldReferencingExpression(extension_field.extension_scope(), | 991 m["field"] = FieldReferencingExpression(extension_field.extension_scope(), |
1036 extension_field, | 992 extension_field, |
1037 "extensions_by_name"); | 993 "extensions_by_name"); |
1038 printer_->Print(m, "$extended_message_class$.RegisterExtension($field$)\n"); | 994 printer_->Print(m, "$extended_message_class$.RegisterExtension($field$)\n"); |
(...skipping 12 matching lines...) Expand all Loading... |
1051 } | 1007 } |
1052 | 1008 |
1053 // Returns a Python expression that instantiates a Python EnumValueDescriptor | 1009 // Returns a Python expression that instantiates a Python EnumValueDescriptor |
1054 // object for the given C++ descriptor. | 1010 // object for the given C++ descriptor. |
1055 void Generator::PrintEnumValueDescriptor( | 1011 void Generator::PrintEnumValueDescriptor( |
1056 const EnumValueDescriptor& descriptor) const { | 1012 const EnumValueDescriptor& descriptor) const { |
1057 // TODO(robinson): Fix up EnumValueDescriptor "type" fields. | 1013 // TODO(robinson): Fix up EnumValueDescriptor "type" fields. |
1058 // More circular references. ::sigh:: | 1014 // More circular references. ::sigh:: |
1059 string options_string; | 1015 string options_string; |
1060 descriptor.options().SerializeToString(&options_string); | 1016 descriptor.options().SerializeToString(&options_string); |
1061 std::map<string, string> m; | 1017 map<string, string> m; |
1062 m["name"] = descriptor.name(); | 1018 m["name"] = descriptor.name(); |
1063 m["index"] = SimpleItoa(descriptor.index()); | 1019 m["index"] = SimpleItoa(descriptor.index()); |
1064 m["number"] = SimpleItoa(descriptor.number()); | 1020 m["number"] = SimpleItoa(descriptor.number()); |
1065 m["options"] = OptionsValue("EnumValueOptions", options_string); | 1021 m["options"] = OptionsValue("EnumValueOptions", options_string); |
1066 printer_->Print( | 1022 printer_->Print( |
1067 m, | 1023 m, |
1068 "_descriptor.EnumValueDescriptor(\n" | 1024 "_descriptor.EnumValueDescriptor(\n" |
1069 " name='$name$', index=$index$, number=$number$,\n" | 1025 " name='$name$', index=$index$, number=$number$,\n" |
1070 " options=$options$,\n" | 1026 " options=$options$,\n" |
1071 " type=None)"); | 1027 " type=None)"); |
(...skipping 12 matching lines...) Expand all Loading... |
1084 return "_descriptor._ParseOptions(" + full_class_name + "(), _b('" //##PY25 | 1040 return "_descriptor._ParseOptions(" + full_class_name + "(), _b('" //##PY25 |
1085 + CEscape(serialized_options)+ "'))"; //##PY25 | 1041 + CEscape(serialized_options)+ "'))"; //##PY25 |
1086 } | 1042 } |
1087 } | 1043 } |
1088 | 1044 |
1089 // Prints an expression for a Python FieldDescriptor for |field|. | 1045 // Prints an expression for a Python FieldDescriptor for |field|. |
1090 void Generator::PrintFieldDescriptor( | 1046 void Generator::PrintFieldDescriptor( |
1091 const FieldDescriptor& field, bool is_extension) const { | 1047 const FieldDescriptor& field, bool is_extension) const { |
1092 string options_string; | 1048 string options_string; |
1093 field.options().SerializeToString(&options_string); | 1049 field.options().SerializeToString(&options_string); |
1094 std::map<string, string> m; | 1050 map<string, string> m; |
1095 m["name"] = field.name(); | 1051 m["name"] = field.name(); |
1096 m["full_name"] = field.full_name(); | 1052 m["full_name"] = field.full_name(); |
1097 m["index"] = SimpleItoa(field.index()); | 1053 m["index"] = SimpleItoa(field.index()); |
1098 m["number"] = SimpleItoa(field.number()); | 1054 m["number"] = SimpleItoa(field.number()); |
1099 m["type"] = SimpleItoa(field.type()); | 1055 m["type"] = SimpleItoa(field.type()); |
1100 m["cpp_type"] = SimpleItoa(field.cpp_type()); | 1056 m["cpp_type"] = SimpleItoa(field.cpp_type()); |
1101 m["label"] = SimpleItoa(field.label()); | 1057 m["label"] = SimpleItoa(field.label()); |
1102 m["has_default_value"] = field.has_default_value() ? "True" : "False"; | 1058 m["has_default_value"] = field.has_default_value() ? "True" : "False"; |
1103 m["default_value"] = StringifyDefaultValue(field); | 1059 m["default_value"] = StringifyDefaultValue(field); |
1104 m["is_extension"] = is_extension ? "True" : "False"; | 1060 m["is_extension"] = is_extension ? "True" : "False"; |
1105 m["options"] = OptionsValue("FieldOptions", options_string); | 1061 m["options"] = OptionsValue("FieldOptions", options_string); |
1106 m["json_name"] = field.has_json_name() ? | |
1107 ", json_name='" + field.json_name() + "'": ""; | |
1108 // We always set message_type and enum_type to None at this point, and then | 1062 // We always set message_type and enum_type to None at this point, and then |
1109 // these fields in correctly after all referenced descriptors have been | 1063 // these fields in correctly after all referenced descriptors have been |
1110 // defined and/or imported (see FixForeignFieldsInDescriptors()). | 1064 // defined and/or imported (see FixForeignFieldsInDescriptors()). |
1111 const char field_descriptor_decl[] = | 1065 const char field_descriptor_decl[] = |
1112 "_descriptor.FieldDescriptor(\n" | 1066 "_descriptor.FieldDescriptor(\n" |
1113 " name='$name$', full_name='$full_name$', index=$index$,\n" | 1067 " name='$name$', full_name='$full_name$', index=$index$,\n" |
1114 " number=$number$, type=$type$, cpp_type=$cpp_type$, label=$label$,\n" | 1068 " number=$number$, type=$type$, cpp_type=$cpp_type$, label=$label$,\n" |
1115 " has_default_value=$has_default_value$, default_value=$default_value$,\n" | 1069 " has_default_value=$has_default_value$, default_value=$default_value$,\n" |
1116 " message_type=None, enum_type=None, containing_type=None,\n" | 1070 " message_type=None, enum_type=None, containing_type=None,\n" |
1117 " is_extension=$is_extension$, extension_scope=None,\n" | 1071 " is_extension=$is_extension$, extension_scope=None,\n" |
1118 " options=$options$$json_name$)"; | 1072 " options=$options$)"; |
1119 printer_->Print(m, field_descriptor_decl); | 1073 printer_->Print(m, field_descriptor_decl); |
1120 } | 1074 } |
1121 | 1075 |
1122 // Helper for Print{Fields,Extensions}InDescriptor(). | 1076 // Helper for Print{Fields,Extensions}InDescriptor(). |
1123 void Generator::PrintFieldDescriptorsInDescriptor( | 1077 void Generator::PrintFieldDescriptorsInDescriptor( |
1124 const Descriptor& message_descriptor, | 1078 const Descriptor& message_descriptor, |
1125 bool is_extension, | 1079 bool is_extension, |
1126 const string& list_variable_name, | 1080 const string& list_variable_name, |
1127 int (Descriptor::*CountFn)() const, | 1081 int (Descriptor::*CountFn)() const, |
1128 const FieldDescriptor* (Descriptor::*GetterFn)(int) const) const { | 1082 const FieldDescriptor* (Descriptor::*GetterFn)(int) const) const { |
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1274 const FieldDescriptor& field = *file_->extension(i); | 1228 const FieldDescriptor& field = *file_->extension(i); |
1275 FixOptionsForField(field); | 1229 FixOptionsForField(field); |
1276 } | 1230 } |
1277 // Prints expressions that set the options for all messages, nested enums, | 1231 // Prints expressions that set the options for all messages, nested enums, |
1278 // nested extensions and message fields. | 1232 // nested extensions and message fields. |
1279 for (int i = 0; i < file_->message_type_count(); ++i) { | 1233 for (int i = 0; i < file_->message_type_count(); ++i) { |
1280 FixOptionsForMessage(*file_->message_type(i)); | 1234 FixOptionsForMessage(*file_->message_type(i)); |
1281 } | 1235 } |
1282 } | 1236 } |
1283 | 1237 |
1284 void Generator::FixOptionsForOneof(const OneofDescriptor& oneof) const { | |
1285 string oneof_options = OptionsValue( | |
1286 "OneofOptions", oneof.options().SerializeAsString()); | |
1287 if (oneof_options != "None") { | |
1288 string oneof_name = strings::Substitute( | |
1289 "$0.$1['$2']", | |
1290 ModuleLevelDescriptorName(*oneof.containing_type()), | |
1291 "oneofs_by_name", oneof.name()); | |
1292 PrintDescriptorOptionsFixingCode(oneof_name, oneof_options, printer_); | |
1293 } | |
1294 } | |
1295 | |
1296 // Prints expressions that set the options for an enum descriptor and its | 1238 // Prints expressions that set the options for an enum descriptor and its |
1297 // value descriptors. | 1239 // value descriptors. |
1298 void Generator::FixOptionsForEnum(const EnumDescriptor& enum_descriptor) const { | 1240 void Generator::FixOptionsForEnum(const EnumDescriptor& enum_descriptor) const { |
1299 string descriptor_name = ModuleLevelDescriptorName(enum_descriptor); | 1241 string descriptor_name = ModuleLevelDescriptorName(enum_descriptor); |
1300 string enum_options = OptionsValue( | 1242 string enum_options = OptionsValue( |
1301 "EnumOptions", enum_descriptor.options().SerializeAsString()); | 1243 "EnumOptions", enum_descriptor.options().SerializeAsString()); |
1302 if (enum_options != "None") { | 1244 if (enum_options != "None") { |
1303 PrintDescriptorOptionsFixingCode(descriptor_name, enum_options, printer_); | 1245 PrintDescriptorOptionsFixingCode(descriptor_name, enum_options, printer_); |
1304 } | 1246 } |
1305 for (int i = 0; i < enum_descriptor.value_count(); ++i) { | 1247 for (int i = 0; i < enum_descriptor.value_count(); ++i) { |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1339 } | 1281 } |
1340 } | 1282 } |
1341 | 1283 |
1342 // Prints expressions that set the options for a message and all its inner | 1284 // Prints expressions that set the options for a message and all its inner |
1343 // types (nested messages, nested enums, extensions, fields). | 1285 // types (nested messages, nested enums, extensions, fields). |
1344 void Generator::FixOptionsForMessage(const Descriptor& descriptor) const { | 1286 void Generator::FixOptionsForMessage(const Descriptor& descriptor) const { |
1345 // Nested messages. | 1287 // Nested messages. |
1346 for (int i = 0; i < descriptor.nested_type_count(); ++i) { | 1288 for (int i = 0; i < descriptor.nested_type_count(); ++i) { |
1347 FixOptionsForMessage(*descriptor.nested_type(i)); | 1289 FixOptionsForMessage(*descriptor.nested_type(i)); |
1348 } | 1290 } |
1349 // Oneofs. | |
1350 for (int i = 0; i < descriptor.oneof_decl_count(); ++i) { | |
1351 FixOptionsForOneof(*descriptor.oneof_decl(i)); | |
1352 } | |
1353 // Enums. | 1291 // Enums. |
1354 for (int i = 0; i < descriptor.enum_type_count(); ++i) { | 1292 for (int i = 0; i < descriptor.enum_type_count(); ++i) { |
1355 FixOptionsForEnum(*descriptor.enum_type(i)); | 1293 FixOptionsForEnum(*descriptor.enum_type(i)); |
1356 } | 1294 } |
1357 // Fields. | 1295 // Fields. |
1358 for (int i = 0; i < descriptor.field_count(); ++i) { | 1296 for (int i = 0; i < descriptor.field_count(); ++i) { |
1359 const FieldDescriptor& field = *descriptor.field(i); | 1297 const FieldDescriptor& field = *descriptor.field(i); |
1360 FixOptionsForField(field); | 1298 FixOptionsForField(field); |
1361 } | 1299 } |
1362 // Extensions. | 1300 // Extensions. |
(...skipping 21 matching lines...) Expand all Loading... |
1384 printer_->Print("$alias$ = $copy_from$.$alias$\n", "alias", module_alias, | 1322 printer_->Print("$alias$ = $copy_from$.$alias$\n", "alias", module_alias, |
1385 "copy_from", copy_from); | 1323 "copy_from", copy_from); |
1386 CopyPublicDependenciesAliases(copy_from, file->public_dependency(i)); | 1324 CopyPublicDependenciesAliases(copy_from, file->public_dependency(i)); |
1387 } | 1325 } |
1388 } | 1326 } |
1389 | 1327 |
1390 } // namespace python | 1328 } // namespace python |
1391 } // namespace compiler | 1329 } // namespace compiler |
1392 } // namespace protobuf | 1330 } // namespace protobuf |
1393 } // namespace google | 1331 } // namespace google |
OLD | NEW |