Example #1
0
static PyObject *get_colormap(PyObject *self, PyObject *args) {
  char *name;
  if (!PyArg_ParseTuple(args, (char *)"s", &name))
    return NULL;

  VMDApp *app = get_vmdapp(); 
  int num_names = app->num_color_category_items(name);
  PyObject *newdict = PyDict_New();
  for (int i=0; i<num_names; i++) {
    const char *key = app->color_category_item(name, i);
    const char *value = app->color_mapping(name, key);
    PyDict_SetItemString(newdict, (char *)key, PyString_FromString(value));
  }
  return newdict;
}
Example #2
0
static PyObject* py_get_colormap(PyObject *self, PyObject *args,
                                 PyObject *kwargs)
{
  const char *kwnames[] = {"name", NULL};
  PyObject *newdict = NULL;
  int i, num_names;
  VMDApp *app;
  char *name;

  if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s:color.get_colormap",
                                   (char**) kwnames, &name))
    return NULL;

  if (!(app = get_vmdapp()))
    return NULL;

  num_names = app->num_color_category_items(name);
  if (!num_names) {
    PyErr_Format(PyExc_ValueError, "Colormap '%s' does not exist", name);
    return NULL;
  }

  if (!(newdict = PyDict_New()))
    goto failure;

  // Populate colormap dictionary
  for (i = 0; i < num_names; i++) {
    const char *key = app->color_category_item(name, i);
    const char *value = app->color_mapping(name, key);

    // SetItemString does correctly steal a reference.
    if (PyDict_SetItemString(newdict, key, as_pystring(value)))
      goto failure;
  }
  return newdict;

failure:
  PyErr_Format(PyExc_RuntimeError, "Problem getting colormap '%s'", name);
  Py_XDECREF(newdict);
  return NULL;
}