Ejemplo n.º 1
0
    def render(self, md):
        expr = self.expr
        if type(expr) is type(''): v = md[expr]
        else: v = expr(md)

        if not self.mapping:
            if type(v) is type(()) and len(v) == 1: v = v[0]
            v = InstanceDict(v, md)

        if self.only:
            _md = md
            md = TemplateDict()
            if hasattr(_md, 'guarded_getattr'):
                md.guarded_getattr = _md.guarded_getattr
            if hasattr(_md, 'guarded_getitem'):
                md.guarded_getitem = _md.guarded_getitem

        md._push(v)
        try:
            return render_blocks(self.section, md)
        finally:
            md._pop(1)
Ejemplo n.º 2
0
    def render(self, md):
        expr=self.expr
        if type(expr) is type(''): v=md[expr]
        else: v=expr(md)

        if not self.mapping:
            if type(v) is type(()) and len(v)==1: v=v[0]
            v=InstanceDict(v,md)

        if self.only:
            _md=md
            md=TemplateDict()
            if hasattr(_md, 'guarded_getattr'):
                md.guarded_getattr = _md.guarded_getattr
            if hasattr(_md, 'guarded_getitem'):
                md.guarded_getitem = _md.guarded_getitem

        md._push(v)
        try: return render_blocks(self.section, md)
        finally: md._pop(1)
Ejemplo n.º 3
0
    def __call__(self, client=None, mapping={}, **kw):
        '''\
        Generate a document from a document template.

        The document will be generated by inserting values into the
        format string specified when the document template was
        created.  Values are inserted using standard python named
        string formats.

        The optional argument 'client' is used to specify a object
        containing values to be looked up.  Values will be looked up
        using getattr, so inheritence of values is supported.  Note
        that names beginning with '_' will not be looked up from the
        client.

        The optional argument, 'mapping' is used to specify a mapping
        object containing values to be inserted.

        Values to be inserted may also be specified using keyword
        arguments.

        Values will be inserted from one of several sources.  The
        sources, in the order in which they are consulted, are:

          o  Keyword arguments,

          o  The 'client' argument,

          o  The 'mapping' argument,

          o  The keyword arguments provided when the object was
             created, and

          o  The 'mapping' argument provided when the template was
             created.

        '''
        # print '============================================================'
        # print '__called__'
        # print self.raw
        # print kw
        # print client
        # print mapping
        # print '============================================================'

        if mapping is None: mapping = {}

        if not hasattr(self, '_v_cooked'):
            try:
                changed = self.__changed__()
            except:
                changed = 1
            self.cook()
            if not changed: self.__changed__(0)

        pushed = None
        try:
            if mapping.__class__ is TemplateDict: pushed = 0
        except:
            pass

        globals = self.globals
        if pushed is not None:
            # We were passed a TemplateDict, so we must be a sub-template
            md = mapping
            push = md._push
            if globals:
                push(self.globals)
                pushed = pushed + 1
        else:
            md = TemplateDict()
            push = md._push
            shared_globals = self.shared_globals
            if shared_globals: push(shared_globals)
            if globals: push(globals)
            if mapping:
                push(mapping)
            md.validate = self.validate
            if client is not None:
                if type(client) == type(()):
                    md.this = client[-1]
                else:
                    md.this = client
            pushed = 0

        level = md.level
        if level > 200:
            raise SystemError, ('infinite recursion in document template')
        md.level = level + 1

        if client is not None:
            if type(client) == type(()):
                # if client is a tuple, it represents a "path" of clients
                # which should be pushed onto the md in order.
                for ob in client:
                    push(InstanceDict(ob, md))  # Circ. Ref. 8-|
                    pushed = pushed + 1
            else:
                # otherwise its just a normal client object.
                push(InstanceDict(client, md))  # Circ. Ref. 8-|
                pushed = pushed + 1

        if self._vars:
            push(self._vars)
            pushed = pushed + 1

        if kw:
            push(kw)
            pushed = pushed + 1

        try:
            value = self.ZDocumentTemplate_beforeRender(md, _marker)
            if value is _marker:
                try:
                    result = render_blocks(self._v_blocks, md)
                except DTReturn, v:
                    result = v.v
                self.ZDocumentTemplate_afterRender(md, result)
                return result
            else:
Ejemplo n.º 4
0
    def __call__(self,client=None,mapping={},**kw):
        '''\
        Generate a document from a document template.

        The document will be generated by inserting values into the
        format string specified when the document template was
        created.  Values are inserted using standard python named
        string formats.

        The optional argument 'client' is used to specify a object
        containing values to be looked up.  Values will be looked up
        using getattr, so inheritence of values is supported.  Note
        that names beginning with '_' will not be looked up from the
        client.

        The optional argument, 'mapping' is used to specify a mapping
        object containing values to be inserted.

        Values to be inserted may also be specified using keyword
        arguments.

        Values will be inserted from one of several sources.  The
        sources, in the order in which they are consulted, are:

          o  Keyword arguments,

          o  The 'client' argument,

          o  The 'mapping' argument,

          o  The keyword arguments provided when the object was
             created, and

          o  The 'mapping' argument provided when the template was
             created.

        '''
        # print '============================================================'
        # print '__called__'
        # print self.raw
        # print kw
        # print client
        # print mapping
        # print '============================================================'

        if mapping is None: mapping = {}
        if hasattr(mapping, 'taintWrapper'): mapping = mapping.taintWrapper()

        if not hasattr(self,'_v_cooked'):
            try: changed=self.__changed__()
            except: changed=1
            self.cook()
            if not changed: self.__changed__(0)

        pushed=None
        try:
            # Support Python 1.5.2, but work better in 2.1
            if (mapping.__class__ is TemplateDict or
                isinstance(mapping, TemplateDict)): pushed=0
        except: pass

        globals=self.globals
        if pushed is not None:
            # We were passed a TemplateDict, so we must be a sub-template
            md=mapping
            push=md._push
            if globals:
                push(self.globals)
                pushed=pushed+1
        else:
            md=TemplateDict()
            push=md._push
            shared_globals=self.shared_globals
            if shared_globals: push(shared_globals)
            if globals: push(globals)
            if mapping:
                push(mapping)
            md.guarded_getattr=self.guarded_getattr
            md.guarded_getitem=self.guarded_getitem
            if client is not None:
                if type(client)==type(()):
                    md.this=client[-1]
                else: md.this=client
            pushed=0

        level=md.level
        if level > 200: raise SystemError, (
            'infinite recursion in document template')
        md.level=level+1

        if client is not None:
            if type(client)==type(()):
                # if client is a tuple, it represents a "path" of clients
                # which should be pushed onto the md in order.
                for ob in client:
                    push(InstanceDict(ob, md)) # Circ. Ref. 8-|
                    pushed=pushed+1
            else:
                # otherwise its just a normal client object.
                push(InstanceDict(client, md)) # Circ. Ref. 8-|
                pushed=pushed+1

        if self._vars:
            push(self._vars)
            pushed=pushed+1

        if kw:
            push(kw)
            pushed=pushed+1

        try:
            value = self.ZDocumentTemplate_beforeRender(md, _marker)
            if value is _marker:
                try: result = render_blocks(self._v_blocks, md)
                except DTReturn, v: result = v.v
                self.ZDocumentTemplate_afterRender(md, result)
                return result
            else:
                return value
        finally:
            if pushed: md._pop(pushed) # Get rid of circular reference!
            md.level=level # Restore previous level