Back to home page

Enduro/X

 
 

    


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