Skip to content

Commit

Permalink
Merged changes from the cDocumentTemplateCleanup branch.
Browse files Browse the repository at this point in the history
  • Loading branch information
hathawsh committed Jun 16, 2000
1 parent 0bc92e9 commit 6b3bf13
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 206 deletions.
12 changes: 3 additions & 9 deletions DT_Util.py
Expand Up @@ -82,8 +82,8 @@
# attributions are listed in the accompanying credits file.
#
##############################################################################
'''$Id: DT_Util.py,v 1.66 2000/05/30 15:59:50 brian Exp $'''
__version__='$Revision: 1.66 $'[11:-2]
'''$Id: DT_Util.py,v 1.67 2000/06/16 19:31:37 shane Exp $'''
__version__='$Revision: 1.67 $'[11:-2]

import regex, string, math, os
from string import strip, join, atoi, lower, split, find
Expand Down Expand Up @@ -208,7 +208,6 @@ def careful_range(md, iFirst, *args):
try:
import ExtensionClass
from cDocumentTemplate import InstanceDict, TemplateDict, render_blocks
from cDocumentTemplate import cDocument
except: from pDocumentTemplate import InstanceDict, TemplateDict, render_blocks


Expand Down Expand Up @@ -260,12 +259,7 @@ class namespace_:
def namespace(self, **kw):
"""Create a tuple consisting of a single instance whos attributes are
provided as keyword arguments."""
try: return self(kw) # Try to use the cDocumentTemplate implementation.
except AttributeError:
r=namespace_()
d=r.__dict__
for k, v in kw.items(): d[k]=v
return r,
return apply(self, (), kw)

d['namespace']=namespace

Expand Down
201 changes: 6 additions & 195 deletions cDocumentTemplate.c
Expand Up @@ -84,7 +84,7 @@
****************************************************************************/
static char cDocumentTemplate_module_documentation[] =
""
"\n$Id: cDocumentTemplate.c,v 1.32 2000/05/11 18:54:14 jim Exp $"
"\n$Id: cDocumentTemplate.c,v 1.33 2000/06/16 19:31:37 shane Exp $"
;

#include "ExtensionClass.h"
Expand Down Expand Up @@ -611,7 +611,7 @@ newDictInstance(PyObject *data)
static PyObject *
MM_call(MM *self, PyObject *args, PyObject *kw)
{
PyObject *r;
PyObject *r, *t;
int i, l=0;

if (args && (l=PyTuple_Size(args)) < 0) return NULL;
Expand All @@ -635,7 +635,9 @@ MM_call(MM *self, PyObject *args, PyObject *kw)
}

ASSIGN(r, OBJECT(newDictInstance(r)));
return r;
UNLESS(t=PyTuple_New(1)) goto err;
PyTuple_SET_ITEM(t, 0, r);
return t;

err:
Py_XDECREF(r);
Expand Down Expand Up @@ -872,201 +874,11 @@ static struct PyMethodDef Module_Level__methods[] = {
{NULL, (PyCFunction)NULL, 0, NULL} /* sentinel */
};

static PyObject *
validate(PyObject *self, PyObject *args)
{
PyObject *inst, *parent, *name, *value, *md, *__roles__, *i, *p;
char *cname="";
long ir;

/* def validate(self, inst, parent, name, value, md): */
UNLESS(PyArg_ParseTuple(args,"OOOOO",&inst,&parent,&name,&value,&md))
return NULL;

if (PyString_Check(name))
{
UNLESS(cname=PyString_AsString(name)) return NULL;
if (*cname=='a' && cname[1]=='q' && cname[2]=='_'
&& strcmp(cname, "aq_explicit") && strcmp(cname, "aq_parent"))
/* We disallow names beginning with "aq_" unless they are
aq_parent or aq_explicit */
return PyInt_FromLong(0);
}

/*
if hasattr(value, '__roles__'): roles=value.__roles__
else:
if (name[:5]=='func_' and
type(parent) in (
types.MethodType, types.FunctionType,
type(DTMLMethod.validate) )
):
# we don't want any function attributes
return 0
if hasattr(parent,'__roles__'): roles=parent.__roles__
elif hasattr(parent, 'aq_acquire'):
try: roles=parent.aq_acquire('__roles__')
except AttributeError:
if hasattr(inst, 'aq_base'): inst=inst.aq_base
if hasattr(parent, 'aq_base'): parent=parent.aq_base
return inst is parent
else:
if hasattr(inst, 'aq_base'): inst=inst.aq_base
if hasattr(parent, 'aq_base'): parent=parent.aq_base
return inst is parent
value=parent
*/
UNLESS(__roles__=PyObject_GetAttr(value,py___roles__))
{
PyErr_Clear();

if (
(*cname=='f' &&
cname[1]=='u' && cname[2]=='n' && cname[3]=='c' && cname[4]=='_')
&&
(PyECMethod_Check(parent)
|| PyFunction_Check(parent)
|| PyMethod_Check(parent)
)
)
return PyInt_FromLong(0);

UNLESS(__roles__=PyObject_GetAttr(parent,py___roles__))
{
PyErr_Clear();
if (__roles__=PyObject_GetAttr(parent,py_acquire))
{
ASSIGN(__roles__,
PyObject_CallFunction(__roles__, "O", py___roles__));
}
}
value=parent;
}
if (! __roles__)
{
PyErr_Clear();

/* Waaaa, check for wrapped objects, waaaaa! */
UNLESS(i=PyObject_GetAttr(inst, py_aq_base))
{
PyErr_Clear();
Py_INCREF(inst);
i=inst;
}
UNLESS(p=PyObject_GetAttr(parent, py_aq_base))
{
PyErr_Clear();
Py_INCREF(parent);
p=parent;
}

ir = i==p;
Py_DECREF(i);
Py_DECREF(p);

return PyInt_FromLong(ir);
}

/* if roles is None: return 1 */
if (__roles__==Py_None)
{
Py_DECREF(__roles__);
return PyInt_FromLong(1);
}

/* try:
if md.AUTHENTICATED_USER.hasRole(value, roles):
return 1
except AttributeError: pass
*/
if (md=PyObject_GetAttr(md,py_AUTHENTICATED_USER))
{
ASSIGN(md,PyObject_GetAttr(md,py_hasRole));
if (md) ASSIGN(md,PyObject_CallFunction(md,"OO",value,__roles__));
if (md)
{
if (PyObject_IsTrue(md))
{
Py_DECREF(__roles__);
return md;
}
Py_DECREF(md);
}
else PyErr_Clear();
}
else PyErr_Clear();

/* for r in self._proxy_roles:
if r in roles: return 1
*/
if (PyObject_IsTrue(__roles__))
if ((md=PyObject_GetAttr(self, py__proxy_roles)))
{
int i,l, isIn;
PyObject *role;

if ((l=PyObject_Length(md)) < 0) PyErr_Clear();
else
{
for (i=0; i < l; i++)
{
UNLESS(role=PySequence_GetItem(md,i))
{
PyErr_Clear();
break;
}
isIn=PySequence_In(__roles__,role);
Py_DECREF(role);
if (isIn < 0)
{
PyErr_Clear();
break;
}
if (isIn)
{
Py_DECREF(md);
return __roles__; /* Any true object would do. */
}
}
}
Py_DECREF(md);
}
else PyErr_Clear();

Py_DECREF(__roles__);

/* raise 'Unauthorized', (
'You are not authorized to access <em>%s</em>.' % name)
*/
if (name=PyString_Format(py_Unauthorized_fmt, name))
{
PyErr_SetObject(py_Unauthorized, name);
Py_DECREF(name);
}
return NULL;
}


static struct PyMethodDef Document_methods[] = {
{"validate", (PyCFunction)validate, METH_VARARGS,
""},
{NULL, (PyCFunction)NULL, 0, NULL} /* sentinel */
};


/* Initialization function for the module (*must* be called initcDocumentTemplate) */

void
initcDocumentTemplate()
{
PyObject *m, *d;
char *rev="$Revision: 1.32 $";
PURE_MIXIN_CLASS(cDocument,
"Base class for documents that adds fast validation method",
Document_methods);
char *rev="$Revision: 1.33 $";

DictInstanceType.ob_type=&PyType_Type;

Expand Down Expand Up @@ -1100,7 +912,6 @@ initcDocumentTemplate()

d = PyModule_GetDict(m);

PyExtensionClass_Export(d,"cDocument",cDocumentType);
PyExtensionClass_Export(d,"InstanceDict",InstanceDictType);
PyExtensionClass_Export(d,"TemplateDict",MMtype);

Expand Down
24 changes: 22 additions & 2 deletions pDocumentTemplate.py
Expand Up @@ -85,8 +85,8 @@
__doc__='''Python implementations of document template some features
$Id: pDocumentTemplate.py,v 1.23 1999/03/10 00:15:08 klm Exp $'''
__version__='$Revision: 1.23 $'[11:-2]
$Id: pDocumentTemplate.py,v 1.24 2000/06/16 19:31:37 shane Exp $'''
__version__='$Revision: 1.24 $'[11:-2]

import string, sys, types
from string import join
Expand Down Expand Up @@ -175,6 +175,15 @@ def keys(self):
kz = kz + d.keys()
return kz

class DictInstance:

def __init__(self, mapping):
self.__d=mapping

def __getattr__(self, name):
try: return self.__d[name]
except KeyError: raise AttributeError, name

class TemplateDict:

level=0
Expand Down Expand Up @@ -220,6 +229,17 @@ def has_key(self,key):

getitem=__getitem__

def __call__(self, *args, **kw):
if args:
if len(args)==1 and not kw:
m=args[0]
else:
m=self.__class__()
for a in args: m._push(a)
if kw: m._push(kw)
else: m=kw
return DictInstance(m)

def render_blocks(blocks, md):
rendered = []
append=rendered.append
Expand Down

0 comments on commit 6b3bf13

Please sign in to comment.