1 // Copyright (c) 2006- Facebook
2 // Distributed under the Thrift Software License
4 // See accompanying file LICENSE or visit the Thrift site at:
5 // http://developers.facebook.com/thrift/
7 #ifndef T_PY_GENERATOR_H
8 #define T_PY_GENERATOR_H
15 #include "t_generator.h"
18 * Python code generator.
20 * @author Mark Slee <mcslee@facebook.com>
22 class t_py_generator
: public t_generator
{
24 t_py_generator(t_program
* program
, bool gen_newstyle
) :
26 gen_newstyle_(gen_newstyle
) {
28 out_dir_base_
= "gen-py";
32 * Init and close methods
35 void init_generator();
36 void close_generator();
39 * Program-level generation functions
42 void generate_typedef (t_typedef
* ttypedef
);
43 void generate_enum (t_enum
* tenum
);
44 void generate_const (t_const
* tconst
);
45 void generate_struct (t_struct
* tstruct
);
46 void generate_xception (t_struct
* txception
);
47 void generate_service (t_service
* tservice
);
49 std::string
render_const_value(t_type
* type
, t_const_value
* value
);
52 * Struct generation code
55 void generate_py_struct(t_struct
* tstruct
, bool is_exception
);
56 void generate_py_struct_definition(std::ofstream
& out
, t_struct
* tstruct
, bool is_xception
=false, bool is_result
=false);
57 void generate_py_struct_reader(std::ofstream
& out
, t_struct
* tstruct
);
58 void generate_py_struct_writer(std::ofstream
& out
, t_struct
* tstruct
);
59 void generate_py_function_helpers(t_function
* tfunction
);
62 * Service-level generation functions
65 void generate_service_helpers (t_service
* tservice
);
66 void generate_service_interface (t_service
* tservice
);
67 void generate_service_client (t_service
* tservice
);
68 void generate_service_remote (t_service
* tservice
);
69 void generate_service_server (t_service
* tservice
);
70 void generate_process_function (t_service
* tservice
, t_function
* tfunction
);
73 * Serialization constructs
76 void generate_deserialize_field (std::ofstream
&out
,
78 std::string prefix
="",
81 void generate_deserialize_struct (std::ofstream
&out
,
83 std::string prefix
="");
85 void generate_deserialize_container (std::ofstream
&out
,
87 std::string prefix
="");
89 void generate_deserialize_set_element (std::ofstream
&out
,
91 std::string prefix
="");
93 void generate_deserialize_map_element (std::ofstream
&out
,
95 std::string prefix
="");
97 void generate_deserialize_list_element (std::ofstream
&out
,
99 std::string prefix
="");
101 void generate_serialize_field (std::ofstream
&out
,
103 std::string prefix
="");
105 void generate_serialize_struct (std::ofstream
&out
,
107 std::string prefix
="");
109 void generate_serialize_container (std::ofstream
&out
,
111 std::string prefix
="");
113 void generate_serialize_map_element (std::ofstream
&out
,
118 void generate_serialize_set_element (std::ofstream
&out
,
122 void generate_serialize_list_element (std::ofstream
&out
,
127 * Helper rendering functions
130 std::string
py_autogen_comment();
131 std::string
py_imports();
132 std::string
render_includes();
133 std::string
render_fastbinary_includes();
134 std::string
declare_field(t_field
* tfield
);
135 std::string
type_name(t_type
* ttype
);
136 std::string
function_signature(t_function
* tfunction
, std::string prefix
="");
137 std::string
argument_list(t_struct
* tstruct
);
138 std::string
type_to_enum(t_type
* ttype
);
139 std::string
type_to_spec_args(t_type
* ttype
);
141 static std::string
get_real_py_module(const t_program
* program
) {
142 std::string real_module
= program
->get_py_module();
143 if (real_module
.empty()) {
144 return program
->get_name();
152 * True iff we should generate new-style classes.
160 std::ofstream f_types_
;
161 std::ofstream f_consts_
;
162 std::ofstream f_service_
;
164 std::string package_dir_
;