Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
30 ashish 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 <iostream>
23
#include <vector>
24
 
25
#include <stdlib.h>
26
#include <sys/stat.h>
27
#include <sys/types.h>
28
#include <sstream>
29
#include <algorithm>
30
#include "t_generator.h"
31
#include "platform.h"
32
using namespace std;
33
 
34
 
35
/**
36
 * Python code generator.
37
 *
38
 */
39
class t_py_generator : public t_generator {
40
 public:
41
  t_py_generator(
42
      t_program* program,
43
      const std::map<std::string, std::string>& parsed_options,
44
      const std::string& option_string)
45
    : t_generator(program)
46
  {
47
    std::map<std::string, std::string>::const_iterator iter;
48
 
49
    iter = parsed_options.find("new_style");
50
    gen_newstyle_ = (iter != parsed_options.end());
51
 
52
    iter = parsed_options.find("twisted");
53
    gen_twisted_ = (iter != parsed_options.end());
54
 
55
    if (gen_twisted_){
56
      out_dir_base_ = "gen-py.twisted";
57
    } else {
58
      out_dir_base_ = "gen-py";
59
    }
60
  }
61
 
62
  /**
63
   * Init and close methods
64
   */
65
 
66
  void init_generator();
67
  void close_generator();
68
 
69
  /**
70
   * Program-level generation functions
71
   */
72
 
73
  void generate_typedef  (t_typedef*  ttypedef);
74
  void generate_enum     (t_enum*     tenum);
75
  void generate_const    (t_const*    tconst);
76
  void generate_struct   (t_struct*   tstruct);
77
  void generate_xception (t_struct*   txception);
78
  void generate_service  (t_service*  tservice);
79
 
80
  std::string render_const_value(t_type* type, t_const_value* value);
81
 
82
  /**
83
   * Struct generation code
84
   */
85
 
86
  void generate_py_struct(t_struct* tstruct, bool is_exception);
87
  void generate_py_struct_definition(std::ofstream& out, t_struct* tstruct, bool is_xception=false, bool is_result=false);
88
  void generate_py_struct_reader(std::ofstream& out, t_struct* tstruct);
89
  void generate_py_struct_writer(std::ofstream& out, t_struct* tstruct);
90
  void generate_py_function_helpers(t_function* tfunction);
91
 
92
  /**
93
   * Service-level generation functions
94
   */
95
 
96
  void generate_service_helpers   (t_service*  tservice);
97
  void generate_service_interface (t_service* tservice);
98
  void generate_service_client    (t_service* tservice);
99
  void generate_service_remote    (t_service* tservice);
100
  void generate_service_server    (t_service* tservice);
101
  void generate_process_function  (t_service* tservice, t_function* tfunction);
102
 
103
  /**
104
   * Serialization constructs
105
   */
106
 
107
  void generate_deserialize_field        (std::ofstream &out,
108
                                          t_field*    tfield,
109
                                          std::string prefix="",
110
                                          bool inclass=false);
111
 
112
  void generate_deserialize_struct       (std::ofstream &out,
113
                                          t_struct*   tstruct,
114
                                          std::string prefix="");
115
 
116
  void generate_deserialize_container    (std::ofstream &out,
117
                                          t_type*     ttype,
118
                                          std::string prefix="");
119
 
120
  void generate_deserialize_set_element  (std::ofstream &out,
121
                                          t_set*      tset,
122
                                          std::string prefix="");
123
 
124
  void generate_deserialize_map_element  (std::ofstream &out,
125
                                          t_map*      tmap,
126
                                          std::string prefix="");
127
 
128
  void generate_deserialize_list_element (std::ofstream &out,
129
                                          t_list*     tlist,
130
                                          std::string prefix="");
131
 
132
  void generate_serialize_field          (std::ofstream &out,
133
                                          t_field*    tfield,
134
                                          std::string prefix="");
135
 
136
  void generate_serialize_struct         (std::ofstream &out,
137
                                          t_struct*   tstruct,
138
                                          std::string prefix="");
139
 
140
  void generate_serialize_container      (std::ofstream &out,
141
                                          t_type*     ttype,
142
                                          std::string prefix="");
143
 
144
  void generate_serialize_map_element    (std::ofstream &out,
145
                                          t_map*      tmap,
146
                                          std::string kiter,
147
                                          std::string viter);
148
 
149
  void generate_serialize_set_element    (std::ofstream &out,
150
                                          t_set*      tmap,
151
                                          std::string iter);
152
 
153
  void generate_serialize_list_element   (std::ofstream &out,
154
                                          t_list*     tlist,
155
                                          std::string iter);
156
 
157
  void generate_python_docstring         (std::ofstream& out,
158
                                          t_struct* tstruct);
159
 
160
  void generate_python_docstring         (std::ofstream& out,
161
                                          t_function* tfunction);
162
 
163
  void generate_python_docstring         (std::ofstream& out,
164
                                          t_doc*    tdoc,
165
                                          t_struct* tstruct,
166
                                          const char* subheader);
167
 
168
  void generate_python_docstring         (std::ofstream& out,
169
                                          t_doc* tdoc);
170
 
171
  /**
172
   * Helper rendering functions
173
   */
174
 
175
  std::string py_autogen_comment();
176
  std::string py_imports();
177
  std::string render_includes();
178
  std::string render_fastbinary_includes();
179
  std::string declare_argument(t_field* tfield);
180
  std::string render_field_default_value(t_field* tfield);
181
  std::string type_name(t_type* ttype);
182
  std::string function_signature(t_function* tfunction, std::string prefix="");
183
  std::string function_signature_if(t_function* tfunction, std::string prefix="");
184
  std::string argument_list(t_struct* tstruct);
185
  std::string type_to_enum(t_type* ttype);
186
  std::string type_to_spec_args(t_type* ttype);
187
 
188
  static std::string get_real_py_module(const t_program* program) {
189
    std::string real_module = program->get_namespace("py");
190
    if (real_module.empty()) {
191
      return program->get_name();
192
    }
193
    return real_module;
194
  }
195
 
196
 private:
197
 
198
  /**
199
   * True iff we should generate new-style classes.
200
   */
201
  bool gen_newstyle_;
202
 
203
  /**
204
   * True iff we should generate Twisted-friendly RPC services.
205
   */
206
  bool gen_twisted_;
207
 
208
  /**
209
   * File streams
210
   */
211
 
212
  std::ofstream f_types_;
213
  std::ofstream f_consts_;
214
  std::ofstream f_service_;
215
 
216
  std::string package_dir_;
217
 
218
};
219
 
220
 
221
/**
222
 * Prepares for file generation by opening up the necessary file output
223
 * streams.
224
 *
225
 * @param tprogram The program to generate
226
 */
227
void t_py_generator::init_generator() {
228
  // Make output directory
229
  string module = get_real_py_module(program_);
230
  package_dir_ = get_out_dir();
231
  while (true) {
232
    // TODO: Do better error checking here.
233
    MKDIR(package_dir_.c_str());
234
    std::ofstream init_py((package_dir_+"/__init__.py").c_str());
235
    init_py.close();
236
    if (module.empty()) {
237
      break;
238
    }
239
    string::size_type pos = module.find('.');
240
    if (pos == string::npos) {
241
      package_dir_ += "/";
242
      package_dir_ += module;
243
      module.clear();
244
    } else {
245
      package_dir_ += "/";
246
      package_dir_ += module.substr(0, pos);
247
      module.erase(0, pos+1);
248
    }
249
  }
250
 
251
  // Make output file
252
  string f_types_name = package_dir_+"/"+"ttypes.py";
253
  f_types_.open(f_types_name.c_str());
254
 
255
  string f_consts_name = package_dir_+"/"+"constants.py";
256
  f_consts_.open(f_consts_name.c_str());
257
 
258
  string f_init_name = package_dir_+"/__init__.py";
259
  ofstream f_init;
260
  f_init.open(f_init_name.c_str());
261
  f_init  <<
262
    "__all__ = ['ttypes', 'constants'";
263
  vector<t_service*> services = program_->get_services();
264
  vector<t_service*>::iterator sv_iter;
265
  for (sv_iter = services.begin(); sv_iter != services.end(); ++sv_iter) {
266
    f_init << ", '" << (*sv_iter)->get_name() << "'";
267
  }
268
  f_init << "]" << endl;
269
  f_init.close();
270
 
271
  // Print header
272
  f_types_ <<
273
    py_autogen_comment() << endl <<
274
    py_imports() << endl <<
275
    render_includes() << endl <<
276
    render_fastbinary_includes() <<
277
    endl << endl;
278
 
279
  f_consts_ <<
280
    py_autogen_comment() << endl <<
281
    py_imports() << endl <<
282
    "from ttypes import *" << endl <<
283
    endl;
284
}
285
 
286
/**
287
 * Renders all the imports necessary for including another Thrift program
288
 */
289
string t_py_generator::render_includes() {
290
  const vector<t_program*>& includes = program_->get_includes();
291
  string result = "";
292
  for (size_t i = 0; i < includes.size(); ++i) {
293
    result += "import " + get_real_py_module(includes[i]) + ".ttypes\n";
294
  }
295
  if (includes.size() > 0) {
296
    result += "\n";
297
  }
298
  return result;
299
}
300
 
301
/**
302
 * Renders all the imports necessary to use the accelerated TBinaryProtocol
303
 */
304
string t_py_generator::render_fastbinary_includes() {
305
  return
306
    "from thrift.transport import TTransport\n"
307
    "from thrift.protocol import TBinaryProtocol\n"
308
    "try:\n"
309
    "  from thrift.protocol import fastbinary\n"
310
    "except:\n"
311
    "  fastbinary = None\n";
312
}
313
 
314
/**
315
 * Autogen'd comment
316
 */
317
string t_py_generator::py_autogen_comment() {
318
  return
319
    std::string("#\n") +
320
    "# Autogenerated by Thrift\n" +
321
    "#\n" +
322
    "# DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING\n" +
323
    "#\n";
324
}
325
 
326
/**
327
 * Prints standard thrift imports
328
 */
329
string t_py_generator::py_imports() {
330
  return
331
    string("from thrift.Thrift import *");
332
}
333
 
334
/**
335
 * Closes the type files
336
 */
337
void t_py_generator::close_generator() {
338
  // Close types file
339
  f_types_.close();
340
  f_consts_.close();
341
}
342
 
343
/**
344
 * Generates a typedef. This is not done in Python, types are all implicit.
345
 *
346
 * @param ttypedef The type definition
347
 */
348
void t_py_generator::generate_typedef(t_typedef* ttypedef) {}
349
 
350
/**
351
 * Generates code for an enumerated type. Done using a class to scope
352
 * the values.
353
 *
354
 * @param tenum The enumeration
355
 */
356
void t_py_generator::generate_enum(t_enum* tenum) {
357
  std::ostringstream to_string_mapping, from_string_mapping;
358
 
359
  f_types_ <<
360
    "class " << tenum->get_name() <<
361
    (gen_newstyle_ ? "(object)" : "") <<
362
    ":" << endl;
363
  indent_up();
364
  generate_python_docstring(f_types_, tenum);
365
 
366
  to_string_mapping << indent() << "_VALUES_TO_NAMES = {" << endl;
367
  from_string_mapping << indent() << "_NAMES_TO_VALUES = {" << endl;
368
 
369
  vector<t_enum_value*> constants = tenum->get_constants();
370
  vector<t_enum_value*>::iterator c_iter;
371
  int value = -1;
372
  for (c_iter = constants.begin(); c_iter != constants.end(); ++c_iter) {
373
    if ((*c_iter)->has_value()) {
374
      value = (*c_iter)->get_value();
375
    } else {
376
      ++value;
377
    }
378
 
379
    f_types_ <<
380
      indent() << (*c_iter)->get_name() << " = " << value << endl;
381
 
382
    // Dictionaries to/from string names of enums
383
    to_string_mapping <<
384
      indent() << indent() << value << ": \"" <<
385
      escape_string((*c_iter)->get_name()) << "\"," << endl;
386
    from_string_mapping <<
387
      indent() << indent() << '"' << escape_string((*c_iter)->get_name()) <<
388
      "\": " << value << ',' << endl;
389
  }
390
  to_string_mapping << indent() << "}" << endl;
391
  from_string_mapping << indent() << "}" << endl;
392
 
393
  indent_down();
394
  f_types_ << endl;
395
  f_types_ << to_string_mapping.str() << endl << from_string_mapping.str() << endl;
396
}
397
 
398
/**
399
 * Generate a constant value
400
 */
401
void t_py_generator::generate_const(t_const* tconst) {
402
  t_type* type = tconst->get_type();
403
  string name = tconst->get_name();
404
  t_const_value* value = tconst->get_value();
405
 
406
  indent(f_consts_) << name << " = " << render_const_value(type, value);
407
  f_consts_ << endl << endl;
408
}
409
 
410
/**
411
 * Prints the value of a constant with the given type. Note that type checking
412
 * is NOT performed in this function as it is always run beforehand using the
413
 * validate_types method in main.cc
414
 */
415
string t_py_generator::render_const_value(t_type* type, t_const_value* value) {
416
  type = get_true_type(type);
417
  std::ostringstream out;
418
 
419
  if (type->is_base_type()) {
420
    t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
421
    switch (tbase) {
422
    case t_base_type::TYPE_STRING:
423
      out << '"' << get_escaped_string(value) << '"';
424
      break;
425
    case t_base_type::TYPE_BOOL:
426
      out << (value->get_integer() > 0 ? "True" : "False");
427
      break;
428
    case t_base_type::TYPE_BYTE:
429
    case t_base_type::TYPE_I16:
430
    case t_base_type::TYPE_I32:
431
    case t_base_type::TYPE_I64:
432
      out << value->get_integer();
433
      break;
434
    case t_base_type::TYPE_DOUBLE:
435
      if (value->get_type() == t_const_value::CV_INTEGER) {
436
        out << value->get_integer();
437
      } else {
438
        out << value->get_double();
439
      }
440
      break;
441
    default:
442
      throw "compiler error: no const of base type " + t_base_type::t_base_name(tbase);
443
    }
444
  } else if (type->is_enum()) {
445
    indent(out) << value->get_integer();
446
  } else if (type->is_struct() || type->is_xception()) {
447
    out << type->get_name() << "(**{" << endl;
448
    indent_up();
449
    const vector<t_field*>& fields = ((t_struct*)type)->get_members();
450
    vector<t_field*>::const_iterator f_iter;
451
    const map<t_const_value*, t_const_value*>& val = value->get_map();
452
    map<t_const_value*, t_const_value*>::const_iterator v_iter;
453
    for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
454
      t_type* field_type = NULL;
455
      for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
456
        if ((*f_iter)->get_name() == v_iter->first->get_string()) {
457
          field_type = (*f_iter)->get_type();
458
        }
459
      }
460
      if (field_type == NULL) {
461
        throw "type error: " + type->get_name() + " has no field " + v_iter->first->get_string();
462
      }
463
      out << indent();
464
      out << render_const_value(g_type_string, v_iter->first);
465
      out << " : ";
466
      out << render_const_value(field_type, v_iter->second);
467
      out << "," << endl;
468
    }
469
    indent_down();
470
    indent(out) << "})";
471
  } else if (type->is_map()) {
472
    t_type* ktype = ((t_map*)type)->get_key_type();
473
    t_type* vtype = ((t_map*)type)->get_val_type();
474
    out << "{" << endl;
475
    indent_up();
476
    const map<t_const_value*, t_const_value*>& val = value->get_map();
477
    map<t_const_value*, t_const_value*>::const_iterator v_iter;
478
    for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
479
      out << indent();
480
      out << render_const_value(ktype, v_iter->first);
481
      out << " : ";
482
      out << render_const_value(vtype, v_iter->second);
483
      out << "," << endl;
484
    }
485
    indent_down();
486
    indent(out) << "}";
487
  } else if (type->is_list() || type->is_set()) {
488
    t_type* etype;
489
    if (type->is_list()) {
490
      etype = ((t_list*)type)->get_elem_type();
491
    } else {
492
      etype = ((t_set*)type)->get_elem_type();
493
    }
494
    if (type->is_set()) {
495
      out << "set(";
496
    }
497
    out << "[" << endl;
498
    indent_up();
499
    const vector<t_const_value*>& val = value->get_list();
500
    vector<t_const_value*>::const_iterator v_iter;
501
    for (v_iter = val.begin(); v_iter != val.end(); ++v_iter) {
502
      out << indent();
503
      out << render_const_value(etype, *v_iter);
504
      out << "," << endl;
505
    }
506
    indent_down();
507
    indent(out) << "]";
508
    if (type->is_set()) {
509
      out << ")";
510
    }
511
  } else {
512
    throw "CANNOT GENERATE CONSTANT FOR TYPE: " + type->get_name();
513
  }
514
 
515
  return out.str();
516
}
517
 
518
/**
519
 * Generates a python struct
520
 */
521
void t_py_generator::generate_struct(t_struct* tstruct) {
522
  generate_py_struct(tstruct, false);
523
}
524
 
525
/**
526
 * Generates a struct definition for a thrift exception. Basically the same
527
 * as a struct but extends the Exception class.
528
 *
529
 * @param txception The struct definition
530
 */
531
void t_py_generator::generate_xception(t_struct* txception) {
532
  generate_py_struct(txception, true);
533
}
534
 
535
/**
536
 * Generates a python struct
537
 */
538
void t_py_generator::generate_py_struct(t_struct* tstruct,
539
                                        bool is_exception) {
540
  generate_py_struct_definition(f_types_, tstruct, is_exception);
541
}
542
 
543
/**
544
 * Generates a struct definition for a thrift data type.
545
 *
546
 * @param tstruct The struct definition
547
 */
548
void t_py_generator::generate_py_struct_definition(ofstream& out,
549
                                                   t_struct* tstruct,
550
                                                   bool is_exception,
551
                                                   bool is_result) {
552
 
553
  const vector<t_field*>& members = tstruct->get_members();
554
  const vector<t_field*>& sorted_members = tstruct->get_sorted_members();
555
  vector<t_field*>::const_iterator m_iter;
556
 
557
  out <<
558
    "class " << tstruct->get_name();
559
  if (is_exception) {
560
    out << "(Exception)";
561
  } else if (gen_newstyle_) {
562
    out << "(object)";
563
  }
564
  out <<
565
    ":" << endl;
566
  indent_up();
567
  generate_python_docstring(out, tstruct);
568
 
569
  out << endl;
570
 
571
  /*
572
     Here we generate the structure specification for the fastbinary codec.
573
     These specifications have the following structure:
574
     thrift_spec -> tuple of item_spec
575
     item_spec -> None | (tag, type_enum, name, spec_args, default)
576
     tag -> integer
577
     type_enum -> TType.I32 | TType.STRING | TType.STRUCT | ...
578
     name -> string_literal
579
     default -> None  # Handled by __init__
580
     spec_args -> None  # For simple types
581
                | (type_enum, spec_args)  # Value type for list/set
582
                | (type_enum, spec_args, type_enum, spec_args)
583
                  # Key and value for map
584
                | (class_name, spec_args_ptr) # For struct/exception
585
     class_name -> identifier  # Basically a pointer to the class
586
     spec_args_ptr -> expression  # just class_name.spec_args
587
 
588
     TODO(dreiss): Consider making this work for structs with negative tags.
589
  */
590
 
591
  // TODO(dreiss): Look into generating an empty tuple instead of None
592
  // for structures with no members.
593
  // TODO(dreiss): Test encoding of structs where some inner structs
594
  // don't have thrift_spec.
595
  if (sorted_members.empty() || (sorted_members[0]->get_key() >= 0)) {
596
    indent(out) << "thrift_spec = (" << endl;
597
    indent_up();
598
 
599
    int sorted_keys_pos = 0;
600
    for (m_iter = sorted_members.begin(); m_iter != sorted_members.end(); ++m_iter) {
601
 
602
      for (; sorted_keys_pos != (*m_iter)->get_key(); sorted_keys_pos++) {
603
        indent(out) << "None, # " << sorted_keys_pos << endl;
604
      }
605
 
606
      indent(out) << "(" << (*m_iter)->get_key() << ", "
607
            << type_to_enum((*m_iter)->get_type()) << ", "
608
            << "'" << (*m_iter)->get_name() << "'" << ", "
609
            << type_to_spec_args((*m_iter)->get_type()) << ", "
610
            << render_field_default_value(*m_iter) << ", "
611
            << "),"
612
            << " # " << sorted_keys_pos
613
            << endl;
614
 
615
      sorted_keys_pos ++;
616
    }
617
 
618
    indent_down();
619
    indent(out) << ")" << endl << endl;
620
  } else {
621
    indent(out) << "thrift_spec = None" << endl;
622
  }
623
 
624
 
625
  if (members.size() > 0) {
626
    out <<
627
      indent() << "def __init__(self,";
628
 
629
    for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
630
      // This fills in default values, as opposed to nulls
631
      out << " " << declare_argument(*m_iter) << ",";
632
    }
633
 
634
    out << "):" << endl;
635
 
636
    indent_up();
637
 
638
    for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) {
639
      // Initialize fields
640
      t_type* type = (*m_iter)->get_type();
641
      if (!type->is_base_type() && !type->is_enum() && (*m_iter)->get_value() != NULL) {
642
        indent(out) <<
643
          "if " << (*m_iter)->get_name() << " is " << "self.thrift_spec[" <<
644
            (*m_iter)->get_key() << "][4]:" << endl;
645
        indent(out) << "  " << (*m_iter)->get_name() << " = " <<
646
          render_field_default_value(*m_iter) << endl;
647
      }
648
      indent(out) <<
649
        "self." << (*m_iter)->get_name() << " = " << (*m_iter)->get_name() << endl;
650
    }
651
 
652
    indent_down();
653
 
654
    out << endl;
655
  }
656
 
657
  generate_py_struct_reader(out, tstruct);
658
  generate_py_struct_writer(out, tstruct);
659
 
660
  // For exceptions only, generate a __str__ method. This is
661
  // because when raised exceptions are printed to the console, __repr__
662
  // isn't used. See python bug #5882
663
  if (is_exception) {
664
    out <<
665
      indent() << "def __str__(self):" << endl <<
666
      indent() << "  return repr(self)" << endl <<
667
      endl;
668
  }
669
 
670
  // Printing utilities so that on the command line thrift
671
  // structs look pretty like dictionaries
672
  out <<
673
    indent() << "def __repr__(self):" << endl <<
674
    indent() << "  L = ['%s=%r' % (key, value)" << endl <<
675
    indent() << "    for key, value in self.__dict__.iteritems()]" << endl <<
676
    indent() << "  return '%s(%s)' % (self.__class__.__name__, ', '.join(L))" << endl <<
677
    endl;
678
 
679
  // Equality and inequality methods that compare by value
680
  out <<
681
    indent() << "def __eq__(self, other):" << endl;
682
  indent_up();
683
  out <<
684
    indent() << "return isinstance(other, self.__class__) and "
685
                "self.__dict__ == other.__dict__" << endl;
686
  indent_down();
687
  out << endl;
688
 
689
  out <<
690
    indent() << "def __ne__(self, other):" << endl;
691
  indent_up();
692
  out <<
693
    indent() << "return not (self == other)" << endl;
694
  indent_down();
695
  out << endl;
696
 
697
  indent_down();
698
}
699
 
700
/**
701
 * Generates the read method for a struct
702
 */
703
void t_py_generator::generate_py_struct_reader(ofstream& out,
704
                                                t_struct* tstruct) {
705
  const vector<t_field*>& fields = tstruct->get_members();
706
  vector<t_field*>::const_iterator f_iter;
707
 
708
  indent(out) <<
709
    "def read(self, iprot):" << endl;
710
  indent_up();
711
 
712
  indent(out) <<
713
    "if iprot.__class__ == TBinaryProtocol.TBinaryProtocolAccelerated "
714
    "and isinstance(iprot.trans, TTransport.CReadableTransport) "
715
    "and self.thrift_spec is not None "
716
    "and fastbinary is not None:" << endl;
717
  indent_up();
718
 
719
  indent(out) <<
720
    "fastbinary.decode_binary(self, iprot.trans, (self.__class__, self.thrift_spec))" << endl;
721
  indent(out) <<
722
    "return" << endl;
723
  indent_down();
724
 
725
  indent(out) <<
726
    "iprot.readStructBegin()" << endl;
727
 
728
  // Loop over reading in fields
729
  indent(out) <<
730
    "while True:" << endl;
731
    indent_up();
732
 
733
    // Read beginning field marker
734
    indent(out) <<
735
      "(fname, ftype, fid) = iprot.readFieldBegin()" << endl;
736
 
737
    // Check for field STOP marker and break
738
    indent(out) <<
739
      "if ftype == TType.STOP:" << endl;
740
    indent_up();
741
    indent(out) <<
742
      "break" << endl;
743
    indent_down();
744
 
745
    // Switch statement on the field we are reading
746
    bool first = true;
747
 
748
    // Generate deserialization code for known cases
749
    for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
750
      if (first) {
751
        first = false;
752
        out <<
753
          indent() << "if ";
754
      } else {
755
        out <<
756
          indent() << "elif ";
757
      }
758
      out << "fid == " << (*f_iter)->get_key() << ":" << endl;
759
      indent_up();
760
      indent(out) << "if ftype == " << type_to_enum((*f_iter)->get_type()) << ":" << endl;
761
      indent_up();
762
      generate_deserialize_field(out, *f_iter, "self.");
763
      indent_down();
764
      out <<
765
        indent() << "else:" << endl <<
766
        indent() << "  iprot.skip(ftype)" << endl;
767
      indent_down();
768
    }
769
 
770
    // In the default case we skip the field
771
    out <<
772
      indent() <<  "else:" << endl <<
773
      indent() <<  "  iprot.skip(ftype)" << endl;
774
 
775
    // Read field end marker
776
    indent(out) <<
777
      "iprot.readFieldEnd()" << endl;
778
 
779
    indent_down();
780
 
781
    indent(out) <<
782
      "iprot.readStructEnd()" << endl;
783
 
784
    indent_down();
785
  out << endl;
786
}
787
 
788
void t_py_generator::generate_py_struct_writer(ofstream& out,
789
                                               t_struct* tstruct) {
790
  string name = tstruct->get_name();
791
  const vector<t_field*>& fields = tstruct->get_sorted_members();
792
  vector<t_field*>::const_iterator f_iter;
793
 
794
  indent(out) <<
795
    "def write(self, oprot):" << endl;
796
  indent_up();
797
 
798
  indent(out) <<
799
    "if oprot.__class__ == TBinaryProtocol.TBinaryProtocolAccelerated "
800
    "and self.thrift_spec is not None "
801
    "and fastbinary is not None:" << endl;
802
  indent_up();
803
 
804
  indent(out) <<
805
    "oprot.trans.write(fastbinary.encode_binary(self, (self.__class__, self.thrift_spec)))" << endl;
806
  indent(out) <<
807
    "return" << endl;
808
  indent_down();
809
 
810
  indent(out) <<
811
    "oprot.writeStructBegin('" << name << "')" << endl;
812
 
813
  for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
814
    // Write field header
815
    indent(out) <<
816
      "if self." << (*f_iter)->get_name() << " != None:" << endl;
817
    indent_up();
818
    indent(out) <<
819
      "oprot.writeFieldBegin(" <<
820
      "'" << (*f_iter)->get_name() << "', " <<
821
      type_to_enum((*f_iter)->get_type()) << ", " <<
822
      (*f_iter)->get_key() << ")" << endl;
823
 
824
    // Write field contents
825
    generate_serialize_field(out, *f_iter, "self.");
826
 
827
    // Write field closer
828
    indent(out) <<
829
      "oprot.writeFieldEnd()" << endl;
830
 
831
    indent_down();
832
  }
833
 
834
  // Write the struct map
835
  out <<
836
    indent() << "oprot.writeFieldStop()" << endl <<
837
    indent() << "oprot.writeStructEnd()" << endl;
838
 
839
  indent_down();
840
  out <<
841
    endl;
842
}
843
 
844
/**
845
 * Generates a thrift service.
846
 *
847
 * @param tservice The service definition
848
 */
849
void t_py_generator::generate_service(t_service* tservice) {
850
  string f_service_name = package_dir_+"/"+service_name_+".py";
851
  f_service_.open(f_service_name.c_str());
852
 
853
  f_service_ <<
854
    py_autogen_comment() << endl <<
855
    py_imports() << endl;
856
 
857
  if (tservice->get_extends() != NULL) {
858
    f_service_ <<
859
      "import " << get_real_py_module(tservice->get_extends()->get_program()) <<
860
      "." << tservice->get_extends()->get_name() << endl;
861
  }
862
 
863
  f_service_ <<
864
    "from ttypes import *" << endl <<
865
    "from thrift.Thrift import TProcessor" << endl <<
866
    render_fastbinary_includes() << endl;
867
 
868
  if (gen_twisted_) {
869
    f_service_ <<
870
      "from zope.interface import Interface, implements" << endl <<
871
      "from twisted.internet import defer" << endl <<
872
      "from thrift.transport import TTwisted" << endl;
873
  }
874
 
875
  f_service_ << endl;
876
 
877
  // Generate the three main parts of the service (well, two for now in PHP)
878
  generate_service_interface(tservice);
879
  generate_service_client(tservice);
880
  generate_service_server(tservice);
881
  generate_service_helpers(tservice);
882
  generate_service_remote(tservice);
883
 
884
  // Close service file
885
  f_service_ << endl;
886
  f_service_.close();
887
}
888
 
889
/**
890
 * Generates helper functions for a service.
891
 *
892
 * @param tservice The service to generate a header definition for
893
 */
894
void t_py_generator::generate_service_helpers(t_service* tservice) {
895
  vector<t_function*> functions = tservice->get_functions();
896
  vector<t_function*>::iterator f_iter;
897
 
898
  f_service_ <<
899
    "# HELPER FUNCTIONS AND STRUCTURES" << endl << endl;
900
 
901
  for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
902
    t_struct* ts = (*f_iter)->get_arglist();
903
    generate_py_struct_definition(f_service_, ts, false);
904
    generate_py_function_helpers(*f_iter);
905
  }
906
}
907
 
908
/**
909
 * Generates a struct and helpers for a function.
910
 *
911
 * @param tfunction The function
912
 */
913
void t_py_generator::generate_py_function_helpers(t_function* tfunction) {
914
  if (!tfunction->is_oneway()) {
915
    t_struct result(program_, tfunction->get_name() + "_result");
916
    t_field success(tfunction->get_returntype(), "success", 0);
917
    if (!tfunction->get_returntype()->is_void()) {
918
      result.append(&success);
919
    }
920
 
921
    t_struct* xs = tfunction->get_xceptions();
922
    const vector<t_field*>& fields = xs->get_members();
923
    vector<t_field*>::const_iterator f_iter;
924
    for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
925
      result.append(*f_iter);
926
    }
927
    generate_py_struct_definition(f_service_, &result, false, true);
928
  }
929
}
930
 
931
/**
932
 * Generates a service interface definition.
933
 *
934
 * @param tservice The service to generate a header definition for
935
 */
936
void t_py_generator::generate_service_interface(t_service* tservice) {
937
  string extends = "";
938
  string extends_if = "";
939
  if (tservice->get_extends() != NULL) {
940
    extends = type_name(tservice->get_extends());
941
    extends_if = "(" + extends + ".Iface)";
942
  } else {
943
    if (gen_twisted_) {
944
      extends_if = "(Interface)";
945
    } else if (gen_newstyle_) {
946
      extends_if = "(object)";
947
    }
948
  }
949
 
950
  f_service_ <<
951
    "class Iface" << extends_if << ":" << endl;
952
  indent_up();
953
  generate_python_docstring(f_service_, tservice);
954
  vector<t_function*> functions = tservice->get_functions();
955
  if (functions.empty()) {
956
    f_service_ <<
957
      indent() << "pass" << endl;
958
  } else {
959
    vector<t_function*>::iterator f_iter;
960
    for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
961
      f_service_ <<
962
        indent() << "def " << function_signature_if(*f_iter) << ":" << endl;
963
      indent_up();
964
      generate_python_docstring(f_service_, (*f_iter));
965
      f_service_ <<
966
        indent() << "pass" << endl << endl;
967
      indent_down();
968
    }
969
  }
970
 
971
  indent_down();
972
  f_service_ <<
973
    endl;
974
}
975
 
976
/**
977
 * Generates a service client definition.
978
 *
979
 * @param tservice The service to generate a server for.
980
 */
981
void t_py_generator::generate_service_client(t_service* tservice) {
982
  string extends = "";
983
  string extends_client = "";
984
  if (tservice->get_extends() != NULL) {
985
    extends = type_name(tservice->get_extends());
986
    if (gen_twisted_) {
987
      extends_client = "(" + extends + ".Client)";
988
    } else {
989
      extends_client = extends + ".Client, ";
990
    }
991
  } else {
992
    if (gen_twisted_ && gen_newstyle_) {
993
        extends_client = "(object)";
994
    }
995
  }
996
 
997
  if (gen_twisted_) {
998
    f_service_ <<
999
      "class Client" << extends_client << ":" << endl <<
1000
      "  implements(Iface)" << endl << endl;
1001
  } else {
1002
    f_service_ <<
1003
      "class Client(" << extends_client << "Iface):" << endl;
1004
  }
1005
  indent_up();
1006
  generate_python_docstring(f_service_, tservice);
1007
 
1008
  // Constructor function
1009
  if (gen_twisted_) {
1010
    f_service_ <<
1011
      indent() << "def __init__(self, transport, oprot_factory):" << endl;
1012
  } else {
1013
    f_service_ <<
1014
      indent() << "def __init__(self, iprot, oprot=None):" << endl;
1015
  }
1016
  if (extends.empty()) {
1017
    if (gen_twisted_) {
1018
      f_service_ <<
1019
        indent() << "  self._transport = transport" << endl <<
1020
        indent() << "  self._oprot_factory = oprot_factory" << endl <<
1021
        indent() << "  self._seqid = 0" << endl <<
1022
        indent() << "  self._reqs = {}" << endl <<
1023
        endl;
1024
    } else {
1025
      f_service_ <<
1026
        indent() << "  self._iprot = self._oprot = iprot" << endl <<
1027
        indent() << "  if oprot != None:" << endl <<
1028
        indent() << "    self._oprot = oprot" << endl <<
1029
        indent() << "  self._seqid = 0" << endl <<
1030
        endl;
1031
    }
1032
  } else {
1033
    if (gen_twisted_) {
1034
      f_service_ <<
1035
        indent() << "  " << extends << ".Client.__init__(self, transport, oprot_factory)" << endl <<
1036
        endl;
1037
    } else {
1038
      f_service_ <<
1039
        indent() << "  " << extends << ".Client.__init__(self, iprot, oprot)" << endl <<
1040
        endl;
1041
    }
1042
  }
1043
 
1044
  // Generate client method implementations
1045
  vector<t_function*> functions = tservice->get_functions();
1046
  vector<t_function*>::const_iterator f_iter;
1047
  for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
1048
    t_struct* arg_struct = (*f_iter)->get_arglist();
1049
    const vector<t_field*>& fields = arg_struct->get_members();
1050
    vector<t_field*>::const_iterator fld_iter;
1051
    string funname = (*f_iter)->get_name();
1052
 
1053
    // Open function
1054
    indent(f_service_) <<
1055
      "def " << function_signature(*f_iter) << ":" << endl;
1056
    indent_up();
1057
    generate_python_docstring(f_service_, (*f_iter));
1058
    if (gen_twisted_) {
1059
      indent(f_service_) << "self._seqid += 1" << endl;
1060
      if (!(*f_iter)->is_oneway()) {
1061
        indent(f_service_) <<
1062
          "d = self._reqs[self._seqid] = defer.Deferred()" << endl;
1063
      }
1064
    }
1065
 
1066
    indent(f_service_) <<
1067
      "self.send_" << funname << "(";
1068
 
1069
    bool first = true;
1070
    for (fld_iter = fields.begin(); fld_iter != fields.end(); ++fld_iter) {
1071
      if (first) {
1072
        first = false;
1073
      } else {
1074
        f_service_ << ", ";
1075
      }
1076
      f_service_ << (*fld_iter)->get_name();
1077
    }
1078
    f_service_ << ")" << endl;
1079
 
1080
    if (!(*f_iter)->is_oneway()) {
1081
      f_service_ << indent();
1082
      if (gen_twisted_) {
1083
        f_service_ << "return d" << endl;
1084
      } else {
1085
        if (!(*f_iter)->get_returntype()->is_void()) {
1086
          f_service_ << "return ";
1087
        }
1088
        f_service_ <<
1089
          "self.recv_" << funname << "()" << endl;
1090
      }
1091
    } else {
1092
      if (gen_twisted_) {
1093
        f_service_ <<
1094
          indent() << "return defer.succeed(None)" << endl;
1095
      }
1096
    }
1097
    indent_down();
1098
    f_service_ << endl;
1099
 
1100
    indent(f_service_) <<
1101
      "def send_" << function_signature(*f_iter) << ":" << endl;
1102
 
1103
    indent_up();
1104
 
1105
    std::string argsname = (*f_iter)->get_name() + "_args";
1106
 
1107
    // Serialize the request header
1108
    if (gen_twisted_) {
1109
      f_service_ <<
1110
        indent() << "oprot = self._oprot_factory.getProtocol(self._transport)" << endl <<
1111
        indent() <<
1112
          "oprot.writeMessageBegin('" << (*f_iter)->get_name() << "', TMessageType.CALL, self._seqid)"
1113
        << endl;
1114
    } else {
1115
      f_service_ <<
1116
        indent() << "self._oprot.writeMessageBegin('" << (*f_iter)->get_name() << "', TMessageType.CALL, self._seqid)" << endl;
1117
    }
1118
 
1119
    f_service_ <<
1120
      indent() << "args = " << argsname << "()" << endl;
1121
 
1122
    for (fld_iter = fields.begin(); fld_iter != fields.end(); ++fld_iter) {
1123
      f_service_ <<
1124
        indent() << "args." << (*fld_iter)->get_name() << " = " << (*fld_iter)->get_name() << endl;
1125
    }
1126
 
1127
    // Write to the stream
1128
    if (gen_twisted_) {
1129
      f_service_ <<
1130
        indent() << "args.write(oprot)" << endl <<
1131
        indent() << "oprot.writeMessageEnd()" << endl <<
1132
        indent() << "oprot.trans.flush()" << endl;
1133
    } else {
1134
      f_service_ <<
1135
        indent() << "args.write(self._oprot)" << endl <<
1136
        indent() << "self._oprot.writeMessageEnd()" << endl <<
1137
        indent() << "self._oprot.trans.flush()" << endl;
1138
    }
1139
 
1140
    indent_down();
1141
 
1142
    if (!(*f_iter)->is_oneway()) {
1143
      std::string resultname = (*f_iter)->get_name() + "_result";
1144
      // Open function
1145
      f_service_ <<
1146
        endl;
1147
      if (gen_twisted_) {
1148
        f_service_ <<
1149
          indent() << "def recv_" << (*f_iter)->get_name() <<
1150
              "(self, iprot, mtype, rseqid):" << endl;
1151
      } else {
1152
        t_struct noargs(program_);
1153
        t_function recv_function((*f_iter)->get_returntype(),
1154
                               string("recv_") + (*f_iter)->get_name(),
1155
                               &noargs);
1156
        f_service_ <<
1157
          indent() << "def " << function_signature(&recv_function) << ":" << endl;
1158
      }
1159
      indent_up();
1160
 
1161
      // TODO(mcslee): Validate message reply here, seq ids etc.
1162
 
1163
      if (gen_twisted_) {
1164
        f_service_ <<
1165
          indent() << "d = self._reqs.pop(rseqid)" << endl;
1166
      } else {
1167
        f_service_ <<
1168
          indent() << "(fname, mtype, rseqid) = self._iprot.readMessageBegin()" << endl;
1169
      }
1170
 
1171
      f_service_ <<
1172
        indent() << "if mtype == TMessageType.EXCEPTION:" << endl <<
1173
        indent() << "  x = TApplicationException()" << endl;
1174
 
1175
      if (gen_twisted_) {
1176
        f_service_ <<
1177
          indent() << "  x.read(iprot)" << endl <<
1178
          indent() << "  iprot.readMessageEnd()" << endl <<
1179
          indent() << "  return d.errback(x)" << endl <<
1180
          indent() << "result = " << resultname << "()" << endl <<
1181
          indent() << "result.read(iprot)" << endl <<
1182
          indent() << "iprot.readMessageEnd()" << endl;
1183
      } else {
1184
        f_service_ <<
1185
          indent() << "  x.read(self._iprot)" << endl <<
1186
          indent() << "  self._iprot.readMessageEnd()" << endl <<
1187
          indent() << "  raise x" << endl <<
1188
          indent() << "result = " << resultname << "()" << endl <<
1189
          indent() << "result.read(self._iprot)" << endl <<
1190
          indent() << "self._iprot.readMessageEnd()" << endl;
1191
      }
1192
 
1193
      // Careful, only return _result if not a void function
1194
      if (!(*f_iter)->get_returntype()->is_void()) {
1195
        f_service_ <<
1196
          indent() << "if result.success != None:" << endl;
1197
          if (gen_twisted_) {
1198
            f_service_ <<
1199
              indent() << "  return d.callback(result.success)" << endl;
1200
          } else {
1201
            f_service_ <<
1202
              indent() << "  return result.success" << endl;
1203
          }
1204
      }
1205
 
1206
      t_struct* xs = (*f_iter)->get_xceptions();
1207
      const std::vector<t_field*>& xceptions = xs->get_members();
1208
      vector<t_field*>::const_iterator x_iter;
1209
      for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
1210
        f_service_ <<
1211
          indent() << "if result." << (*x_iter)->get_name() << " != None:" << endl;
1212
          if (gen_twisted_) {
1213
            f_service_ <<
1214
              indent() << "  return d.errback(result." << (*x_iter)->get_name() << ")" << endl;
1215
 
1216
          } else {
1217
            f_service_ <<
1218
              indent() << "  raise result." << (*x_iter)->get_name() << "" << endl;
1219
          }
1220
      }
1221
 
1222
      // Careful, only return _result if not a void function
1223
      if ((*f_iter)->get_returntype()->is_void()) {
1224
        if (gen_twisted_) {
1225
          indent(f_service_) <<
1226
            "return d.callback(None)" << endl;
1227
        } else {
1228
          indent(f_service_) <<
1229
            "return" << endl;
1230
        }
1231
      } else {
1232
        if (gen_twisted_) {
1233
          f_service_ <<
1234
            indent() << "return d.errback(TApplicationException(TApplicationException.MISSING_RESULT, \"" << (*f_iter)->get_name() << " failed: unknown result\"))" << endl;
1235
        } else {
1236
          f_service_ <<
1237
            indent() << "raise TApplicationException(TApplicationException.MISSING_RESULT, \"" << (*f_iter)->get_name() << " failed: unknown result\");" << endl;
1238
        }
1239
      }
1240
 
1241
      // Close function
1242
      indent_down();
1243
      f_service_ << endl;
1244
    }
1245
  }
1246
 
1247
  indent_down();
1248
  f_service_ <<
1249
    endl;
1250
}
1251
 
1252
/**
1253
 * Generates a command line tool for making remote requests
1254
 *
1255
 * @param tservice The service to generate a remote for.
1256
 */
1257
void t_py_generator::generate_service_remote(t_service* tservice) {
1258
  vector<t_function*> functions = tservice->get_functions();
1259
  vector<t_function*>::iterator f_iter;
1260
 
1261
  string f_remote_name = package_dir_+"/"+service_name_+"-remote";
1262
  ofstream f_remote;
1263
  f_remote.open(f_remote_name.c_str());
1264
 
1265
  f_remote <<
1266
    "#!/usr/bin/env python" << endl <<
1267
    py_autogen_comment() << endl <<
1268
    "import sys" << endl <<
1269
    "import pprint" << endl <<
1270
    "from urlparse import urlparse" << endl <<
1271
    "from thrift.transport import TTransport" << endl <<
1272
    "from thrift.transport import TSocket" << endl <<
1273
    "from thrift.transport import THttpClient" << endl <<
1274
    "from thrift.protocol import TBinaryProtocol" << endl <<
1275
    endl;
1276
 
1277
  f_remote <<
1278
    "import " << service_name_ << endl <<
1279
    "from ttypes import *" << endl <<
1280
    endl;
1281
 
1282
  f_remote <<
1283
    "if len(sys.argv) <= 1 or sys.argv[1] == '--help':" << endl <<
1284
    "  print ''" << endl <<
1285
    "  print 'Usage: ' + sys.argv[0] + ' [-h host:port] [-u url] [-f[ramed]] function [arg1 [arg2...]]'" << endl <<
1286
    "  print ''" << endl <<
1287
    "  print 'Functions:'" << endl;
1288
  for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
1289
    f_remote <<
1290
      "  print '  " << (*f_iter)->get_returntype()->get_name() << " " << (*f_iter)->get_name() << "(";
1291
    t_struct* arg_struct = (*f_iter)->get_arglist();
1292
    const std::vector<t_field*>& args = arg_struct->get_members();
1293
    vector<t_field*>::const_iterator a_iter;
1294
    int num_args = args.size();
1295
    bool first = true;
1296
    for (int i = 0; i < num_args; ++i) {
1297
      if (first) {
1298
        first = false;
1299
      } else {
1300
        f_remote << ", ";
1301
      }
1302
      f_remote <<
1303
        args[i]->get_type()->get_name() << " " << args[i]->get_name();
1304
    }
1305
    f_remote << ")'" << endl;
1306
  }
1307
  f_remote <<
1308
    "  print ''" << endl <<
1309
    "  sys.exit(0)" << endl <<
1310
    endl;
1311
 
1312
  f_remote <<
1313
    "pp = pprint.PrettyPrinter(indent = 2)" << endl <<
1314
    "host = 'localhost'" << endl <<
1315
    "port = 9090" << endl <<
1316
    "uri = ''" << endl <<
1317
    "framed = False" << endl <<
1318
    "http = False" << endl <<
1319
    "argi = 1" << endl <<
1320
    endl <<
1321
    "if sys.argv[argi] == '-h':" << endl <<
1322
    "  parts = sys.argv[argi+1].split(':') " << endl <<
1323
    "  host = parts[0]" << endl <<
1324
    "  port = int(parts[1])" << endl <<
1325
    "  argi += 2" << endl <<
1326
    endl <<
1327
    "if sys.argv[argi] == '-u':" << endl <<
1328
    "  url = urlparse(sys.argv[argi+1])" << endl <<
1329
    "  parts = url[1].split(':') " << endl <<
1330
    "  host = parts[0]" << endl <<
1331
    "  if len(parts) > 1:" << endl <<
1332
    "    port = int(parts[1])" << endl <<
1333
    "  else:" << endl <<
1334
    "    port = 80" << endl <<
1335
    "  uri = url[2]" << endl <<
1336
    "  http = True" << endl <<
1337
    "  argi += 2" << endl <<
1338
    endl <<
1339
    "if sys.argv[argi] == '-f' or sys.argv[argi] == '-framed':" << endl <<
1340
    "  framed = True" << endl <<
1341
    "  argi += 1" << endl <<
1342
    endl <<
1343
    "cmd = sys.argv[argi]" << endl <<
1344
    "args = sys.argv[argi+1:]" << endl <<
1345
    endl <<
1346
    "if http:" << endl <<
1347
    "  transport = THttpClient.THttpClient(host, port, uri)" << endl <<
1348
    "else:" << endl <<
1349
    "  socket = TSocket.TSocket(host, port)" << endl <<
1350
    "  if framed:" << endl <<
1351
    "    transport = TTransport.TFramedTransport(socket)" << endl <<
1352
    "  else:" << endl <<
1353
    "    transport = TTransport.TBufferedTransport(socket)" << endl <<
1354
    "protocol = TBinaryProtocol.TBinaryProtocol(transport)" << endl <<
1355
    "client = " << service_name_ << ".Client(protocol)" << endl <<
1356
    "transport.open()" << endl <<
1357
    endl;
1358
 
1359
  // Generate the dispatch methods
1360
  bool first = true;
1361
 
1362
  for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
1363
    if (first) {
1364
      first = false;
1365
    } else {
1366
      f_remote << "el";
1367
    }
1368
 
1369
    t_struct* arg_struct = (*f_iter)->get_arglist();
1370
    const std::vector<t_field*>& args = arg_struct->get_members();
1371
    vector<t_field*>::const_iterator a_iter;
1372
    int num_args = args.size();
1373
 
1374
    f_remote <<
1375
      "if cmd == '" << (*f_iter)->get_name() << "':" << endl <<
1376
      "  if len(args) != " << num_args << ":" << endl <<
1377
      "    print '" << (*f_iter)->get_name() << " requires " << num_args << " args'" << endl <<
1378
      "    sys.exit(1)" << endl <<
1379
      "  pp.pprint(client." << (*f_iter)->get_name() << "(";
1380
    for (int i = 0; i < num_args; ++i) {
1381
      if (args[i]->get_type()->is_string()) {
1382
        f_remote << "args[" << i << "],";
1383
      } else {
1384
        f_remote << "eval(args[" << i << "]),";
1385
      }
1386
    }
1387
    f_remote << "))" << endl;
1388
 
1389
    f_remote << endl;
1390
  }
1391
 
1392
  f_remote << "transport.close()" << endl;
1393
 
1394
  // Close service file
1395
  f_remote.close();
1396
 
1397
  // Make file executable, love that bitwise OR action
1398
  chmod(f_remote_name.c_str(),
1399
          S_IRUSR
1400
        | S_IWUSR
1401
        | S_IXUSR
1402
#ifndef MINGW
1403
        | S_IRGRP
1404
        | S_IXGRP
1405
        | S_IROTH
1406
        | S_IXOTH
1407
#endif
1408
  );
1409
}
1410
 
1411
/**
1412
 * Generates a service server definition.
1413
 *
1414
 * @param tservice The service to generate a server for.
1415
 */
1416
void t_py_generator::generate_service_server(t_service* tservice) {
1417
  // Generate the dispatch methods
1418
  vector<t_function*> functions = tservice->get_functions();
1419
  vector<t_function*>::iterator f_iter;
1420
 
1421
  string extends = "";
1422
  string extends_processor = "";
1423
  if (tservice->get_extends() != NULL) {
1424
    extends = type_name(tservice->get_extends());
1425
    extends_processor = extends + ".Processor, ";
1426
  }
1427
 
1428
  // Generate the header portion
1429
  if (gen_twisted_) {
1430
    f_service_ <<
1431
      "class Processor(" << extends_processor << "TProcessor):" << endl <<
1432
      "  implements(Iface)" << endl << endl;
1433
  } else {
1434
    f_service_ <<
1435
      "class Processor(" << extends_processor << "Iface, TProcessor):" << endl;
1436
  }
1437
 
1438
  indent_up();
1439
 
1440
  indent(f_service_) <<
1441
    "def __init__(self, handler):" << endl;
1442
  indent_up();
1443
  if (extends.empty()) {
1444
    if (gen_twisted_) {
1445
      f_service_ <<
1446
        indent() << "self._handler = Iface(handler)" << endl;
1447
    } else {
1448
      f_service_ <<
1449
        indent() << "self._handler = handler" << endl;
1450
    }
1451
 
1452
    f_service_ <<
1453
      indent() << "self._processMap = {}" << endl;
1454
  } else {
1455
    if (gen_twisted_) {
1456
      f_service_ <<
1457
        indent() << extends << ".Processor.__init__(self, Iface(handler))" << endl;
1458
    } else {
1459
      f_service_ <<
1460
        indent() << extends << ".Processor.__init__(self, handler)" << endl;
1461
    }
1462
  }
1463
  for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
1464
    f_service_ <<
1465
      indent() << "self._processMap[\"" << (*f_iter)->get_name() << "\"] = Processor.process_" << (*f_iter)->get_name() << endl;
1466
  }
1467
  indent_down();
1468
  f_service_ << endl;
1469
 
1470
  // Generate the server implementation
1471
  indent(f_service_) <<
1472
    "def process(self, iprot, oprot):" << endl;
1473
  indent_up();
1474
 
1475
  f_service_ <<
1476
    indent() << "(name, type, seqid) = iprot.readMessageBegin()" << endl;
1477
 
1478
  // TODO(mcslee): validate message
1479
 
1480
  // HOT: dictionary function lookup
1481
  f_service_ <<
1482
    indent() << "if name not in self._processMap:" << endl <<
1483
    indent() << "  iprot.skip(TType.STRUCT)" << endl <<
1484
    indent() << "  iprot.readMessageEnd()" << endl <<
1485
    indent() << "  x = TApplicationException(TApplicationException.UNKNOWN_METHOD, 'Unknown function %s' % (name))" << endl <<
1486
    indent() << "  oprot.writeMessageBegin(name, TMessageType.EXCEPTION, seqid)" << endl <<
1487
    indent() << "  x.write(oprot)" << endl <<
1488
    indent() << "  oprot.writeMessageEnd()" << endl <<
1489
    indent() << "  oprot.trans.flush()" << endl;
1490
 
1491
  if (gen_twisted_) {
1492
    f_service_ <<
1493
      indent() << "  return defer.succeed(None)" << endl;
1494
  } else {
1495
    f_service_ <<
1496
      indent() << "  return" << endl;
1497
  }
1498
 
1499
  f_service_ <<
1500
    indent() << "else:" << endl;
1501
 
1502
  if (gen_twisted_) {
1503
    f_service_ <<
1504
      indent() << "  return self._processMap[name](self, seqid, iprot, oprot)" << endl;
1505
  } else {
1506
    f_service_ <<
1507
      indent() << "  self._processMap[name](self, seqid, iprot, oprot)" << endl;
1508
 
1509
    // Read end of args field, the T_STOP, and the struct close
1510
    f_service_ <<
1511
      indent() << "return True" << endl;
1512
  }
1513
 
1514
  indent_down();
1515
  f_service_ << endl;
1516
 
1517
  // Generate the process subfunctions
1518
  for (f_iter = functions.begin(); f_iter != functions.end(); ++f_iter) {
1519
    generate_process_function(tservice, *f_iter);
1520
  }
1521
 
1522
  indent_down();
1523
  f_service_ << endl;
1524
}
1525
 
1526
/**
1527
 * Generates a process function definition.
1528
 *
1529
 * @param tfunction The function to write a dispatcher for
1530
 */
1531
void t_py_generator::generate_process_function(t_service* tservice,
1532
                                               t_function* tfunction) {
1533
  // Open function
1534
  indent(f_service_) <<
1535
    "def process_" << tfunction->get_name() <<
1536
    "(self, seqid, iprot, oprot):" << endl;
1537
  indent_up();
1538
 
1539
  string argsname = tfunction->get_name() + "_args";
1540
  string resultname = tfunction->get_name() + "_result";
1541
 
1542
  f_service_ <<
1543
    indent() << "args = " << argsname << "()" << endl <<
1544
    indent() << "args.read(iprot)" << endl <<
1545
    indent() << "iprot.readMessageEnd()" << endl;
1546
 
1547
  t_struct* xs = tfunction->get_xceptions();
1548
  const std::vector<t_field*>& xceptions = xs->get_members();
1549
  vector<t_field*>::const_iterator x_iter;
1550
 
1551
  // Declare result for non oneway function
1552
  if (!tfunction->is_oneway()) {
1553
    f_service_ <<
1554
      indent() << "result = " << resultname << "()" << endl;
1555
  }
1556
 
1557
  if (gen_twisted_) {
1558
    // Generate the function call
1559
    t_struct* arg_struct = tfunction->get_arglist();
1560
    const std::vector<t_field*>& fields = arg_struct->get_members();
1561
    vector<t_field*>::const_iterator f_iter;
1562
 
1563
    f_service_ <<
1564
      indent() << "d = defer.maybeDeferred(self._handler." <<
1565
        tfunction->get_name() << ", ";
1566
    bool first = true;
1567
    for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
1568
      if (first) {
1569
        first = false;
1570
      } else {
1571
        f_service_ << ", ";
1572
      }
1573
      f_service_ << "args." << (*f_iter)->get_name();
1574
    }
1575
    f_service_ << ")" << endl;
1576
 
1577
    // Shortcut out here for oneway functions
1578
    if (tfunction->is_oneway()) {
1579
      f_service_ <<
1580
        indent() << "return d" << endl;
1581
      indent_down();
1582
      f_service_ << endl;
1583
      return;
1584
    }
1585
 
1586
    f_service_ <<
1587
      indent() <<
1588
        "d.addCallback(self.write_results_success_" <<
1589
          tfunction->get_name() << ", result, seqid, oprot)" << endl;
1590
 
1591
    if (xceptions.size() > 0) {
1592
      f_service_ <<
1593
        indent() <<
1594
          "d.addErrback(self.write_results_exception_" <<
1595
            tfunction->get_name() << ", result, seqid, oprot)" << endl;
1596
    }
1597
 
1598
    f_service_ <<
1599
      indent() << "return d" << endl;
1600
 
1601
    indent_down();
1602
    f_service_ << endl;
1603
 
1604
    indent(f_service_) <<
1605
        "def write_results_success_" << tfunction->get_name() <<
1606
        "(self, success, result, seqid, oprot):" << endl;
1607
    indent_up();
1608
    f_service_ <<
1609
      indent() << "result.success = success" << endl <<
1610
      indent() << "oprot.writeMessageBegin(\"" << tfunction->get_name() <<
1611
        "\", TMessageType.REPLY, seqid)" << endl <<
1612
      indent() << "result.write(oprot)" << endl <<
1613
      indent() << "oprot.writeMessageEnd()" << endl <<
1614
      indent() << "oprot.trans.flush()" << endl;
1615
    indent_down();
1616
    f_service_ << endl;
1617
 
1618
    // Try block for a function with exceptions
1619
    if (!tfunction->is_oneway() && xceptions.size() > 0) {
1620
      indent(f_service_) <<
1621
        "def write_results_exception_" << tfunction->get_name() <<
1622
        "(self, error, result, seqid, oprot):" << endl;
1623
      indent_up();
1624
      f_service_ <<
1625
        indent() << "try:" << endl;
1626
 
1627
      // Kinda absurd
1628
      f_service_ <<
1629
        indent() << "  error.raiseException()" << endl;
1630
      for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
1631
        f_service_ <<
1632
          indent() << "except " << type_name((*x_iter)->get_type()) << ", " << (*x_iter)->get_name() << ":" << endl;
1633
        if (!tfunction->is_oneway()) {
1634
          indent_up();
1635
          f_service_ <<
1636
            indent() << "result." << (*x_iter)->get_name() << " = " << (*x_iter)->get_name() << endl;
1637
          indent_down();
1638
        } else {
1639
          f_service_ <<
1640
            indent() << "pass" << endl;
1641
        }
1642
      }
1643
      f_service_ <<
1644
        indent() << "oprot.writeMessageBegin(\"" << tfunction->get_name() <<
1645
          "\", TMessageType.REPLY, seqid)" << endl <<
1646
        indent() << "result.write(oprot)" << endl <<
1647
        indent() << "oprot.writeMessageEnd()" << endl <<
1648
        indent() << "oprot.trans.flush()" << endl;
1649
      indent_down();
1650
      f_service_ << endl;
1651
    }
1652
  } else {
1653
 
1654
    // Try block for a function with exceptions
1655
    if (xceptions.size() > 0) {
1656
      f_service_ <<
1657
        indent() << "try:" << endl;
1658
      indent_up();
1659
    }
1660
 
1661
    // Generate the function call
1662
    t_struct* arg_struct = tfunction->get_arglist();
1663
    const std::vector<t_field*>& fields = arg_struct->get_members();
1664
    vector<t_field*>::const_iterator f_iter;
1665
 
1666
    f_service_ << indent();
1667
    if (!tfunction->is_oneway() && !tfunction->get_returntype()->is_void()) {
1668
      f_service_ << "result.success = ";
1669
    }
1670
    f_service_ <<
1671
      "self._handler." << tfunction->get_name() << "(";
1672
    bool first = true;
1673
    for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
1674
      if (first) {
1675
        first = false;
1676
      } else {
1677
        f_service_ << ", ";
1678
      }
1679
      f_service_ << "args." << (*f_iter)->get_name();
1680
    }
1681
    f_service_ << ")" << endl;
1682
 
1683
    if (!tfunction->is_oneway() && xceptions.size() > 0) {
1684
      indent_down();
1685
      for (x_iter = xceptions.begin(); x_iter != xceptions.end(); ++x_iter) {
1686
        f_service_ <<
1687
          indent() << "except " << type_name((*x_iter)->get_type()) << ", " << (*x_iter)->get_name() << ":" << endl;
1688
        if (!tfunction->is_oneway()) {
1689
          indent_up();
1690
          f_service_ <<
1691
            indent() << "result." << (*x_iter)->get_name() << " = " << (*x_iter)->get_name() << endl;
1692
          indent_down();
1693
        } else {
1694
          f_service_ <<
1695
            indent() << "pass" << endl;
1696
        }
1697
      }
1698
    }
1699
 
1700
    // Shortcut out here for oneway functions
1701
    if (tfunction->is_oneway()) {
1702
      f_service_ <<
1703
        indent() << "return" << endl;
1704
      indent_down();
1705
      f_service_ << endl;
1706
      return;
1707
    }
1708
 
1709
    f_service_ <<
1710
      indent() << "oprot.writeMessageBegin(\"" << tfunction->get_name() << "\", TMessageType.REPLY, seqid)" << endl <<
1711
      indent() << "result.write(oprot)" << endl <<
1712
      indent() << "oprot.writeMessageEnd()" << endl <<
1713
      indent() << "oprot.trans.flush()" << endl;
1714
 
1715
    // Close function
1716
    indent_down();
1717
    f_service_ << endl;
1718
  }
1719
}
1720
 
1721
/**
1722
 * Deserializes a field of any type.
1723
 */
1724
void t_py_generator::generate_deserialize_field(ofstream &out,
1725
                                                t_field* tfield,
1726
                                                string prefix,
1727
                                                bool inclass) {
1728
  t_type* type = get_true_type(tfield->get_type());
1729
 
1730
  if (type->is_void()) {
1731
    throw "CANNOT GENERATE DESERIALIZE CODE FOR void TYPE: " +
1732
      prefix + tfield->get_name();
1733
  }
1734
 
1735
  string name = prefix + tfield->get_name();
1736
 
1737
  if (type->is_struct() || type->is_xception()) {
1738
    generate_deserialize_struct(out,
1739
                                (t_struct*)type,
1740
                                 name);
1741
  } else if (type->is_container()) {
1742
    generate_deserialize_container(out, type, name);
1743
  } else if (type->is_base_type() || type->is_enum()) {
1744
    indent(out) <<
1745
      name << " = iprot.";
1746
 
1747
    if (type->is_base_type()) {
1748
      t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
1749
      switch (tbase) {
1750
      case t_base_type::TYPE_VOID:
1751
        throw "compiler error: cannot serialize void field in a struct: " +
1752
          name;
1753
        break;
1754
      case t_base_type::TYPE_STRING:
1755
        out << "readString();";
1756
        break;
1757
      case t_base_type::TYPE_BOOL:
1758
        out << "readBool();";
1759
        break;
1760
      case t_base_type::TYPE_BYTE:
1761
        out << "readByte();";
1762
        break;
1763
      case t_base_type::TYPE_I16:
1764
        out << "readI16();";
1765
        break;
1766
      case t_base_type::TYPE_I32:
1767
        out << "readI32();";
1768
        break;
1769
      case t_base_type::TYPE_I64:
1770
        out << "readI64();";
1771
        break;
1772
      case t_base_type::TYPE_DOUBLE:
1773
        out << "readDouble();";
1774
        break;
1775
      default:
1776
        throw "compiler error: no PHP name for base type " + t_base_type::t_base_name(tbase);
1777
      }
1778
    } else if (type->is_enum()) {
1779
      out << "readI32();";
1780
    }
1781
    out << endl;
1782
 
1783
  } else {
1784
    printf("DO NOT KNOW HOW TO DESERIALIZE FIELD '%s' TYPE '%s'\n",
1785
           tfield->get_name().c_str(), type->get_name().c_str());
1786
  }
1787
}
1788
 
1789
/**
1790
 * Generates an unserializer for a struct, calling read()
1791
 */
1792
void t_py_generator::generate_deserialize_struct(ofstream &out,
1793
                                                  t_struct* tstruct,
1794
                                                  string prefix) {
1795
  out <<
1796
    indent() << prefix << " = " << type_name(tstruct) << "()" << endl <<
1797
    indent() << prefix << ".read(iprot)" << endl;
1798
}
1799
 
1800
/**
1801
 * Serialize a container by writing out the header followed by
1802
 * data and then a footer.
1803
 */
1804
void t_py_generator::generate_deserialize_container(ofstream &out,
1805
                                                    t_type* ttype,
1806
                                                    string prefix) {
1807
  string size = tmp("_size");
1808
  string ktype = tmp("_ktype");
1809
  string vtype = tmp("_vtype");
1810
  string etype = tmp("_etype");
1811
 
1812
  t_field fsize(g_type_i32, size);
1813
  t_field fktype(g_type_byte, ktype);
1814
  t_field fvtype(g_type_byte, vtype);
1815
  t_field fetype(g_type_byte, etype);
1816
 
1817
  // Declare variables, read header
1818
  if (ttype->is_map()) {
1819
    out <<
1820
      indent() << prefix << " = {}" << endl <<
1821
      indent() << "(" << ktype << ", " << vtype << ", " << size << " ) = iprot.readMapBegin() " << endl;
1822
  } else if (ttype->is_set()) {
1823
    out <<
1824
      indent() << prefix << " = set()" << endl <<
1825
      indent() << "(" << etype << ", " << size << ") = iprot.readSetBegin()" << endl;
1826
  } else if (ttype->is_list()) {
1827
    out <<
1828
      indent() << prefix << " = []" << endl <<
1829
      indent() << "(" << etype << ", " << size << ") = iprot.readListBegin()" << endl;
1830
  }
1831
 
1832
  // For loop iterates over elements
1833
  string i = tmp("_i");
1834
  indent(out) <<
1835
    "for " << i << " in xrange(" << size << "):" << endl;
1836
 
1837
    indent_up();
1838
 
1839
    if (ttype->is_map()) {
1840
      generate_deserialize_map_element(out, (t_map*)ttype, prefix);
1841
    } else if (ttype->is_set()) {
1842
      generate_deserialize_set_element(out, (t_set*)ttype, prefix);
1843
    } else if (ttype->is_list()) {
1844
      generate_deserialize_list_element(out, (t_list*)ttype, prefix);
1845
    }
1846
 
1847
    indent_down();
1848
 
1849
  // Read container end
1850
  if (ttype->is_map()) {
1851
    indent(out) << "iprot.readMapEnd()" << endl;
1852
  } else if (ttype->is_set()) {
1853
    indent(out) << "iprot.readSetEnd()" << endl;
1854
  } else if (ttype->is_list()) {
1855
    indent(out) << "iprot.readListEnd()" << endl;
1856
  }
1857
}
1858
 
1859
 
1860
/**
1861
 * Generates code to deserialize a map
1862
 */
1863
void t_py_generator::generate_deserialize_map_element(ofstream &out,
1864
                                                       t_map* tmap,
1865
                                                       string prefix) {
1866
  string key = tmp("_key");
1867
  string val = tmp("_val");
1868
  t_field fkey(tmap->get_key_type(), key);
1869
  t_field fval(tmap->get_val_type(), val);
1870
 
1871
  generate_deserialize_field(out, &fkey);
1872
  generate_deserialize_field(out, &fval);
1873
 
1874
  indent(out) <<
1875
    prefix << "[" << key << "] = " << val << endl;
1876
}
1877
 
1878
/**
1879
 * Write a set element
1880
 */
1881
void t_py_generator::generate_deserialize_set_element(ofstream &out,
1882
                                                       t_set* tset,
1883
                                                       string prefix) {
1884
  string elem = tmp("_elem");
1885
  t_field felem(tset->get_elem_type(), elem);
1886
 
1887
  generate_deserialize_field(out, &felem);
1888
 
1889
  indent(out) <<
1890
    prefix << ".add(" << elem << ")" << endl;
1891
}
1892
 
1893
/**
1894
 * Write a list element
1895
 */
1896
void t_py_generator::generate_deserialize_list_element(ofstream &out,
1897
                                                        t_list* tlist,
1898
                                                        string prefix) {
1899
  string elem = tmp("_elem");
1900
  t_field felem(tlist->get_elem_type(), elem);
1901
 
1902
  generate_deserialize_field(out, &felem);
1903
 
1904
  indent(out) <<
1905
    prefix << ".append(" << elem << ")" << endl;
1906
}
1907
 
1908
 
1909
/**
1910
 * Serializes a field of any type.
1911
 *
1912
 * @param tfield The field to serialize
1913
 * @param prefix Name to prepend to field name
1914
 */
1915
void t_py_generator::generate_serialize_field(ofstream &out,
1916
                                               t_field* tfield,
1917
                                               string prefix) {
1918
  t_type* type = get_true_type(tfield->get_type());
1919
 
1920
  // Do nothing for void types
1921
  if (type->is_void()) {
1922
    throw "CANNOT GENERATE SERIALIZE CODE FOR void TYPE: " +
1923
      prefix + tfield->get_name();
1924
  }
1925
 
1926
  if (type->is_struct() || type->is_xception()) {
1927
    generate_serialize_struct(out,
1928
                              (t_struct*)type,
1929
                              prefix + tfield->get_name());
1930
  } else if (type->is_container()) {
1931
    generate_serialize_container(out,
1932
                                 type,
1933
                                 prefix + tfield->get_name());
1934
  } else if (type->is_base_type() || type->is_enum()) {
1935
 
1936
    string name = prefix + tfield->get_name();
1937
 
1938
    indent(out) <<
1939
      "oprot.";
1940
 
1941
    if (type->is_base_type()) {
1942
      t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
1943
      switch (tbase) {
1944
      case t_base_type::TYPE_VOID:
1945
        throw
1946
          "compiler error: cannot serialize void field in a struct: " + name;
1947
        break;
1948
      case t_base_type::TYPE_STRING:
1949
        out << "writeString(" << name << ")";
1950
        break;
1951
      case t_base_type::TYPE_BOOL:
1952
        out << "writeBool(" << name << ")";
1953
        break;
1954
      case t_base_type::TYPE_BYTE:
1955
        out << "writeByte(" << name << ")";
1956
        break;
1957
      case t_base_type::TYPE_I16:
1958
        out << "writeI16(" << name << ")";
1959
        break;
1960
      case t_base_type::TYPE_I32:
1961
        out << "writeI32(" << name << ")";
1962
        break;
1963
      case t_base_type::TYPE_I64:
1964
        out << "writeI64(" << name << ")";
1965
        break;
1966
      case t_base_type::TYPE_DOUBLE:
1967
        out << "writeDouble(" << name << ")";
1968
        break;
1969
      default:
1970
        throw "compiler error: no PHP name for base type " + t_base_type::t_base_name(tbase);
1971
      }
1972
    } else if (type->is_enum()) {
1973
      out << "writeI32(" << name << ")";
1974
    }
1975
    out << endl;
1976
  } else {
1977
    printf("DO NOT KNOW HOW TO SERIALIZE FIELD '%s%s' TYPE '%s'\n",
1978
           prefix.c_str(),
1979
           tfield->get_name().c_str(),
1980
           type->get_name().c_str());
1981
  }
1982
}
1983
 
1984
/**
1985
 * Serializes all the members of a struct.
1986
 *
1987
 * @param tstruct The struct to serialize
1988
 * @param prefix  String prefix to attach to all fields
1989
 */
1990
void t_py_generator::generate_serialize_struct(ofstream &out,
1991
                                               t_struct* tstruct,
1992
                                               string prefix) {
1993
  indent(out) <<
1994
    prefix << ".write(oprot)" << endl;
1995
}
1996
 
1997
void t_py_generator::generate_serialize_container(ofstream &out,
1998
                                                  t_type* ttype,
1999
                                                  string prefix) {
2000
  if (ttype->is_map()) {
2001
    indent(out) <<
2002
      "oprot.writeMapBegin(" <<
2003
      type_to_enum(((t_map*)ttype)->get_key_type()) << ", " <<
2004
      type_to_enum(((t_map*)ttype)->get_val_type()) << ", " <<
2005
      "len(" << prefix << "))" << endl;
2006
  } else if (ttype->is_set()) {
2007
    indent(out) <<
2008
      "oprot.writeSetBegin(" <<
2009
      type_to_enum(((t_set*)ttype)->get_elem_type()) << ", " <<
2010
      "len(" << prefix << "))" << endl;
2011
  } else if (ttype->is_list()) {
2012
    indent(out) <<
2013
      "oprot.writeListBegin(" <<
2014
      type_to_enum(((t_list*)ttype)->get_elem_type()) << ", " <<
2015
      "len(" << prefix << "))" << endl;
2016
  }
2017
 
2018
  if (ttype->is_map()) {
2019
    string kiter = tmp("kiter");
2020
    string viter = tmp("viter");
2021
    indent(out) <<
2022
      "for " << kiter << "," << viter << " in " << prefix << ".items():" << endl;
2023
    indent_up();
2024
    generate_serialize_map_element(out, (t_map*)ttype, kiter, viter);
2025
    indent_down();
2026
  } else if (ttype->is_set()) {
2027
    string iter = tmp("iter");
2028
    indent(out) <<
2029
      "for " << iter << " in " << prefix << ":" << endl;
2030
    indent_up();
2031
    generate_serialize_set_element(out, (t_set*)ttype, iter);
2032
    indent_down();
2033
  } else if (ttype->is_list()) {
2034
    string iter = tmp("iter");
2035
    indent(out) <<
2036
      "for " << iter << " in " << prefix << ":" << endl;
2037
    indent_up();
2038
    generate_serialize_list_element(out, (t_list*)ttype, iter);
2039
    indent_down();
2040
  }
2041
 
2042
  if (ttype->is_map()) {
2043
    indent(out) <<
2044
      "oprot.writeMapEnd()" << endl;
2045
  } else if (ttype->is_set()) {
2046
    indent(out) <<
2047
      "oprot.writeSetEnd()" << endl;
2048
  } else if (ttype->is_list()) {
2049
    indent(out) <<
2050
      "oprot.writeListEnd()" << endl;
2051
  }
2052
}
2053
 
2054
/**
2055
 * Serializes the members of a map.
2056
 *
2057
 */
2058
void t_py_generator::generate_serialize_map_element(ofstream &out,
2059
                                                     t_map* tmap,
2060
                                                     string kiter,
2061
                                                     string viter) {
2062
  t_field kfield(tmap->get_key_type(), kiter);
2063
  generate_serialize_field(out, &kfield, "");
2064
 
2065
  t_field vfield(tmap->get_val_type(), viter);
2066
  generate_serialize_field(out, &vfield, "");
2067
}
2068
 
2069
/**
2070
 * Serializes the members of a set.
2071
 */
2072
void t_py_generator::generate_serialize_set_element(ofstream &out,
2073
                                                     t_set* tset,
2074
                                                     string iter) {
2075
  t_field efield(tset->get_elem_type(), iter);
2076
  generate_serialize_field(out, &efield, "");
2077
}
2078
 
2079
/**
2080
 * Serializes the members of a list.
2081
 */
2082
void t_py_generator::generate_serialize_list_element(ofstream &out,
2083
                                                      t_list* tlist,
2084
                                                      string iter) {
2085
  t_field efield(tlist->get_elem_type(), iter);
2086
  generate_serialize_field(out, &efield, "");
2087
}
2088
 
2089
/**
2090
 * Generates the docstring for a given struct.
2091
 */
2092
void t_py_generator::generate_python_docstring(ofstream& out,
2093
                                               t_struct* tstruct) {
2094
  generate_python_docstring(out, tstruct, tstruct, "Attributes");
2095
}
2096
 
2097
/**
2098
 * Generates the docstring for a given function.
2099
 */
2100
void t_py_generator::generate_python_docstring(ofstream& out,
2101
                                               t_function* tfunction) {
2102
  generate_python_docstring(out, tfunction, tfunction->get_arglist(), "Parameters");
2103
}
2104
 
2105
/**
2106
 * Generates the docstring for a struct or function.
2107
 */
2108
void t_py_generator::generate_python_docstring(ofstream& out,
2109
                                               t_doc*    tdoc,
2110
                                               t_struct* tstruct,
2111
                                               const char* subheader) {
2112
  bool has_doc = false;
2113
  stringstream ss;
2114
  if (tdoc->has_doc()) {
2115
    has_doc = true;
2116
    ss << tdoc->get_doc();
2117
  }
2118
 
2119
  const vector<t_field*>& fields = tstruct->get_members();
2120
  if (fields.size() > 0) {
2121
    if (has_doc) {
2122
      ss << endl;
2123
    }
2124
    has_doc = true;
2125
    ss << subheader << ":\n";
2126
    vector<t_field*>::const_iterator p_iter;
2127
    for (p_iter = fields.begin(); p_iter != fields.end(); ++p_iter) {
2128
      t_field* p = *p_iter;
2129
      ss << " - " << p->get_name();
2130
      if (p->has_doc()) {
2131
        ss << ": " << p->get_doc();
2132
      } else {
2133
        ss << endl;
2134
      }
2135
    }
2136
  }
2137
 
2138
  if (has_doc) {
2139
    generate_docstring_comment(out,
2140
      "\"\"\"\n",
2141
      "", ss.str(),
2142
      "\"\"\"\n");
2143
  }
2144
}
2145
 
2146
/**
2147
 * Generates the docstring for a generic object.
2148
 */
2149
void t_py_generator::generate_python_docstring(ofstream& out,
2150
                                               t_doc* tdoc) {
2151
  if (tdoc->has_doc()) {
2152
    generate_docstring_comment(out,
2153
      "\"\"\"\n",
2154
      "", tdoc->get_doc(),
2155
      "\"\"\"\n");
2156
  }
2157
}
2158
 
2159
/**
2160
 * Declares an argument, which may include initialization as necessary.
2161
 *
2162
 * @param tfield The field
2163
 */
2164
string t_py_generator::declare_argument(t_field* tfield) {
2165
  std::ostringstream result;
2166
  result << tfield->get_name() << "=";
2167
  if (tfield->get_value() != NULL) {
2168
    result << "thrift_spec[" <<
2169
      tfield->get_key() << "][4]";
2170
  } else {
2171
    result << "None";
2172
  }
2173
  return result.str();
2174
}
2175
 
2176
/**
2177
 * Renders a field default value, returns None otherwise.
2178
 *
2179
 * @param tfield The field
2180
 */
2181
string t_py_generator::render_field_default_value(t_field* tfield) {
2182
  t_type* type = get_true_type(tfield->get_type());
2183
  if (tfield->get_value() != NULL) {
2184
    return render_const_value(type, tfield->get_value());
2185
  } else {
2186
    return "None";
2187
  }
2188
}
2189
 
2190
/**
2191
 * Renders a function signature of the form 'type name(args)'
2192
 *
2193
 * @param tfunction Function definition
2194
 * @return String of rendered function definition
2195
 */
2196
string t_py_generator::function_signature(t_function* tfunction,
2197
                                           string prefix) {
2198
  // TODO(mcslee): Nitpicky, no ',' if argument_list is empty
2199
  return
2200
    prefix + tfunction->get_name() +
2201
    "(self, " + argument_list(tfunction->get_arglist()) + ")";
2202
}
2203
 
2204
/**
2205
 * Renders an interface function signature of the form 'type name(args)'
2206
 *
2207
 * @param tfunction Function definition
2208
 * @return String of rendered function definition
2209
 */
2210
string t_py_generator::function_signature_if(t_function* tfunction,
2211
                                           string prefix) {
2212
  // TODO(mcslee): Nitpicky, no ',' if argument_list is empty
2213
  string signature = prefix + tfunction->get_name() + "(";
2214
  if (!gen_twisted_) {
2215
    signature += "self, ";
2216
  }
2217
  signature += argument_list(tfunction->get_arglist()) + ")";
2218
  return signature;
2219
}
2220
 
2221
 
2222
/**
2223
 * Renders a field list
2224
 */
2225
string t_py_generator::argument_list(t_struct* tstruct) {
2226
  string result = "";
2227
 
2228
  const vector<t_field*>& fields = tstruct->get_members();
2229
  vector<t_field*>::const_iterator f_iter;
2230
  bool first = true;
2231
  for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
2232
    if (first) {
2233
      first = false;
2234
    } else {
2235
      result += ", ";
2236
    }
2237
    result += (*f_iter)->get_name();
2238
  }
2239
  return result;
2240
}
2241
 
2242
string t_py_generator::type_name(t_type* ttype) {
2243
  t_program* program = ttype->get_program();
2244
  if (ttype->is_service()) {
2245
    return get_real_py_module(program) + "." + ttype->get_name();
2246
  }
2247
  if (program != NULL && program != program_) {
2248
    return get_real_py_module(program) + ".ttypes." + ttype->get_name();
2249
  }
2250
  return ttype->get_name();
2251
}
2252
 
2253
/**
2254
 * Converts the parse type to a Python tyoe
2255
 */
2256
string t_py_generator::type_to_enum(t_type* type) {
2257
  type = get_true_type(type);
2258
 
2259
  if (type->is_base_type()) {
2260
    t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
2261
    switch (tbase) {
2262
    case t_base_type::TYPE_VOID:
2263
      throw "NO T_VOID CONSTRUCT";
2264
    case t_base_type::TYPE_STRING:
2265
      return "TType.STRING";
2266
    case t_base_type::TYPE_BOOL:
2267
      return "TType.BOOL";
2268
    case t_base_type::TYPE_BYTE:
2269
      return "TType.BYTE";
2270
    case t_base_type::TYPE_I16:
2271
      return "TType.I16";
2272
    case t_base_type::TYPE_I32:
2273
      return "TType.I32";
2274
    case t_base_type::TYPE_I64:
2275
      return "TType.I64";
2276
    case t_base_type::TYPE_DOUBLE:
2277
      return "TType.DOUBLE";
2278
    }
2279
  } else if (type->is_enum()) {
2280
    return "TType.I32";
2281
  } else if (type->is_struct() || type->is_xception()) {
2282
    return "TType.STRUCT";
2283
  } else if (type->is_map()) {
2284
    return "TType.MAP";
2285
  } else if (type->is_set()) {
2286
    return "TType.SET";
2287
  } else if (type->is_list()) {
2288
    return "TType.LIST";
2289
  }
2290
 
2291
  throw "INVALID TYPE IN type_to_enum: " + type->get_name();
2292
}
2293
 
2294
/** See the comment inside generate_py_struct_definition for what this is. */
2295
string t_py_generator::type_to_spec_args(t_type* ttype) {
2296
  while (ttype->is_typedef()) {
2297
    ttype = ((t_typedef*)ttype)->get_type();
2298
  }
2299
 
2300
  if (ttype->is_base_type() || ttype->is_enum()) {
2301
    return "None";
2302
  } else if (ttype->is_struct() || ttype->is_xception()) {
2303
    return "(" + type_name(ttype) + ", " + type_name(ttype) + ".thrift_spec)";
2304
  } else if (ttype->is_map()) {
2305
    return "(" +
2306
      type_to_enum(((t_map*)ttype)->get_key_type()) + "," +
2307
      type_to_spec_args(((t_map*)ttype)->get_key_type()) + "," +
2308
      type_to_enum(((t_map*)ttype)->get_val_type()) + "," +
2309
      type_to_spec_args(((t_map*)ttype)->get_val_type()) +
2310
      ")";
2311
 
2312
  } else if (ttype->is_set()) {
2313
    return "(" +
2314
      type_to_enum(((t_set*)ttype)->get_elem_type()) + "," +
2315
      type_to_spec_args(((t_set*)ttype)->get_elem_type()) +
2316
      ")";
2317
 
2318
  } else if (ttype->is_list()) {
2319
    return "(" +
2320
      type_to_enum(((t_list*)ttype)->get_elem_type()) + "," +
2321
      type_to_spec_args(((t_list*)ttype)->get_elem_type()) +
2322
      ")";
2323
  }
2324
 
2325
  throw "INVALID TYPE IN type_to_spec_args: " + ttype->get_name();
2326
}
2327
 
2328
 
2329
THRIFT_REGISTER_GENERATOR(py, "Python",
2330
"    new_style:       Generate new-style classes.\n" \
2331
"    twisted:         Generate Twisted-friendly RPC services.\n"
2332
);