From 4f67d228a6c8207e7dda7623a1bf664dac01b378 Mon Sep 17 00:00:00 2001 From: "eduardo.davila@creatis.insa-lyon.fr" Date: Thu, 16 Nov 2023 17:17:37 +0100 Subject: [PATCH] Clean code for Python --- kernel/appli/bbs2cpp/bbs2.cxx | 37 +++++++++++++--------------- kernel/src/bbtkInterpreterPython.cxx | 24 +++++++++++++----- 2 files changed, 35 insertions(+), 26 deletions(-) diff --git a/kernel/appli/bbs2cpp/bbs2.cxx b/kernel/appli/bbs2cpp/bbs2.cxx index 184b48c..2b881d2 100644 --- a/kernel/appli/bbs2cpp/bbs2.cxx +++ b/kernel/appli/bbs2cpp/bbs2.cxx @@ -55,7 +55,7 @@ int main(int argc, char* argv[]) { if (argc<3) { - printf("bbs2 \n"); + printf("bbs2 \n"); return 1; } @@ -66,12 +66,15 @@ int main(int argc, char* argv[]) // file = fileBase + std::string(".") + extention; std::string fileBase,path; - std::string extention( argv[2] ); - fileBase = bbtk::Utilities::ExtractScriptName(argv[1],path); - std::string file(path+"/"+fileBase+"."+extention); +//EED 2023-11-15 +// std::string extention( argv[3] ); +// fileBase = bbtk::Utilities::ExtractScriptName(argv[1],path); +// std::string file(path+"/"+fileBase+"."+extention); + std::string file( argv[2] ); printf("file %s \n",file.c_str() ); - if ( extention.compare("cxx")==0) + std::string extentionCxx = file.substr( file.length()-3, 3); + if ( extentionCxx.compare(".cxx")==0) { printf("bbs -> cxx\n"); bbtk::Interpreter::Pointer I = bbtk::Interpreter::New(file); @@ -84,7 +87,8 @@ int main(int argc, char* argv[]) } // try } // if cxx - if ( extention.compare("py")==0) + std::string extentionPy = file.substr( file.length()-2, 2); + if ( extentionPy.compare("py")==0) { printf("bbs -> py\n"); bbtk::InterpreterPython::Pointer I = bbtk::InterpreterPython::New(); @@ -94,28 +98,19 @@ int main(int argc, char* argv[]) I->InterpretFile(argv[1]); std::vector pythonBBTK= ((bbtk::InterpreterPython*)(I.get()))->pythonBBTK ; FILE *ff=fopen(file.c_str(),"w+"); - fprintf(ff,"\n" ); - fprintf(ff,"from bbtk.bbtkBlackBox import *\n" ); - fprintf(ff,"\n" ); - fprintf(ff,"class %s(ComplexBlackBox):\n",fileBase.c_str() ); - fprintf(ff,"\n" ); - fprintf(ff," def __init__(self, nameBox):\n" ); - fprintf(ff," super().__init__(nameBox)\n" ); - fprintf(ff,"\n" ); - fprintf(ff," def Run(self):\n" ); int i,size=pythonBBTK.size(); for (i=0; i js\n"); bbtk::InterpreterJavaScript::Pointer I = bbtk::InterpreterJavaScript::New(); @@ -126,9 +121,11 @@ int main(int argc, char* argv[]) std::vector javascriptBBTK= ((bbtk::InterpreterJavaScript*)(I.get()))->javascriptBBTK ; FILE *ff=fopen(file.c_str(),"w+"); fprintf(ff,"import * as bbtk from './bbtk.js' \n" ); - fprintf(ff,"export class %s{\n",fileBase.c_str() ); +//clean this code +// fprintf(ff,"export class %s{\n",fileBase.c_str() ); fprintf(ff," Run(){\n" ); - fprintf(ff," let mCBjs = new bbtk.ComplexBlackBox('%s')\n",fileBase.c_str()); +//clean this code +// fprintf(ff," let mCBjs = new bbtk.ComplexBlackBox('%s')\n",fileBase.c_str()); int i,size=javascriptBBTK.size(); for (i=0; ipythonBBTK.push_back(code); } //========================================================================= @@ -88,7 +88,7 @@ namespace bbtk const std::string &input) // virtual { //ex: mCB.Connection( "Box10" , "Out", "Box11", "In") - std::string code("Connection('"+boxfrom+"','"+output+"','"+boxto+"','"+input+"')"); + std::string code(" self.Connection('"+boxfrom+"','"+output+"','"+boxto+"','"+input+"')"); this->pythonBBTK.push_back(code); } //========================================================================= @@ -96,7 +96,7 @@ namespace bbtk //========================================================================= void InterpreterPython::commandInput(const std::string &name,const std::string &box,const std::string &input,const std::string &help) { - std::string code("DECLARE_INPUT('"+name+"','"+box+"."+input+"')"); + std::string code(" self.DECLARE_INPUT('"+name+"','"+box+"."+input+"')"); this->pythonBBTK.push_back(code); } //========================================================================= @@ -104,7 +104,7 @@ namespace bbtk //========================================================================= void InterpreterPython::commandOutput(const std::string &name,const std::string &box,const std::string &output,const std::string &help) { - std::string code("DECLARE_OUTPUT('"+name+"','"+box+"."+output+"')"); + std::string code(" self.DECLARE_OUTPUT('"+name+"','"+box+"."+output+"')"); this->pythonBBTK.push_back(code); } //========================================================================= @@ -114,7 +114,7 @@ namespace bbtk void InterpreterPython::commandSet(const std::string &box,const std::string &input,const std::string &value) // virtual { //ex: mCB.Set("Box10","In2","/hola.mhd") - std::string code("Set('"+box+"','"+input+"','"+value+"')"); + std::string code(" self.Set('"+box+"','"+input+"','"+value+"')"); this->pythonBBTK.push_back(code); } //========================================================================= @@ -122,6 +122,18 @@ namespace bbtk //========================================================================= void InterpreterPython::commandDefine(const std::string &name,const std::string &pack,const std::string &scriptfilename) // virtual { + + this->pythonBBTK.push_back(" "); + this->pythonBBTK.push_back("from bbtk.bbtkBlackBox import *"); + this->pythonBBTK.push_back(" "); + this->pythonBBTK.push_back("class "+pack+"_"+name+"(ComplexBlackBox):"); + this->pythonBBTK.push_back(" "); + this->pythonBBTK.push_back(" def __init__(self, nameBox):"); + this->pythonBBTK.push_back(" super().__init__(nameBox)"); + this->pythonBBTK.push_back(" self.Init()"); + this->pythonBBTK.push_back(" "); + this->pythonBBTK.push_back(" def Init(self):"); + } //========================================================================= @@ -137,7 +149,7 @@ namespace bbtk void InterpreterPython::commandExec(const std::string &word) // virtual { //Ex: mCB.AddToExecutableLst("Box13") - std::string code("AddToExecutableLst('"+word+"')"); + std::string code(" self.AddToExecutableLst('"+word+"')"); this->pythonBBTK.push_back(code); } //========================================================================= -- 2.47.1