123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189 |
- #ifndef _PY_NAMESPACE_OBJ_H
- #define _PY_NAMESPACE_OBJ_H
- #define PY_SSIZE_T_CLEAN
- #include <Python.h>
- #include <structmember.h>
- #include "namespace.h"
- typedef struct {
- PyObject_HEAD
- LSUP_NSMap *ob_struct;
- } NSMapObject;
- static int
- NSMap_init (NSMapObject *self, PyObject *args)
- {
- self->ob_struct = LSUP_nsmap_new();
- if (!self->ob_struct) {
- PyErr_SetString (PyExc_SystemError, "Could not create namespace.");
- return -1;
- }
- return 0;
- }
- static void
- NSMap_dealloc (NSMapObject *self)
- {
- LSUP_nsmap_free (self->ob_struct);
- Py_TYPE (self)->tp_free ((PyObject *) self);
- }
- static PyObject *
- NSMap_add (PyObject *self, PyObject *args)
- {
- const char *pfx, *ns;
- if (! PyArg_ParseTuple (args, "ss", &pfx, &ns)) return NULL;
- if (LSUP_nsmap_add (((NSMapObject *)self)->ob_struct, pfx, ns) < 0) {
- PyErr_SetString (PyExc_ValueError, "Error adding namespace.");
- return NULL;
- }
- Py_RETURN_NONE;
- }
- static PyObject *
- NSMap_remove (PyObject *self, PyObject *pfx_obj)
- {
- if (PyUnicode_READY (pfx_obj) < 0) return NULL;
- const char *pfx = PyUnicode_AsUTF8 (pfx_obj);
- if (LSUP_nsmap_remove (((NSMapObject *)self)->ob_struct, pfx) < 0) {
- PyErr_SetString (PyExc_ValueError, "Error removing namespace.");
- return NULL;
- }
- Py_RETURN_NONE;
- }
- static PyObject *
- NSMap_get (PyObject *self, PyObject *pfx_obj)
- {
- if (PyUnicode_READY (pfx_obj) < 0) return NULL;
- const char *pfx = PyUnicode_AsUTF8 (pfx_obj);
- const char *ns = LSUP_nsmap_get_ns (((NSMapObject *)self)->ob_struct, pfx);
- if (!ns) Py_RETURN_NONE;
- PyObject *ns_obj = PyUnicode_FromString (ns);
- Py_INCREF (ns_obj);
- return (ns_obj);
- }
- static PyObject *
- NSMap_denormalize_uri (PyObject *self, PyObject *fq_uri_obj)
- {
- if (PyUnicode_READY (fq_uri_obj) < 0) return NULL;
- const char *fq_uri = PyUnicode_AsUTF8 (fq_uri_obj);
- char *pfx_uri;
- LSUP_rc rc = LSUP_nsmap_denormalize_uri (
- ((NSMapObject *)self)->ob_struct, fq_uri, &pfx_uri);
- if (rc < 0) {
- PyErr_SetString (PyExc_ValueError, "Error normalizing URI.");
- return NULL;
- } else if (rc == LSUP_NORESULT) Py_RETURN_NONE;
- PyObject *uri_obj = PyUnicode_FromString (pfx_uri);
- Py_INCREF (uri_obj);
- return (uri_obj);
- }
- static PyObject *
- NSMap_normalize_uri (PyObject *self, PyObject *pfx_uri_obj)
- {
- if (PyUnicode_READY (pfx_uri_obj) < 0) return NULL;
- const char *pfx_uri = PyUnicode_AsUTF8 (pfx_uri_obj);
- char *fq_uri;
- LSUP_rc rc = LSUP_nsmap_normalize_uri (
- ((NSMapObject *)self)->ob_struct, pfx_uri, &fq_uri);
- if (rc < 0) {
- PyErr_SetString (PyExc_ValueError, "Error denormalizing URI.");
- return NULL;
- }
- PyObject *uri_obj = PyUnicode_FromString (fq_uri);
- Py_INCREF (uri_obj);
- return (uri_obj);
- }
- static PyObject *
- NSMap_as_dict (PyObject *self)
- {
- const char ***data = LSUP_nsmap_dump (((NSMapObject *)self)->ob_struct);
- PyObject *data_obj = PyDict_New();
- // TODO PyDictProxy_New(PyObject *mapping)
- for (size_t i = 0; data[i] != NULL; i++) {
- PyObject *val_obj = PyUnicode_FromString(data[i][1]);
- if (PyDict_SetItemString (data_obj, data[i][0], val_obj) < 0)
- return NULL;
- }
- Py_INCREF (data_obj);
- return data_obj;
- }
- static PyMethodDef NSMap_methods[] = {
- {
- "add", (PyCFunction) NSMap_add, METH_VARARGS,
- "Add a namespace prefix."
- },
- {
- "remove", (PyCFunction) NSMap_remove, METH_O,
- "Remove a namespace prefix."
- },
- {
- "get", (PyCFunction) NSMap_get, METH_O,
- "Get the fully qualified namespace for a prefix."
- },
- {
- "normalize_uri", (PyCFunction) NSMap_normalize_uri, METH_O,
- "Normalize a URI (i.e. convert from prefixed to fully qualified)."
- },
- {
- "denormalize_uri", (PyCFunction) NSMap_denormalize_uri, METH_O,
- "Denormalize a URI (i.e. convert from fully qualified to prefixed)."
- },
- {
- "as_dict", (PyCFunction) NSMap_as_dict, METH_NOARGS,
- "Namespace map as a dictionary of prefixes and namespaces.",
- },
- {NULL},
- };
- PyTypeObject NSMapType = {
- PyVarObject_HEAD_INIT(NULL, 0)
- .tp_name = "namespace.NSMap",
- .tp_doc = "Namespace prefix map.",
- .tp_basicsize = sizeof(NSMapObject),
- .tp_itemsize = 0,
- .tp_flags = Py_TPFLAGS_DEFAULT,
- .tp_new = PyType_GenericNew,
- .tp_init = (initproc) NSMap_init,
- .tp_dealloc = (destructor) NSMap_dealloc,
- .tp_methods = NSMap_methods,
- };
- #endif /* _PY_NAMESPACE_OBJ */
|