home *** CD-ROM | disk | FTP | other *** search
- /* Copyright (c) 1995 by Lele Gaifax. All Rights Reserved
- *
- * $Source: /LocalDeveloper/cvs/net/psa/pyobjc/Modules/nsdefaults.c,v $
- * $Revision: 1.3 $
- * $Date: 1996/10/03 18:32:48 $
- *
- */
-
-
- #include "Python.h"
- #include "modsupport.h" /* For getargs() etc. */
- // #include <appkit/Application.h> /* For NXHomeDirectory() */
- extern const char *NXHomeDirectory(void); /* let's use gcc! */
-
- #include <defaults/defaults.h>
- #include <db/db.h>
-
- static PyObject * NsdefaultsError;
-
- /* Given a Python dictionary, allocate a NXDefaultVector with the same
- number of elements plus one for the ending null ''sentinel'' and
- initialize it with the content of the dictionary.
-
- Elements of the array point to the string in the dictionary, so
- it's not a good idea use it after VALUES has been DECREFed.
-
- Both keys and values must be string, otherwise NULL is returned.
-
- Returns the new NXDefaultVector. */
-
- static struct _NXDefault *
- build_vector_from_dict (PyObject * values)
- {
- struct _NXDefault * vector;
- int pos;
- int idx;
- PyObject * key;
- PyObject * value;
-
- vector = PyMem_NEW(struct _NXDefault, PyDict_Size (values) + 1);
- idx = pos = 0;
- while (PyDict_Next (values, &pos, &key, &value))
- {
- char * n = PyString_AsString (key);
- char * v = PyString_AsString (value);
-
- if (!n || !v)
- {
- PyMem_DEL (vector);
- PyErr_SetString (NsdefaultsError, "Keys and values must be string");
- return NULL;
- }
-
- vector[idx].name = n;
- vector[idx].value = v;
-
- idx++;
- }
- vector[idx].name = vector[idx].value = 0;
- return vector;
- }
-
- static char register_defaults_doc[] =
- "Register default parameters in the cache.\n\
- \n\
- Arguments:\n\
- OWNER the name of the owner of these parameters\n\
- PARAMS a dictionary containing the parameters with their\n\
- default values\n\
- \n\
- Returns:\n\
- A boolean value, TRUE on succeding operation, FALSE if something goes\n\
- wrong.";
-
- static PyObject *
- register_defaults (PyObject * self, PyObject * args)
- {
- const char * owner;
- PyObject * values;
-
- if (PyArg_ParseTuple (args, "sO!;a string and a dictionary", &owner, &PyDict_Type, &values))
- {
- struct _NXDefault * vector = build_vector_from_dict (values);
- int status;
-
- if (!vector)
- return NULL;
-
- if (! (status = NXRegisterDefaults (owner, vector)))
- PyErr_SetString (NsdefaultsError, "could't open the default database");
-
- PyMem_DEL(vector);
-
- if (! status)
- {
- Py_INCREF (Py_False);
- return Py_False;
- }
- else
- {
- Py_INCREF (Py_True);
- return Py_True;
- }
- }
- else
- return NULL;
- }
-
- static char get_default_value_doc[] =
- "Look in the cache for a parameter and return its value if found.\n\
- \n\
- Arguments:\n\
- OWNER the name of the owner of the parameter\n\
- PARAM the name of the paramenter\n\
- \n\
- Returns:\n\
- A string representing the value of the parameter if it is found, None\n\
- otherwise.";
-
- static PyObject *
- get_default_value (PyObject * self, PyObject * args)
- {
- char * owner;
- char * name;
-
- if (PyArg_ParseTuple (args, "ss;two strings", &owner, &name))
- {
- const char * value = NXGetDefaultValue (owner, name);
-
- if (value)
- {
- return Py_BuildValue ("s", value);
- }
- else
- {
- Py_INCREF(Py_None);
- return Py_None;
- }
- }
- else
- return NULL;
- }
-
- static char read_default_doc[] =
- "Look in the Default Database for a parameter and return its value if found.\n\
- \n\
- Arguments:\n\
- OWNER the name of the owner of the parameter\n\
- PARAM the name of the paramenter\n\
- \n\
- Returns:\n\
- A string representing the value of the parameter if it is found, None\n\
- otherwise.";
-
- static PyObject *
- read_default (PyObject * self, PyObject * args)
- {
- char * owner;
- char * name;
-
- if (PyArg_ParseTuple (args, "ss;two strings", &owner, &name))
- {
- const char * value = NXReadDefault (owner, name);
-
- if (value)
- {
- return Py_BuildValue ("s", value);
- }
- else
- {
- Py_INCREF(Py_None);
- return Py_None;
- }
- }
- else
- return NULL;
- }
-
- static char remove_default_doc[] =
- "Remove from the Default Database a parameter.\n\
- \n\
- Arguments:\n\
- OWNER the name of the owner of the parameter\n\
- PARAM the name of the parameter\n\
- \n\
- Returns:\n\
- A boolean value, TRUE on succeding operation, FALSE if something goes\n\
- wrong.";
-
- static PyObject *
- remove_default (PyObject * self, PyObject * args)
- {
- char * owner;
- char * name;
-
- if (PyArg_ParseTuple (args, "ss;two strings", &owner, &name))
- {
- int status = NXRemoveDefault (owner, name);
-
- if (status == 0)
- {
- PyErr_SetString (NsdefaultsError, "couldn't remove the value");
- Py_INCREF (Py_False);
- return Py_False;
- }
- else
- {
- Py_INCREF (Py_True);
- return Py_True;
- }
- }
- else
- return NULL;
- }
-
- static char set_default_doc[] =
- "Store a default parameters with its value in the cache.\n\
- \n\
- Arguments:\n\
- OWNER the name of the owner of this parameter\n\
- PARAM the name of the parameter\n\
- VALUE its value\n\
- \n\
- Returns:\n\
- A boolean value, TRUE on succeding operation, FALSE if something goes\n\
- wrong.";
-
- static PyObject *
- set_default (PyObject * self, PyObject * args)
- {
- char * owner;
- char * name;
- char * value;
-
- if (PyArg_ParseTuple (args, "sss;three strings", &owner, &name, &value))
- {
- int status = NXSetDefault (owner, name, value);
-
- if (status == 0)
- {
- PyErr_SetString (NsdefaultsError, "couldn't set the value");
- Py_INCREF (Py_False);
- return Py_False;
- }
- else
- {
- Py_INCREF (Py_True);
- return Py_True;
- }
- }
- else
- return NULL;
- }
-
- static char update_default_doc[] =
- "Update a default parameter in the cache from the Default Database.\n\
- \n\
- Arguments:\n\
- OWNER the name of the owner of this parameter\n\
- PARAM the name of the parameter\n\
- \n\
- Returns:\n\
- A string representing the previous value of the parameter if found,\n\
- None otherwise.";
-
- static PyObject *
- update_default (PyObject * self, PyObject * args)
- {
- char * owner;
- char * name;
-
- if (PyArg_ParseTuple (args, "ss;two strings", &owner, &name))
- {
- const char * value = NXUpdateDefault (owner, name);
-
- if (value)
- {
- return Py_BuildValue ("s", value);
- }
- else
- {
- Py_INCREF(Py_None);
- return Py_None;
- }
- }
- else
- return NULL;
- }
-
- static char update_defaults_doc[] =
- "Update a the cache re-reading the entire Default Database.\n\
- \n\
- Arguments:\n\
- None\n\
- \n\
- Returns:\n\
- None";
-
- static PyObject *
- update_defaults (PyObject * self, PyObject * args)
- {
- NXUpdateDefaults ();
-
- Py_INCREF(Py_None);
- return Py_None;
- }
-
- static char write_default_doc[] =
- "Store a default parameters with its value in the Default Database.\n\
- \n\
- Arguments:\n\
- OWNER the name of the owner of this parameter\n\
- PARAM the name of the parameter\n\
- VALUE its value\n\
- \n\
- Returns:\n\
- A boolean value, TRUE on succeding operation, FALSE if something goes\n\
- wrong.";
-
- static PyObject *
- write_default (PyObject * self, PyObject * args)
- {
- char * owner;
- char * name;
- char * value;
-
- if (PyArg_ParseTuple (args, "sss;three strings", &owner, &name, &value))
- {
- int status = NXWriteDefault (owner, name, value);
-
- if (status == 0)
- {
- PyErr_SetString (NsdefaultsError, "couldn't write the value");
- Py_INCREF (Py_False);
- return Py_False;
- }
- else
- {
- Py_INCREF (Py_True);
- return Py_True;
- }
- }
- else
- return NULL;
- }
-
- static char write_defaults_doc[] =
- "Write a set of parameters and their values into the Default Database.\n\
- \n\
- Arguments:\n\
- OWNER the name of the owner of these parameters\n\
- PARAMS a dictionary containing the parameters with their\n\
- default values\n\
- \n\
- Returns:\n\
- An integer value representing the number of succesfully written parameters";
-
- static PyObject *
- write_defaults (PyObject * self, PyObject * args)
- {
- const char * owner;
- PyObject * values;
-
- if (PyArg_ParseTuple (args, "sO!;a string and a dictionary", &owner, &PyDict_Type, &values))
- {
- struct _NXDefault * vector = build_vector_from_dict (values);
- int written;
-
- if (!vector)
- return NULL;
-
- written = NXWriteDefaults (owner, vector);
-
- PyMem_DEL(vector);
-
- return Py_BuildValue ("i", written);
- }
- else
- return NULL;
- }
-
- static char set_defaults_user_doc[] =
- "Set the name of the user whose database you want to use by subsequent calls to these functions.\n\
- \n\
- Arguments:\n\
- USER the name of the user\n\
- \n\
- Returns:\n\
- A string containing the name of the previous user.";
-
- static PyObject *
- set_defaults_user (PyObject * self, PyObject * args)
- {
- char * user;
-
- if (PyArg_ParseTuple (args, "s;a string", &user))
- {
- const char * value = NXSetDefaultsUser (user);
-
- if (value)
- {
- return Py_BuildValue ("s", value);
- }
- else
- {
- Py_INCREF(Py_None);
- return Py_None;
- }
- }
- else
- return NULL;
- }
-
- static char defaults_doc[] =
- "Build a dictionary corresponding to the current Defaults database content.\n\
- Does not honour NXSetDefaultsUser() setting, always reads .NeXT/.NeXTdefaults\n\
- in the current user's home directory.\n\
- \n\
- Arguments:\n\
- OWNER optional name of the owner. If not given, fetch all entries.\n\
- \n\
- Returns:\n\
- A dictionary containing name and value of each default of OWNER if given,\n\
- otherwise a dictionary of dictionaries, one for each owner";
-
- static PyObject *
- defaults (PyObject * self, PyObject * args)
- {
- char *ownern = NULL;
-
- if (PyArg_ParseTuple (args, "|s;a string", &ownern))
- {
- #define DEFDBNAME "/.NeXT/.NeXTdefaults"
- const char *userhome = NXHomeDirectory();
- char dbname[strlen (userhome) + sizeof DEFDBNAME];
- Database *db;
-
- strcpy (dbname, userhome);
- strcat (dbname, DEFDBNAME);
-
- if ((db = dbOpen (dbname)))
- {
- Data data;
- char key[1024], content[1024];
- PyObject *dict;
-
- data.k.s = key;
- data.c.s = content;
-
- dict = PyDict_New();
- if (!dict)
- return NULL;
-
- for (dbFirst (db, &data); dbGet (db, &data); dbNext (db, &data))
- {
- char *o = key;
- register char *def = key;
-
- key[data.k.n] = '\0';
- content[data.c.n] = '\0';
-
- while (*def != '\377')
- def++;
-
- *def = 0;
- def++;
-
- if (!ownern || !strcmp (o, ownern))
- {
- PyObject *pydef = PyString_FromString (def);
- PyObject *pyval = PyString_FromString (content);
-
- if (!pydef || !pyval)
- {
- Py_XDECREF(pydef);
- Py_XDECREF(pyval);
- Py_DECREF(dict);
-
- return NULL;
- }
-
- if (ownern)
- {
- PyDict_SetItem (dict, pydef, pyval);
- }
- else
- {
- PyObject *ownerd = PyDict_GetItemString (dict, o);
-
- if (!ownerd)
- {
- PyErr_Clear();
- ownerd = PyDict_New();
- PyDict_SetItemString (dict, o, ownerd);
- Py_DECREF(ownerd);
- }
-
- PyDict_SetItem (ownerd, pydef, pyval);
- }
-
- Py_DECREF(pydef);
- Py_DECREF(pyval);
- }
- }
-
- return dict;
- }
- else
- {
- #define ERRMSG "Can't open `%s'"
- char errmsg[strlen (dbname) + sizeof ERRMSG];
-
- sprintf (errmsg, ERRMSG, dbname);
- PyErr_SetString (NsdefaultsError, errmsg);
- #undef ERRMSG
- }
- }
-
- return NULL;
- }
-
- /* List of functions defined in the module */
-
- static PyMethodDef nsdefaults_methods[] = {
- { "registerDefaults", (PyCFunction) register_defaults, METH_VARARGS, register_defaults_doc },
- { "getDefaultValue", (PyCFunction) get_default_value, METH_VARARGS, get_default_value_doc },
- { "readDefault", (PyCFunction) read_default, METH_VARARGS, read_default_doc },
- { "removeDefault", (PyCFunction) remove_default, METH_VARARGS, remove_default_doc },
- { "setDefault", (PyCFunction) set_default, METH_VARARGS, set_default_doc },
- { "updateDefault", (PyCFunction) update_default, METH_VARARGS, update_default_doc },
- { "updateDefaults", (PyCFunction) update_defaults, METH_VARARGS, update_defaults_doc },
- { "writeDefault", (PyCFunction) write_default, METH_VARARGS, write_default_doc },
- { "writeDefaults", (PyCFunction) write_defaults, METH_VARARGS, write_defaults_doc },
- { "setDefaultsUser", (PyCFunction) set_defaults_user, METH_VARARGS, set_defaults_user_doc },
- { "defaults", (PyCFunction) defaults, METH_VARARGS, defaults_doc },
- { 0, 0, 0, 0 } /* sentinel */
- };
-
- static char nsdefaults_doc[] =
- "Wrapper around the NXDefault facility on NeXTSTEP";
-
- void
- initnsdefaults()
- {
- PyObject *m, *d;
-
- /* Create the module and add the functions */
- m = Py_InitModule4 ("nsdefaults", nsdefaults_methods, nsdefaults_doc, NULL, PYTHON_API_VERSION);
-
- /* Add some symbolic constants to the module */
- d = PyModule_GetDict(m);
- NsdefaultsError = PyString_FromString ("nsdefaults.error");
- PyDict_SetItemString (d, "error", NsdefaultsError);
-
- /* Check for errors */
- if (PyErr_Occurred())
- Py_FatalError ("can't initialize module nsdefaults");
- }
-