from prestring.go import GoModule m = GoModule() with m.import_group() as im: im("fmt") with m.func("main"): m.stmt('fmt.Println("Hello, 世界")') print(m)
def main(): parser = argparse.ArgumentParser() parser.add_argument("--src", required=True) parser.add_argument("--dst", required=True) args = parser.parse_args() reader = Reader() with open(args.src) as rf: src_world = reader.read_world(json.load(rf, object_pairs_hook=OrderedDict)) src_world.normalize() with open(args.dst) as rf: dst_world = reader.read_world(json.load(rf, object_pairs_hook=OrderedDict)) dst_world.normalize() gencoder = MiniCodeGenerator(TypeMappingResolver([])) m = GoModule() m.package("convert") with m.import_group() as im: iw = ImportWriter(im) convertor = TypeConvertor(iw, gencoder, src_world, dst_world) iw.import_(src_world["model"]) iw.import_(dst_world["def"]) @convertor.as_override("string", src_world["bson"].fulladdress("ObjectId")) def string_to_object_id(convertor, m, value, *args): new_prefix = convertor.import_writer.import_(src_world["bson"]) return "{}.ObjectIdHex({})".format(new_prefix, value) @convertor.as_override(src_world["bson"].fulladdress("ObjectId"), "string") def object_id_to_string(convertor, m, value, *args): return "{}.Hex()".format(value) @convertor.as_override(src_world["model"].fulladdress("Date"), "time.Time") def model_date_to_time(convertor, m, value, *args): return "{}.Time()".format(value) @convertor.as_override(src_world["bson"].fulladdress("ObjectId"), ("pointer", dst_world["def"].fulladdress("ID"))) def object_id_to_id(convertor, m, value, *args): return "ConvertID({})".format(value) m.sep() cw = ConvertWriter(m, convertor) for module in dst_world.modules.values(): for file in module.files.values(): for struct in file.structs.values(): for module in src_world.modules.values(): if struct.name in module: print("@", struct.name, file=sys.stderr) cw.write_function(module[struct.name], struct) elif struct.name.startswith("Enduser") and struct.name[len("Enduser"):] in module: print("<", struct.name, file=sys.stderr) cw.write_function(module[struct.name[len("Enduser"):]], struct) elif struct.name.startswith("Tuner") and struct.name[len("Tuner"):] in module: print(">", struct.name, file=sys.stderr) cw.write_function(module[struct.name[len("Tuner"):]], struct) # cw.write_function(src_world["model"]["Page"], dst_world["def"]["Page"]) # # cw.write_function(dst_world["def"]["Page"], src_world["model"]["Page"]) # cw.write_function(src_world["model"]["User"], dst_world["def"]["User"]) print(m)