blob: 4732561cc370ea4093ce7c2f582dbf3f5606d28f [file] [log] [blame]
// Copyright (c) 2006- Facebook
// Distributed under the Thrift Software License
//
// See accompanying file LICENSE or visit the Thrift site at:
// http://developers.facebook.com/thrift/
#ifndef T_PY_GENERATOR_H
#define T_PY_GENERATOR_H
#include <string>
#include <fstream>
#include <iostream>
#include <vector>
#include "t_generator.h"
/**
* Python code generator.
*
* @author Mark Slee <mcslee@facebook.com>
*/
class t_py_generator : public t_generator {
public:
t_py_generator(t_program* program, bool gen_newstyle) :
t_generator(program),
gen_newstyle_(gen_newstyle) {
out_dir_base_ = "gen-py";
}
/**
* Init and close methods
*/
void init_generator();
void close_generator();
/**
* Program-level generation functions
*/
void generate_typedef (t_typedef* ttypedef);
void generate_enum (t_enum* tenum);
void generate_const (t_const* tconst);
void generate_struct (t_struct* tstruct);
void generate_xception (t_struct* txception);
void generate_service (t_service* tservice);
std::string render_const_value(t_type* type, t_const_value* value);
/**
* Struct generation code
*/
void generate_py_struct(t_struct* tstruct, bool is_exception);
void generate_py_struct_definition(std::ofstream& out, t_struct* tstruct, bool is_xception=false, bool is_result=false);
void generate_py_struct_reader(std::ofstream& out, t_struct* tstruct);
void generate_py_struct_writer(std::ofstream& out, t_struct* tstruct);
void generate_py_function_helpers(t_function* tfunction);
/**
* Service-level generation functions
*/
void generate_service_helpers (t_service* tservice);
void generate_service_interface (t_service* tservice);
void generate_service_client (t_service* tservice);
void generate_service_remote (t_service* tservice);
void generate_service_server (t_service* tservice);
void generate_process_function (t_service* tservice, t_function* tfunction);
/**
* Serialization constructs
*/
void generate_deserialize_field (std::ofstream &out,
t_field* tfield,
std::string prefix="",
bool inclass=false);
void generate_deserialize_struct (std::ofstream &out,
t_struct* tstruct,
std::string prefix="");
void generate_deserialize_container (std::ofstream &out,
t_type* ttype,
std::string prefix="");
void generate_deserialize_set_element (std::ofstream &out,
t_set* tset,
std::string prefix="");
void generate_deserialize_map_element (std::ofstream &out,
t_map* tmap,
std::string prefix="");
void generate_deserialize_list_element (std::ofstream &out,
t_list* tlist,
std::string prefix="");
void generate_serialize_field (std::ofstream &out,
t_field* tfield,
std::string prefix="");
void generate_serialize_struct (std::ofstream &out,
t_struct* tstruct,
std::string prefix="");
void generate_serialize_container (std::ofstream &out,
t_type* ttype,
std::string prefix="");
void generate_serialize_map_element (std::ofstream &out,
t_map* tmap,
std::string kiter,
std::string viter);
void generate_serialize_set_element (std::ofstream &out,
t_set* tmap,
std::string iter);
void generate_serialize_list_element (std::ofstream &out,
t_list* tlist,
std::string iter);
/**
* Helper rendering functions
*/
std::string py_autogen_comment();
std::string py_imports();
std::string render_includes();
std::string render_fastbinary_includes();
std::string declare_field(t_field* tfield);
std::string type_name(t_type* ttype);
std::string function_signature(t_function* tfunction, std::string prefix="");
std::string argument_list(t_struct* tstruct);
std::string type_to_enum(t_type* ttype);
std::string type_to_spec_args(t_type* ttype);
static std::string get_real_py_module(const t_program* program) {
std::string real_module = program->get_py_module();
if (real_module.empty()) {
return program->get_name();
}
return real_module;
}
private:
/**
* True iff we should generate new-style classes.
*/
bool gen_newstyle_;
/**
* File streams
*/
std::ofstream f_types_;
std::ofstream f_consts_;
std::ofstream f_service_;
std::string package_dir_;
};
#endif