bool ProgrammableOpAttributes::SetupPipeline(const JSONNode& atts, stringVector& args, const std::string& parent) { if(atts.GetType() != JSONNode::JSONARRAY) return false; const JSONNode::JSONArray& array = atts.GetArray(); for(int i = 0; i < array.size(); ++i) { /// need key, value pair /// this can be in the form of a dictionary, "a = b", pair tuple (a,b), or a pair array [a,b] JSONNode node = array[i]; JSONNode key,value; if(node.GetType() == JSONNode::JSONARRAY) { if(node.GetArray().size() != 2) continue; key = node.GetArray()[0]; value = node.GetArray()[1]; } else if(node.GetType() == JSONNode::JSONOBJECT) { /// parse through dictionary and compute arguments from names.. const JSONNode::JSONObject& obj = node.GetJsonObject(); if(obj.size() != 1) continue; const JSONNode::JSONObject::const_iterator itr = obj.begin(); key = itr->first; value = itr->second; } else if(node.GetType() == JSONNode::JSONSTRING) { std::string pair = node.GetString(); int index = pair.find("="); if(index == std::string::npos) continue; key = pair.substr(0,index); value = trim(pair.substr(index+1)); } if(key.GetType() != JSONNode::JSONSTRING) continue; std::string keystr = trim(key.GetString()); std::ostringstream str; str << "import json\n"; if(value.GetType() == JSONNode::JSONSTRING) { std::string v = trim(value.GetString()); ///character at 0 and has : if(v.find(":") != std::string::npos && v.find(":") == 0) { /// optionally handle whether it can be as_vtkarray, as_ndarray, or as_rarray size_t index = v.find(":as_ndarray"); if(index == std::string::npos) index = v.find(":as_rarray"); if(index != std::string::npos) { std::string newName = getNextName(); v = v.substr(0,index); AddNode(newName, "as_ndarray"); AddConnection(v, newName, "in"); AddConnection(newName,parent,keystr); } else { index = v.find(":as_vtkarray"); if(index != std::string::npos) v = v.substr(0,index); AddConnection(v,parent,keystr); } } else { std::string escapedCode = trim(value.GetString()); replace(escapedCode,"\n","\\\n"); replace(escapedCode,"'","\""); escapedCode = "'" + escapedCode + "'"; str << "try:\n" << " a = json.loads(" << escapedCode << ")\n" << "except:\n" << " a = " << escapedCode << "\n" << "setout(a)\n"; AddPythonScript(keystr,stringVector(),str.str()); AddNode(keystr,keystr); AddConnection(keystr,parent,keystr); } } else { str << "setout(json.loads('" << trim(value.ToString()) << "'))\n"; AddPythonScript(keystr,stringVector(),str.str()); AddNode(keystr,keystr); AddConnection(keystr,parent,keystr); } args.push_back(keystr); } return true; }