0001 #!/usr/bin/pscript
0002
0003 userlog("Generate python server");
0004
0005 //Include wizard base.
0006 compilestring(getwizardbase())();
0007
0008 //Provision class
0009 class PyServer extends WizardBase
0010 {
0011 constructor()
0012 {
0013 base.constructor();
0014 }
0015
0016 //Configure it:
0017 </ order=0, name = "XATMI Server Name (script)", type = "string", min=1, max=512 />
0018 svname = "testsv";
0019
0020 </ order=1, name = "Service name", type = "string", min=1, max=30 />
0021 svcname = "TESTSV";
0022
0023 </ order=2, name = "Use UBF?", type = "yn"/>
0024 useubf = "y";
0025
0026 </ order=3, name = "INI File section (optional, will read config if set)",
0027 type = "string", depend="(::prov.useubf==\"y\")", min=0/>
0028 config = "";
0029
0030 pyServerFile = "";
0031
0032 function getOutputFiles()
0033 {
0034 pyServerFile=appHome+"/"+svname+".py";
0035 }
0036
0037 ////////////////////////////////////////////////////////////////////////
0038 //Build Python Server code
0039 ////////////////////////////////////////////////////////////////////////
0040 pyServerVal = "";
0041 function buildServer()
0042 {
0043
0044 pyServerVal =
0045 @"#!/usr/bin/env python3
0046
0047 import sys
0048 import endurox as e
0049 "+(config==""?"":@"
0050 PROGSECTION="""+config+@"""
0051 ")+@"
0052 class Server:
0053
0054 def tpsvrinit(self, args):
0055 e.tplog_info(""Doing server init..."");"+(config!=""?@"
0056 # Read configuration
0057 buf = dict()
0058 buf[""data""]=dict()
0059 buf[""data""][""EX_CC_CMD""]=""g""
0060 buf[""data""][""EX_CC_LOOKUPSECTION""]=""%s/%s"" % (PROGSECTION, e.tuxgetenv(""NDRX_CCTAG""))
0061
0062 tperrno, tpurcode, buf = e.tpcall(""@CCONF"", buf)
0063
0064 if 0!=tperrno:
0065 e.tplog_error(""Failed to get configuration: %d"" % tperrno)
0066 return e.EXFAIL
0067
0068 e.tplogprintubf(e.log_info, ""Got configuration"", buf)
0069
0070 if ""EX_CC_KEY"" in buf[""data""]:
0071 for idx, key in enumerate(buf[""data""][""EX_CC_KEY""]):
0072 e.tplog_info(""Got config key [%s] at index %d""% (key, idx));
0073 if key==""mykey1"":
0074 e.tplog_info(""Got mykey1 key... [%s]"" % buf[""data""][""EX_CC_VALUE""][idx])
0075 elif key==""someparam2"":
0076 e.tplog_info(""Got someparam2 key... [%s]"" % buf[""data""][""EX_CC_VALUE""][idx])
0077 else:
0078 e.tplog_debug(""Unknown parameter [%s]"" % key);
0079 ":@"")+@"
0080 e.tpadvertise("""+svcname+@""", """+svcname+@""", Server."+svcname+@")
0081 return 0
0082
0083 def tpsvrdone(self):
0084 e.tplog_info(""Server shutdown"")
0085
0086 def "+svcname+@"(self, args):"+(useubf=="y"?@"
0087 e.tplogprintubf(e.log_info, ""Incoming request:"", args.data)
0088 args.data[""data""][""T_STRING_2_FLD""]=""Hello World from XATMI server""":"")+@"
0089 return e.tpreturn(e.TPSUCCESS, 0, args.data)
0090
0091 if __name__ == ""__main__"":
0092 e.tprun(Server(), sys.argv)
0093 ";
0094 }
0095 }
0096 ////////////////////////////////////////////////////////////////////////
0097 //Server END
0098 ////////////////////////////////////////////////////////////////////////
0099
0100 //Run the mater installer
0101 function install()
0102 {
0103 local root = getroottable();
0104
0105 //Create a provision object
0106 root["prov"] <- PyServer();
0107
0108 if (!::prov.isDefaulted)
0109 {
0110 ::prov.runInteractive();
0111 }
0112
0113 if (::prov.validatAndPrintConfig())
0114 {
0115 ::prov.getOutputFiles();
0116 ::prov.buildServer();
0117
0118 if (!::prov.writeFile(::prov.pyServerFile, ::prov.pyServerVal))
0119 {
0120 return false;
0121 }
0122
0123 if (!::prov.setExec(::prov.pyServerFile))
0124 {
0125 return false;
0126 }
0127
0128 }
0129 else
0130 {
0131 return false;
0132 }
0133
0134 return true;
0135 }
0136
0137 if (::install())
0138 {
0139 print("Python server gen ok!\n");
0140
0141 return 0;
0142 }
0143 else
0144 {
0145 print("Python server gen failed!\n");
0146 return -1;
0147 }