[1326] | 1 | |
---|
[671] | 2 | /* |
---|
| 3 | * Rappture Encode Python Interface |
---|
| 4 | * |
---|
| 5 | * ====================================================================== |
---|
| 6 | * Derrick S. Kearney, Purdue University |
---|
| 7 | * Copyright (c) 2004-2007 Purdue Research Foundation |
---|
| 8 | * |
---|
| 9 | * See the file "license.terms" for information on usage and |
---|
| 10 | * redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
---|
| 11 | * ====================================================================== |
---|
| 12 | */ |
---|
| 13 | #include <Python.h> |
---|
| 14 | #include <RpEncode.h> |
---|
| 15 | |
---|
| 16 | static PyObject *ErrorObject; |
---|
| 17 | |
---|
| 18 | PyDoc_STRVAR(RpEncode_isbinary_doc, |
---|
| 19 | "isbinary(data)\n\ |
---|
| 20 | \n\ |
---|
| 21 | Check to see if there are binary characters\n\ |
---|
| 22 | in the provided data."); |
---|
| 23 | |
---|
| 24 | static PyObject* |
---|
| 25 | RpEncode_isbinary(PyObject *self, PyObject *args, PyObject *keywds) |
---|
| 26 | { |
---|
| 27 | const char* data = NULL; |
---|
| 28 | int dlen = 0; |
---|
| 29 | PyObject* rv = NULL; |
---|
| 30 | |
---|
[1326] | 31 | static char *kwlist[] = { |
---|
[1694] | 32 | (char *)"data", |
---|
| 33 | NULL |
---|
[1326] | 34 | }; |
---|
[671] | 35 | |
---|
| 36 | if (PyTuple_Size(args) != 1) { |
---|
| 37 | PyErr_SetString(PyExc_TypeError,"isbinary() takes exactly 1 argument"); |
---|
| 38 | return NULL; |
---|
| 39 | } |
---|
| 40 | |
---|
| 41 | if (!PyArg_ParseTupleAndKeywords(args, keywds, "s#", |
---|
| 42 | kwlist, &data, &dlen)) { |
---|
| 43 | PyErr_SetString(PyExc_TypeError,"isbinary() takes exactly 1 argument"); |
---|
| 44 | return NULL; |
---|
| 45 | } |
---|
| 46 | |
---|
[1384] | 47 | rv = PyFloat_FromDouble(Rappture::encoding::isBinary(data,dlen)); |
---|
[671] | 48 | return rv; |
---|
| 49 | } |
---|
| 50 | |
---|
| 51 | PyDoc_STRVAR(RpEncode_encode_doc, |
---|
| 52 | "encode(data,flags)\n\ |
---|
| 53 | \n\ |
---|
| 54 | Encode the provided data with gzip compression and base64 \n\ |
---|
| 55 | according to the provided flags"); |
---|
| 56 | |
---|
| 57 | static PyObject* |
---|
| 58 | RpEncode_encode(PyObject *self, PyObject *args, PyObject *keywds) |
---|
| 59 | { |
---|
| 60 | const char* data = NULL; |
---|
| 61 | int dlen = 0; |
---|
| 62 | int flags = 0; |
---|
| 63 | PyObject* rv = NULL; |
---|
| 64 | Rappture::Outcome err; |
---|
| 65 | |
---|
[1326] | 66 | static char *kwlist[] = { |
---|
[1694] | 67 | (char *)"data", |
---|
| 68 | (char *)"flags", |
---|
| 69 | NULL |
---|
[1326] | 70 | }; |
---|
[671] | 71 | |
---|
| 72 | if (PyTuple_Size(args) != 2) { |
---|
| 73 | PyErr_SetString(PyExc_TypeError,"encode() takes exactly 2 arguments"); |
---|
| 74 | return NULL; |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | if (!PyArg_ParseTupleAndKeywords(args, keywds, "s#i", |
---|
| 78 | kwlist, &data, &dlen, &flags)) { |
---|
| 79 | PyErr_SetString(PyExc_TypeError,"encode() takes exactly 2 arguments"); |
---|
| 80 | return NULL; |
---|
| 81 | } |
---|
| 82 | |
---|
[1368] | 83 | Rappture::Buffer buf(data, dlen); |
---|
| 84 | if (!Rappture::encoding::encode(err, buf, flags)) { |
---|
[1694] | 85 | std::string outStr; |
---|
[1326] | 86 | |
---|
| 87 | outStr = err.remark(); |
---|
[1694] | 88 | outStr += "\n"; |
---|
| 89 | outStr += err.context(); |
---|
[1368] | 90 | PyErr_SetString(PyExc_RuntimeError, outStr.c_str()); |
---|
[671] | 91 | buf.clear(); |
---|
| 92 | return NULL; |
---|
| 93 | } |
---|
| 94 | rv = PyString_FromStringAndSize(buf.bytes(),buf.size()); |
---|
| 95 | return rv; |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | PyDoc_STRVAR(RpEncode_decode_doc, |
---|
| 99 | "decode(data,flags)\n\ |
---|
| 100 | \n\ |
---|
[1088] | 101 | Decode the provided data with gzip compression and base64 \n\ |
---|
[671] | 102 | according to the provided flags"); |
---|
| 103 | |
---|
| 104 | static PyObject* |
---|
| 105 | RpEncode_decode(PyObject *self, PyObject *args, PyObject *keywds) |
---|
| 106 | { |
---|
| 107 | const char* data = NULL; |
---|
| 108 | int dlen = 0; |
---|
| 109 | int flags = 0; |
---|
| 110 | PyObject* rv = NULL; |
---|
| 111 | Rappture::Outcome err; |
---|
| 112 | |
---|
[1326] | 113 | static char *kwlist[] = { |
---|
[1694] | 114 | (char *)"data", |
---|
| 115 | (char *)"flags", |
---|
| 116 | NULL |
---|
[1326] | 117 | }; |
---|
[671] | 118 | |
---|
| 119 | if (PyTuple_Size(args) != 2) { |
---|
[1088] | 120 | PyErr_SetString(PyExc_TypeError,"decode() takes exactly 2 arguments"); |
---|
[671] | 121 | return NULL; |
---|
| 122 | } |
---|
| 123 | |
---|
| 124 | if (!PyArg_ParseTupleAndKeywords(args, keywds, "s#i", |
---|
| 125 | kwlist, &data, &dlen, &flags)) { |
---|
[1088] | 126 | PyErr_SetString(PyExc_TypeError,"decode() takes exactly 2 arguments"); |
---|
[671] | 127 | return NULL; |
---|
| 128 | } |
---|
| 129 | |
---|
[1368] | 130 | Rappture::Buffer buf(data, dlen); |
---|
| 131 | if (!Rappture::encoding::decode(err, buf, flags)) { |
---|
[1694] | 132 | std::string outStr; |
---|
[1326] | 133 | |
---|
| 134 | outStr = err.remark(); |
---|
[1694] | 135 | outStr += "\n"; |
---|
| 136 | outStr += err.context(); |
---|
[671] | 137 | PyErr_SetString(PyExc_RuntimeError,outStr.c_str()); |
---|
| 138 | return NULL; |
---|
| 139 | } |
---|
| 140 | rv = PyString_FromStringAndSize(buf.bytes(),buf.size()); |
---|
| 141 | return rv; |
---|
| 142 | } |
---|
| 143 | |
---|
| 144 | /* ---------- */ |
---|
| 145 | |
---|
| 146 | |
---|
| 147 | /* List of functions defined in the module */ |
---|
| 148 | |
---|
| 149 | static PyMethodDef RpEncode_Methods[] = { |
---|
| 150 | |
---|
| 151 | {"isbinary", (PyCFunction)RpEncode_isbinary, METH_VARARGS|METH_KEYWORDS, |
---|
| 152 | RpEncode_isbinary_doc}, |
---|
| 153 | {"encode", (PyCFunction)RpEncode_encode, METH_VARARGS|METH_KEYWORDS, |
---|
| 154 | RpEncode_encode_doc}, |
---|
| 155 | {"decode", (PyCFunction)RpEncode_decode, METH_VARARGS|METH_KEYWORDS, |
---|
| 156 | RpEncode_decode_doc}, |
---|
| 157 | |
---|
| 158 | {NULL, NULL} /* sentinel */ |
---|
| 159 | }; |
---|
| 160 | |
---|
| 161 | PyDoc_STRVAR(module_doc, "Rappture Encode Module for Python."); |
---|
| 162 | |
---|
| 163 | /* Initialization function for the module */ |
---|
| 164 | |
---|
| 165 | PyMODINIT_FUNC |
---|
| 166 | initencoding(void) |
---|
| 167 | { |
---|
| 168 | PyObject *m; |
---|
| 169 | |
---|
| 170 | /* Create the module and add the functions */ |
---|
| 171 | m = Py_InitModule3("encoding", RpEncode_Methods, module_doc); |
---|
| 172 | |
---|
| 173 | if (ErrorObject == NULL) { |
---|
[1326] | 174 | ErrorObject = PyErr_NewException((char *)"Rappture.encoding.error", |
---|
[1694] | 175 | NULL, NULL); |
---|
[1326] | 176 | if (ErrorObject == NULL) { |
---|
[671] | 177 | return; |
---|
[1694] | 178 | } |
---|
[671] | 179 | } |
---|
| 180 | Py_INCREF(ErrorObject); |
---|
| 181 | PyModule_AddObject(m, "error", ErrorObject); |
---|
| 182 | |
---|
| 183 | return; |
---|
| 184 | } |
---|