python module should be working
This commit is contained in:
parent
87d5ae68f7
commit
f65d251e41
|
@ -10,6 +10,7 @@
|
|||
#include <libxml/xmlwriter.h>
|
||||
#include <dexter.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <json/json.h>
|
||||
#include <xml2json.h>
|
||||
|
||||
|
@ -28,9 +29,15 @@ static PyMethodDef dexpy_methods[] = {
|
|||
#define PyMODINIT_FUNC void
|
||||
#endif
|
||||
|
||||
static PyObject *jsonmodule;
|
||||
|
||||
PyMODINIT_FUNC
|
||||
initdexpy(void)
|
||||
{
|
||||
jsonmodule = PyImport_ImportModule("json");
|
||||
if(jsonmodule == NULL)
|
||||
return NULL;
|
||||
|
||||
PyObject* m;
|
||||
|
||||
dexpy_DexPyType.tp_new = PyType_GenericNew;
|
||||
|
@ -62,20 +69,32 @@ DexPy_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
|
|||
static int
|
||||
DexPy_init(DexPy *self, PyObject *args, PyObject *kwds)
|
||||
{
|
||||
char *script = "";
|
||||
PyObject *script;
|
||||
char *string = "";
|
||||
char *incl = "";
|
||||
|
||||
if (!PyArg_ParseTuple(args, "S|S", &script, &incl)) {
|
||||
if (!PyArg_ParseTuple(args, "O|S", &script, &incl)) {
|
||||
Py_DECREF(self);
|
||||
return NULL;
|
||||
return -1;
|
||||
}
|
||||
|
||||
self->dex = dex_compile(script, incl);
|
||||
PyObject *dumps = PyObject_GetAttrString(jsonmodule, "dumps");
|
||||
if(dumps == NULL) return -1;
|
||||
|
||||
if(PyDict_Check(script)){
|
||||
script = PyObject_CallFunctionObjArgs(dumps, script, NULL);
|
||||
if(script == NULL) return -1;
|
||||
}
|
||||
|
||||
string = PyString_AsString(script);
|
||||
if(string == NULL) return -1;
|
||||
|
||||
self->dex = dex_compile(string, incl);
|
||||
|
||||
if(self->dex->error != NULL) {
|
||||
PyErr_SetString(PyExc_RuntimeError, self->dex->error);
|
||||
Py_DECREF(self);
|
||||
return NULL;
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
@ -86,16 +105,16 @@ static PyObject *
|
|||
pythonize_recurse(xmlNodePtr xml) {
|
||||
if(xml == NULL) return NULL;
|
||||
xmlNodePtr child;
|
||||
static PyObject * obj = NULL;
|
||||
|
||||
PyObject * obj = NULL;
|
||||
|
||||
switch(xml->type) {
|
||||
case XML_ELEMENT_NODE:
|
||||
child = xml->children;
|
||||
if(xml->ns == NULL) {
|
||||
child = xml;
|
||||
child = xml;
|
||||
obj = PyDict_New();
|
||||
while(child != NULL) {
|
||||
PyDict_SetItem(obj, Py_BuildValue("S", child->name), pythonize_recurse(child->children));
|
||||
PyDict_SetItemString(obj, child->name, pythonize_recurse(child->children));
|
||||
child = child->next;
|
||||
}
|
||||
} else if(!strcmp(xml->ns->prefix, "dexter")) {
|
||||
|
@ -111,7 +130,7 @@ pythonize_recurse(xmlNodePtr xml) {
|
|||
}
|
||||
break;
|
||||
case XML_TEXT_NODE:
|
||||
obj = Py_BuildValue("S", xml->content);
|
||||
obj = Py_BuildValue("s", xml->content);
|
||||
break;
|
||||
}
|
||||
if(obj == NULL) {
|
||||
|
@ -137,13 +156,13 @@ DexPy_parse_doc(dexPtr dex, xmlDocPtr xml, char *type) {
|
|||
if(!strcmp(type, "json")) {
|
||||
struct json_object *json = xml2json(xml->children->children);
|
||||
char* str = json_object_to_json_string(json);
|
||||
output = Py_BuildValue("S", str);
|
||||
output = Py_BuildValue("s", str);
|
||||
json_object_put(json);
|
||||
} else if(!strcmp(type, "xml")) {
|
||||
char* str;
|
||||
int size;
|
||||
xmlDocDumpMemory(xml, &str, &size);
|
||||
output = Py_BuildValue("S", str);
|
||||
output = Py_BuildValue("s", str);
|
||||
} else {
|
||||
output = pythonize_recurse(xml->children->children);
|
||||
if(output == NULL){
|
||||
|
@ -159,9 +178,10 @@ DexPy_parse(DexPy *self, PyObject *args, PyObject *keywords)
|
|||
{
|
||||
char *file = NULL;
|
||||
char *string = NULL;
|
||||
char *input = "python";
|
||||
char *input = "html";
|
||||
char *output = "python";
|
||||
int len;
|
||||
xmlDocPtr xml;
|
||||
|
||||
static char * list[] = { "file", "string", "input", "output", NULL };
|
||||
|
||||
|
@ -170,11 +190,18 @@ DexPy_parse(DexPy *self, PyObject *args, PyObject *keywords)
|
|||
return NULL;
|
||||
}
|
||||
|
||||
if(file != NULL) {
|
||||
return DexPy_parse_doc(self->dex, dex_parse_file(self->dex, file, !strcmp(input, "html")), output);
|
||||
} else {
|
||||
return DexPy_parse_doc(self->dex, dex_parse_string(self->dex, string, len, !strcmp(input, "html")), output);
|
||||
if(self->dex == NULL) {
|
||||
PyErr_SetString(PyExc_RuntimeError, "dex data is NULL");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if(file != NULL) {
|
||||
xml = dex_parse_file(self->dex, file, !strcmp(input, "html"));
|
||||
} else {
|
||||
xml = dex_parse_string(self->dex, string, len, !strcmp(input, "html"));
|
||||
}
|
||||
|
||||
return DexPy_parse_doc(self->dex, xml, output);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -1,78 +0,0 @@
|
|||
#ifndef DEXPYMODULE_H_INCLUDED
|
||||
#define DEXPYMODULE_H_INCLUDED
|
||||
|
||||
#ifndef PyMODINIT_FUNC /* declarations for DLL import/export */
|
||||
#define PyMODINIT_FUNC void
|
||||
#endif
|
||||
|
||||
#include <Python.h>
|
||||
|
||||
typedef struct {
|
||||
PyObject_HEAD
|
||||
dexPtr dex;
|
||||
} CDexter;
|
||||
|
||||
static PyMethodDef dexpy_methods[] = {
|
||||
{NULL} /* Sentinel */
|
||||
};
|
||||
|
||||
static PyMemberDef CDexter_members[] = {
|
||||
{NULL} /* Sentinel */
|
||||
};
|
||||
|
||||
static PyMethodDef CDexter_methods[] = {
|
||||
{"parse_string", (PyCFunction)CDexter_parse_string, METH_VARARGS,
|
||||
"Parses an in-memory string with the current dex"
|
||||
},
|
||||
{"parse_file", (PyCFunction)CDexter_parse_file, METH_VARARGS,
|
||||
"Parses file or url with the current dex"
|
||||
},
|
||||
{NULL} /* Sentinel */
|
||||
};
|
||||
|
||||
static PyTypeObject dexpy_CDexterType = {
|
||||
PyObject_HEAD_INIT(NULL)
|
||||
0, /*ob_size*/
|
||||
"dexpy.CDexter", /*tp_name*/
|
||||
sizeof(CDexter), /*tp_basicsize*/
|
||||
0, /*tp_itemsize*/
|
||||
0, /*tp_dealloc*/
|
||||
0, /*tp_print*/
|
||||
0, /*tp_getattr*/
|
||||
0, /*tp_setattr*/
|
||||
0, /*tp_compare*/
|
||||
0, /*tp_repr*/
|
||||
0, /*tp_as_number*/
|
||||
0, /*tp_as_sequence*/
|
||||
0, /*tp_as_mapping*/
|
||||
0, /*tp_hash */
|
||||
0, /*tp_call*/
|
||||
0, /*tp_str*/
|
||||
0, /*tp_getattro*/
|
||||
0, /*tp_setattro*/
|
||||
0, /*tp_as_buffer*/
|
||||
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
|
||||
"CDexter objects", /* tp_doc */
|
||||
0, /* tp_traverse */
|
||||
0, /* tp_clear */
|
||||
0, /* tp_richcompare */
|
||||
0, /* tp_weaklistoffset */
|
||||
0, /* tp_iter */
|
||||
0, /* tp_iternext */
|
||||
CDexter_methods, /* tp_methods */
|
||||
CDexter_members, /* tp_members */
|
||||
0, /* tp_getset */
|
||||
0, /* tp_base */
|
||||
0, /* tp_dict */
|
||||
0, /* tp_descr_get */
|
||||
0, /* tp_descr_set */
|
||||
0, /* tp_dictoffset */
|
||||
0, /* tp_init */
|
||||
0, /* tp_alloc */
|
||||
0, /* tp_new */
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
#endif
|
|
@ -1,3 +1,5 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
import re
|
||||
from os import system
|
||||
from distutils.core import setup, Extension
|
||||
|
|
|
@ -1,20 +1,30 @@
|
|||
#!/usr/bin/env python
|
||||
import unittest
|
||||
from dexpy import DexPy
|
||||
from inspect import currentframe
|
||||
from os.path import dirname
|
||||
|
||||
class TestDexPy(unittest.TestCase):
|
||||
|
||||
def testnothing(self):
|
||||
self.assertEqual(1, 1)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
def setUp(self):
|
||||
self.dex = DexPy({'title': 'title'})
|
||||
self.alt_dex = DexPy('{"title": "title"}')
|
||||
self.__file__ = currentframe().f_code.co_filename
|
||||
self.__dir__ = dirname(self.__file__)
|
||||
self.file = self.__dir__ + '/../../test/yelp.html'
|
||||
self.json = '{ "title": "\\t\\tNick\'s Crispy Tacos - Russian Hill - San Francisco, CA\\n" }'
|
||||
|
||||
def test_file_xml(self):
|
||||
parsed = self.dex.parse(file = self.file, output = "json")
|
||||
self.assertEquals(self.json, parsed)
|
||||
|
||||
def test_json_file_xml(self):
|
||||
parsed = self.alt_dex.parse(file = self.file, output = "json")
|
||||
self.assertEquals(self.json, parsed)
|
||||
|
||||
def test_native(self):
|
||||
parsed = self.alt_dex.parse(file = self.file, output = "python")
|
||||
self.assertEquals({ "title": "\t\tNick's Crispy Tacos - Russian Hill - San Francisco, CA\n" }, parsed)
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
Loading…
Reference in New Issue