]>
Commit | Line | Data |
---|---|---|
f67539c2 TL |
1 | /* |
2 | * Licensed to the Apache Software Foundation (ASF) under one | |
3 | * or more contributor license agreements. See the NOTICE file | |
4 | * distributed with this work for additional information | |
5 | * regarding copyright ownership. The ASF licenses this file | |
6 | * to you under the Apache License, Version 2.0 (the | |
7 | * "License"); you may not use this file except in compliance | |
8 | * with the License. You may obtain a copy of the License at | |
9 | * | |
10 | * http://www.apache.org/licenses/LICENSE-2.0 | |
11 | * | |
12 | * Unless required by applicable law or agreed to in writing, | |
13 | * software distributed under the License is distributed on an | |
14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
15 | * KIND, either express or implied. See the License for the | |
16 | * specific language governing permissions and limitations | |
17 | * under the License. | |
18 | */ | |
19 | ||
20 | #include <string> | |
21 | #include <fstream> | |
22 | #include <iomanip> | |
23 | #include <iostream> | |
24 | #include <limits> | |
25 | #include <vector> | |
26 | ||
27 | #include <stdlib.h> | |
28 | #include <sys/stat.h> | |
29 | #include <sys/types.h> | |
30 | #include <sstream> | |
31 | #include <algorithm> | |
32 | #include "thrift/platform.h" | |
33 | #include "thrift/version.h" | |
34 | #include "thrift/generate/t_generator.h" | |
35 | ||
36 | using std::map; | |
37 | using std::ostream; | |
38 | using std::ostringstream; | |
39 | using std::string; | |
40 | using std::stringstream; | |
41 | using std::vector; | |
42 | ||
43 | static const string endl = "\n"; // avoid ostream << std::endl flushes | |
44 | ||
45 | /** | |
46 | * Python code generator. | |
47 | * | |
48 | */ | |
49 | class t_py_generator : public t_generator { | |
50 | public: | |
51 | t_py_generator(t_program* program, | |
52 | const std::map<std::string, std::string>& parsed_options, | |
53 | const std::string& option_string) | |
54 | : t_generator (program) { | |
55 | std::map<std::string, std::string>::const_iterator iter; | |
56 | ||
57 | gen_newstyle_ = true; | |
58 | gen_utf8strings_ = true; | |
59 | gen_dynbase_ = false; | |
60 | gen_slots_ = false; | |
61 | gen_tornado_ = false; | |
62 | gen_zope_interface_ = false; | |
63 | gen_twisted_ = false; | |
64 | gen_dynamic_ = false; | |
65 | coding_ = ""; | |
66 | gen_dynbaseclass_ = ""; | |
67 | gen_dynbaseclass_exc_ = ""; | |
68 | gen_dynbaseclass_frozen_ = ""; | |
69 | import_dynbase_ = ""; | |
70 | package_prefix_ = ""; | |
71 | for( iter = parsed_options.begin(); iter != parsed_options.end(); ++iter) { | |
72 | if( iter->first.compare("new_style") == 0) { | |
73 | pwarning(0, "new_style is enabled by default, so the option will be removed in the near future.\n"); | |
74 | } else if( iter->first.compare("old_style") == 0) { | |
75 | gen_newstyle_ = false; | |
76 | pwarning(0, "old_style is deprecated and may be removed in the future.\n"); | |
77 | } else if( iter->first.compare("utf8strings") == 0) { | |
78 | pwarning(0, "utf8strings is enabled by default, so the option will be removed in the near future.\n"); | |
79 | } else if( iter->first.compare("no_utf8strings") == 0) { | |
80 | gen_utf8strings_ = false; | |
81 | } else if( iter->first.compare("slots") == 0) { | |
82 | gen_slots_ = true; | |
83 | } else if( iter->first.compare("package_prefix") == 0) { | |
84 | package_prefix_ = iter->second; | |
85 | } else if( iter->first.compare("dynamic") == 0) { | |
86 | gen_dynamic_ = true; | |
87 | gen_newstyle_ = false; // dynamic is newstyle | |
88 | if( gen_dynbaseclass_.empty()) { | |
89 | gen_dynbaseclass_ = "TBase"; | |
90 | } | |
91 | if( gen_dynbaseclass_frozen_.empty()) { | |
92 | gen_dynbaseclass_frozen_ = "TFrozenBase"; | |
93 | } | |
94 | if( gen_dynbaseclass_exc_.empty()) { | |
95 | gen_dynbaseclass_exc_ = "TExceptionBase"; | |
96 | } | |
97 | if( import_dynbase_.empty()) { | |
98 | import_dynbase_ = "from thrift.protocol.TBase import TBase, TFrozenBase, TExceptionBase, TTransport\n"; | |
99 | } | |
100 | } else if( iter->first.compare("dynbase") == 0) { | |
101 | gen_dynbase_ = true; | |
102 | gen_dynbaseclass_ = (iter->second); | |
103 | } else if( iter->first.compare("dynfrozen") == 0) { | |
104 | gen_dynbaseclass_frozen_ = (iter->second); | |
105 | } else if( iter->first.compare("dynexc") == 0) { | |
106 | gen_dynbaseclass_exc_ = (iter->second); | |
107 | } else if( iter->first.compare("dynimport") == 0) { | |
108 | gen_dynbase_ = true; | |
109 | import_dynbase_ = (iter->second); | |
110 | } else if( iter->first.compare("zope.interface") == 0) { | |
111 | gen_zope_interface_ = true; | |
112 | } else if( iter->first.compare("twisted") == 0) { | |
113 | gen_twisted_ = true; | |
114 | gen_zope_interface_ = true; | |
115 | } else if( iter->first.compare("tornado") == 0) { | |
116 | gen_tornado_ = true; | |
117 | } else if( iter->first.compare("coding") == 0) { | |
118 | coding_ = iter->second; | |
119 | } else { | |
120 | throw "unknown option py:" + iter->first; | |
121 | } | |
122 | } | |
123 | ||
124 | if (gen_twisted_ && gen_tornado_) { | |
125 | throw "at most one of 'twisted' and 'tornado' are allowed"; | |
126 | } | |
127 | ||
128 | copy_options_ = option_string; | |
129 | ||
130 | if (gen_twisted_) { | |
131 | out_dir_base_ = "gen-py.twisted"; | |
132 | } else if (gen_tornado_) { | |
133 | out_dir_base_ = "gen-py.tornado"; | |
134 | } else { | |
135 | out_dir_base_ = "gen-py"; | |
136 | } | |
137 | } | |
138 | ||
139 | std::string indent_str() const override { | |
140 | return " "; | |
141 | } | |
142 | ||
143 | /** | |
144 | * Init and close methods | |
145 | */ | |
146 | ||
147 | void init_generator() override; | |
148 | void close_generator() override; | |
149 | ||
150 | /** | |
151 | * Program-level generation functions | |
152 | */ | |
153 | ||
154 | void generate_typedef(t_typedef* ttypedef) override; | |
155 | void generate_enum(t_enum* tenum) override; | |
156 | void generate_const(t_const* tconst) override; | |
157 | void generate_struct(t_struct* tstruct) override; | |
158 | void generate_forward_declaration(t_struct* tstruct) override; | |
159 | void generate_xception(t_struct* txception) override; | |
160 | void generate_service(t_service* tservice) override; | |
161 | ||
162 | std::string render_const_value(t_type* type, t_const_value* value); | |
163 | ||
164 | /** | |
165 | * Struct generation code | |
166 | */ | |
167 | ||
168 | void generate_py_struct(t_struct* tstruct, bool is_exception); | |
169 | void generate_py_thrift_spec(std::ostream& out, t_struct* tstruct, bool is_exception); | |
170 | void generate_py_struct_definition(std::ostream& out, | |
171 | t_struct* tstruct, | |
172 | bool is_xception = false); | |
173 | void generate_py_struct_reader(std::ostream& out, t_struct* tstruct); | |
174 | void generate_py_struct_writer(std::ostream& out, t_struct* tstruct); | |
175 | void generate_py_struct_required_validator(std::ostream& out, t_struct* tstruct); | |
176 | void generate_py_function_helpers(t_function* tfunction); | |
177 | ||
178 | /** | |
179 | * Service-level generation functions | |
180 | */ | |
181 | ||
182 | void generate_service_helpers(t_service* tservice); | |
183 | void generate_service_interface(t_service* tservice); | |
184 | void generate_service_client(t_service* tservice); | |
185 | void generate_service_remote(t_service* tservice); | |
186 | void generate_service_server(t_service* tservice); | |
187 | void generate_process_function(t_service* tservice, t_function* tfunction); | |
188 | ||
189 | /** | |
190 | * Serialization constructs | |
191 | */ | |
192 | ||
193 | void generate_deserialize_field(std::ostream& out, | |
194 | t_field* tfield, | |
195 | std::string prefix = ""); | |
196 | ||
197 | void generate_deserialize_struct(std::ostream& out, t_struct* tstruct, std::string prefix = ""); | |
198 | ||
199 | void generate_deserialize_container(std::ostream& out, t_type* ttype, std::string prefix = ""); | |
200 | ||
201 | void generate_deserialize_set_element(std::ostream& out, t_set* tset, std::string prefix = ""); | |
202 | ||
203 | void generate_deserialize_map_element(std::ostream& out, t_map* tmap, std::string prefix = ""); | |
204 | ||
205 | void generate_deserialize_list_element(std::ostream& out, | |
206 | t_list* tlist, | |
207 | std::string prefix = ""); | |
208 | ||
209 | void generate_serialize_field(std::ostream& out, t_field* tfield, std::string prefix = ""); | |
210 | ||
211 | void generate_serialize_struct(std::ostream& out, t_struct* tstruct, std::string prefix = ""); | |
212 | ||
213 | void generate_serialize_container(std::ostream& out, t_type* ttype, std::string prefix = ""); | |
214 | ||
215 | void generate_serialize_map_element(std::ostream& out, | |
216 | t_map* tmap, | |
217 | std::string kiter, | |
218 | std::string viter); | |
219 | ||
220 | void generate_serialize_set_element(std::ostream& out, t_set* tmap, std::string iter); | |
221 | ||
222 | void generate_serialize_list_element(std::ostream& out, t_list* tlist, std::string iter); | |
223 | ||
224 | void generate_python_docstring(std::ostream& out, t_struct* tstruct); | |
225 | ||
226 | void generate_python_docstring(std::ostream& out, t_function* tfunction); | |
227 | ||
228 | void generate_python_docstring(std::ostream& out, | |
229 | t_doc* tdoc, | |
230 | t_struct* tstruct, | |
231 | const char* subheader); | |
232 | ||
233 | void generate_python_docstring(std::ostream& out, t_doc* tdoc); | |
234 | ||
235 | /** | |
236 | * Helper rendering functions | |
237 | */ | |
238 | ||
239 | std::string py_autogen_comment(); | |
240 | std::string py_imports(); | |
241 | std::string render_includes(); | |
242 | std::string declare_argument(t_field* tfield); | |
243 | std::string render_field_default_value(t_field* tfield); | |
244 | std::string type_name(t_type* ttype); | |
245 | std::string function_signature(t_function* tfunction, bool interface = false); | |
246 | std::string argument_list(t_struct* tstruct, | |
247 | std::vector<std::string>* pre = NULL, | |
248 | std::vector<std::string>* post = NULL); | |
249 | std::string type_to_enum(t_type* ttype); | |
250 | std::string type_to_spec_args(t_type* ttype); | |
251 | ||
252 | static bool is_valid_namespace(const std::string& sub_namespace) { | |
253 | return sub_namespace == "twisted"; | |
254 | } | |
255 | ||
256 | static std::string get_real_py_module(const t_program* program, bool gen_twisted, std::string package_dir="") { | |
257 | if (gen_twisted) { | |
258 | std::string twisted_module = program->get_namespace("py.twisted"); | |
259 | if (!twisted_module.empty()) { | |
260 | return twisted_module; | |
261 | } | |
262 | } | |
263 | ||
264 | std::string real_module = program->get_namespace("py"); | |
265 | if (real_module.empty()) { | |
266 | return program->get_name(); | |
267 | } | |
268 | return package_dir + real_module; | |
269 | } | |
270 | ||
271 | static bool is_immutable(t_type* ttype) { | |
272 | return ttype->annotations_.find("python.immutable") != ttype->annotations_.end(); | |
273 | } | |
274 | ||
275 | private: | |
276 | ||
277 | /** | |
278 | * True if we should generate new-style classes. | |
279 | */ | |
280 | bool gen_newstyle_; | |
281 | ||
282 | /** | |
283 | * True if we should generate dynamic style classes. | |
284 | */ | |
285 | bool gen_dynamic_; | |
286 | ||
287 | bool gen_dynbase_; | |
288 | std::string gen_dynbaseclass_; | |
289 | std::string gen_dynbaseclass_frozen_; | |
290 | std::string gen_dynbaseclass_exc_; | |
291 | ||
292 | std::string import_dynbase_; | |
293 | ||
294 | bool gen_slots_; | |
295 | ||
296 | std::string copy_options_; | |
297 | ||
298 | /** | |
299 | * True if we should generate code for use with zope.interface. | |
300 | */ | |
301 | bool gen_zope_interface_; | |
302 | ||
303 | /** | |
304 | * True if we should generate Twisted-friendly RPC services. | |
305 | */ | |
306 | bool gen_twisted_; | |
307 | ||
308 | /** | |
309 | * True if we should generate code for use with Tornado | |
310 | */ | |
311 | bool gen_tornado_; | |
312 | ||
313 | /** | |
314 | * True if strings should be encoded using utf-8. | |
315 | */ | |
316 | bool gen_utf8strings_; | |
317 | ||
318 | /** | |
319 | * specify generated file encoding | |
320 | * eg. # -*- coding: utf-8 -*- | |
321 | */ | |
322 | string coding_; | |
323 | ||
324 | string package_prefix_; | |
325 | ||
326 | /** | |
327 | * File streams | |
328 | */ | |
329 | ||
330 | ofstream_with_content_based_conditional_update f_types_; | |
331 | ofstream_with_content_based_conditional_update f_consts_; | |
332 | ofstream_with_content_based_conditional_update f_service_; | |
333 | ||
334 | std::string package_dir_; | |
335 | std::string module_; | |
336 | ||
337 | protected: | |
338 | std::set<std::string> lang_keywords() const override { | |
339 | std::string keywords[] = { "False", "None", "True", "and", "as", "assert", "break", "class", | |
340 | "continue", "def", "del", "elif", "else", "except", "exec", "finally", "for", "from", | |
341 | "global", "if", "import", "in", "is", "lambda", "nonlocal", "not", "or", "pass", "print", | |
342 | "raise", "return", "try", "while", "with", "yield" }; | |
343 | return std::set<std::string>(keywords, keywords + sizeof(keywords)/sizeof(keywords[0]) ); | |
344 | } | |
345 | }; | |
346 | ||
347 | /** | |
348 | * Prepares for file generation by opening up the necessary file output | |
349 | * streams. | |
350 | * | |
351 | * @param tprogram The program to generate | |
352 | */ | |
353 | void t_py_generator::init_generator() { | |
354 | // Make output directory | |
355 | string module = get_real_py_module(program_, gen_twisted_); | |
356 | package_dir_ = get_out_dir(); | |
357 | module_ = module; | |
358 | while (true) { | |
359 | // TODO: Do better error checking here. | |
360 | MKDIR(package_dir_.c_str()); | |
361 | std::ofstream init_py((package_dir_ + "/__init__.py").c_str(), std::ios_base::app); | |
362 | init_py.close(); | |
363 | if (module.empty()) { | |
364 | break; | |
365 | } | |
366 | string::size_type pos = module.find('.'); | |
367 | if (pos == string::npos) { | |
368 | package_dir_ += "/"; | |
369 | package_dir_ += module; | |
370 | module.clear(); | |
371 | } else { | |
372 | package_dir_ += "/"; | |
373 | package_dir_ += module.substr(0, pos); | |
374 | module.erase(0, pos + 1); | |
375 | } | |
376 | } | |
377 | ||
378 | // Make output file | |
379 | string f_types_name = package_dir_ + "/" + "ttypes.py"; | |
380 | f_types_.open(f_types_name.c_str()); | |
381 | ||
382 | string f_consts_name = package_dir_ + "/" + "constants.py"; | |
383 | f_consts_.open(f_consts_name.c_str()); | |
384 | ||
385 | string f_init_name = package_dir_ + "/__init__.py"; | |
386 | ofstream_with_content_based_conditional_update f_init; | |
387 | f_init.open(f_init_name.c_str()); | |
388 | f_init << "__all__ = ['ttypes', 'constants'"; | |
389 | vector<t_service*> services = program_->get_services(); | |
390 | vector<t_service*>::iterator sv_iter; | |
391 | for (sv_iter = services.begin(); sv_iter != services.end(); ++sv_iter) { | |
392 | f_init << ", '" << (*sv_iter)->get_name() << "'"; | |
393 | } | |
394 | f_init << "]" << endl; | |
395 | f_init.close(); | |
396 | ||
397 | // Print header | |
398 | f_types_ << py_autogen_comment() << endl | |
399 | << py_imports() << endl | |
400 | << render_includes() << endl | |
401 | << "from thrift.transport import TTransport" << endl | |
402 | << import_dynbase_; | |
403 | ||
404 | f_types_ << "all_structs = []" << endl; | |
405 | ||
406 | f_consts_ << | |
407 | py_autogen_comment() << endl << | |
408 | py_imports() << endl << | |
409 | "from .ttypes import *" << endl; | |
410 | } | |
411 | ||
412 | /** | |
413 | * Renders all the imports necessary for including another Thrift program | |
414 | */ | |
415 | string t_py_generator::render_includes() { | |
416 | const vector<t_program*>& includes = program_->get_includes(); | |
417 | string result = ""; | |
418 | for (auto include : includes) { | |
419 | result += "import " + get_real_py_module(include, gen_twisted_, package_prefix_) + ".ttypes\n"; | |
420 | } | |
421 | return result; | |
422 | } | |
423 | ||
424 | /** | |
425 | * Autogen'd comment | |
426 | */ | |
427 | string t_py_generator::py_autogen_comment() { | |
428 | string coding; | |
429 | if (!coding_.empty()) { | |
430 | coding = "# -*- coding: " + coding_ + " -*-\n"; | |
431 | } | |
432 | return coding + std::string("#\n") + "# Autogenerated by Thrift Compiler (" + THRIFT_VERSION + ")\n" | |
433 | + "#\n" + "# DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING\n" + "#\n" | |
434 | + "# options string: " + copy_options_ + "\n" + "#\n"; | |
435 | } | |
436 | ||
437 | /** | |
438 | * Prints standard thrift imports | |
439 | */ | |
440 | string t_py_generator::py_imports() { | |
441 | ostringstream ss; | |
442 | ss << "from thrift.Thrift import TType, TMessageType, TFrozenDict, TException, " | |
443 | "TApplicationException" | |
444 | << endl | |
445 | << "from thrift.protocol.TProtocol import TProtocolException" | |
446 | << endl | |
447 | << "from thrift.TRecursive import fix_spec" | |
448 | << endl; | |
449 | ||
450 | if (gen_utf8strings_) { | |
451 | ss << endl << "import sys"; | |
452 | } | |
453 | return ss.str(); | |
454 | } | |
455 | ||
456 | /** | |
457 | * Closes the type files | |
458 | */ | |
459 | void t_py_generator::close_generator() { | |
460 | ||
461 | // Fix thrift_spec definitions for recursive structs. | |
462 | f_types_ << "fix_spec(all_structs)" << endl; | |
463 | f_types_ << "del all_structs" << endl; | |
464 | ||
465 | // Close types file | |
466 | f_types_.close(); | |
467 | f_consts_.close(); | |
468 | } | |
469 | ||
470 | /** | |
471 | * Generates a typedef. This is not done in Python, types are all implicit. | |
472 | * | |
473 | * @param ttypedef The type definition | |
474 | */ | |
475 | void t_py_generator::generate_typedef(t_typedef* ttypedef) { | |
476 | (void)ttypedef; | |
477 | } | |
478 | ||
479 | /** | |
480 | * Generates code for an enumerated type. Done using a class to scope | |
481 | * the values. | |
482 | * | |
483 | * @param tenum The enumeration | |
484 | */ | |
485 | void t_py_generator::generate_enum(t_enum* tenum) { | |
486 | std::ostringstream to_string_mapping, from_string_mapping; | |
487 | ||
488 | f_types_ << endl << endl << "class " << tenum->get_name() << (gen_newstyle_ ? "(object)" : "") | |
489 | << (gen_dynamic_ ? "(" + gen_dynbaseclass_ + ")" : "") << ":" << endl; | |
490 | indent_up(); | |
491 | generate_python_docstring(f_types_, tenum); | |
492 | ||
493 | to_string_mapping << indent() << "_VALUES_TO_NAMES = {" << endl; | |
494 | from_string_mapping << indent() << "_NAMES_TO_VALUES = {" << endl; | |
495 | ||
496 | vector<t_enum_value*> constants = tenum->get_constants(); | |
497 | vector<t_enum_value*>::iterator c_iter; | |
498 | for (c_iter = constants.begin(); c_iter != constants.end(); ++c_iter) { | |
499 | int value = (*c_iter)->get_value(); | |
500 | indent(f_types_) << (*c_iter)->get_name() << " = " << value << endl; | |
501 | ||
502 | // Dictionaries to/from string names of enums | |
503 | to_string_mapping << indent() << indent() << value << ": \"" | |
504 | << escape_string((*c_iter)->get_name()) << "\"," << endl; | |
505 | from_string_mapping << indent() << indent() << '"' << escape_string((*c_iter)->get_name()) | |
506 | << "\": " << value << ',' << endl; | |
507 | } | |
508 | to_string_mapping << indent() << "}" << endl; | |
509 | from_string_mapping << indent() << "}" << endl; | |
510 | ||
511 | indent_down(); | |
512 | f_types_ << endl; | |
513 | f_types_ << to_string_mapping.str() << endl << from_string_mapping.str(); | |
514 | } | |
515 | ||
516 | /** | |
517 | * Generate a constant value | |
518 | */ | |
519 | void t_py_generator::generate_const(t_const* tconst) { | |
520 | t_type* type = tconst->get_type(); | |
521 | string name = tconst->get_name(); | |
522 | t_const_value* value = tconst->get_value(); | |
523 | ||
524 | indent(f_consts_) << name << " = " << render_const_value(type, value); | |
525 | f_consts_ << endl; | |
526 | } | |
527 | ||
528 | /** | |
529 | * Prints the value of a constant with the given type. Note that type checking | |
530 | * is NOT performed in this function as it is always run beforehand using the | |
531 | * validate_types method in main.cc | |
532 | */ | |
533 | string t_py_generator::render_const_value(t_type* type, t_const_value* value) { | |
534 | type = get_true_type(type); | |
535 | std::ostringstream out; | |
536 | ||
537 | if (type->is_base_type()) { | |
538 | t_base_type::t_base tbase = ((t_base_type*)type)->get_base(); | |
539 | switch (tbase) { | |
540 | case t_base_type::TYPE_STRING: | |
541 | if (((t_base_type*)type)->is_binary()) { | |
542 | out << 'b'; | |
543 | } | |
544 | out << '"' << get_escaped_string(value) << '"'; | |
545 | break; | |
546 | case t_base_type::TYPE_BOOL: | |
547 | out << (value->get_integer() > 0 ? "True" : "False"); | |
548 | break; | |
549 | case t_base_type::TYPE_I8: | |
550 | case t_base_type::TYPE_I16: | |
551 | case t_base_type::TYPE_I32: | |
552 | case t_base_type::TYPE_I64: | |
553 | out << value->get_integer(); | |
554 | break; | |
555 | case t_base_type::TYPE_DOUBLE: | |
556 | if (value->get_type() == t_const_value::CV_INTEGER) { | |
557 | out << "float(" << value->get_integer() << ")"; | |
558 | } else { | |
559 | out << emit_double_as_string(value->get_double()); | |
560 | } | |
561 | break; | |
562 | default: | |
563 | throw "compiler error: no const of base type " + t_base_type::t_base_name(tbase); | |
564 | } | |
565 | } else if (type->is_enum()) { | |
566 | out << value->get_integer(); | |
567 | } else if (type->is_struct() || type->is_xception()) { | |
568 | out << type_name(type) << "(**{" << endl; | |
569 | indent_up(); | |
570 | const vector<t_field*>& fields = ((t_struct*)type)->get_members(); | |
571 | vector<t_field*>::const_iterator f_iter; | |
572 | const map<t_const_value*, t_const_value*, t_const_value::value_compare>& val = value->get_map(); | |
573 | map<t_const_value*, t_const_value*, t_const_value::value_compare>::const_iterator v_iter; | |
574 | for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) { | |
575 | t_type* field_type = NULL; | |
576 | for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) { | |
577 | if ((*f_iter)->get_name() == v_iter->first->get_string()) { | |
578 | field_type = (*f_iter)->get_type(); | |
579 | } | |
580 | } | |
581 | if (field_type == NULL) { | |
582 | throw "type error: " + type->get_name() + " has no field " + v_iter->first->get_string(); | |
583 | } | |
584 | indent(out) << render_const_value(g_type_string, v_iter->first) << ": " | |
585 | << render_const_value(field_type, v_iter->second) << "," << endl; | |
586 | } | |
587 | indent_down(); | |
588 | indent(out) << "})"; | |
589 | } else if (type->is_map()) { | |
590 | t_type* ktype = ((t_map*)type)->get_key_type(); | |
591 | t_type* vtype = ((t_map*)type)->get_val_type(); | |
592 | if (is_immutable(type)) { | |
593 | out << "TFrozenDict("; | |
594 | } | |
595 | out << "{" << endl; | |
596 | indent_up(); | |
597 | const map<t_const_value*, t_const_value*, t_const_value::value_compare>& val = value->get_map(); | |
598 | map<t_const_value*, t_const_value*, t_const_value::value_compare>::const_iterator v_iter; | |
599 | for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) { | |
600 | indent(out) << render_const_value(ktype, v_iter->first) << ": " | |
601 | << render_const_value(vtype, v_iter->second) << "," << endl; | |
602 | } | |
603 | indent_down(); | |
604 | indent(out) << "}"; | |
605 | if (is_immutable(type)) { | |
606 | out << ")"; | |
607 | } | |
608 | } else if (type->is_list() || type->is_set()) { | |
609 | t_type* etype; | |
610 | if (type->is_list()) { | |
611 | etype = ((t_list*)type)->get_elem_type(); | |
612 | } else { | |
613 | etype = ((t_set*)type)->get_elem_type(); | |
614 | } | |
615 | if (type->is_set()) { | |
616 | if (is_immutable(type)) { | |
617 | out << "frozen"; | |
618 | } | |
619 | out << "set("; | |
620 | } | |
621 | if (is_immutable(type) || type->is_set()) { | |
622 | out << "(" << endl; | |
623 | } else { | |
624 | out << "[" << endl; | |
625 | } | |
626 | indent_up(); | |
627 | const vector<t_const_value*>& val = value->get_list(); | |
628 | vector<t_const_value*>::const_iterator v_iter; | |
629 | for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) { | |
630 | indent(out) << render_const_value(etype, *v_iter) << "," << endl; | |
631 | } | |
632 | indent_down(); | |
633 | if (is_immutable(type) || type->is_set()) { | |
634 | indent(out) << ")"; | |
635 | } else { | |
636 | indent(out) << "]"; | |
637 | } | |
638 | if (type->is_set()) { | |
639 | out << ")"; | |
640 | } | |
641 | } else { | |
642 | throw "CANNOT GENERATE CONSTANT FOR TYPE: " + type->get_name(); | |
643 | } | |
644 | ||
645 | return out.str(); | |
646 | } | |
647 | ||
648 | /** | |
649 | * Generates the "forward declarations" for python structs. | |
650 | * These are actually full class definitions so that calls to generate_struct | |
651 | * can add the thrift_spec field. This is needed so that all thrift_spec | |
652 | * definitions are grouped at the end of the file to enable co-recursive structs. | |
653 | */ | |
654 | void t_py_generator::generate_forward_declaration(t_struct* tstruct) { | |
655 | generate_py_struct(tstruct, tstruct->is_xception()); | |
656 | } | |
657 | ||
658 | /** | |
659 | * Generates a python struct | |
660 | */ | |
661 | void t_py_generator::generate_struct(t_struct* tstruct) { | |
662 | generate_py_thrift_spec(f_types_, tstruct, false); | |
663 | } | |
664 | ||
665 | /** | |
666 | * Generates a struct definition for a thrift exception. Basically the same | |
667 | * as a struct but extends the Exception class. | |
668 | * | |
669 | * @param txception The struct definition | |
670 | */ | |
671 | void t_py_generator::generate_xception(t_struct* txception) { | |
672 | generate_py_thrift_spec(f_types_, txception, true); | |
673 | } | |
674 | ||
675 | /** | |
676 | * Generates a python struct | |
677 | */ | |
678 | void t_py_generator::generate_py_struct(t_struct* tstruct, bool is_exception) { | |
679 | generate_py_struct_definition(f_types_, tstruct, is_exception); | |
680 | } | |
681 | ||
682 | ||
683 | /** | |
684 | * Generate the thrift_spec for a struct | |
685 | * For example, | |
686 | * all_structs.append(Recursive) | |
687 | * Recursive.thrift_spec = ( | |
688 | * None, # 0 | |
689 | * (1, TType.LIST, 'Children', (TType.STRUCT, (Recursive, None), False), None, ), # 1 | |
690 | * ) | |
691 | */ | |
692 | void t_py_generator::generate_py_thrift_spec(ostream& out, | |
693 | t_struct* tstruct, | |
694 | bool /*is_exception*/) { | |
695 | const vector<t_field*>& sorted_members = tstruct->get_sorted_members(); | |
696 | vector<t_field*>::const_iterator m_iter; | |
697 | ||
698 | // Add struct definition to list so thrift_spec can be fixed for recursive structures. | |
699 | indent(out) << "all_structs.append(" << tstruct->get_name() << ")" << endl; | |
700 | ||
701 | if (sorted_members.empty() || (sorted_members[0]->get_key() >= 0)) { | |
702 | indent(out) << tstruct->get_name() << ".thrift_spec = (" << endl; | |
703 | indent_up(); | |
704 | ||
705 | int sorted_keys_pos = 0; | |
706 | for (m_iter = sorted_members.begin(); m_iter != sorted_members.end(); ++m_iter) { | |
707 | ||
708 | for (; sorted_keys_pos != (*m_iter)->get_key(); sorted_keys_pos++) { | |
709 | indent(out) << "None, # " << sorted_keys_pos << endl; | |
710 | } | |
711 | ||
712 | indent(out) << "(" << (*m_iter)->get_key() << ", " << type_to_enum((*m_iter)->get_type()) | |
713 | << ", " | |
714 | << "'" << (*m_iter)->get_name() << "'" | |
715 | << ", " << type_to_spec_args((*m_iter)->get_type()) << ", " | |
716 | << render_field_default_value(*m_iter) << ", " | |
717 | << ")," | |
718 | << " # " << sorted_keys_pos << endl; | |
719 | ||
720 | sorted_keys_pos++; | |
721 | } | |
722 | ||
723 | indent_down(); | |
724 | indent(out) << ")" << endl; | |
725 | } else { | |
726 | indent(out) << tstruct->get_name() << ".thrift_spec = ()" << endl; | |
727 | } | |
728 | } | |
729 | ||
730 | /** | |
731 | * Generates a struct definition for a thrift data type. | |
732 | * | |
733 | * @param tstruct The struct definition | |
734 | */ | |
735 | void t_py_generator::generate_py_struct_definition(ostream& out, | |
736 | t_struct* tstruct, | |
737 | bool is_exception) { | |
738 | const vector<t_field*>& members = tstruct->get_members(); | |
739 | const vector<t_field*>& sorted_members = tstruct->get_sorted_members(); | |
740 | vector<t_field*>::const_iterator m_iter; | |
741 | ||
742 | out << endl << endl << "class " << tstruct->get_name(); | |
743 | if (is_exception) { | |
744 | if (gen_dynamic_) { | |
745 | out << "(" << gen_dynbaseclass_exc_ << ")"; | |
746 | } else { | |
747 | out << "(TException)"; | |
748 | } | |
749 | } else if (gen_dynamic_) { | |
750 | if (is_immutable(tstruct)) { | |
751 | out << "(" << gen_dynbaseclass_frozen_ << ")"; | |
752 | } else { | |
753 | out << "(" << gen_dynbaseclass_ << ")"; | |
754 | } | |
755 | } else if (gen_newstyle_) { | |
756 | out << "(object)"; | |
757 | } | |
758 | out << ":" << endl; | |
759 | indent_up(); | |
760 | generate_python_docstring(out, tstruct); | |
761 | ||
762 | out << endl; | |
763 | ||
764 | /* | |
765 | Here we generate the structure specification for the fastbinary codec. | |
766 | These specifications have the following structure: | |
767 | thrift_spec -> tuple of item_spec | |
768 | item_spec -> None | (tag, type_enum, name, spec_args, default) | |
769 | tag -> integer | |
770 | type_enum -> TType.I32 | TType.STRING | TType.STRUCT | ... | |
771 | name -> string_literal | |
772 | default -> None # Handled by __init__ | |
773 | spec_args -> None # For simple types | |
774 | | (type_enum, spec_args) # Value type for list/set | |
775 | | (type_enum, spec_args, type_enum, spec_args) | |
776 | # Key and value for map | |
777 | | (class_name, spec_args_ptr) # For struct/exception | |
778 | class_name -> identifier # Basically a pointer to the class | |
779 | spec_args_ptr -> expression # just class_name.spec_args | |
780 | ||
781 | TODO(dreiss): Consider making this work for structs with negative tags. | |
782 | */ | |
783 | ||
784 | if (gen_slots_) { | |
785 | indent(out) << "__slots__ = (" << endl; | |
786 | indent_up(); | |
787 | for (m_iter = sorted_members.begin(); m_iter != sorted_members.end(); ++m_iter) { | |
788 | indent(out) << "'" << (*m_iter)->get_name() << "'," << endl; | |
789 | } | |
790 | indent_down(); | |
791 | indent(out) << ")" << endl << endl; | |
792 | } | |
793 | ||
794 | // TODO(dreiss): Look into generating an empty tuple instead of None | |
795 | // for structures with no members. | |
796 | // TODO(dreiss): Test encoding of structs where some inner structs | |
797 | // don't have thrift_spec. | |
798 | ||
799 | if (members.size() > 0) { | |
800 | out << endl; | |
801 | out << indent() << "def __init__(self,"; | |
802 | ||
803 | for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) { | |
804 | out << " " << declare_argument(*m_iter) << ","; | |
805 | } | |
806 | out << "):" << endl; | |
807 | ||
808 | indent_up(); | |
809 | ||
810 | for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) { | |
811 | // Initialize fields | |
812 | t_type* type = (*m_iter)->get_type(); | |
813 | if (!type->is_base_type() && !type->is_enum() && (*m_iter)->get_value() != NULL) { | |
814 | indent(out) << "if " << (*m_iter)->get_name() << " is " | |
815 | << "self.thrift_spec[" << (*m_iter)->get_key() << "][4]:" << endl; | |
816 | indent_up(); | |
817 | indent(out) << (*m_iter)->get_name() << " = " << render_field_default_value(*m_iter) | |
818 | << endl; | |
819 | indent_down(); | |
820 | } | |
821 | ||
822 | if (is_immutable(tstruct)) { | |
823 | if (gen_newstyle_ || gen_dynamic_) { | |
824 | indent(out) << "super(" << tstruct->get_name() << ", self).__setattr__('" | |
825 | << (*m_iter)->get_name() << "', " << (*m_iter)->get_name() << ")" << endl; | |
826 | } else { | |
827 | indent(out) << "self.__dict__['" << (*m_iter)->get_name() | |
828 | << "'] = " << (*m_iter)->get_name() << endl; | |
829 | } | |
830 | } else { | |
831 | indent(out) << "self." << (*m_iter)->get_name() << " = " << (*m_iter)->get_name() << endl; | |
832 | } | |
833 | } | |
834 | ||
835 | indent_down(); | |
836 | } | |
837 | ||
838 | if (is_immutable(tstruct)) { | |
839 | out << endl; | |
840 | out << indent() << "def __setattr__(self, *args):" << endl | |
841 | << indent() << indent_str() << "raise TypeError(\"can't modify immutable instance\")" << endl | |
842 | << endl; | |
843 | out << indent() << "def __delattr__(self, *args):" << endl | |
844 | << indent() << indent_str() << "raise TypeError(\"can't modify immutable instance\")" << endl | |
845 | << endl; | |
846 | ||
847 | // Hash all of the members in order, and also hash in the class | |
848 | // to avoid collisions for stuff like single-field structures. | |
849 | out << indent() << "def __hash__(self):" << endl | |
850 | << indent() << indent_str() << "return hash(self.__class__) ^ hash(("; | |
851 | ||
852 | for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) { | |
853 | out << "self." << (*m_iter)->get_name() << ", "; | |
854 | } | |
855 | ||
856 | out << "))" << endl; | |
857 | } | |
858 | ||
859 | if (!gen_dynamic_) { | |
860 | out << endl; | |
861 | generate_py_struct_reader(out, tstruct); | |
862 | generate_py_struct_writer(out, tstruct); | |
863 | } | |
864 | ||
865 | // For exceptions only, generate a __str__ method. This is | |
866 | // because when raised exceptions are printed to the console, __repr__ | |
867 | // isn't used. See python bug #5882 | |
868 | if (is_exception) { | |
869 | out << endl; | |
870 | out << indent() << "def __str__(self):" << endl | |
871 | << indent() << indent_str() << "return repr(self)" << endl; | |
872 | } | |
873 | ||
874 | if (!gen_slots_) { | |
875 | out << endl; | |
876 | // Printing utilities so that on the command line thrift | |
877 | // structs look pretty like dictionaries | |
878 | indent(out) << "def __repr__(self):" << endl; | |
879 | indent_up(); | |
880 | out << indent() << "L = ['%s=%r' % (key, value)" << endl | |
881 | << indent() << " for key, value in self.__dict__.items()]" << endl | |
882 | << indent() << "return '%s(%s)' % (self.__class__.__name__, ', '.join(L))" << endl | |
883 | << endl; | |
884 | indent_down(); | |
885 | ||
886 | // Equality and inequality methods that compare by value | |
887 | out << indent() << "def __eq__(self, other):" << endl; | |
888 | indent_up(); | |
889 | out << indent() << "return isinstance(other, self.__class__) and " | |
890 | "self.__dict__ == other.__dict__" << endl; | |
891 | indent_down(); | |
892 | out << endl; | |
893 | ||
894 | out << indent() << "def __ne__(self, other):" << endl; | |
895 | indent_up(); | |
896 | ||
897 | out << indent() << "return not (self == other)" << endl; | |
898 | indent_down(); | |
899 | } else if (!gen_dynamic_) { | |
900 | out << endl; | |
901 | // no base class available to implement __eq__ and __repr__ and __ne__ for us | |
902 | // so we must provide one that uses __slots__ | |
903 | indent(out) << "def __repr__(self):" << endl; | |
904 | indent_up(); | |
905 | out << indent() << "L = ['%s=%r' % (key, getattr(self, key))" << endl | |
906 | << indent() << " for key in self.__slots__]" << endl | |
907 | << indent() << "return '%s(%s)' % (self.__class__.__name__, ', '.join(L))" << endl | |
908 | << endl; | |
909 | indent_down(); | |
910 | ||
911 | // Equality method that compares each attribute by value and type, walking __slots__ | |
912 | out << indent() << "def __eq__(self, other):" << endl; | |
913 | indent_up(); | |
914 | out << indent() << "if not isinstance(other, self.__class__):" << endl | |
915 | << indent() << indent_str() << "return False" << endl | |
916 | << indent() << "for attr in self.__slots__:" << endl | |
917 | << indent() << indent_str() << "my_val = getattr(self, attr)" << endl | |
918 | << indent() << indent_str() << "other_val = getattr(other, attr)" << endl | |
919 | << indent() << indent_str() << "if my_val != other_val:" << endl | |
920 | << indent() << indent_str() << indent_str() << "return False" << endl | |
921 | << indent() << "return True" << endl | |
922 | << endl; | |
923 | indent_down(); | |
924 | ||
925 | out << indent() << "def __ne__(self, other):" << endl | |
926 | << indent() << indent_str() << "return not (self == other)" << endl; | |
927 | } | |
928 | indent_down(); | |
929 | } | |
930 | ||
931 | /** | |
932 | * Generates the read method for a struct | |
933 | */ | |
934 | void t_py_generator::generate_py_struct_reader(ostream& out, t_struct* tstruct) { | |
935 | const vector<t_field*>& fields = tstruct->get_members(); | |
936 | vector<t_field*>::const_iterator f_iter; | |
937 | ||
938 | if (is_immutable(tstruct)) { | |
939 | out << indent() << "@classmethod" << endl << indent() << "def read(cls, iprot):" << endl; | |
940 | } else { | |
941 | indent(out) << "def read(self, iprot):" << endl; | |
942 | } | |
943 | indent_up(); | |
944 | ||
945 | const char* id = is_immutable(tstruct) ? "cls" : "self"; | |
946 | ||
947 | indent(out) << "if iprot._fast_decode is not None " | |
948 | "and isinstance(iprot.trans, TTransport.CReadableTransport) " | |
949 | "and " | |
950 | << id << ".thrift_spec is not None:" << endl; | |
951 | indent_up(); | |
952 | ||
953 | if (is_immutable(tstruct)) { | |
954 | indent(out) << "return iprot._fast_decode(None, iprot, [cls, cls.thrift_spec])" << endl; | |
955 | } else { | |
956 | indent(out) << "iprot._fast_decode(self, iprot, [self.__class__, self.thrift_spec])" << endl; | |
957 | indent(out) << "return" << endl; | |
958 | } | |
959 | indent_down(); | |
960 | ||
961 | indent(out) << "iprot.readStructBegin()" << endl; | |
962 | ||
963 | if (is_immutable(tstruct)) { | |
964 | for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) { | |
965 | t_field* tfield = *f_iter; | |
966 | std::ostringstream result; | |
967 | result << tfield->get_name() << " = "; | |
968 | if (tfield->get_value() != NULL) { | |
969 | result << render_field_default_value(tfield); | |
970 | } else { | |
971 | result << "None"; | |
972 | } | |
973 | indent(out) << result.str() << endl; | |
974 | } | |
975 | } | |
976 | ||
977 | // Loop over reading in fields | |
978 | indent(out) << "while True:" << endl; | |
979 | indent_up(); | |
980 | ||
981 | // Read beginning field marker | |
982 | indent(out) << "(fname, ftype, fid) = iprot.readFieldBegin()" << endl; | |
983 | ||
984 | // Check for field STOP marker and break | |
985 | indent(out) << "if ftype == TType.STOP:" << endl; | |
986 | indent_up(); | |
987 | indent(out) << "break" << endl; | |
988 | indent_down(); | |
989 | ||
990 | // Switch statement on the field we are reading | |
991 | bool first = true; | |
992 | ||
993 | // Generate deserialization code for known cases | |
994 | for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) { | |
995 | if (first) { | |
996 | first = false; | |
997 | out << indent() << "if "; | |
998 | } else { | |
999 | out << indent() << "elif "; | |
1000 | } | |
1001 | out << "fid == " << (*f_iter)->get_key() << ":" << endl; | |
1002 | indent_up(); | |
1003 | indent(out) << "if ftype == " << type_to_enum((*f_iter)->get_type()) << ":" << endl; | |
1004 | indent_up(); | |
1005 | if (is_immutable(tstruct)) { | |
1006 | generate_deserialize_field(out, *f_iter); | |
1007 | } else { | |
1008 | generate_deserialize_field(out, *f_iter, "self."); | |
1009 | } | |
1010 | indent_down(); | |
1011 | out << indent() << "else:" << endl << indent() << indent_str() << "iprot.skip(ftype)" << endl; | |
1012 | indent_down(); | |
1013 | } | |
1014 | ||
1015 | // In the default case we skip the field | |
1016 | out << indent() << "else:" << endl << indent() << indent_str() << "iprot.skip(ftype)" << endl; | |
1017 | ||
1018 | // Read field end marker | |
1019 | indent(out) << "iprot.readFieldEnd()" << endl; | |
1020 | ||
1021 | indent_down(); | |
1022 | ||
1023 | indent(out) << "iprot.readStructEnd()" << endl; | |
1024 | ||
1025 | if (is_immutable(tstruct)) { | |
1026 | indent(out) << "return cls(" << endl; | |
1027 | indent_up(); | |
1028 | for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) { | |
1029 | indent(out) << (*f_iter)->get_name() << "=" << (*f_iter)->get_name() << "," << endl; | |
1030 | } | |
1031 | indent_down(); | |
1032 | indent(out) << ")" << endl; | |
1033 | } | |
1034 | ||
1035 | indent_down(); | |
1036 | out << endl; | |
1037 | } | |
1038 | ||
1039 | void t_py_generator::generate_py_struct_writer(ostream& out, t_struct* tstruct) { | |
1040 | string name = tstruct->get_name(); | |
1041 | const vector<t_field*>& fields = tstruct->get_sorted_members(); | |
1042 | vector<t_field*>::const_iterator f_iter; | |
1043 | ||
1044 | indent(out) << "def write(self, oprot):" << endl; | |
1045 | indent_up(); | |
1046 | ||
1047 | indent(out) << "if oprot._fast_encode is not None and self.thrift_spec is not None:" << endl; | |
1048 | indent_up(); | |
1049 | ||
1050 | indent(out) | |
1051 | << "oprot.trans.write(oprot._fast_encode(self, [self.__class__, self.thrift_spec]))" | |
1052 | << endl; | |
1053 | indent(out) << "return" << endl; | |
1054 | indent_down(); | |
1055 | ||
1056 | indent(out) << "oprot.writeStructBegin('" << name << "')" << endl; | |
1057 | ||
1058 | for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) { | |
1059 | // Write field header | |
1060 | indent(out) << "if self." << (*f_iter)->get_name() << " is not None:" << endl; | |
1061 | indent_up(); | |
1062 | indent(out) << "oprot.writeFieldBegin(" | |
1063 | << "'" << (*f_iter)->get_name() << "', " << type_to_enum((*f_iter)->get_type()) | |
1064 | << ", " << (*f_iter)->get_key() << ")" << endl; | |
1065 | ||
1066 | // Write field contents | |
1067 | generate_serialize_field(out, *f_iter, "self."); | |
1068 | ||
1069 | // Write field closer | |
1070 | indent(out) << "oprot.writeFieldEnd()" << endl; | |
1071 | ||
1072 | indent_down(); | |
1073 | } | |
1074 | ||
1075 | // Write the struct map | |
1076 | out << indent() << "oprot.writeFieldStop()" << endl << indent() << "oprot.writeStructEnd()" | |
1077 | << endl; | |
1078 | ||
1079 | out << endl; | |
1080 | ||
1081 | indent_down(); | |
1082 | generate_py_struct_required_validator(out, tstruct); | |
1083 | } | |
1084 | ||
1085 | void t_py_generator::generate_py_struct_required_validator(ostream& out, t_struct* tstruct) { | |
1086 | indent(out) << "def validate(self):" << endl; | |
1087 | indent_up(); | |
1088 | ||
1089 | const vector<t_field*>& fields = tstruct->get_members(); | |
1090 | ||
1091 | if (fields.size() > 0) { | |
1092 | vector<t_field*>::const_iterator f_iter; | |
1093 | ||
1094 | for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) { | |
1095 | t_field* field = (*f_iter); | |
1096 | if (field->get_req() == t_field::T_REQUIRED) { | |
1097 | indent(out) << "if self." << field->get_name() << " is None:" << endl; | |
1098 | indent(out) << indent_str() << "raise TProtocolException(message='Required field " | |
1099 | << field->get_name() << " is unset!')" << endl; | |
1100 | } | |
1101 | } | |
1102 | } | |
1103 | ||
1104 | indent(out) << "return" << endl; | |
1105 | indent_down(); | |
1106 | } | |
1107 | ||
1108 | /** | |
1109 | * Generates a thrift service. | |
1110 | * | |
1111 | * @param tservice The service definition | |
1112 | */ | |
1113 | void t_py_generator::generate_service(t_service* tservice) { | |
1114 | string f_service_name = package_dir_ + "/" + service_name_ + ".py"; | |
1115 | f_service_.open(f_service_name.c_str()); | |
1116 | ||
1117 | f_service_ << py_autogen_comment() << endl << py_imports() << endl; | |
1118 | ||
1119 | if (tservice->get_extends() != NULL) { | |
1120 | f_service_ << "import " | |
1121 | << get_real_py_module(tservice->get_extends()->get_program(), gen_twisted_, package_prefix_) << "." | |
1122 | << tservice->get_extends()->get_name() << endl; | |
1123 | } | |
1124 | ||
1125 | f_service_ << "import logging" << endl | |
1126 | << "from .ttypes import *" << endl | |
1127 | << "from thrift.Thrift import TProcessor" << endl | |
1128 | << "from thrift.transport import TTransport" << endl | |
1129 | << import_dynbase_; | |
1130 | if (gen_zope_interface_) { | |
1131 | f_service_ << "from zope.interface import Interface, implementer" << endl; | |
1132 | } | |
1133 | ||
1134 | if (gen_twisted_) { | |
1135 | f_service_ << "from twisted.internet import defer" << endl | |
1136 | << "from thrift.transport import TTwisted" << endl; | |
1137 | } else if (gen_tornado_) { | |
1138 | f_service_ << "from tornado import gen" << endl; | |
1139 | f_service_ << "from tornado import concurrent" << endl; | |
1140 | } | |
1141 | ||
1142 | f_service_ << "all_structs = []" << endl; | |
1143 | ||
1144 | // Generate the three main parts of the service | |
1145 | generate_service_interface(tservice); | |
1146 | generate_service_client(tservice); | |
1147 | generate_service_server(tservice); | |
1148 | generate_service_helpers(tservice); | |
1149 | generate_service_remote(tservice); | |
1150 | ||
1151 | // Close service file | |
1152 | f_service_ << "fix_spec(all_structs)" << endl | |
1153 | << "del all_structs" << endl << endl; | |
1154 | f_service_.close(); | |
1155 | } | |
1156 | ||
1157 | /** | |
1158 | * Generates helper functions for a service. | |
1159 | * | |
1160 | * @param tservice The service to generate a header definition for | |
1161 | */ | |
1162 | void t_py_generator::generate_service_helpers(t_service* tservice) { | |
1163 | vector<t_function*> functions = tservice->get_functions(); | |
1164 | vector<t_function*>::iterator f_iter; | |
1165 | ||
1166 | f_service_ << endl << "# HELPER FUNCTIONS AND STRUCTURES" << endl; | |
1167 | ||
1168 | for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) { | |
1169 | t_struct* ts = (*f_iter)->get_arglist(); | |
1170 | generate_py_struct_definition(f_service_, ts, false); | |
1171 | generate_py_thrift_spec(f_service_, ts, false); | |
1172 | generate_py_function_helpers(*f_iter); | |
1173 | } | |
1174 | } | |
1175 | ||
1176 | /** | |
1177 | * Generates a struct and helpers for a function. | |
1178 | * | |
1179 | * @param tfunction The function | |
1180 | */ | |
1181 | void t_py_generator::generate_py_function_helpers(t_function* tfunction) { | |
1182 | if (!tfunction->is_oneway()) { | |
1183 | t_struct result(program_, tfunction->get_name() + "_result"); | |
1184 | t_field success(tfunction->get_returntype(), "success", 0); | |
1185 | if (!tfunction->get_returntype()->is_void()) { | |
1186 | result.append(&success); | |
1187 | } | |
1188 | ||
1189 | t_struct* xs = tfunction->get_xceptions(); | |
1190 | const vector<t_field*>& fields = xs->get_members(); | |
1191 | vector<t_field*>::const_iterator f_iter; | |
1192 | for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) { | |
1193 | result.append(*f_iter); | |
1194 | } | |
1195 | generate_py_struct_definition(f_service_, &result, false); | |
1196 | generate_py_thrift_spec(f_service_, &result, false); | |
1197 | } | |
1198 | } | |
1199 | ||
1200 | /** | |
1201 | * Generates a service interface definition. | |
1202 | * | |
1203 | * @param tservice The service to generate a header definition for | |
1204 | */ | |
1205 | void t_py_generator::generate_service_interface(t_service* tservice) { | |
1206 | string extends = ""; | |
1207 | string extends_if = ""; | |
1208 | if (tservice->get_extends() != NULL) { | |
1209 | extends = type_name(tservice->get_extends()); | |
1210 | extends_if = "(" + extends + ".Iface)"; | |
1211 | } else { | |
1212 | if (gen_zope_interface_) { | |
1213 | extends_if = "(Interface)"; | |
1214 | } else if (gen_newstyle_ || gen_dynamic_ || gen_tornado_) { | |
1215 | extends_if = "(object)"; | |
1216 | } | |
1217 | } | |
1218 | ||
1219 | f_service_ << endl << endl << "class Iface" << extends_if << ":" << endl; | |
1220 | indent_up(); | |
1221 | generate_python_docstring(f_service_, tservice); | |
1222 | vector<t_function*> functions = tservice->get_functions(); | |
1223 | if (functions.empty()) { | |
1224 | f_service_ << indent() << "pass" << endl; | |
1225 | } else { | |
1226 | vector<t_function*>::iterator f_iter; | |
1227 | bool first = true; | |
1228 | for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) { | |
1229 | if (first) { | |
1230 | first = false; | |
1231 | } else { | |
1232 | f_service_ << endl; | |
1233 | } | |
1234 | f_service_ << indent() << "def " << function_signature(*f_iter, true) << ":" << endl; | |
1235 | indent_up(); | |
1236 | generate_python_docstring(f_service_, (*f_iter)); | |
1237 | f_service_ << indent() << "pass" << endl; | |
1238 | indent_down(); | |
1239 | } | |
1240 | } | |
1241 | ||
1242 | indent_down(); | |
1243 | } | |
1244 | ||
1245 | /** | |
1246 | * Generates a service client definition. | |
1247 | * | |
1248 | * @param tservice The service to generate a server for. | |
1249 | */ | |
1250 | void t_py_generator::generate_service_client(t_service* tservice) { | |
1251 | string extends = ""; | |
1252 | string extends_client = ""; | |
1253 | if (tservice->get_extends() != NULL) { | |
1254 | extends = type_name(tservice->get_extends()); | |
1255 | if (gen_zope_interface_) { | |
1256 | extends_client = "(" + extends + ".Client)"; | |
1257 | } else { | |
1258 | extends_client = extends + ".Client, "; | |
1259 | } | |
1260 | } else { | |
1261 | if (gen_zope_interface_ && (gen_newstyle_ || gen_dynamic_)) { | |
1262 | extends_client = "(object)"; | |
1263 | } | |
1264 | } | |
1265 | ||
1266 | f_service_ << endl << endl; | |
1267 | ||
1268 | if (gen_zope_interface_) { | |
1269 | f_service_ << "@implementer(Iface)" << endl | |
1270 | << "class Client" << extends_client << ":" << endl | |
1271 | << endl; | |
1272 | } else { | |
1273 | f_service_ << "class Client(" << extends_client << "Iface):" << endl; | |
1274 | } | |
1275 | indent_up(); | |
1276 | generate_python_docstring(f_service_, tservice); | |
1277 | ||
1278 | // Constructor function | |
1279 | if (gen_twisted_) { | |
1280 | f_service_ << indent() << "def __init__(self, transport, oprot_factory):" << endl; | |
1281 | } else if (gen_tornado_) { | |
1282 | f_service_ << indent() | |
1283 | << "def __init__(self, transport, iprot_factory, oprot_factory=None):" << endl; | |
1284 | } else { | |
1285 | f_service_ << indent() << "def __init__(self, iprot, oprot=None):" << endl; | |
1286 | } | |
1287 | indent_up(); | |
1288 | if (extends.empty()) { | |
1289 | if (gen_twisted_) { | |
1290 | f_service_ << indent() << "self._transport = transport" << endl | |
1291 | << indent() << "self._oprot_factory = oprot_factory" << endl | |
1292 | << indent() << "self._seqid = 0" << endl | |
1293 | << indent() << "self._reqs = {}" << endl; | |
1294 | } else if (gen_tornado_) { | |
1295 | f_service_ << indent() << "self._transport = transport" << endl | |
1296 | << indent() << "self._iprot_factory = iprot_factory" << endl | |
1297 | << indent() << "self._oprot_factory = (oprot_factory if oprot_factory is not None" | |
1298 | << endl | |
1299 | << indent() << " else iprot_factory)" << endl | |
1300 | << indent() << "self._seqid = 0" << endl | |
1301 | << indent() << "self._reqs = {}" << endl | |
1302 | << indent() << "self._transport.io_loop.spawn_callback(self._start_receiving)" | |
1303 | << endl; | |
1304 | } else { | |
1305 | f_service_ << indent() << "self._iprot = self._oprot = iprot" << endl | |
1306 | << indent() << "if oprot is not None:" << endl | |
1307 | << indent() << indent_str() << "self._oprot = oprot" << endl | |
1308 | << indent() << "self._seqid = 0" << endl; | |
1309 | } | |
1310 | } else { | |
1311 | if (gen_twisted_) { | |
1312 | f_service_ << indent() << extends | |
1313 | << ".Client.__init__(self, transport, oprot_factory)" << endl; | |
1314 | } else if (gen_tornado_) { | |
1315 | f_service_ << indent() << extends | |
1316 | << ".Client.__init__(self, transport, iprot_factory, oprot_factory)" << endl; | |
1317 | } else { | |
1318 | f_service_ << indent() << extends << ".Client.__init__(self, iprot, oprot)" << endl; | |
1319 | } | |
1320 | } | |
1321 | indent_down(); | |
1322 | ||
1323 | if (gen_tornado_ && extends.empty()) { | |
1324 | f_service_ << endl << | |
1325 | indent() << "@gen.engine" << endl << | |
1326 | indent() << "def _start_receiving(self):" << endl; | |
1327 | indent_up(); | |
1328 | indent(f_service_) << "while True:" << endl; | |
1329 | indent_up(); | |
1330 | f_service_ << indent() << "try:" << endl | |
1331 | << indent() << indent_str() << "frame = yield self._transport.readFrame()" << endl | |
1332 | << indent() << "except TTransport.TTransportException as e:" << endl | |
1333 | << indent() << indent_str() << "for future in self._reqs.values():" << endl | |
1334 | << indent() << indent_str() << indent_str() << "future.set_exception(e)" << endl | |
1335 | << indent() << indent_str() << "self._reqs = {}" << endl | |
1336 | << indent() << indent_str() << "return" << endl | |
1337 | << indent() << "tr = TTransport.TMemoryBuffer(frame)" << endl | |
1338 | << indent() << "iprot = self._iprot_factory.getProtocol(tr)" << endl | |
1339 | << indent() << "(fname, mtype, rseqid) = iprot.readMessageBegin()" << endl | |
1340 | << indent() << "method = getattr(self, 'recv_' + fname)" << endl | |
1341 | << indent() << "future = self._reqs.pop(rseqid, None)" << endl | |
1342 | << indent() << "if not future:" << endl | |
1343 | << indent() << indent_str() << "# future has already been discarded" << endl | |
1344 | << indent() << indent_str() << "continue" << endl | |
1345 | << indent() << "try:" << endl | |
1346 | << indent() << indent_str() << "result = method(iprot, mtype, rseqid)" << endl | |
1347 | << indent() << "except Exception as e:" << endl | |
1348 | << indent() << indent_str() << "future.set_exception(e)" << endl | |
1349 | << indent() << "else:" << endl | |
1350 | << indent() << indent_str() << "future.set_result(result)" << endl; | |
1351 | indent_down(); | |
1352 | indent_down(); | |
1353 | } | |
1354 | ||
1355 | // Generate client method implementations | |
1356 | vector<t_function*> functions = tservice->get_functions(); | |
1357 | vector<t_function*>::const_iterator f_iter; | |
1358 | for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) { | |
1359 | t_struct* arg_struct = (*f_iter)->get_arglist(); | |
1360 | const vector<t_field*>& fields = arg_struct->get_members(); | |
1361 | vector<t_field*>::const_iterator fld_iter; | |
1362 | string funname = (*f_iter)->get_name(); | |
1363 | ||
1364 | f_service_ << endl; | |
1365 | // Open function | |
1366 | indent(f_service_) << "def " << function_signature(*f_iter, false) << ":" << endl; | |
1367 | indent_up(); | |
1368 | generate_python_docstring(f_service_, (*f_iter)); | |
1369 | if (gen_twisted_) { | |
1370 | indent(f_service_) << "seqid = self._seqid = self._seqid + 1" << endl; | |
1371 | indent(f_service_) << "self._reqs[seqid] = defer.Deferred()" << endl << endl; | |
1372 | indent(f_service_) << "d = defer.maybeDeferred(self.send_" << funname; | |
1373 | ||
1374 | } else if (gen_tornado_) { | |
1375 | indent(f_service_) << "self._seqid += 1" << endl; | |
1376 | if (!(*f_iter)->is_oneway()) { | |
1377 | indent(f_service_) << "future = self._reqs[self._seqid] = concurrent.Future()" << endl; | |
1378 | } | |
1379 | indent(f_service_) << "self.send_" << funname << "("; | |
1380 | ||
1381 | } else { | |
1382 | indent(f_service_) << "self.send_" << funname << "("; | |
1383 | } | |
1384 | ||
1385 | bool first = true; | |
1386 | if (gen_twisted_) { | |
1387 | // we need a leading comma if there are args, since it's called as maybeDeferred(funcname, | |
1388 | // arg) | |
1389 | first = false; | |
1390 | } | |
1391 | for (fld_iter = fields.begin(); fld_iter != fields.end(); ++fld_iter) { | |
1392 | if (first) { | |
1393 | first = false; | |
1394 | } else { | |
1395 | f_service_ << ", "; | |
1396 | } | |
1397 | f_service_ << (*fld_iter)->get_name(); | |
1398 | } | |
1399 | ||
1400 | f_service_ << ")" << endl; | |
1401 | ||
1402 | if (!(*f_iter)->is_oneway()) { | |
1403 | if (gen_twisted_) { | |
1404 | // nothing. See the next block. | |
1405 | } else if (gen_tornado_) { | |
1406 | indent(f_service_) << "return future" << endl; | |
1407 | } else { | |
1408 | f_service_ << indent(); | |
1409 | if (!(*f_iter)->get_returntype()->is_void()) { | |
1410 | f_service_ << "return "; | |
1411 | } | |
1412 | f_service_ << "self.recv_" << funname << "()" << endl; | |
1413 | } | |
1414 | } | |
1415 | indent_down(); | |
1416 | ||
1417 | if (gen_twisted_) { | |
1418 | // This block injects the body of the send_<> method for twisted (and a cb/eb pair) | |
1419 | indent_up(); | |
1420 | indent(f_service_) << "d.addCallbacks(" << endl; | |
1421 | ||
1422 | indent_up(); | |
1423 | f_service_ << indent() << "callback=self.cb_send_" << funname << "," << endl << indent() | |
1424 | << "callbackArgs=(seqid,)," << endl << indent() << "errback=self.eb_send_" | |
1425 | << funname << "," << endl << indent() << "errbackArgs=(seqid,))" << endl; | |
1426 | indent_down(); | |
1427 | ||
1428 | indent(f_service_) << "return d" << endl; | |
1429 | indent_down(); | |
1430 | f_service_ << endl; | |
1431 | ||
1432 | indent(f_service_) << "def cb_send_" << funname << "(self, _, seqid):" << endl; | |
1433 | indent_up(); | |
1434 | if ((*f_iter)->is_oneway()) { | |
1435 | // if one-way, fire the deferred & remove it from _reqs | |
1436 | f_service_ << indent() << "d = self._reqs.pop(seqid)" << endl << indent() | |
1437 | << "d.callback(None)" << endl << indent() << "return d" << endl; | |
1438 | } else { | |
1439 | f_service_ << indent() << "return self._reqs[seqid]" << endl; | |
1440 | } | |
1441 | indent_down(); | |
1442 | f_service_ << endl; | |
1443 | ||
1444 | // add an errback to fail the request if the call to send_<> raised an exception | |
1445 | indent(f_service_) << "def eb_send_" << funname << "(self, f, seqid):" << endl; | |
1446 | indent_up(); | |
1447 | f_service_ << indent() << "d = self._reqs.pop(seqid)" << endl << indent() << "d.errback(f)" | |
1448 | << endl << indent() << "return d" << endl; | |
1449 | indent_down(); | |
1450 | } | |
1451 | ||
1452 | f_service_ << endl; | |
1453 | indent(f_service_) << "def send_" << function_signature(*f_iter, false) << ":" << endl; | |
1454 | indent_up(); | |
1455 | ||
1456 | std::string argsname = (*f_iter)->get_name() + "_args"; | |
1457 | std::string messageType = (*f_iter)->is_oneway() ? "TMessageType.ONEWAY" : "TMessageType.CALL"; | |
1458 | ||
1459 | // Serialize the request header | |
1460 | if (gen_twisted_ || gen_tornado_) { | |
1461 | f_service_ << indent() << "oprot = self._oprot_factory.getProtocol(self._transport)" << endl | |
1462 | << indent() << "oprot.writeMessageBegin('" << (*f_iter)->get_name() << "', " | |
1463 | << messageType << ", self._seqid)" << endl; | |
1464 | } else { | |
1465 | f_service_ << indent() << "self._oprot.writeMessageBegin('" << (*f_iter)->get_name() << "', " | |
1466 | << messageType << ", self._seqid)" << endl; | |
1467 | } | |
1468 | ||
1469 | f_service_ << indent() << "args = " << argsname << "()" << endl; | |
1470 | ||
1471 | for (fld_iter = fields.begin(); fld_iter != fields.end(); ++fld_iter) { | |
1472 | f_service_ << indent() << "args." << (*fld_iter)->get_name() << " = " | |
1473 | << (*fld_iter)->get_name() << endl; | |
1474 | } | |
1475 | ||
1476 | // Write to the stream | |
1477 | if (gen_twisted_ || gen_tornado_) { | |
1478 | f_service_ << indent() << "args.write(oprot)" << endl << indent() << "oprot.writeMessageEnd()" | |
1479 | << endl << indent() << "oprot.trans.flush()" << endl; | |
1480 | } else { | |
1481 | f_service_ << indent() << "args.write(self._oprot)" << endl << indent() | |
1482 | << "self._oprot.writeMessageEnd()" << endl << indent() | |
1483 | << "self._oprot.trans.flush()" << endl; | |
1484 | } | |
1485 | ||
1486 | indent_down(); | |
1487 | ||
1488 | if (!(*f_iter)->is_oneway()) { | |
1489 | std::string resultname = (*f_iter)->get_name() + "_result"; | |
1490 | // Open function | |
1491 | f_service_ << endl; | |
1492 | if (gen_twisted_ || gen_tornado_) { | |
1493 | f_service_ << indent() << "def recv_" << (*f_iter)->get_name() | |
1494 | << "(self, iprot, mtype, rseqid):" << endl; | |
1495 | } else { | |
1496 | t_struct noargs(program_); | |
1497 | t_function recv_function((*f_iter)->get_returntype(), | |
1498 | string("recv_") + (*f_iter)->get_name(), | |
1499 | &noargs); | |
1500 | f_service_ << indent() << "def " << function_signature(&recv_function) << ":" << endl; | |
1501 | } | |
1502 | indent_up(); | |
1503 | ||
1504 | // TODO(mcslee): Validate message reply here, seq ids etc. | |
1505 | ||
1506 | if (gen_twisted_) { | |
1507 | f_service_ << indent() << "d = self._reqs.pop(rseqid)" << endl; | |
1508 | } else if (gen_tornado_) { | |
1509 | } else { | |
1510 | f_service_ << indent() << "iprot = self._iprot" << endl << indent() | |
1511 | << "(fname, mtype, rseqid) = iprot.readMessageBegin()" << endl; | |
1512 | } | |
1513 | ||
1514 | f_service_ << indent() << "if mtype == TMessageType.EXCEPTION:" << endl | |
1515 | << indent() << indent_str() << "x = TApplicationException()" << endl; | |
1516 | ||
1517 | if (gen_twisted_) { | |
1518 | f_service_ << indent() << indent_str() << "x.read(iprot)" << endl << indent() | |
1519 | << indent_str() << "iprot.readMessageEnd()" << endl << indent() << indent_str() << "return d.errback(x)" | |
1520 | << endl << indent() << "result = " << resultname << "()" << endl << indent() | |
1521 | << "result.read(iprot)" << endl << indent() << "iprot.readMessageEnd()" << endl; | |
1522 | } else { | |
1523 | f_service_ << indent() << indent_str() << "x.read(iprot)" << endl << indent() | |
1524 | << indent_str() << "iprot.readMessageEnd()" << endl << indent() << indent_str() << "raise x" << endl | |
1525 | << indent() << "result = " << resultname << "()" << endl << indent() | |
1526 | << "result.read(iprot)" << endl << indent() << "iprot.readMessageEnd()" << endl; | |
1527 | } | |
1528 | ||
1529 | // Careful, only return _result if not a void function | |
1530 | if (!(*f_iter)->get_returntype()->is_void()) { | |
1531 | f_service_ << indent() << "if result.success is not None:" << endl; | |
1532 | if (gen_twisted_) { | |
1533 | f_service_ << indent() << indent_str() << "return d.callback(result.success)" << endl; | |
1534 | } else { | |
1535 | f_service_ << indent() << indent_str() << "return result.success" << endl; | |
1536 | } | |
1537 | } | |
1538 | ||
1539 | t_struct* xs = (*f_iter)->get_xceptions(); | |
1540 | const std::vector<t_field*>& xceptions = xs->get_members(); | |
1541 | vector<t_field*>::const_iterator x_iter; | |
1542 | for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) { | |
1543 | const string& xname = (*x_iter)->get_name(); | |
1544 | f_service_ << indent() << "if result." << xname << " is not None:" << endl; | |
1545 | if (gen_twisted_) { | |
1546 | f_service_ << indent() << indent_str() << "return d.errback(result." << xname << ")" | |
1547 | << endl; | |
1548 | } else { | |
1549 | f_service_ << indent() << indent_str() << "raise result." << xname << "" << endl; | |
1550 | } | |
1551 | } | |
1552 | ||
1553 | // Careful, only return _result if not a void function | |
1554 | if ((*f_iter)->get_returntype()->is_void()) { | |
1555 | if (gen_twisted_) { | |
1556 | f_service_ << indent() << "return d.callback(None)" << endl; | |
1557 | } else { | |
1558 | f_service_ << indent() << "return" << endl; | |
1559 | } | |
1560 | } else { | |
1561 | if (gen_twisted_) { | |
1562 | f_service_ | |
1563 | << indent() | |
1564 | << "return d.errback(TApplicationException(TApplicationException.MISSING_RESULT, \"" | |
1565 | << (*f_iter)->get_name() << " failed: unknown result\"))" << endl; | |
1566 | } else { | |
1567 | f_service_ << indent() | |
1568 | << "raise TApplicationException(TApplicationException.MISSING_RESULT, \"" | |
1569 | << (*f_iter)->get_name() << " failed: unknown result\")" << endl; | |
1570 | } | |
1571 | } | |
1572 | ||
1573 | // Close function | |
1574 | indent_down(); | |
1575 | } | |
1576 | } | |
1577 | ||
1578 | indent_down(); | |
1579 | } | |
1580 | ||
1581 | /** | |
1582 | * Generates a command line tool for making remote requests | |
1583 | * | |
1584 | * @param tservice The service to generate a remote for. | |
1585 | */ | |
1586 | void t_py_generator::generate_service_remote(t_service* tservice) { | |
1587 | vector<t_function*> functions = tservice->get_functions(); | |
1588 | // Get all function from parents | |
1589 | t_service* parent = tservice->get_extends(); | |
1590 | while (parent != NULL) { | |
1591 | vector<t_function*> p_functions = parent->get_functions(); | |
1592 | functions.insert(functions.end(), p_functions.begin(), p_functions.end()); | |
1593 | parent = parent->get_extends(); | |
1594 | } | |
1595 | vector<t_function*>::iterator f_iter; | |
1596 | ||
1597 | string f_remote_name = package_dir_ + "/" + service_name_ + "-remote"; | |
1598 | ofstream_with_content_based_conditional_update f_remote; | |
1599 | f_remote.open(f_remote_name.c_str()); | |
1600 | ||
1601 | f_remote << | |
1602 | "#!/usr/bin/env python" << endl << | |
1603 | py_autogen_comment() << endl << | |
1604 | "import sys" << endl << | |
1605 | "import pprint" << endl << | |
1606 | "if sys.version_info[0] > 2:" << endl << | |
1607 | indent_str() << "from urllib.parse import urlparse" << endl << | |
1608 | "else:" << endl << | |
1609 | indent_str() << "from urlparse import urlparse" << endl << | |
1610 | "from thrift.transport import TTransport, TSocket, TSSLSocket, THttpClient" << endl << | |
1611 | "from thrift.protocol.TBinaryProtocol import TBinaryProtocol" << endl << | |
1612 | endl; | |
1613 | ||
1614 | f_remote << | |
1615 | "from " << module_ << " import " << service_name_ << endl << | |
1616 | "from " << module_ << ".ttypes import *" << endl << | |
1617 | endl; | |
1618 | ||
1619 | f_remote << | |
1620 | "if len(sys.argv) <= 1 or sys.argv[1] == '--help':" << endl << | |
1621 | indent_str() << "print('')" << endl << | |
1622 | indent_str() << "print('Usage: ' + sys.argv[0] + ' [-h host[:port]] [-u url] [-f[ramed]] [-s[sl]] [-novalidate] [-ca_certs certs] [-keyfile keyfile] [-certfile certfile] function [arg1 [arg2...]]')" << endl << | |
1623 | indent_str() << "print('')" << endl << | |
1624 | indent_str() << "print('Functions:')" << endl; | |
1625 | for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) { | |
1626 | f_remote << indent_str() << "print(' " << (*f_iter)->get_returntype()->get_name() << " " | |
1627 | << (*f_iter)->get_name() << "("; | |
1628 | t_struct* arg_struct = (*f_iter)->get_arglist(); | |
1629 | const std::vector<t_field*>& args = arg_struct->get_members(); | |
1630 | std::vector<t_field*>::size_type num_args = args.size(); | |
1631 | bool first = true; | |
1632 | for (std::vector<t_field*>::size_type i = 0; i < num_args; ++i) { | |
1633 | if (first) { | |
1634 | first = false; | |
1635 | } else { | |
1636 | f_remote << ", "; | |
1637 | } | |
1638 | f_remote << args[i]->get_type()->get_name() << " " << args[i]->get_name(); | |
1639 | } | |
1640 | f_remote << ")')" << endl; | |
1641 | } | |
1642 | f_remote << indent_str() << "print('')" << endl << indent_str() << "sys.exit(0)" << endl << endl; | |
1643 | ||
1644 | f_remote << "pp = pprint.PrettyPrinter(indent=2)" << endl | |
1645 | << "host = 'localhost'" << endl | |
1646 | << "port = 9090" << endl | |
1647 | << "uri = ''" << endl | |
1648 | << "framed = False" << endl | |
1649 | << "ssl = False" << endl | |
1650 | << "validate = True" << endl | |
1651 | << "ca_certs = None" << endl | |
1652 | << "keyfile = None" << endl | |
1653 | << "certfile = None" << endl | |
1654 | << "http = False" << endl | |
1655 | << "argi = 1" << endl | |
1656 | << endl | |
1657 | << "if sys.argv[argi] == '-h':" << endl | |
1658 | << indent_str() << "parts = sys.argv[argi + 1].split(':')" << endl | |
1659 | << indent_str() << "host = parts[0]" << endl | |
1660 | << indent_str() << "if len(parts) > 1:" << endl | |
1661 | << indent_str() << indent_str() << "port = int(parts[1])" << endl | |
1662 | << indent_str() << "argi += 2" << endl | |
1663 | << endl | |
1664 | << "if sys.argv[argi] == '-u':" << endl | |
1665 | << indent_str() << "url = urlparse(sys.argv[argi + 1])" << endl | |
1666 | << indent_str() << "parts = url[1].split(':')" << endl | |
1667 | << indent_str() << "host = parts[0]" << endl | |
1668 | << indent_str() << "if len(parts) > 1:" << endl | |
1669 | << indent_str() << indent_str() << "port = int(parts[1])" << endl | |
1670 | << indent_str() << "else:" << endl | |
1671 | << indent_str() << indent_str() << "port = 80" << endl | |
1672 | << indent_str() << "uri = url[2]" << endl | |
1673 | << indent_str() << "if url[4]:" << endl | |
1674 | << indent_str() << indent_str() << "uri += '?%s' % url[4]" << endl | |
1675 | << indent_str() << "http = True" << endl | |
1676 | << indent_str() << "argi += 2" << endl | |
1677 | << endl | |
1678 | << "if sys.argv[argi] == '-f' or sys.argv[argi] == '-framed':" << endl | |
1679 | << indent_str() << "framed = True" << endl | |
1680 | << indent_str() << "argi += 1" << endl | |
1681 | << endl | |
1682 | << "if sys.argv[argi] == '-s' or sys.argv[argi] == '-ssl':" << endl | |
1683 | << indent_str() << "ssl = True" << endl | |
1684 | << indent_str() << "argi += 1" << endl | |
1685 | << endl | |
1686 | << "if sys.argv[argi] == '-novalidate':" << endl | |
1687 | << indent_str() << "validate = False" << endl | |
1688 | << indent_str() << "argi += 1" << endl | |
1689 | << endl | |
1690 | << "if sys.argv[argi] == '-ca_certs':" << endl | |
1691 | << indent_str() << "ca_certs = sys.argv[argi+1]" << endl | |
1692 | << indent_str() << "argi += 2" << endl | |
1693 | << endl | |
1694 | << "if sys.argv[argi] == '-keyfile':" << endl | |
1695 | << indent_str() << "keyfile = sys.argv[argi+1]" << endl | |
1696 | << indent_str() << "argi += 2" << endl | |
1697 | << endl | |
1698 | << "if sys.argv[argi] == '-certfile':" << endl | |
1699 | << indent_str() << "certfile = sys.argv[argi+1]" << endl | |
1700 | << indent_str() << "argi += 2" << endl | |
1701 | << endl | |
1702 | << "cmd = sys.argv[argi]" << endl | |
1703 | << "args = sys.argv[argi + 1:]" << endl | |
1704 | << endl | |
1705 | << "if http:" << endl | |
1706 | << indent_str() << "transport = THttpClient.THttpClient(host, port, uri)" << endl | |
1707 | << "else:" << endl | |
1708 | << indent_str() << "if ssl:" << endl | |
1709 | << indent_str() << indent_str() << "socket = TSSLSocket.TSSLSocket(host, port, " | |
1710 | "validate=validate, ca_certs=ca_certs, keyfile=keyfile, certfile=certfile)" | |
1711 | << endl | |
1712 | << indent_str() << "else:" << endl | |
1713 | << indent_str() << indent_str() << "socket = TSocket.TSocket(host, port)" << endl | |
1714 | << indent_str() << "if framed:" << endl | |
1715 | << indent_str() << indent_str() << "transport = TTransport.TFramedTransport(socket)" << endl | |
1716 | << indent_str() << "else:" << endl | |
1717 | << indent_str() << indent_str() << "transport = TTransport.TBufferedTransport(socket)" << endl | |
1718 | << "protocol = TBinaryProtocol(transport)" << endl | |
1719 | << "client = " << service_name_ << ".Client(protocol)" << endl | |
1720 | << "transport.open()" << endl | |
1721 | << endl; | |
1722 | ||
1723 | // Generate the dispatch methods | |
1724 | bool first = true; | |
1725 | ||
1726 | for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) { | |
1727 | if (first) { | |
1728 | first = false; | |
1729 | } else { | |
1730 | f_remote << "el"; | |
1731 | } | |
1732 | ||
1733 | t_struct* arg_struct = (*f_iter)->get_arglist(); | |
1734 | const std::vector<t_field*>& args = arg_struct->get_members(); | |
1735 | std::vector<t_field*>::size_type num_args = args.size(); | |
1736 | ||
1737 | f_remote << "if cmd == '" << (*f_iter)->get_name() << "':" << endl; | |
1738 | indent_up(); | |
1739 | f_remote << indent() << "if len(args) != " << num_args << ":" << endl | |
1740 | << indent() << indent_str() << "print('" << (*f_iter)->get_name() << " requires " << num_args | |
1741 | << " args')" << endl | |
1742 | << indent() << indent_str() << "sys.exit(1)" << endl | |
1743 | << indent() << "pp.pprint(client." << (*f_iter)->get_name() << "("; | |
1744 | indent_down(); | |
1745 | bool first_arg = true; | |
1746 | for (std::vector<t_field*>::size_type i = 0; i < num_args; ++i) { | |
1747 | if (first_arg) | |
1748 | first_arg = false; | |
1749 | else | |
1750 | f_remote << " "; | |
1751 | if (args[i]->get_type()->is_string()) { | |
1752 | f_remote << "args[" << i << "],"; | |
1753 | } else { | |
1754 | f_remote << "eval(args[" << i << "]),"; | |
1755 | } | |
1756 | } | |
1757 | f_remote << "))" << endl; | |
1758 | ||
1759 | f_remote << endl; | |
1760 | } | |
1761 | ||
1762 | if (functions.size() > 0) { | |
1763 | f_remote << "else:" << endl; | |
1764 | f_remote << indent_str() << "print('Unrecognized method %s' % cmd)" << endl; | |
1765 | f_remote << indent_str() << "sys.exit(1)" << endl; | |
1766 | f_remote << endl; | |
1767 | } | |
1768 | ||
1769 | f_remote << "transport.close()" << endl; | |
1770 | ||
1771 | // Close service file | |
1772 | f_remote.close(); | |
1773 | ||
1774 | #ifndef _MSC_VER | |
1775 | ||
1776 | // Make file executable, love that bitwise OR action | |
1777 | chmod(f_remote_name.c_str(), | |
1778 | S_IRUSR | S_IWUSR | S_IXUSR | |
1779 | #ifndef _WIN32 | |
1780 | | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH | |
1781 | #endif | |
1782 | ); | |
1783 | ||
1784 | #endif // _MSC_VER | |
1785 | } | |
1786 | ||
1787 | /** | |
1788 | * Generates a service server definition. | |
1789 | * | |
1790 | * @param tservice The service to generate a server for. | |
1791 | */ | |
1792 | void t_py_generator::generate_service_server(t_service* tservice) { | |
1793 | // Generate the dispatch methods | |
1794 | vector<t_function*> functions = tservice->get_functions(); | |
1795 | vector<t_function*>::iterator f_iter; | |
1796 | ||
1797 | string extends = ""; | |
1798 | string extends_processor = ""; | |
1799 | if (tservice->get_extends() != NULL) { | |
1800 | extends = type_name(tservice->get_extends()); | |
1801 | extends_processor = extends + ".Processor, "; | |
1802 | } | |
1803 | ||
1804 | f_service_ << endl << endl; | |
1805 | ||
1806 | // Generate the header portion | |
1807 | if (gen_zope_interface_) { | |
1808 | f_service_ << "@implementer(Iface)" << endl | |
1809 | << "class Processor(" << extends_processor << "TProcessor):" << endl; | |
1810 | } else { | |
1811 | f_service_ << "class Processor(" << extends_processor << "Iface, TProcessor):" << endl; | |
1812 | } | |
1813 | ||
1814 | indent_up(); | |
1815 | ||
1816 | indent(f_service_) << "def __init__(self, handler):" << endl; | |
1817 | indent_up(); | |
1818 | if (extends.empty()) { | |
1819 | if (gen_zope_interface_) { | |
1820 | f_service_ << indent() << "self._handler = Iface(handler)" << endl; | |
1821 | } else { | |
1822 | f_service_ << indent() << "self._handler = handler" << endl; | |
1823 | } | |
1824 | ||
1825 | f_service_ << indent() << "self._processMap = {}" << endl; | |
1826 | } else { | |
1827 | if (gen_zope_interface_) { | |
1828 | f_service_ << indent() << extends << ".Processor.__init__(self, Iface(handler))" << endl; | |
1829 | } else { | |
1830 | f_service_ << indent() << extends << ".Processor.__init__(self, handler)" << endl; | |
1831 | } | |
1832 | } | |
1833 | for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) { | |
1834 | f_service_ << indent() << "self._processMap[\"" << (*f_iter)->get_name() | |
1835 | << "\"] = Processor.process_" << (*f_iter)->get_name() << endl; | |
1836 | } | |
1837 | f_service_ << indent() << "self._on_message_begin = None" << endl; | |
1838 | indent_down(); | |
1839 | f_service_ << endl; | |
1840 | ||
1841 | f_service_ << indent() << "def on_message_begin(self, func):" << endl; | |
1842 | indent_up(); | |
1843 | f_service_ << indent() << "self._on_message_begin = func" << endl; | |
1844 | indent_down(); | |
1845 | f_service_ << endl; | |
1846 | ||
1847 | // Generate the server implementation | |
1848 | f_service_ << indent() << "def process(self, iprot, oprot):" << endl; | |
1849 | indent_up(); | |
1850 | ||
1851 | f_service_ << indent() << "(name, type, seqid) = iprot.readMessageBegin()" << endl; | |
1852 | f_service_ << indent() << "if self._on_message_begin:" << endl; | |
1853 | indent_up(); | |
1854 | f_service_ << indent() << "self._on_message_begin(name, type, seqid)" << endl; | |
1855 | indent_down(); | |
1856 | ||
1857 | // TODO(mcslee): validate message | |
1858 | ||
1859 | // HOT: dictionary function lookup | |
1860 | f_service_ << indent() << "if name not in self._processMap:" << endl; | |
1861 | indent_up(); | |
1862 | f_service_ << indent() << "iprot.skip(TType.STRUCT)" << endl | |
1863 | << indent() << "iprot.readMessageEnd()" << endl | |
1864 | << indent() | |
1865 | << "x = TApplicationException(TApplicationException.UNKNOWN_METHOD, 'Unknown " | |
1866 | "function %s' % (name))" | |
1867 | << endl | |
1868 | << indent() << "oprot.writeMessageBegin(name, TMessageType.EXCEPTION, seqid)" << endl | |
1869 | << indent() << "x.write(oprot)" << endl | |
1870 | << indent() << "oprot.writeMessageEnd()" << endl | |
1871 | << indent() << "oprot.trans.flush()" << endl; | |
1872 | ||
1873 | if (gen_twisted_) { | |
1874 | f_service_ << indent() << "return defer.succeed(None)" << endl; | |
1875 | } else { | |
1876 | f_service_ << indent() << "return" << endl; | |
1877 | } | |
1878 | indent_down(); | |
1879 | ||
1880 | f_service_ << indent() << "else:" << endl; | |
1881 | ||
1882 | if (gen_twisted_ || gen_tornado_) { | |
1883 | f_service_ << indent() << indent_str() | |
1884 | << "return self._processMap[name](self, seqid, iprot, oprot)" << endl; | |
1885 | } else { | |
1886 | f_service_ << indent() << indent_str() << "self._processMap[name](self, seqid, iprot, oprot)" | |
1887 | << endl; | |
1888 | ||
1889 | // Read end of args field, the T_STOP, and the struct close | |
1890 | f_service_ << indent() << "return True" << endl; | |
1891 | } | |
1892 | ||
1893 | indent_down(); | |
1894 | ||
1895 | // Generate the process subfunctions | |
1896 | for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) { | |
1897 | f_service_ << endl; | |
1898 | generate_process_function(tservice, *f_iter); | |
1899 | } | |
1900 | ||
1901 | indent_down(); | |
1902 | } | |
1903 | ||
1904 | /** | |
1905 | * Generates a process function definition. | |
1906 | * | |
1907 | * @param tfunction The function to write a dispatcher for | |
1908 | */ | |
1909 | void t_py_generator::generate_process_function(t_service* tservice, t_function* tfunction) { | |
1910 | (void)tservice; | |
1911 | // Open function | |
1912 | if (gen_tornado_) { | |
1913 | f_service_ << indent() << "@gen.coroutine" << endl << indent() << "def process_" | |
1914 | << tfunction->get_name() << "(self, seqid, iprot, oprot):" << endl; | |
1915 | } else { | |
1916 | f_service_ << indent() << "def process_" << tfunction->get_name() | |
1917 | << "(self, seqid, iprot, oprot):" << endl; | |
1918 | } | |
1919 | ||
1920 | indent_up(); | |
1921 | ||
1922 | string argsname = tfunction->get_name() + "_args"; | |
1923 | string resultname = tfunction->get_name() + "_result"; | |
1924 | ||
1925 | f_service_ << indent() << "args = " << argsname << "()" << endl << indent() << "args.read(iprot)" | |
1926 | << endl << indent() << "iprot.readMessageEnd()" << endl; | |
1927 | ||
1928 | t_struct* xs = tfunction->get_xceptions(); | |
1929 | const std::vector<t_field*>& xceptions = xs->get_members(); | |
1930 | vector<t_field*>::const_iterator x_iter; | |
1931 | ||
1932 | // Declare result for non oneway function | |
1933 | if (!tfunction->is_oneway()) { | |
1934 | f_service_ << indent() << "result = " << resultname << "()" << endl; | |
1935 | } | |
1936 | ||
1937 | if (gen_twisted_) { | |
1938 | // Generate the function call | |
1939 | t_struct* arg_struct = tfunction->get_arglist(); | |
1940 | const std::vector<t_field*>& fields = arg_struct->get_members(); | |
1941 | vector<t_field*>::const_iterator f_iter; | |
1942 | ||
1943 | f_service_ << indent() << "d = defer.maybeDeferred(self._handler." << tfunction->get_name() | |
1944 | << ", "; | |
1945 | bool first = true; | |
1946 | for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) { | |
1947 | if (first) { | |
1948 | first = false; | |
1949 | } else { | |
1950 | f_service_ << ", "; | |
1951 | } | |
1952 | f_service_ << "args." << (*f_iter)->get_name(); | |
1953 | } | |
1954 | f_service_ << ")" << endl; | |
1955 | ||
1956 | if (tfunction->is_oneway()) { | |
1957 | f_service_ << indent() << "d.addErrback(self.handle_exception_" << tfunction->get_name() | |
1958 | << ", seqid)" << endl; | |
1959 | } else { | |
1960 | f_service_ << indent() << "d.addCallback(self.write_results_success_" << tfunction->get_name() | |
1961 | << ", result, seqid, oprot)" << endl | |
1962 | << indent() << "d.addErrback(self.write_results_exception_" | |
1963 | << tfunction->get_name() << ", result, seqid, oprot)" << endl; | |
1964 | } | |
1965 | f_service_ << indent() << "return d" << endl << endl; | |
1966 | ||
1967 | indent_down(); | |
1968 | ||
1969 | if (tfunction->is_oneway()) { | |
1970 | indent(f_service_) << "def handle_exception_" << tfunction->get_name() | |
1971 | << "(self, error, seqid):" << endl; | |
1972 | } else { | |
1973 | indent(f_service_) << "def write_results_success_" << tfunction->get_name() | |
1974 | << "(self, success, result, seqid, oprot):" << endl; | |
1975 | indent_up(); | |
1976 | if (!tfunction->get_returntype()->is_void()) { | |
1977 | f_service_ << indent() << "result.success = success" << endl; | |
1978 | } | |
1979 | f_service_ << indent() << "oprot.writeMessageBegin(\"" << tfunction->get_name() | |
1980 | << "\", TMessageType.REPLY, seqid)" << endl | |
1981 | << indent() << "result.write(oprot)" << endl | |
1982 | << indent() << "oprot.writeMessageEnd()" << endl | |
1983 | << indent() << "oprot.trans.flush()" << endl | |
1984 | << endl; | |
1985 | indent_down(); | |
1986 | ||
1987 | indent(f_service_) << "def write_results_exception_" << tfunction->get_name() | |
1988 | << "(self, error, result, seqid, oprot):" << endl; | |
1989 | } | |
1990 | indent_up(); | |
1991 | if (!tfunction->is_oneway()) { | |
1992 | f_service_ << indent() << "msg_type = TMessageType.REPLY" << endl; | |
1993 | } | |
1994 | f_service_ << indent() << "try:" << endl; | |
1995 | ||
1996 | // Kinda absurd | |
1997 | f_service_ << indent() << indent_str() << "error.raiseException()" << endl; | |
1998 | if (!tfunction->is_oneway()) { | |
1999 | for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) { | |
2000 | const string& xname = (*x_iter)->get_name(); | |
2001 | f_service_ << indent() << "except " << type_name((*x_iter)->get_type()) << " as " << xname | |
2002 | << ":" << endl; | |
2003 | indent_up(); | |
2004 | f_service_ << indent() << "result." << xname << " = " << xname << endl; | |
2005 | indent_down(); | |
2006 | } | |
2007 | } | |
2008 | f_service_ << indent() << "except TTransport.TTransportException:" << endl | |
2009 | << indent() << indent_str() << "raise" << endl; | |
2010 | if (!tfunction->is_oneway()) { | |
2011 | f_service_ << indent() << "except TApplicationException as ex:" << endl | |
2012 | << indent() << indent_str() | |
2013 | << "logging.exception('TApplication exception in handler')" << endl | |
2014 | << indent() << indent_str() << "msg_type = TMessageType.EXCEPTION" << endl | |
2015 | << indent() << indent_str() << "result = ex" << endl | |
2016 | << indent() << "except Exception:" << endl | |
2017 | << indent() << indent_str() | |
2018 | << "logging.exception('Unexpected exception in handler')" << endl | |
2019 | << indent() << indent_str() << "msg_type = TMessageType.EXCEPTION" << endl | |
2020 | << indent() << indent_str() | |
2021 | << "result = TApplicationException(TApplicationException.INTERNAL_ERROR, " | |
2022 | "'Internal error')" | |
2023 | << endl | |
2024 | << indent() << "oprot.writeMessageBegin(\"" << tfunction->get_name() | |
2025 | << "\", msg_type, seqid)" << endl | |
2026 | << indent() << "result.write(oprot)" << endl | |
2027 | << indent() << "oprot.writeMessageEnd()" << endl | |
2028 | << indent() << "oprot.trans.flush()" << endl; | |
2029 | } else { | |
2030 | f_service_ << indent() << "except Exception:" << endl | |
2031 | << indent() << indent_str() | |
2032 | << "logging.exception('Exception in oneway handler')" << endl; | |
2033 | } | |
2034 | indent_down(); | |
2035 | ||
2036 | } else if (gen_tornado_) { | |
2037 | // Generate the function call | |
2038 | t_struct* arg_struct = tfunction->get_arglist(); | |
2039 | const std::vector<t_field*>& fields = arg_struct->get_members(); | |
2040 | vector<t_field*>::const_iterator f_iter; | |
2041 | ||
2042 | if (!tfunction->is_oneway()) { | |
2043 | indent(f_service_) << "msg_type = TMessageType.REPLY" << endl; | |
2044 | } | |
2045 | f_service_ << indent() << "try:" << endl; | |
2046 | indent_up(); | |
2047 | f_service_ << indent(); | |
2048 | if (!tfunction->is_oneway() && !tfunction->get_returntype()->is_void()) { | |
2049 | f_service_ << "result.success = "; | |
2050 | } | |
2051 | f_service_ << "yield gen.maybe_future(self._handler." << tfunction->get_name() << "("; | |
2052 | bool first = true; | |
2053 | for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) { | |
2054 | if (first) { | |
2055 | first = false; | |
2056 | } else { | |
2057 | f_service_ << ", "; | |
2058 | } | |
2059 | f_service_ << "args." << (*f_iter)->get_name(); | |
2060 | } | |
2061 | f_service_ << "))" << endl; | |
2062 | ||
2063 | indent_down(); | |
2064 | if (!tfunction->is_oneway()) { | |
2065 | for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) { | |
2066 | const string& xname = (*x_iter)->get_name(); | |
2067 | f_service_ << indent() << "except " << type_name((*x_iter)->get_type()) << " as " << xname | |
2068 | << ":" << endl | |
2069 | << indent() << indent_str() << "result." << xname << " = " << xname << endl; | |
2070 | } | |
2071 | } | |
2072 | f_service_ << indent() << "except TTransport.TTransportException:" << endl | |
2073 | << indent() << indent_str() << "raise" << endl; | |
2074 | if (!tfunction->is_oneway()) { | |
2075 | f_service_ << indent() << "except TApplicationException as ex:" << endl | |
2076 | << indent() << indent_str() | |
2077 | << "logging.exception('TApplication exception in handler')" << endl | |
2078 | << indent() << indent_str() << "msg_type = TMessageType.EXCEPTION" << endl | |
2079 | << indent() << indent_str() << "result = ex" << endl | |
2080 | << indent() << "except Exception:" << endl | |
2081 | << indent() << indent_str() | |
2082 | << "logging.exception('Unexpected exception in handler')" << endl | |
2083 | << indent() << indent_str() << "msg_type = TMessageType.EXCEPTION" << endl | |
2084 | << indent() << indent_str() | |
2085 | << "result = TApplicationException(TApplicationException.INTERNAL_ERROR, " | |
2086 | "'Internal error')" | |
2087 | << endl; | |
2088 | } else { | |
2089 | f_service_ << indent() << "except Exception:" << endl | |
2090 | << indent() << indent_str() | |
2091 | << "logging.exception('Exception in oneway handler')" << endl; | |
2092 | } | |
2093 | ||
2094 | if (!tfunction->is_oneway()) { | |
2095 | f_service_ << indent() << "oprot.writeMessageBegin(\"" << tfunction->get_name() | |
2096 | << "\", msg_type, seqid)" << endl | |
2097 | << indent() << "result.write(oprot)" << endl | |
2098 | << indent() << "oprot.writeMessageEnd()" << endl | |
2099 | << indent() << "oprot.trans.flush()" << endl; | |
2100 | } | |
2101 | ||
2102 | // Close function | |
2103 | indent_down(); | |
2104 | ||
2105 | } else { // py | |
2106 | // Try block for a function with exceptions | |
2107 | // It also catches arbitrary exceptions raised by handler method to propagate them to the client | |
2108 | f_service_ << indent() << "try:" << endl; | |
2109 | indent_up(); | |
2110 | ||
2111 | // Generate the function call | |
2112 | t_struct* arg_struct = tfunction->get_arglist(); | |
2113 | const std::vector<t_field*>& fields = arg_struct->get_members(); | |
2114 | vector<t_field*>::const_iterator f_iter; | |
2115 | ||
2116 | f_service_ << indent(); | |
2117 | if (!tfunction->is_oneway() && !tfunction->get_returntype()->is_void()) { | |
2118 | f_service_ << "result.success = "; | |
2119 | } | |
2120 | f_service_ << "self._handler." << tfunction->get_name() << "("; | |
2121 | bool first = true; | |
2122 | for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) { | |
2123 | if (first) { | |
2124 | first = false; | |
2125 | } else { | |
2126 | f_service_ << ", "; | |
2127 | } | |
2128 | f_service_ << "args." << (*f_iter)->get_name(); | |
2129 | } | |
2130 | f_service_ << ")" << endl; | |
2131 | if (!tfunction->is_oneway()) { | |
2132 | f_service_ << indent() << "msg_type = TMessageType.REPLY" << endl; | |
2133 | } | |
2134 | ||
2135 | indent_down(); | |
2136 | f_service_ << indent() | |
2137 | << "except TTransport.TTransportException:" << endl | |
2138 | << indent() << indent_str() << "raise" << endl; | |
2139 | ||
2140 | if (!tfunction->is_oneway()) { | |
2141 | for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) { | |
2142 | const string& xname = (*x_iter)->get_name(); | |
2143 | f_service_ << indent() << "except " << type_name((*x_iter)->get_type()) << " as " << xname | |
2144 | << ":" << endl; | |
2145 | indent_up(); | |
2146 | f_service_ << indent() << "msg_type = TMessageType.REPLY" << endl; | |
2147 | f_service_ << indent() << "result." << xname << " = " << xname << endl; | |
2148 | indent_down(); | |
2149 | } | |
2150 | ||
2151 | f_service_ << indent() << "except TApplicationException as ex:" << endl | |
2152 | << indent() << indent_str() | |
2153 | << "logging.exception('TApplication exception in handler')" << endl | |
2154 | << indent() << indent_str() << "msg_type = TMessageType.EXCEPTION" << endl | |
2155 | << indent() << indent_str() << "result = ex" << endl | |
2156 | << indent() << "except Exception:" << endl | |
2157 | << indent() << indent_str() | |
2158 | << "logging.exception('Unexpected exception in handler')" << endl | |
2159 | << indent() << indent_str() << "msg_type = TMessageType.EXCEPTION" << endl | |
2160 | << indent() << indent_str() | |
2161 | << "result = TApplicationException(TApplicationException.INTERNAL_ERROR, " | |
2162 | "'Internal error')" | |
2163 | << endl | |
2164 | << indent() << "oprot.writeMessageBegin(\"" << tfunction->get_name() | |
2165 | << "\", msg_type, seqid)" << endl | |
2166 | << indent() << "result.write(oprot)" << endl | |
2167 | << indent() << "oprot.writeMessageEnd()" << endl | |
2168 | << indent() << "oprot.trans.flush()" << endl; | |
2169 | } else { | |
2170 | f_service_ << indent() << "except Exception:" << endl | |
2171 | << indent() << indent_str() << "logging.exception('Exception in oneway handler')" << endl; | |
2172 | } | |
2173 | ||
2174 | // Close function | |
2175 | indent_down(); | |
2176 | } | |
2177 | } | |
2178 | ||
2179 | /** | |
2180 | * Deserializes a field of any type. | |
2181 | */ | |
2182 | void t_py_generator::generate_deserialize_field(ostream& out, | |
2183 | t_field* tfield, | |
2184 | string prefix) { | |
2185 | t_type* type = get_true_type(tfield->get_type()); | |
2186 | ||
2187 | if (type->is_void()) { | |
2188 | throw "CANNOT GENERATE DESERIALIZE CODE FOR void TYPE: " + prefix + tfield->get_name(); | |
2189 | } | |
2190 | ||
2191 | string name = prefix + tfield->get_name(); | |
2192 | ||
2193 | if (type->is_struct() || type->is_xception()) { | |
2194 | generate_deserialize_struct(out, (t_struct*)type, name); | |
2195 | } else if (type->is_container()) { | |
2196 | generate_deserialize_container(out, type, name); | |
2197 | } else if (type->is_base_type() || type->is_enum()) { | |
2198 | indent(out) << name << " = iprot."; | |
2199 | ||
2200 | if (type->is_base_type()) { | |
2201 | t_base_type::t_base tbase = ((t_base_type*)type)->get_base(); | |
2202 | switch (tbase) { | |
2203 | case t_base_type::TYPE_VOID: | |
2204 | throw "compiler error: cannot serialize void field in a struct: " + name; | |
2205 | case t_base_type::TYPE_STRING: | |
2206 | if (type->is_binary()) { | |
2207 | out << "readBinary()"; | |
2208 | } else if(!gen_utf8strings_) { | |
2209 | out << "readString()"; | |
2210 | } else { | |
2211 | out << "readString().decode('utf-8') if sys.version_info[0] == 2 else iprot.readString()"; | |
2212 | } | |
2213 | break; | |
2214 | case t_base_type::TYPE_BOOL: | |
2215 | out << "readBool()"; | |
2216 | break; | |
2217 | case t_base_type::TYPE_I8: | |
2218 | out << "readByte()"; | |
2219 | break; | |
2220 | case t_base_type::TYPE_I16: | |
2221 | out << "readI16()"; | |
2222 | break; | |
2223 | case t_base_type::TYPE_I32: | |
2224 | out << "readI32()"; | |
2225 | break; | |
2226 | case t_base_type::TYPE_I64: | |
2227 | out << "readI64()"; | |
2228 | break; | |
2229 | case t_base_type::TYPE_DOUBLE: | |
2230 | out << "readDouble()"; | |
2231 | break; | |
2232 | default: | |
2233 | throw "compiler error: no Python name for base type " + t_base_type::t_base_name(tbase); | |
2234 | } | |
2235 | } else if (type->is_enum()) { | |
2236 | out << "readI32()"; | |
2237 | } | |
2238 | out << endl; | |
2239 | ||
2240 | } else { | |
2241 | printf("DO NOT KNOW HOW TO DESERIALIZE FIELD '%s' TYPE '%s'\n", | |
2242 | tfield->get_name().c_str(), | |
2243 | type->get_name().c_str()); | |
2244 | } | |
2245 | } | |
2246 | ||
2247 | /** | |
2248 | * Generates an unserializer for a struct, calling read() | |
2249 | */ | |
2250 | void t_py_generator::generate_deserialize_struct(ostream& out, t_struct* tstruct, string prefix) { | |
2251 | if (is_immutable(tstruct)) { | |
2252 | out << indent() << prefix << " = " << type_name(tstruct) << ".read(iprot)" << endl; | |
2253 | } else { | |
2254 | out << indent() << prefix << " = " << type_name(tstruct) << "()" << endl | |
2255 | << indent() << prefix << ".read(iprot)" << endl; | |
2256 | } | |
2257 | } | |
2258 | ||
2259 | /** | |
2260 | * Serialize a container by writing out the header followed by | |
2261 | * data and then a footer. | |
2262 | */ | |
2263 | void t_py_generator::generate_deserialize_container(ostream& out, t_type* ttype, string prefix) { | |
2264 | string size = tmp("_size"); | |
2265 | string ktype = tmp("_ktype"); | |
2266 | string vtype = tmp("_vtype"); | |
2267 | string etype = tmp("_etype"); | |
2268 | ||
2269 | t_field fsize(g_type_i32, size); | |
2270 | t_field fktype(g_type_i8, ktype); | |
2271 | t_field fvtype(g_type_i8, vtype); | |
2272 | t_field fetype(g_type_i8, etype); | |
2273 | ||
2274 | // Declare variables, read header | |
2275 | if (ttype->is_map()) { | |
2276 | out << indent() << prefix << " = {}" << endl << indent() << "(" << ktype << ", " << vtype | |
2277 | << ", " << size << ") = iprot.readMapBegin()" << endl; | |
2278 | } else if (ttype->is_set()) { | |
2279 | out << indent() << prefix << " = set()" << endl << indent() << "(" << etype << ", " << size | |
2280 | << ") = iprot.readSetBegin()" << endl; | |
2281 | } else if (ttype->is_list()) { | |
2282 | out << indent() << prefix << " = []" << endl << indent() << "(" << etype << ", " << size | |
2283 | << ") = iprot.readListBegin()" << endl; | |
2284 | } | |
2285 | ||
2286 | // For loop iterates over elements | |
2287 | string i = tmp("_i"); | |
2288 | indent(out) << | |
2289 | "for " << i << " in range(" << size << "):" << endl; | |
2290 | ||
2291 | indent_up(); | |
2292 | ||
2293 | if (ttype->is_map()) { | |
2294 | generate_deserialize_map_element(out, (t_map*)ttype, prefix); | |
2295 | } else if (ttype->is_set()) { | |
2296 | generate_deserialize_set_element(out, (t_set*)ttype, prefix); | |
2297 | } else if (ttype->is_list()) { | |
2298 | generate_deserialize_list_element(out, (t_list*)ttype, prefix); | |
2299 | } | |
2300 | ||
2301 | indent_down(); | |
2302 | ||
2303 | // Read container end | |
2304 | if (ttype->is_map()) { | |
2305 | indent(out) << "iprot.readMapEnd()" << endl; | |
2306 | if (is_immutable(ttype)) { | |
2307 | indent(out) << prefix << " = TFrozenDict(" << prefix << ")" << endl; | |
2308 | } | |
2309 | } else if (ttype->is_set()) { | |
2310 | indent(out) << "iprot.readSetEnd()" << endl; | |
2311 | if (is_immutable(ttype)) { | |
2312 | indent(out) << prefix << " = frozenset(" << prefix << ")" << endl; | |
2313 | } | |
2314 | } else if (ttype->is_list()) { | |
2315 | if (is_immutable(ttype)) { | |
2316 | indent(out) << prefix << " = tuple(" << prefix << ")" << endl; | |
2317 | } | |
2318 | indent(out) << "iprot.readListEnd()" << endl; | |
2319 | } | |
2320 | } | |
2321 | ||
2322 | /** | |
2323 | * Generates code to deserialize a map | |
2324 | */ | |
2325 | void t_py_generator::generate_deserialize_map_element(ostream& out, t_map* tmap, string prefix) { | |
2326 | string key = tmp("_key"); | |
2327 | string val = tmp("_val"); | |
2328 | t_field fkey(tmap->get_key_type(), key); | |
2329 | t_field fval(tmap->get_val_type(), val); | |
2330 | ||
2331 | generate_deserialize_field(out, &fkey); | |
2332 | generate_deserialize_field(out, &fval); | |
2333 | ||
2334 | indent(out) << prefix << "[" << key << "] = " << val << endl; | |
2335 | } | |
2336 | ||
2337 | /** | |
2338 | * Write a set element | |
2339 | */ | |
2340 | void t_py_generator::generate_deserialize_set_element(ostream& out, t_set* tset, string prefix) { | |
2341 | string elem = tmp("_elem"); | |
2342 | t_field felem(tset->get_elem_type(), elem); | |
2343 | ||
2344 | generate_deserialize_field(out, &felem); | |
2345 | ||
2346 | indent(out) << prefix << ".add(" << elem << ")" << endl; | |
2347 | } | |
2348 | ||
2349 | /** | |
2350 | * Write a list element | |
2351 | */ | |
2352 | void t_py_generator::generate_deserialize_list_element(ostream& out, | |
2353 | t_list* tlist, | |
2354 | string prefix) { | |
2355 | string elem = tmp("_elem"); | |
2356 | t_field felem(tlist->get_elem_type(), elem); | |
2357 | ||
2358 | generate_deserialize_field(out, &felem); | |
2359 | ||
2360 | indent(out) << prefix << ".append(" << elem << ")" << endl; | |
2361 | } | |
2362 | ||
2363 | /** | |
2364 | * Serializes a field of any type. | |
2365 | * | |
2366 | * @param tfield The field to serialize | |
2367 | * @param prefix Name to prepend to field name | |
2368 | */ | |
2369 | void t_py_generator::generate_serialize_field(ostream& out, t_field* tfield, string prefix) { | |
2370 | t_type* type = get_true_type(tfield->get_type()); | |
2371 | ||
2372 | // Do nothing for void types | |
2373 | if (type->is_void()) { | |
2374 | throw "CANNOT GENERATE SERIALIZE CODE FOR void TYPE: " + prefix + tfield->get_name(); | |
2375 | } | |
2376 | ||
2377 | if (type->is_struct() || type->is_xception()) { | |
2378 | generate_serialize_struct(out, (t_struct*)type, prefix + tfield->get_name()); | |
2379 | } else if (type->is_container()) { | |
2380 | generate_serialize_container(out, type, prefix + tfield->get_name()); | |
2381 | } else if (type->is_base_type() || type->is_enum()) { | |
2382 | ||
2383 | string name = prefix + tfield->get_name(); | |
2384 | ||
2385 | indent(out) << "oprot."; | |
2386 | ||
2387 | if (type->is_base_type()) { | |
2388 | t_base_type::t_base tbase = ((t_base_type*)type)->get_base(); | |
2389 | switch (tbase) { | |
2390 | case t_base_type::TYPE_VOID: | |
2391 | throw "compiler error: cannot serialize void field in a struct: " + name; | |
2392 | break; | |
2393 | case t_base_type::TYPE_STRING: | |
2394 | if (type->is_binary()) { | |
2395 | out << "writeBinary(" << name << ")"; | |
2396 | } else if (!gen_utf8strings_) { | |
2397 | out << "writeString(" << name << ")"; | |
2398 | } else { | |
2399 | out << "writeString(" << name << ".encode('utf-8') if sys.version_info[0] == 2 else " << name << ")"; | |
2400 | } | |
2401 | break; | |
2402 | case t_base_type::TYPE_BOOL: | |
2403 | out << "writeBool(" << name << ")"; | |
2404 | break; | |
2405 | case t_base_type::TYPE_I8: | |
2406 | out << "writeByte(" << name << ")"; | |
2407 | break; | |
2408 | case t_base_type::TYPE_I16: | |
2409 | out << "writeI16(" << name << ")"; | |
2410 | break; | |
2411 | case t_base_type::TYPE_I32: | |
2412 | out << "writeI32(" << name << ")"; | |
2413 | break; | |
2414 | case t_base_type::TYPE_I64: | |
2415 | out << "writeI64(" << name << ")"; | |
2416 | break; | |
2417 | case t_base_type::TYPE_DOUBLE: | |
2418 | out << "writeDouble(" << name << ")"; | |
2419 | break; | |
2420 | default: | |
2421 | throw "compiler error: no Python name for base type " + t_base_type::t_base_name(tbase); | |
2422 | } | |
2423 | } else if (type->is_enum()) { | |
2424 | out << "writeI32(" << name << ")"; | |
2425 | } | |
2426 | out << endl; | |
2427 | } else { | |
2428 | printf("DO NOT KNOW HOW TO SERIALIZE FIELD '%s%s' TYPE '%s'\n", | |
2429 | prefix.c_str(), | |
2430 | tfield->get_name().c_str(), | |
2431 | type->get_name().c_str()); | |
2432 | } | |
2433 | } | |
2434 | ||
2435 | /** | |
2436 | * Serializes all the members of a struct. | |
2437 | * | |
2438 | * @param tstruct The struct to serialize | |
2439 | * @param prefix String prefix to attach to all fields | |
2440 | */ | |
2441 | void t_py_generator::generate_serialize_struct(ostream& out, t_struct* tstruct, string prefix) { | |
2442 | (void)tstruct; | |
2443 | indent(out) << prefix << ".write(oprot)" << endl; | |
2444 | } | |
2445 | ||
2446 | void t_py_generator::generate_serialize_container(ostream& out, t_type* ttype, string prefix) { | |
2447 | if (ttype->is_map()) { | |
2448 | indent(out) << "oprot.writeMapBegin(" << type_to_enum(((t_map*)ttype)->get_key_type()) << ", " | |
2449 | << type_to_enum(((t_map*)ttype)->get_val_type()) << ", " | |
2450 | << "len(" << prefix << "))" << endl; | |
2451 | } else if (ttype->is_set()) { | |
2452 | indent(out) << "oprot.writeSetBegin(" << type_to_enum(((t_set*)ttype)->get_elem_type()) << ", " | |
2453 | << "len(" << prefix << "))" << endl; | |
2454 | } else if (ttype->is_list()) { | |
2455 | indent(out) << "oprot.writeListBegin(" << type_to_enum(((t_list*)ttype)->get_elem_type()) | |
2456 | << ", " | |
2457 | << "len(" << prefix << "))" << endl; | |
2458 | } | |
2459 | ||
2460 | if (ttype->is_map()) { | |
2461 | string kiter = tmp("kiter"); | |
2462 | string viter = tmp("viter"); | |
2463 | indent(out) << "for " << kiter << ", " << viter << " in " << prefix << ".items():" << endl; | |
2464 | indent_up(); | |
2465 | generate_serialize_map_element(out, (t_map*)ttype, kiter, viter); | |
2466 | indent_down(); | |
2467 | } else if (ttype->is_set()) { | |
2468 | string iter = tmp("iter"); | |
2469 | indent(out) << "for " << iter << " in " << prefix << ":" << endl; | |
2470 | indent_up(); | |
2471 | generate_serialize_set_element(out, (t_set*)ttype, iter); | |
2472 | indent_down(); | |
2473 | } else if (ttype->is_list()) { | |
2474 | string iter = tmp("iter"); | |
2475 | indent(out) << "for " << iter << " in " << prefix << ":" << endl; | |
2476 | indent_up(); | |
2477 | generate_serialize_list_element(out, (t_list*)ttype, iter); | |
2478 | indent_down(); | |
2479 | } | |
2480 | ||
2481 | if (ttype->is_map()) { | |
2482 | indent(out) << "oprot.writeMapEnd()" << endl; | |
2483 | } else if (ttype->is_set()) { | |
2484 | indent(out) << "oprot.writeSetEnd()" << endl; | |
2485 | } else if (ttype->is_list()) { | |
2486 | indent(out) << "oprot.writeListEnd()" << endl; | |
2487 | } | |
2488 | } | |
2489 | ||
2490 | /** | |
2491 | * Serializes the members of a map. | |
2492 | * | |
2493 | */ | |
2494 | void t_py_generator::generate_serialize_map_element(ostream& out, | |
2495 | t_map* tmap, | |
2496 | string kiter, | |
2497 | string viter) { | |
2498 | t_field kfield(tmap->get_key_type(), kiter); | |
2499 | generate_serialize_field(out, &kfield, ""); | |
2500 | ||
2501 | t_field vfield(tmap->get_val_type(), viter); | |
2502 | generate_serialize_field(out, &vfield, ""); | |
2503 | } | |
2504 | ||
2505 | /** | |
2506 | * Serializes the members of a set. | |
2507 | */ | |
2508 | void t_py_generator::generate_serialize_set_element(ostream& out, t_set* tset, string iter) { | |
2509 | t_field efield(tset->get_elem_type(), iter); | |
2510 | generate_serialize_field(out, &efield, ""); | |
2511 | } | |
2512 | ||
2513 | /** | |
2514 | * Serializes the members of a list. | |
2515 | */ | |
2516 | void t_py_generator::generate_serialize_list_element(ostream& out, t_list* tlist, string iter) { | |
2517 | t_field efield(tlist->get_elem_type(), iter); | |
2518 | generate_serialize_field(out, &efield, ""); | |
2519 | } | |
2520 | ||
2521 | /** | |
2522 | * Generates the docstring for a given struct. | |
2523 | */ | |
2524 | void t_py_generator::generate_python_docstring(ostream& out, t_struct* tstruct) { | |
2525 | generate_python_docstring(out, tstruct, tstruct, "Attributes"); | |
2526 | } | |
2527 | ||
2528 | /** | |
2529 | * Generates the docstring for a given function. | |
2530 | */ | |
2531 | void t_py_generator::generate_python_docstring(ostream& out, t_function* tfunction) { | |
2532 | generate_python_docstring(out, tfunction, tfunction->get_arglist(), "Parameters"); | |
2533 | } | |
2534 | ||
2535 | /** | |
2536 | * Generates the docstring for a struct or function. | |
2537 | */ | |
2538 | void t_py_generator::generate_python_docstring(ostream& out, | |
2539 | t_doc* tdoc, | |
2540 | t_struct* tstruct, | |
2541 | const char* subheader) { | |
2542 | bool has_doc = false; | |
2543 | stringstream ss; | |
2544 | if (tdoc->has_doc()) { | |
2545 | has_doc = true; | |
2546 | ss << tdoc->get_doc(); | |
2547 | } | |
2548 | ||
2549 | const vector<t_field*>& fields = tstruct->get_members(); | |
2550 | if (fields.size() > 0) { | |
2551 | if (has_doc) { | |
2552 | ss << endl; | |
2553 | } | |
2554 | has_doc = true; | |
2555 | ss << subheader << ":\n"; | |
2556 | vector<t_field*>::const_iterator p_iter; | |
2557 | for (p_iter = fields.begin(); p_iter != fields.end(); ++p_iter) { | |
2558 | t_field* p = *p_iter; | |
2559 | ss << " - " << p->get_name(); | |
2560 | if (p->has_doc()) { | |
2561 | ss << ": " << p->get_doc(); | |
2562 | } else { | |
2563 | ss << endl; | |
2564 | } | |
2565 | } | |
2566 | } | |
2567 | ||
2568 | if (has_doc) { | |
2569 | generate_docstring_comment(out, "\"\"\"\n", "", ss.str(), "\"\"\"\n"); | |
2570 | } | |
2571 | } | |
2572 | ||
2573 | /** | |
2574 | * Generates the docstring for a generic object. | |
2575 | */ | |
2576 | void t_py_generator::generate_python_docstring(ostream& out, t_doc* tdoc) { | |
2577 | if (tdoc->has_doc()) { | |
2578 | generate_docstring_comment(out, "\"\"\"\n", "", tdoc->get_doc(), "\"\"\"\n"); | |
2579 | } | |
2580 | } | |
2581 | ||
2582 | /** | |
2583 | * Declares an argument, which may include initialization as necessary. | |
2584 | * | |
2585 | * @param tfield The field | |
2586 | */ | |
2587 | string t_py_generator::declare_argument(t_field* tfield) { | |
2588 | std::ostringstream result; | |
2589 | result << tfield->get_name() << "="; | |
2590 | if (tfield->get_value() != NULL) { | |
2591 | result << render_field_default_value(tfield); | |
2592 | } else { | |
2593 | result << "None"; | |
2594 | } | |
2595 | return result.str(); | |
2596 | } | |
2597 | ||
2598 | /** | |
2599 | * Renders a field default value, returns None otherwise. | |
2600 | * | |
2601 | * @param tfield The field | |
2602 | */ | |
2603 | string t_py_generator::render_field_default_value(t_field* tfield) { | |
2604 | t_type* type = get_true_type(tfield->get_type()); | |
2605 | if (tfield->get_value() != NULL) { | |
2606 | return render_const_value(type, tfield->get_value()); | |
2607 | } else { | |
2608 | return "None"; | |
2609 | } | |
2610 | } | |
2611 | ||
2612 | /** | |
2613 | * Renders a function signature of the form 'type name(args)' | |
2614 | * | |
2615 | * @param tfunction Function definition | |
2616 | * @return String of rendered function definition | |
2617 | */ | |
2618 | string t_py_generator::function_signature(t_function* tfunction, bool interface) { | |
2619 | vector<string> pre; | |
2620 | vector<string> post; | |
2621 | string signature = tfunction->get_name() + "("; | |
2622 | ||
2623 | if (!(gen_zope_interface_ && interface)) { | |
2624 | pre.emplace_back("self"); | |
2625 | } | |
2626 | ||
2627 | signature += argument_list(tfunction->get_arglist(), &pre, &post) + ")"; | |
2628 | return signature; | |
2629 | } | |
2630 | ||
2631 | /** | |
2632 | * Renders a field list | |
2633 | */ | |
2634 | string t_py_generator::argument_list(t_struct* tstruct, vector<string>* pre, vector<string>* post) { | |
2635 | string result = ""; | |
2636 | ||
2637 | const vector<t_field*>& fields = tstruct->get_members(); | |
2638 | vector<t_field*>::const_iterator f_iter; | |
2639 | vector<string>::const_iterator s_iter; | |
2640 | bool first = true; | |
2641 | if (pre) { | |
2642 | for (s_iter = pre->begin(); s_iter != pre->end(); ++s_iter) { | |
2643 | if (first) { | |
2644 | first = false; | |
2645 | } else { | |
2646 | result += ", "; | |
2647 | } | |
2648 | result += *s_iter; | |
2649 | } | |
2650 | } | |
2651 | for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) { | |
2652 | if (first) { | |
2653 | first = false; | |
2654 | } else { | |
2655 | result += ", "; | |
2656 | } | |
2657 | result += (*f_iter)->get_name(); | |
2658 | } | |
2659 | if (post) { | |
2660 | for (s_iter = post->begin(); s_iter != post->end(); ++s_iter) { | |
2661 | if (first) { | |
2662 | first = false; | |
2663 | } else { | |
2664 | result += ", "; | |
2665 | } | |
2666 | result += *s_iter; | |
2667 | } | |
2668 | } | |
2669 | return result; | |
2670 | } | |
2671 | ||
2672 | string t_py_generator::type_name(t_type* ttype) { | |
2673 | while (ttype->is_typedef()) { | |
2674 | ttype = ((t_typedef*)ttype)->get_type(); | |
2675 | } | |
2676 | ||
2677 | t_program* program = ttype->get_program(); | |
2678 | if (ttype->is_service()) { | |
2679 | return get_real_py_module(program, gen_twisted_, package_prefix_) + "." + ttype->get_name(); | |
2680 | } | |
2681 | if (program != NULL && program != program_) { | |
2682 | return get_real_py_module(program, gen_twisted_, package_prefix_) + ".ttypes." + ttype->get_name(); | |
2683 | } | |
2684 | return ttype->get_name(); | |
2685 | } | |
2686 | ||
2687 | /** | |
2688 | * Converts the parse type to a Python tyoe | |
2689 | */ | |
2690 | string t_py_generator::type_to_enum(t_type* type) { | |
2691 | type = get_true_type(type); | |
2692 | ||
2693 | if (type->is_base_type()) { | |
2694 | t_base_type::t_base tbase = ((t_base_type*)type)->get_base(); | |
2695 | switch (tbase) { | |
2696 | case t_base_type::TYPE_VOID: | |
2697 | throw "NO T_VOID CONSTRUCT"; | |
2698 | case t_base_type::TYPE_STRING: | |
2699 | return "TType.STRING"; | |
2700 | case t_base_type::TYPE_BOOL: | |
2701 | return "TType.BOOL"; | |
2702 | case t_base_type::TYPE_I8: | |
2703 | return "TType.BYTE"; | |
2704 | case t_base_type::TYPE_I16: | |
2705 | return "TType.I16"; | |
2706 | case t_base_type::TYPE_I32: | |
2707 | return "TType.I32"; | |
2708 | case t_base_type::TYPE_I64: | |
2709 | return "TType.I64"; | |
2710 | case t_base_type::TYPE_DOUBLE: | |
2711 | return "TType.DOUBLE"; | |
2712 | } | |
2713 | } else if (type->is_enum()) { | |
2714 | return "TType.I32"; | |
2715 | } else if (type->is_struct() || type->is_xception()) { | |
2716 | return "TType.STRUCT"; | |
2717 | } else if (type->is_map()) { | |
2718 | return "TType.MAP"; | |
2719 | } else if (type->is_set()) { | |
2720 | return "TType.SET"; | |
2721 | } else if (type->is_list()) { | |
2722 | return "TType.LIST"; | |
2723 | } | |
2724 | ||
2725 | throw "INVALID TYPE IN type_to_enum: " + type->get_name(); | |
2726 | } | |
2727 | ||
2728 | /** See the comment inside generate_py_struct_definition for what this is. */ | |
2729 | string t_py_generator::type_to_spec_args(t_type* ttype) { | |
2730 | while (ttype->is_typedef()) { | |
2731 | ttype = ((t_typedef*)ttype)->get_type(); | |
2732 | } | |
2733 | ||
2734 | if (ttype->is_binary()) { | |
2735 | return "'BINARY'"; | |
2736 | } else if (gen_utf8strings_ && ttype->is_base_type() | |
2737 | && reinterpret_cast<t_base_type*>(ttype)->is_string()) { | |
2738 | return "'UTF8'"; | |
2739 | } else if (ttype->is_base_type() || ttype->is_enum()) { | |
2740 | return "None"; | |
2741 | } else if (ttype->is_struct() || ttype->is_xception()) { | |
2742 | return "[" + type_name(ttype) + ", None]"; | |
2743 | } else if (ttype->is_map()) { | |
2744 | return "(" + type_to_enum(((t_map*)ttype)->get_key_type()) + ", " | |
2745 | + type_to_spec_args(((t_map*)ttype)->get_key_type()) + ", " | |
2746 | + type_to_enum(((t_map*)ttype)->get_val_type()) + ", " | |
2747 | + type_to_spec_args(((t_map*)ttype)->get_val_type()) + ", " | |
2748 | + (is_immutable(ttype) ? "True" : "False") + ")"; | |
2749 | ||
2750 | } else if (ttype->is_set()) { | |
2751 | return "(" + type_to_enum(((t_set*)ttype)->get_elem_type()) + ", " | |
2752 | + type_to_spec_args(((t_set*)ttype)->get_elem_type()) + ", " | |
2753 | + (is_immutable(ttype) ? "True" : "False") + ")"; | |
2754 | ||
2755 | } else if (ttype->is_list()) { | |
2756 | return "(" + type_to_enum(((t_list*)ttype)->get_elem_type()) + ", " | |
2757 | + type_to_spec_args(((t_list*)ttype)->get_elem_type()) + ", " | |
2758 | + (is_immutable(ttype) ? "True" : "False") + ")"; | |
2759 | } | |
2760 | ||
2761 | throw "INVALID TYPE IN type_to_spec_args: " + ttype->get_name(); | |
2762 | } | |
2763 | ||
2764 | THRIFT_REGISTER_GENERATOR( | |
2765 | py, | |
2766 | "Python", | |
2767 | " zope.interface: Generate code for use with zope.interface.\n" | |
2768 | " twisted: Generate Twisted-friendly RPC services.\n" | |
2769 | " tornado: Generate code for use with Tornado.\n" | |
2770 | " no_utf8strings: Do not Encode/decode strings using utf8 in the generated code. Basically no effect for Python 3.\n" | |
2771 | " coding=CODING: Add file encoding declare in generated file.\n" | |
2772 | " slots: Generate code using slots for instance members.\n" | |
2773 | " dynamic: Generate dynamic code, less code generated but slower.\n" | |
2774 | " dynbase=CLS Derive generated classes from class CLS instead of TBase.\n" | |
2775 | " dynfrozen=CLS Derive generated immutable classes from class CLS instead of TFrozenBase.\n" | |
2776 | " dynexc=CLS Derive generated exceptions from CLS instead of TExceptionBase.\n" | |
2777 | " dynimport='from foo.bar import CLS'\n" | |
2778 | " Add an import line to generated code to find the dynbase class.\n" | |
2779 | " package_prefix='top.package.'\n" | |
2780 | " Package prefix for generated files.\n" | |
2781 | " old_style: Deprecated. Generate old-style classes.\n") |