Home | History | Annotate | Download | only in python
      1 /*
      2  * Copyright 2001-2004 Brandon Long
      3  * All Rights Reserved.
      4  *
      5  * ClearSilver Templating System
      6  *
      7  * This code is made available under the terms of the ClearSilver License.
      8  * http://www.clearsilver.net/license.hdf
      9  *
     10  */
     11 
     12 #include <Python.h>
     13 #include "ClearSilver.h"
     14 
     15 #define NEO_CGI_MODULE
     16 #include "p_neo_util.h"
     17 
     18 
     19 #define CSObjectCheck(a) (!(strcmp((a)->ob_type->tp_name, CSObjectType.tp_name)))
     20 
     21 typedef struct _CSObject
     22 {
     23    PyObject_HEAD
     24    CSPARSE *data;
     25 } CSObject;
     26 
     27 static PyObject *p_cs_value_get_attr (CSObject *self, char *name);
     28 static void p_cs_dealloc (CSObject *ho);
     29 
     30 static PyTypeObject CSObjectType =
     31 {
     32   PyObject_HEAD_INIT(NULL)
     33     0,			             /*ob_size*/
     34   "CSObjectType",	             /*tp_name*/
     35   sizeof(CSObject),	     /*tp_size*/
     36   0,			             /*tp_itemsize*/
     37   /* methods */
     38   (destructor)p_cs_dealloc,	     /*tp_dealloc*/
     39   0,			             /*tp_print*/
     40   (getattrfunc)p_cs_value_get_attr,     /*tp_getattr*/
     41   0,			             /*tp_setattr*/
     42   0,			             /*tp_compare*/
     43   (reprfunc)0,                       /*tp_repr*/
     44   0,                                 /* tp_as_number */
     45   0,                                 /* tp_as_sequence */
     46   0,                                 /* tp_as_mapping */
     47   0,                                 /* tp_as_hash */
     48 };
     49 
     50 static void p_cs_dealloc (CSObject *ho)
     51 {
     52   /* ne_warn("deallocating hdf: %X", ho); */
     53   if (ho->data)
     54   {
     55     cs_destroy (&(ho->data));
     56   }
     57   PyObject_DEL(ho);
     58 }
     59 
     60 PyObject * p_cs_to_object (CSPARSE *data)
     61 {
     62   PyObject *rv;
     63 
     64   if (data == NULL)
     65   {
     66     rv = Py_None;
     67     Py_INCREF (rv);
     68   }
     69   else
     70   {
     71     CSObject *ho = PyObject_NEW (CSObject, &CSObjectType);
     72     if (ho == NULL) return NULL;
     73     ho->data = data;
     74     rv = (PyObject *) ho;
     75     /* ne_warn("allocating cs: %X", ho); */
     76   }
     77   return rv;
     78 }
     79 
     80 static PyObject * p_cs_init (PyObject *self, PyObject *args)
     81 {
     82   CSPARSE *cs = NULL;
     83   NEOERR *err;
     84   PyObject *ho;
     85   HDF *hdf;
     86 
     87   if (!PyArg_ParseTuple(args, "O:CS(HDF Object)", &ho))
     88     return NULL;
     89 
     90   hdf = p_object_to_hdf (ho);
     91   if (hdf == NULL)
     92   {
     93     PyErr_BadArgument();
     94     return NULL;
     95   }
     96 
     97   err = cs_init (&cs, hdf);
     98   if (err) return p_neo_error (err);
     99   err = cgi_register_strfuncs(cs);
    100   if (err) return p_neo_error (err);
    101   return p_cs_to_object (cs);
    102 }
    103 
    104 static PyObject * p_cs_parse_file (PyObject *self, PyObject *args)
    105 {
    106   CSObject *co = (CSObject *)self;
    107   NEOERR *err;
    108   char *path;
    109 
    110   if (!PyArg_ParseTuple(args, "s:parseFile(path)", &path))
    111     return NULL;
    112 
    113   err = cs_parse_file (co->data, path);
    114   if (err) return p_neo_error(err);
    115   Py_INCREF(Py_None);
    116   return Py_None;
    117 }
    118 
    119 static PyObject * p_cs_parse_str (PyObject *self, PyObject *args)
    120 {
    121   CSObject *co = (CSObject *)self;
    122   NEOERR *err;
    123   char *s, *ms;
    124   int l;
    125 
    126   if (!PyArg_ParseTuple(args, "s#:parseStr(string)", &s, &l))
    127     return NULL;
    128 
    129   ms = strdup(s);
    130   if (ms == NULL) return PyErr_NoMemory();
    131 
    132   err = cs_parse_string (co->data, ms, l);
    133   if (err) return p_neo_error(err);
    134   Py_INCREF(Py_None);
    135   return Py_None;
    136 }
    137 
    138 static NEOERR *render_cb (void *ctx, char *buf)
    139 {
    140   STRING *str= (STRING *)ctx;
    141 
    142   return nerr_pass(string_append(str, buf));
    143 }
    144 
    145 static PyObject * p_cs_render (PyObject *self, PyObject *args)
    146 {
    147   CSObject *co = (CSObject *)self;
    148   NEOERR *err;
    149   STRING str;
    150   PyObject *rv;
    151 
    152   string_init(&str);
    153   err = cs_render (co->data, &str, render_cb);
    154   if (err) return p_neo_error(err);
    155   rv = Py_BuildValue ("s", str.buf);
    156   string_clear (&str);
    157   return rv;
    158 }
    159 
    160 static PyMethodDef CSMethods[] =
    161 {
    162   {"parseFile", p_cs_parse_file, METH_VARARGS, NULL},
    163   {"parseStr", p_cs_parse_str, METH_VARARGS, NULL},
    164   {"render", p_cs_render, METH_VARARGS, NULL},
    165   {NULL, NULL}
    166 };
    167 
    168 static PyMethodDef ModuleMethods[] =
    169 {
    170   {"CS", p_cs_init, METH_VARARGS, NULL},
    171   {NULL, NULL}
    172 };
    173 
    174 PyObject *p_cs_value_get_attr (CSObject *ho, char *name)
    175 {
    176   return Py_FindMethod(CSMethods, (PyObject *)ho, name);
    177 }
    178 
    179 DL_EXPORT(void) initneo_cs(void)
    180 {
    181   PyObject *m, *d;
    182 
    183   CSObjectType.ob_type = &PyType_Type;
    184 
    185   m = Py_InitModule("neo_cs", ModuleMethods);
    186   d = PyModule_GetDict(m);
    187 }
    188