PyMapNode.C 9.18 KB
Newer Older
1 2
/*****************************************************************************
*
brugger's avatar
 
brugger committed
3
* Copyright (c) 2000 - 2011, Lawrence Livermore National Security, LLC
4
* Produced at the Lawrence Livermore National Laboratory
brugger's avatar
 
brugger committed
5
* LLNL-CODE-442911
6 7
* All rights reserved.
*
brugger's avatar
 
brugger committed
8
* This file is  part of VisIt. For  details, see https://visit.llnl.gov/.  The
9 10 11 12 13 14 15 16 17 18
* full copyright notice is contained in the file COPYRIGHT located at the root
* of the VisIt distribution or at http://www.llnl.gov/visit/copyright.html.
*
* Redistribution  and  use  in  source  and  binary  forms,  with  or  without
* modification, are permitted provided that the following conditions are met:
*
*  - Redistributions of  source code must  retain the above  copyright notice,
*    this list of conditions and the disclaimer below.
*  - Redistributions in binary form must reproduce the above copyright notice,
*    this  list of  conditions  and  the  disclaimer (as noted below)  in  the
brugger's avatar
 
brugger committed
19 20 21
*    documentation and/or other materials provided with the distribution.
*  - Neither the name of  the LLNS/LLNL nor the names of  its contributors may
*    be used to endorse or promote products derived from this software without
22 23 24 25 26
*    specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT  HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR  IMPLIED WARRANTIES, INCLUDING,  BUT NOT  LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND  FITNESS FOR A PARTICULAR  PURPOSE
brugger's avatar
 
brugger committed
27 28 29
* ARE  DISCLAIMED. IN  NO EVENT  SHALL LAWRENCE  LIVERMORE NATIONAL  SECURITY,
* LLC, THE  U.S.  DEPARTMENT OF  ENERGY  OR  CONTRIBUTORS BE  LIABLE  FOR  ANY
* DIRECT,  INDIRECT,   INCIDENTAL,   SPECIAL,   EXEMPLARY,  OR   CONSEQUENTIAL
30 31 32 33 34 35 36 37 38 39 40 41
* DAMAGES (INCLUDING, BUT NOT  LIMITED TO, PROCUREMENT OF  SUBSTITUTE GOODS OR
* SERVICES; LOSS OF  USE, DATA, OR PROFITS; OR  BUSINESS INTERRUPTION) HOWEVER
* CAUSED  AND  ON  ANY  THEORY  OF  LIABILITY,  WHETHER  IN  CONTRACT,  STRICT
* LIABILITY, OR TORT  (INCLUDING NEGLIGENCE OR OTHERWISE)  ARISING IN ANY  WAY
* OUT OF THE  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*
*****************************************************************************/

#include <PyMapNode.h>
#include <PyVariant.h>
#include <snprintf.h>
42
#include <DebugStream.h>
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58

///////////////////////////////////////////////////////////////////////////////
//
// VisIt module helper method.
//
///////////////////////////////////////////////////////////////////////////////

// ****************************************************************************
// Method: PyMapNode_Wrap
//
// Purpose: 
//   Converts a MapNode to a python dictonary. 
//
// Programmer: Cyrus Harrison
// Creation:   Mon Dec 17 15:20:25 PST 2007
//
brugger's avatar
 
brugger committed
59 60 61 62 63 64
// Modifications:
//   Eric Brugger, Fri Jan 11 09:54:04 PST 2008
//   I added some Python version specific coding to get around a problem
//   with older versions not having a const qualifier for the string argument
//   to PyDict_SetItemString.
//
65 66 67 68 69
// ****************************************************************************

PyObject *
PyMapNode_Wrap(const MapNode &node)
{
70
    if(node.Type() == MapNode::EMPTY_TYPE && node.GetNumEntries() == 0)
71 72 73 74 75 76
    {
        Py_INCREF(Py_None);
        return Py_None;
    }
    
    // must be a variant, use variant helper
77
    if(node.Type() != MapNode::EMPTY_TYPE)
78 79 80 81 82 83 84 85 86
    {
        return PyVariant_Wrap(node);
    }
    
    // we have a dict with map nodes as entries
    PyObject *dict = PyDict_New();
    stringVector entry_names;
    node.GetEntryNames(entry_names);    
    
87
    for(size_t i=0;i<entry_names.size();i++)
88 89 90 91 92
    {
        const MapNode *child_node = node.GetEntry(entry_names[i]);
        if(child_node == NULL)
            continue;
        PyObject *child = PyMapNode_Wrap(*child_node);
brugger's avatar
 
brugger committed
93
#if (PY_MAJOR_VERSION < 2) || ((PY_MAJOR_VERSION == 2) && (PY_MINOR_VERSION < 5))
brugger's avatar
 
brugger committed
94
        char *str = new char[entry_names[i].length()+1];
brugger's avatar
 
brugger committed
95 96 97 98 99 100
        strcpy(str, entry_names[i].c_str());
        PyDict_SetItemString(dict, str, child);   
        delete [] str;
#else
        PyDict_SetItemString(dict, entry_names[i].c_str(), child);   
#endif
101 102 103 104 105
    }

    return dict;
}

106 107 108 109 110 111 112 113 114 115

// ****************************************************************************
// Method: PyDict_To_MapNode
//
// Purpose: 
//   Converts a python Dictionary to a MapNode.
//
// Programmer: Kathleen Bonnell 
// Creation:   July 13, 2011 
//
116 117 118 119
// Modifications:
//   Kathleen Biagas, Wed Sep  7 11:56:23 PDT 2011
//   Allow ints and doubles in same sequence.
//
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
// ****************************************************************************

bool
PyDict_To_MapNode(PyObject *obj, MapNode &mn)
{
    if (!PyDict_Check(obj))
        return false;

    bool success = true;
    Py_ssize_t pos = 0;
    PyObject *key = NULL;
    PyObject *value = NULL;

    while(PyDict_Next(obj, &pos, &key, &value)) 
    {
        std::string mkey;
        if (PyString_Check(key))
            mkey = PyString_AS_STRING(key);
        else
        {
            return false;
        }

        if (PyTuple_Check(value) ||
            PyList_Check(value))
        {
            PyObject *item = PySequence_GetItem(value, 0);
            if (PyFloat_Check(item))
            {
                 doubleVector mval;
                 mval.push_back(PyFloat_AS_DOUBLE(item));
                 for (Py_ssize_t i = 1; i < PySequence_Size(value); ++i)
                 {
                     item = PySequence_GetItem(value, i);
                     if (PyFloat_Check(item))
                         mval.push_back(PyFloat_AS_DOUBLE(item));
                     else if (PyInt_Check(item))
                         mval.push_back((double)PyInt_AS_LONG(item));
                     else 
                     {
                         debug3 << "PyDict_To_MapNode: tuples/lists must "
                                << "contain same type." << endl;
                         return false;
                     }
                 }
                 mn[mkey] = mval;
            }
            else if (PyInt_Check(item))
            {
169
                 int ni = 1, nd = 0, no = 0;
170 171 172
                 for (Py_ssize_t i = 1; i < PySequence_Size(value); ++i)
                 {
                     item = PySequence_GetItem(value, i);
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
                     if (PyFloat_Check(item))
                         nd++;
                     else if (PyInt_Check(item))
                         ni++;
                     else 
                         no++;
                 }
                 if (no != 0)
                 {
                     debug3 << "PyDict_To_MapNode: tuples/lists must "
                            << "contain same type." << endl;
                     return false;
                 }
                 else if (nd != 0)
                 {
                     // process as doubleVector
                     doubleVector mval;
                     mval.push_back((double)PyInt_AS_LONG(item));
                     for (Py_ssize_t i = 1; i < PySequence_Size(value); ++i)
192
                     {
193 194 195 196 197
                         item = PySequence_GetItem(value, i);
                         if (PyFloat_Check(item))
                             mval.push_back(PyFloat_AS_DOUBLE(item));
                         else if (PyInt_Check(item))
                             mval.push_back((double)PyInt_AS_LONG(item));
198
                     }
199
                     mn[mkey] = mval;
200
                 }
201 202 203 204 205 206 207 208 209 210 211
                 else
                 {
                     intVector mval;
                     mval.push_back(PyInt_AS_LONG(item));
                     for (Py_ssize_t i = 1; i < PySequence_Size(value); ++i)
                     {
                         item = PySequence_GetItem(value, i);
                         mval.push_back(PyInt_AS_LONG(item));
                     }
                     mn[mkey] = mval;
                }
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
            }
            else if (PyString_Check(item))
            {
                 stringVector mval;
                 mval.push_back(PyString_AS_STRING(item));
                 for (Py_ssize_t i = 1; i < PySequence_Size(value); ++i)
                 {
                     item = PySequence_GetItem(value, i);
                     if (!PyString_Check(item))
                     {
                         debug3 << "PyDict_To_MapNode: tuples/lists must "
                                << "contain same type." << endl;
                         return false;
                     }
                     mval.push_back(PyString_AS_STRING(item));
                 }
                 mn[mkey] = mval;
            }
            else
            {
                debug3 << "PyDict_To_MapNode: type " 
                       << item->ob_type->tp_name 
                       << " not currently implemented." << endl;
                return false;
            }
        }
        else if (PyFloat_Check(value))
        {
            mn[mkey] = (double) PyFloat_AS_DOUBLE(value); 
        }
        else if (PyInt_Check(value))
        {
            mn[mkey] = (int) PyInt_AS_LONG(value); 
        }
        else if (PyString_Check(value))
        {
            mn[mkey] = (std::string) PyString_AS_STRING(value); 
        }
        else
        {
            debug3 << "PyDict_To_MapNode: type " 
                   << value->ob_type->tp_name 
                   << " not currently implemented." << endl;
            return false;
        }
    }
    return true;
}