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;
}
예제 #2
0
파일: WlzSAToWlz.c 프로젝트: dscho/Woolz
int		main(int argc, char *argv[])
{
  FILE		*inFile, *outFile;
  char 		optList[] = "hv";
  int		option;
  int		verboseFlg=0;
  WlzObject	*obj;
  WlzDomain	domain;
  WlzValues	values;
  WlzBoundList	*tmpBnd;
  char		*name;
  AlcDLPList	*bndDLPList;
  AlcDLPItem	*bndItem;
  NamedBndItem	*namedBndItem;
  int		noNameCntr=0;
  char		noNameBuf[32];

  /* read the argument list and check for an input file */
  opterr = 0;
  while( (option = getopt(argc, argv, optList)) != EOF ){
    switch( option ){

    case 'v':
      verboseFlg = 1;
      break;

    case 'h':
      usage(argv[0]);
      return WLZ_ERR_NONE;

    default:
      usage(argv[0]);
      return WLZ_ERR_UNSPECIFIED;
    }
  }

  inFile = stdin;
  if( optind < argc ){
    if( (inFile = fopen(*(argv+optind), "r")) == NULL ){
      fprintf(stderr, "%s: can't open file %s\n", argv[0], *(argv+optind));
      usage(argv[0]);
      return( 1 );
    }
  }

  /* read XML file building boundary lists */
  /* quick fix read  - read until no new names */
  bndDLPList = AlcDLPListNew(NULL);
  name = NULL;
  while((name = getNextName(inFile)) != NULL){
    /* check for empty name */
    if( strcmp(name, " ") == 0 ){
      noNameCntr++;
      sprintf(noNameBuf, "NoName_%03d", noNameCntr);
      name = AlcStrDup(noNameBuf);
    }
    /* convert comma to underscore */
    tmpBnd = getNextBoundary(inFile);
    addToBndList(bndDLPList, name, tmpBnd);
  }

  /* write boundary lists */
  bndItem = bndDLPList->head;
  while( bndItem ){
    namedBndItem = (NamedBndItem *) bndItem->entry;
    name = (char *) AlcMalloc(sizeof(char)*(strlen(namedBndItem->name)+16));
    sprintf(name, "%s.wlz", namedBndItem->name);
    if((outFile = fopen(name, "w")) != NULL){
      domain.b = namedBndItem->bnd;
      values.core = NULL;
      obj = WlzMakeMain(WLZ_BOUNDLIST, domain, values, NULL, NULL, NULL);
      WlzWriteObj(outFile, obj);
      WlzFreeObj(obj);
      fclose(outFile);
    }
    AlcFree(name);
    bndItem = bndItem->next;
    if( bndItem == bndDLPList->head ){
      break;
    }
  }

  exit(0);
}