def run(table_name: str, table_fields: dict, output_path: str): """ Run schema file generation Generate schema_name.py file inside schema package :param table_name: name of the table :param table_fields: description of table field (name, type...) :param output_path: path of the output directory :return: generated filename """ logger.info(f"Start {table_name} schema generation") filename = f"{table_name}_schema.py" file_schema = open( output_path + config[CONFIG_ENV].SCHEMA_PACKAGE_PATH + filename, "a") class_declaration = declare_schema_class(table_name) schema_attributes = schema_fields(table_fields) if "datetime" in schema_attributes: imports = format_imports(pydantic_import(), datetime_import()) else: imports = format_imports(pydantic_import()) content = imports + class_declaration + schema_fields( table_fields) + orm_mode() file_schema.write(content) file_schema.close() return filename
def start_api(output_path: str): print(output_path) try: logger.info("Running api... swagger documentation is available at http://0.0.0.0:8080/docs") os.system(f"python {output_path}/app.py") except OSError: logger.error("Fail to start generated api, verify that output path is absolute path") pass
def wrapper(*args): file = generation_function(*args) if file in os.listdir( os.getenv("OUTPUT_PATH") + "/" + package_name): logger.info( f"Successfully create {file} in {package_name} package") return True else: logger.error( f"Fail to create {file} in {package_name} package") return False
def extract_table_structure(json_path: str): """ Extract table structure :param json_path: path of the json describing tables :return: dict mapping table type """ table_description = parse_json(json_path) tables = extract_tables_name(table_description) number_of_tables = len(tables) logger.info("Find {} tables in file {} : {}".format( number_of_tables, json_path, tables)) table_mapped_type = map_dict_type(table_description) return table_mapped_type
def run(self): user_args = self.user_args tables_content = extract_table_structure(user_args.file) # Generate api structure and base files self.create_common_files(tables_content) # Generate crud foreach table describe in input file [self.create_table_files(table, fields) for table, fields in tables_content.items()] logger.info(f"Generation finished, folder has been created at location {user_args.output_dir}") # Start api if user_args.start.lower() == "true": self.start_api(self.output_path) else: logger.warning(f"Parameter start is set to False, api will not start automatically after files generation")
def run(tables_name: list, host: str, port: int, output_path: str): """ Create app.py file, the entrypoint of the api under root directory :param tables_name: name of the table :param host: host of the generated api :param port: port of the generated api :param output_path: path of the output directory :return: generated filename """ logger.info(f"Start app entrypoint generation under {output_path}") filename = "app.py" file_app = open(output_path + filename, "a") imports = format_imports(uvicorn_import(), fastapi_core_import(), db_base_import(True), routers_import(tables_name)) content = imports + fast_setup_content() + include_routers( tables_name) + exec_app(host, port) file_app.write(content) file_app.close() return filename
logger.info("Running api... swagger documentation is available at http://0.0.0.0:8080/docs") os.system(f"python {output_path}/app.py") except OSError: logger.error("Fail to start generated api, verify that output path is absolute path") pass def run(self): user_args = self.user_args tables_content = extract_table_structure(user_args.file) # Generate api structure and base files self.create_common_files(tables_content) # Generate crud foreach table describe in input file [self.create_table_files(table, fields) for table, fields in tables_content.items()] logger.info(f"Generation finished, folder has been created at location {user_args.output_dir}") # Start api if user_args.start.lower() == "true": self.start_api(self.output_path) else: logger.warning(f"Parameter start is set to False, api will not start automatically after files generation") if __name__ == '__main__': logger.info("Start CrudGen {} ...".format(config[CONFIG_ENV].VERSION)) user_args = set_parameters() crudgen = Crudgen(user_args) crudgen.run()