예제 #1
0
 def makeContext(self, *args):
     ctx = context.WovenContext()
     ctx.remember(convertToData(self.remember, ctx), inevow.IData)
     for a in args:
         ctx = context.WovenContext(ctx, invisible())
         ctx.remember(convertToData(a, ctx), inevow.IData)
     return ctx
예제 #2
0
 def makeContext(self, *args):
     ctx = context.WovenContext()
     ctx.remember(convertToData(self.remember, ctx), inevow.IData)
     for a in args:
         ctx = context.WovenContext(ctx, invisible())
         ctx.remember(convertToData(a, ctx), inevow.IData)
     return ctx
예제 #3
0
 def test_2_dictOriginal(self):
     data = {'hello': 'world'}
     ctx = context.WovenContext()
     ctx.remember(APage(data), inevow.IData)
     self.assertEquals(data, convertToData(ctx.locate(inevow.IData), ctx))
     self.assertEquals('foo', convertToData(directive('foo'), ctx))
     self.assertEquals('world', convertToData(directive('hello'), ctx))
예제 #4
0
 def test_2_dictOriginal(self):
     data = {'hello': 'world'}
     ctx = context.WovenContext()
     ctx.remember(APage(data), inevow.IData)
     # IGettable should return our dictionary
     self.assertEquals(data, convertToData(ctx.locate(inevow.IData), ctx))
     # IContainer on the *Page*, not the dictionary, should work
     self.assertEquals('foo', convertToData(directive('foo'), ctx))
     # IContainer on the Page should delegate to IContainer(self.original) if no data_ method
     self.assertEquals('world', convertToData(directive('hello'), ctx))
예제 #5
0
 def test_2_dictOriginal(self):
     data = {'hello': 'world'}
     ctx = context.WovenContext()
     ctx.remember(APage(data), inevow.IData)
     # IGettable should return our dictionary
     self.assertEquals(data, convertToData(ctx.locate(inevow.IData), ctx))
     # IContainer on the *Page*, not the dictionary, should work
     self.assertEquals('foo', convertToData(directive('foo'), ctx))
     # IContainer on the Page should delegate to IContainer(self.original) if no data_ method
     self.assertEquals('world', convertToData(directive('hello'), ctx))
예제 #6
0
 def test_list_directive(self):
     d = directive('2')
     ctx = self.makeContext([0, 1, 42, 3, 4])
     self.assertEquals(42, convertToData(d, ctx))
     self.assertRaises(IndexError, convertToData, directive('9999'), ctx)
     self.assertRaises(ValueError, convertToData, directive('HAHAHAHA'),
                       ctx)
예제 #7
0
 def test_function_accessor(self):
     def foo(context, data):
         return 42
     ctx = self.makeContext()
     self.assertEquals(42, convertToData(foo, ctx))
     d = directive('this wont work')
     ctx2 = self.makeContext(foo)
     self.assertRaises(NoAccessor, convertToData, d, ctx2)
예제 #8
0
    def test_function_accessor(self):
        def foo(context, data):
            return 42

        ctx = self.makeContext()
        self.assertEquals(42, convertToData(foo, ctx))
        d = directive('this wont work')
        ctx2 = self.makeContext(foo)
        self.assertRaises(NoAccessor, convertToData, d, ctx2)
예제 #9
0
def FunctionSerializer(original, context, nocontextfun=FunctionSerializer_nocontext):
    if context.precompile:
        return WovenContext(tag=invisible(render=original))
    else:
        data = convertToData(context.locate(IData), context)
        try:
            nocontext = nocontextfun(original)
            if nocontext is True:
                result = original(data)
            else:
                if nocontext is PASS_SELF:
                    renderer = context.locate(IRenderer)
                    result = original(renderer, context, data)
                else:
                    result = original(context, data)
        except StopIteration:
            raise RuntimeError, "User function %r raised StopIteration." % original
        return serialize(result, context)
예제 #10
0
def FunctionSerializer(original, context, nocontextfun=FunctionSerializer_nocontext):
    if context.precompile:
        return WovenContext(tag=invisible(render=original))
    else:
        data = convertToData(context.locate(IData), context)
        try:
            nocontext = nocontextfun(original)
            if nocontext is True:
                if hasattr(original, "__code__") and (
                    original.__code__.co_argcount == 3
                    or (original.__code__.co_argcount == 2 and original.__code__.co_varnames[0] != "self")
                ):
                    result = original(context, data)
                else:
                    result = original(data)
            else:
                if nocontext is PASS_SELF:
                    renderer = context.locate(IRenderer)
                    result = original(renderer, context, data)
                else:
                    result = original(context, data)
        except StopIteration:
            raise RuntimeError("User function %r raised StopIteration." % original)
        return serialize(result, context)
예제 #11
0
 def test_simple(self):
     d = directive('foo')
     ctx = self.makeContext()
     self.assertEquals(thefoo, convertToData(d, ctx))
예제 #12
0
 def test_dict_directive(self):
     d = directive('one')
     ctx = self.makeContext({'one': 1, 'two': 2})
     self.assertEquals(1, convertToData(d, ctx))
     self.assertRaises(KeyError, convertToData, directive('asdfasdf'), ctx)
예제 #13
0
 def test_1_noneOriginal(self):
     data = None
     ctx = context.WovenContext()
     ctx.remember(APage(data), inevow.IData)
     self.assertEquals(data, convertToData(ctx.locate(inevow.IData), ctx))
     self.assertEquals('foo', convertToData(directive('foo'), ctx))
예제 #14
0
def TagSerializer(original, context, contextIsMine=False):
    """
    Original is the tag.
    Context is either:
      - the context of someone up the chain (if contextIsMine is False)
      - this tag's context (if contextIsMine is True)
    """
    visible = bool(original.tagName)
    if visible and context.isAttrib:
        raise RuntimeError, "Tried to render tag '%s' in an tag attribute context." % (original.tagName)
    if context.precompile and original.macro:
        toBeRenderedBy = original.macro
        if isinstance(toBeRenderedBy, directive):
            toBeRenderedBy = IMacroFactory(context).macro(context, toBeRenderedBy.name)
        original.macro = Unset
        newContext = WovenContext(context, original)
        yield serialize(toBeRenderedBy(newContext), newContext)
        return
    if context.precompile and (
        [x for x in original._specials.values() 
        if x is not None and x is not Unset]
        or original.slotData):
        nestedcontext = WovenContext(precompile=context.precompile, isAttrib=context.isAttrib)
        original = original.clone(deep=False)
        if not contextIsMine:
            context = WovenContext(context, original)
        context.tag.children = precompile(context.tag.children, nestedcontext)
        yield context
        return
    if original.pattern is not Unset and original.pattern is not None:
        return
    if not contextIsMine:
        if original.render:
            original = original.clone(deep=False)
        context = WovenContext(context, original)
    if original.data is not Unset:
        newdata = convertToData(original.data, context)
        if isinstance(newdata, util.Deferred):
            yield newdata.addCallback(lambda newdata: _datacallback(newdata, context))
        else:
            _datacallback(newdata, context)
    if original.render:
        toBeRenderedBy = original.render
        original._clearSpecials()
        yield serialize(toBeRenderedBy, context)
        return
    if not visible:
        for child in original.children:
            yield serialize(child, context)
        return
    yield '<%s' % original.tagName
    if original.attributes:
        attribContext = WovenContext(parent=context, precompile=context.precompile, isAttrib=True)
        for (k, v) in original.attributes.iteritems():
            if v is None:
                continue
            yield ' %s="' % k
            yield serialize(v, attribContext)
            yield '"'
    if not original.children:
        if original.tagName in allowSingleton:
            yield ' />'
        else:
            yield '></%s>' % original.tagName
    else:
        yield '>'
        for child in original.children:
            yield serialize(child, context)        
        yield '</%s>' % original.tagName
예제 #15
0
 def test_roundAndRound(self):
     ctx = self.makeContext(directive('factory'), directive('0'),
                            directive('factory'))
     self.assertEquals(f, convertToData(directive('0'), ctx))
예제 #16
0
 def test_dict_through_directive(self):
     d1, d2 = directive('dict'), directive('one')
     ctx = self.makeContext(d1)
     self.assertEquals(1, convertToData(d2, ctx))
예제 #17
0
 def test_list_directive(self):
     d= directive('2')
     ctx = self.makeContext([0, 1, 42, 3, 4])
     self.assertEquals(42, convertToData(d, ctx))
     self.assertRaises(IndexError, convertToData, directive('9999'), ctx)
     self.assertRaises(ValueError, convertToData, directive('HAHAHAHA'), ctx)
예제 #18
0
 def test_dict_directive(self):
     d = directive('one')
     ctx = self.makeContext({'one': 1, 'two': 2})
     self.assertEquals(1, convertToData(d, ctx))
     self.assertRaises(KeyError, convertToData, directive('asdfasdf'), ctx)
예제 #19
0
 def test_simple(self):
     d = directive('foo')
     ctx = self.makeContext()
     self.assertEquals(thefoo, convertToData(d, ctx))
예제 #20
0
 def test_dict_through_directive(self):
     d1, d2 = directive('dict'), directive('one')
     ctx = self.makeContext(d1)
     self.assertEquals(1, convertToData(d2, ctx))
예제 #21
0
 def test_list_through_directive(self):
     d1, d2 = directive('list'), directive('1')
     ctx = self.makeContext(d1)
     self.assertEquals(99, convertToData(d2, ctx))
예제 #22
0
 def test_list_through_directive(self):
     d1, d2 = directive('list'), directive('1')
     ctx = self.makeContext(d1)
     self.assertEquals(99, convertToData(d2, ctx))
예제 #23
0
def TagSerializer(original, context, contextIsMine=False):
    """
    Original is the tag.
    Context is either:
      - the context of someone up the chain (if contextIsMine is False)
      - this tag's context (if contextIsMine is True)
    """
    #    print "TagSerializer:",original, "ContextIsMine",contextIsMine, "Context:",context
    visible = bool(original.tagName)

    if visible and context.isAttrib:
        raise RuntimeError, "Tried to render tag '%s' in an tag attribute context." % (
            original.tagName)

    if context.precompile and original.macro:
        toBeRenderedBy = original.macro
        ## Special case for directive; perhaps this could be handled some other way with an interface?
        if isinstance(toBeRenderedBy, directive):
            toBeRenderedBy = IMacroFactory(context).macro(
                context, toBeRenderedBy.name)
        original.macro = Unset
        newContext = WovenContext(context, original)
        yield serialize(toBeRenderedBy(newContext), newContext)
        return

    ## TODO: Do we really need to bypass precompiling for *all* specials?
    ## Perhaps just render?
    if context.precompile and ([
            x for x in original._specials.values()
            if x is not None and x is not Unset
    ] or original.slotData):
        ## The tags inside this one get a "fresh" parent chain, because
        ## when the context yielded here is serialized, the parent
        ## chain gets reconnected to the actual parents at that
        ## point, since the render function here could change
        ## the actual parentage hierarchy.
        nestedcontext = WovenContext(precompile=context.precompile,
                                     isAttrib=context.isAttrib)

        # If necessary, remember the MacroFactory onto the new context chain.
        macroFactory = IMacroFactory(context, None)
        if macroFactory is not None:
            nestedcontext.remember(macroFactory, IMacroFactory)

        original = original.clone(deep=False)
        if not contextIsMine:
            context = WovenContext(context, original)
        context.tag.children = precompile(context.tag.children, nestedcontext)

        yield context
        return

    ## Don't render patterns
    if original.pattern is not Unset and original.pattern is not None:
        return

    if not contextIsMine:
        if original.render:
            ### We must clone our tag before passing to a render function
            original = original.clone(deep=False)
        context = WovenContext(context, original)

    if original.data is not Unset:
        newdata = convertToData(original.data, context)
        if isinstance(newdata, util.Deferred):
            yield newdata.addCallback(
                lambda newdata: _datacallback(newdata, context))
        else:
            _datacallback(newdata, context)

    if original.render:
        ## If we have a render function we want to render what it returns,
        ## not our tag
        toBeRenderedBy = original.render
        # erase special attribs so if the renderer returns the tag,
        # the specials won't be on the context twice.
        original._clearSpecials()
        yield serialize(toBeRenderedBy, context)
        return

    if not visible:
        for child in original.children:
            yield serialize(child, context)
        return

    yield '<%s' % original.tagName
    if original.attributes:
        attribContext = WovenContext(parent=context,
                                     precompile=context.precompile,
                                     isAttrib=True)
        for (k, v) in original.attributes.iteritems():
            if v is None:
                continue
            yield ' %s="' % k
            yield serialize(v, attribContext)
            yield '"'
    if not original.children:
        if original.tagName in allowSingleton:
            yield ' />'
        else:
            yield '></%s>' % original.tagName
    else:
        yield '>'
        for child in original.children:
            yield serialize(child, context)
        yield '</%s>' % original.tagName
예제 #24
0
 def test_roundAndRound(self):
     ctx = self.makeContext(
         directive('factory'), directive('0'), directive('factory')
     )
     self.assertEquals(f, convertToData(directive('0'), ctx))
예제 #25
0
def TagSerializer(original, context, contextIsMine=False):
    """
    Original is the tag.
    Context is either:
      - the context of someone up the chain (if contextIsMine is False)
      - this tag's context (if contextIsMine is True)
    """
    #    print "TagSerializer:",original, "ContextIsMine",contextIsMine, "Context:",context
    visible = bool(original.tagName)

    if visible and context.isAttrib:
        raise RuntimeError("Tried to render tag '%s' in an tag attribute context." % (original.tagName))

    if context.precompile and original.macro:
        toBeRenderedBy = original.macro
        ## Special case for directive; perhaps this could be handled some other way with an interface?
        if isinstance(toBeRenderedBy, directive):
            toBeRenderedBy = IMacroFactory(context).macro(context, toBeRenderedBy.name)
        original.macro = Unset
        newContext = WovenContext(context, original)
        yield serialize(toBeRenderedBy(newContext), newContext)
        return

    ## TODO: Do we really need to bypass precompiling for *all* specials?
    ## Perhaps just render?
    if context.precompile and (
        [x for x in list(original._specials.values()) if x is not None and x is not Unset] or original.slotData
    ):
        ## The tags inside this one get a "fresh" parent chain, because
        ## when the context yielded here is serialized, the parent
        ## chain gets reconnected to the actual parents at that
        ## point, since the render function here could change
        ## the actual parentage hierarchy.
        nestedcontext = WovenContext(precompile=context.precompile, isAttrib=context.isAttrib)

        # If necessary, remember the MacroFactory onto the new context chain.
        macroFactory = IMacroFactory(context, None)
        if macroFactory is not None:
            nestedcontext.remember(macroFactory, IMacroFactory)

        original = original.clone(deep=False)
        if not contextIsMine:
            context = WovenContext(context, original)
        context.tag.children = precompile(context.tag.children, nestedcontext)

        yield context
        return

    ## Don't render patterns
    if original.pattern is not Unset and original.pattern is not None:
        return

    if not contextIsMine:
        if original.render:
            ### We must clone our tag before passing to a render function
            original = original.clone(deep=False)
        context = WovenContext(context, original)

    if original.data is not Unset:
        newdata = convertToData(original.data, context)
        if isinstance(newdata, util.Deferred):
            yield newdata.addCallback(lambda newdata: _datacallback(newdata, context))
        else:
            _datacallback(newdata, context)

    if original.render:
        ## If we have a render function we want to render what it returns,
        ## not our tag
        toBeRenderedBy = original.render
        # erase special attribs so if the renderer returns the tag,
        # the specials won't be on the context twice.
        original._clearSpecials()
        yield serialize(toBeRenderedBy, context)
        return

    if not visible:
        for child in original.children:
            yield serialize(child, context)
        return

    yield "<%s" % original.tagName
    if original.attributes:
        attribContext = WovenContext(parent=context, precompile=context.precompile, isAttrib=True)
        for (k, v) in sorted(original.attributes.items()):
            if v is None:
                continue
            yield ' %s="' % k
            yield serialize(v, attribContext)
            yield '"'
    if not original.children:
        if original.tagName in allowSingleton:
            yield " />"
        else:
            yield "></%s>" % original.tagName
    else:
        yield ">"
        for child in original.children:
            yield serialize(child, context)
        yield "</%s>" % original.tagName
예제 #26
0
 def test_1_noneOriginal(self):
     data = None
     ctx = context.WovenContext()
     ctx.remember(APage(data), inevow.IData)
     self.assertEquals(data, convertToData(ctx.locate(inevow.IData), ctx))
     self.assertEquals('foo', convertToData(directive('foo'), ctx))