template

import lison_source


def title(cmd):
    return "-module(qx"+str(cmd)+"json2record).\n"\
           +"-include(\"../include/webnet.hrl\").\n"\
           +"-include(\"../include/qx"+str(cmd)+".hrl\").\n"\
           +"-compile([export_all]).\n"\
           +"-import(webnetCommon, [binaryJoin/2, makeQxHead/6, str2Int/2, loop/5, padding/2]).\n"


def parse(cmd):
    fun_name = "parseJsonStr(JsonObj)->\n"\
               +"   {_, Item} = JsonObj,\n"\
               +"   Layer0List = dict:fetch(\"mcu_count\", dict:from_list(Item)),\n"\
               +"   [16#"+str(cmd)+",#qx"+str(cmd)+"msgbodylayer0\n"\
               +"   {\n"\
               +"      cmdCode = padding(\"cmd_code\", Item),\n"\
               +"      mcuCount = length(Layer0List),\n"\
               +"      layer1Msg = loop(0 ,length(Layer0List) ,Layer0List ,{} ,fun func1/2)\n"\
               +"   }].\n"
               
    return fun_name


def fun1(cmd):
    fun1 = "func1(Index, List)->\n"\
           +"   {_, Item} = lists:nth(Index, List),\n"\
           +"   Layer1List = dict:fetch(\"mcu_count\", dict:from_list(Item)),\n"\
           +"   #qx"+str(cmd)+"msgbodylayer1\n"\
           +"   {\n"\
           +"      mcuAddr = padding(\"brd_addr\", Item),\n"\
           +"      checkPoints = length(Layer1List),\n"\
           +"      layer2Msg = loop(0 ,length(Layer1List) ,Layer1List ,{} ,fun func2/2)\n"\
           +"   }.\n"
    return fun1


def fun2(cmd):
    fun2 = "func2(Index, List)->\n"\
           +"   {_, Item} = lists:nth(Index, List),\n"\
           +"   #qx"+str(cmd)+"msgbodylayer2\n"\
           +"   {\n"\
           
    return fun2
    
def getContent():
    content =""
    for i in range(len(lison_source.source)):
       content += "      "+lison_source.source[i]\
                  +" = padding("\
                  +"\""+lison_source.json_name[i]+"\""+",Item),\n"
    return content+"   }"






def main():
    cmd = input("输入命令码:")
    count = input("输入层数:")
    file_name = "qx"+str(cmd)+"json2record.erl"
    f = open(file_name,"w")
    Out = title(cmd)+parse(cmd)+fun1(cmd)+fun2(cmd)+getContent()
    f.write(Out)
    f.close()
    
if __name__ == '__main__':
    main()

你可能感兴趣的:(硬件)