# -*- test-case-name: nevow.test.test_newflat -*-
# Copyright (c) 2008 Divmod.
# See LICENSE for details.

"""
Context-free flattener/serializer for rendering Python objects, possibly
complex or arbitrarily nested, as strings.

Immediate future plans:

  - Deprecate IRenderer and getFlattener cases in _flatten
  - Write a precompiler which is more friendly towards _flatten
  - Write a pretty "render stack" formatter for the information in
    FlattenerError._roots

"""

from sys import exc_info
from types import GeneratorType
from traceback import extract_tb, format_list

from twisted.internet.defer import Deferred
from twisted.python import compat

from nevow.inevow import IRenderable, IRenderer, IRendererFactory, IData
from nevow.inevow import IRequest
from nevow.url import URL
from nevow.stan import _PrecompiledSlot, Unset, Proto, Tag, Entity, slot, xml
from nevow.stan import directive
from nevow.context import WovenContext
from nevow.flat.flatstan import allowSingleton
from nevow.flat import flattenFactory
from nevow.flat.ten import getFlattener
from nevow.tags import raw


class FlattenerError(Exception):
    """
    An error occurred while flattening an object.

    @ivar _roots: A list of the objects on the flattener's stack at the time
        the unflattenable object was encountered.  The first element is least
        deeply nested object and the last element is the most deeply nested.
    """
    def __init__(self, exception, roots, traceback):
        self._exception = exception
        self._roots = roots
        self._traceback = traceback
        Exception.__init__(self, exception, roots, traceback)


    def _formatRoot(self, obj):
        """
        Convert an object from C{self._roots} to a string suitable for
        inclusion in a render-traceback (like a normal Python traceback, but
        can include "frame" source locations which are not in Python source
        files).

        @param obj: Any object which can be a render step I{root}.
            Typically, L{Tag}s, strings, and other simple Python types.

        @return: A string representation of C{obj}.
        @rtype: L{str}
        """
        if isinstance(obj, (bytes, compat.unicode)):
            # It's somewhat unlikely that there will ever be a bytes in the roots
            # list.  However, something like a MemoryError during a bytes.replace
            # call (eg, replacing " with &quot;) could possibly cause this.
            # Likewise, UTF-8 encoding a unicode string to a byte string might
            # fail like this.
            if len(obj) > 40:
                if isinstance(obj, bytes):
                    prefix = 1
                else:
                    prefix = 2
                return repr(obj[:20])[:-1] + '<...>' + repr(obj[-20:])[prefix:]
            else:
                return repr(obj)
        elif isinstance(obj, Tag):
            if obj.filename is None:
                return 'Tag <' + obj.tagName + '>'
            else:
                return "File \"%s\", line %d, column %d, in \"%s\"" % (
                    obj.filename, obj.lineNumber,
                    obj.columnNumber, obj.tagName)
        else:
            return repr(obj)


    def __repr__(self):
        if self._roots:
            roots = '  ' + '\n  '.join([
                    self._formatRoot(r) for r in self._roots]) + '\n'
        else:
            roots = ''
        if self._traceback:
            traceback = '\n'.join([
                    line
                    for entry in format_list(self._traceback)
                    for line in entry.splitlines()]) + '\n'
        else:
            traceback = ''
        return (
            'Exception while flattening:\n' +
            roots + traceback +
            self._exception.__class__.__name__ + ': ' +
            str(self._exception) + '\n')


    def __str__(self):
        return repr(self)



class UnfilledSlot(Exception):
    """
    During flattening, a slot with no associated data was encountered.
    """
    def __str__(self):
        return 'UnfilledSlot(%r)' % self.args



class UnsupportedType(Exception):
    """
    During flattening, an object of a type which cannot be flattened was
    encountered.
    """
    def __str__(self):
        return "UnsupportedType(%r)" % self.args



def escapedData(data, inAttribute, inXML):
    """
    Escape a string for inclusion in a document.

    @type data: C{str}
    @param data: The string to escape.

    @type inAttribute: C{bool}
    @param inAttribute: A flag which, if set, indicates that the string should
        be quoted for use as the value of an XML tag value.

    @type inXML: C{bool}
    @param inXML: A flag which, if set, indicates that the string should be
        quoted for use as an XML text node or as the value of an XML tag value.

    @rtype: C{str}
    @return: The quoted form of C{data} as a native string.
    """
    if inXML or inAttribute:
        data = data.replace(u'&', u'&amp;'
            ).replace(u'<', u'&lt;'
            ).replace(u'>', u'&gt;')
    if inAttribute:
        data = data.replace(u'"', u'&quot;')

    if not isinstance(data, compat.unicode):
        data = compat.unicode(data)

    return data



def _ctxForRequest(request, slotData, renderFactory, inAttribute):
    """
    Create a L{WovenContext} which can be used to by the
    backwards-compatibility support of L{IRenderer} and L{getFlattener} to
    continue rendering a response for the given request.
    """
    ctx = WovenContext()
    ctx.isAttrib = inAttribute
    ctx.remember(None, IData) # Even though IData(ctx) can never return None,
                              # remembering None here is somehow very important
                              # for preventing a TypeError from happening when
                              # ctx.locate(IData) is invoked, since it is
                              # sometimes invoked from a codepath other than
                              # __conform__. -exarkun
    ctx.remember(request, IRequest)
    for slotGroup in slotData:
        if slotGroup is not None:
            for k, v in list(slotGroup.items()):
                ctx.fillSlots(k, v)
    if renderFactory is not None:
        ctx.remember(_OldRendererFactory(renderFactory), IRendererFactory)
    return ctx



def _getSlotValue(name, slotData):
    """
    Find the value of the named slot in the given stack of slot data.
    """
    for slotFrame in slotData[::-1]:
        if slotFrame is not None and name in slotFrame:
            return slotFrame[name]
    else:
        raise UnfilledSlot(name)



def _flatten(request, write, root, slotData, renderFactory, inAttribute, inXML):
    """
    Make C{root} slightly more flat by yielding all or part of it as strings or
    generators.

    @param request: A request object which will be passed to
        L{IRenderable.render}.

    @param root: An object to be made flatter.  This may be of type C{unicode},
        C{str}, L{raw}, L{Proto}, L{xml}, L{slot}, L{_PrecompiledSlot}, L{Tag},
        L{URL}, L{tuple}, L{list}, L{GeneratorType}, L{Entity}, L{Deferred}, or
        it may be an object which is adaptable to L{IRenderable}.  Deprecated
        backwards-compatibility support is also present for objects adaptable
        to L{IRenderer} or for which a flattener has been registered via
        L{registerFlattener}.

    @param slotData: A C{list} of C{dict} mapping C{str} slot names to data
        with which those slots will be replaced.

    @param inAttribute: A flag which, if set, indicates that C{str} and
        C{unicode} instances encountered must be quoted as for XML tag
        attribute values.

    @param inXML: A flag which, if set, indicates that C{str} and C{unicode}
        instances encountered must be quoted as for XML text node data.

    @return: An iterator which yields C{str}, L{Deferred}, and more iterators
        of the same type.
    """
    if isinstance(root, WovenContext):
        # WovenContext is supported via the getFlattener case, but that is a
        # very slow case.  Checking here is an optimization.  It also lets us
        # avoid the deprecation warning which would be emitted whenever a
        # precompiled document was flattened, since those contain WovenContexts
        # for tags with render directives. -exarkun
        inAttribute = root.isAttrib
        inXML = True
        root = root.tag

    if isinstance(root, raw):
        root = compat.unicode(root)
        if inAttribute:
            root = root.replace('"', '&quot;')
        write(root)
    elif isinstance(root, Proto):
        root = compat.unicode(root)
        if root:
            if root in allowSingleton:
                write(u'<' + root + u' />')
            else:
                write(u'<' + root + '></' + root + '>')
    elif isinstance(root, (compat.unicode, bytes)):
        write(escapedData(root, inAttribute, inXML))
    elif isinstance(root, slot):
        slotValue = _getSlotValue(root.name, slotData)
        yield _flatten(request, write, slotValue, slotData, renderFactory,
                       inAttribute, inXML)
    elif isinstance(root, _PrecompiledSlot):
        slotValue = _getSlotValue(root.name, slotData)
        yield _flatten(request, write, slotValue, slotData, renderFactory,
                       root.isAttrib, inXML)
    elif isinstance(root, Tag):
        if root.pattern is Unset or root.pattern is None:
            slotData.append(root.slotData)
            if root.render is Unset:
                if not root.tagName:
                    yield _flatten(request, write, root.children,
                                   slotData, renderFactory,
                                   False, True)
                else:
                    write(u'<')
                    if isinstance(root.tagName, bytes):
                        tagName = root.tagName.decode('utf-8')
                    else:
                        tagName = compat.unicode(root.tagName)
                    write(tagName)
                    for k, v in sorted(root.attributes.items()):
                        if isinstance(k, bytes):
                            k = k.decode('utf-8')
                        write(u" " + k + "=\"")
                        yield _flatten(request, write, v, slotData,
                                       renderFactory, True, True)
                        write(u"\"")
                    if root.children or tagName not in allowSingleton:
                        write(u'>')
                        yield _flatten(request, write, root.children,
                                       slotData, renderFactory,
                                       False, True)
                        write(u'</' + tagName + '>')
                    else:
                        write(u' />')
            else:
                if isinstance(root.render, directive):
                    rendererName = root.render.name
                else:
                    rendererName = root.render
                root = root.clone(False)
                del root._specials['render']
                result = renderFactory.renderer(rendererName)(request, root)
                yield _flatten(request, write, result, slotData, renderFactory,
                               None, inXML)
            slotData.pop()
    elif isinstance(root, URL):
        write(escapedData(compat.unicode(root), inAttribute, inXML))
    elif isinstance(root, (tuple, list, GeneratorType)):
        for element in root:
            yield _flatten(request, write, element, slotData, renderFactory,
                           inAttribute, inXML)
    elif isinstance(root, Entity):
        write(u'&#')
        write(root.num)
        write(u';')
    elif isinstance(root, xml):
        if isinstance(root.content, bytes):
            write(root.content.decode('utf-8'))
        else:
            write(root.content)
    elif isinstance(root, Deferred):
        yield root.addCallback(
            lambda result: (result, _flatten(request, write, result, slotData,
                                             renderFactory, inAttribute,
                                             inXML)))
    else:
        renderable = IRenderable(root, None)
        if renderable is not None:
            # [] for the slotData parameter of this call to _flatten means
            # slots returned by this renderable's render method won't be filled
            # with data which has so far accumulated in the slotData stack.
            # This seems like a reasonable thing to me, since a renderable is a
            # piece of Python code.  It should be isolated from this other
            # stuff, which is primarily data. -exarkun
            yield _flatten(request, write, renderable.render(request), [],
                           renderable, inAttribute, inXML)
        else:
            renderer = IRenderer(root, None)
            if renderer is not None:
                ctx = _ctxForRequest(request, slotData, None, inAttribute)
                results = []
                synchronous = []
                flattened = flattenFactory(renderer, ctx, results.append,
                                           lambda ign: None)
                def cbFlattened(result):
                    synchronous.append(None)
                    return (result, (compat.unicode(s) for s in results))
                flattened.addCallback(cbFlattened)
                if synchronous:
                    write(u''.join(map(str, results)))
                else:
                    yield flattened
            else:
                flattener = getFlattener(root)
                if flattener is not None:
                    ctx = _ctxForRequest(request, slotData, renderFactory,
                                         inAttribute)
                    yield _flatten(request, write, flattener(root, ctx),
                                   slotData, renderFactory, False, False)
                else:
                    raise UnsupportedType(root)



class _OldRendererFactory(object):
    """
    Adapter from L{IRenderable} to L{IRenderFactory}, used to provide support
    for using the old flattener on new kinds of view objects.
    """
    def __init__(self, newRendererFactory):
        self.newRendererFactory = newRendererFactory


    def renderer(self, context, name):
        f = self.newRendererFactory.renderer(name)
        def render(ctx, data):
            return f(
                IRequest(ctx, None),
                ctx.tag,
                )
        return render



def flatten(request, write, root, inAttribute, inXML):
    """
    Make C{root} into an iterable of C{str} and L{Deferred}.

    @param request: A request object which will be passed to
        L{IRenderable.render}.

    @param root: An object to be made flatter.  This may be of type C{unicode},
        C{str}, L{Proto}, L{slot}, L{Tag}, L{URL}, L{tuple}, L{list},
        L{Entity}, L{Deferred}, or it may be an object which is adaptable to
        L{IRenderable}.

    @type inAttribute: C{bool}
    @param inAttribute: A flag which, if set, indicates that the string should
        be quoted for use as the value of an XML tag value.

    @type inXML: C{bool}
    @param inXML: A flag which, if set, indicates that the string should be
        quoted for use as an XML text node or as the value of an XML tag value.

    @return: An iterator which yields objects of type C{str} and L{Deferred}.
        A L{Deferred} is only yielded when one is encountered in the process of
        flattening C{root}.  The returned iterator must not be iterated again
        until the L{Deferred} is called back.
    """
    stack = [_flatten(request, write, root, [], None, inAttribute, inXML)]
    while stack:
        try:
            # In Python 2.5, after an exception, a generator's gi_frame is
            # None.
            frame = stack[-1].gi_frame
            element = next(stack[-1])
        except StopIteration:
            stack.pop()
        except Exception as e:
            stack.pop()
            roots = []
            for generator in stack:
                roots.append(generator.gi_frame.f_locals.get('root', ''))
            roots.append(frame.f_locals['root'])
            raise FlattenerError(e, roots, extract_tb(exc_info()[2]))
        else:
            if isinstance(element, (bytes, compat.unicode)):
                write(element)
            elif isinstance(element, Deferred):
                def cbx(xxx_todo_changeme):
                    (original, toFlatten) = xxx_todo_changeme
                    stack.append(toFlatten)
                    return original
                yield element.addCallback(cbx)
            else:
                stack.append(element)



def _flattensome(state, write, schedule, result):
    """
    Take strings from an iterator and pass them to a writer function.

    @param state: An iterator of C{str} and L{Deferred}.  C{str} instances will
        be passed to C{write}.  L{Deferred} instances will be waited on before
        resuming iteration of C{state}.

    @param write: A callable which will be invoked with each C{str}
        produced by iterating C{state}.

    @param schedule: A callable which will arrange for a function to be called
        with some positional arguments I{later}.  This is used to avoid
        unbounded call stack depth due to already-fired L{Deferred}s produced
        by C{state}.

    @param result: A L{Deferred} which will be called back when C{state} has
        been completely flattened into C{write} or which will be errbacked if
        an unexpected exception occurs.

    @return: C{None}
    """
    while True:
        try:
            element = next(state)
        except StopIteration:
            result.callback(None)
        except:
            result.errback()
        else:
            if type(element) is str:
                write(element)
                continue
            else:
                def cby(original):
                    schedule(_flattensome, state, write, schedule, result)
                    return original
                element.addCallbacks(cby, result.errback)
        break



def _schedule(f, *a):
    """
    Scheduler for use with L{_flattensome} which uses L{IReactorTime.callLater}
    to schedule calls.  This works around the fact that L{Deferred} can exceed
    the stack limit in certain cases.
    """
    # XXX SUCKY
    from twisted.internet import reactor
    reactor.callLater(0, f, *a)



def deferflatten(request, root, inAttribute, inXML, write):
    """
    Incrementally write out a string representation of C{root} using C{write}.

    In order to create a string representation, C{root} will be decomposed into
    simpler objects which will themselves be decomposed and so on until strings
    or objects which can easily be converted to strings are encountered.

    @param request: A request object which will be passed to the C{render}
        method of any L{IRenderable} provider which is encountered.

    @param root: An object to be made flatter.  This may be of type C{unicode},
        C{str}, L{raw}, L{Proto}, L{xml}, L{slot}, L{_PrecompiledSlot}, L{Tag},
        L{URL}, L{tuple}, L{list}, L{GeneratorType}, L{Entity}, L{Deferred}, or
        it may be an object which is adaptable to L{IRenderable}.  Deprecated
        backwards-compatibility support is also present for objects adaptable
        to L{IRenderer} or for which a flattener has been registered via
        L{registerFlattener}.

    @type inAttribute: C{bool}
    @param inAttribute: A flag which, if set, indicates that the string should
        be quoted for use as the value of an XML tag value.

    @type inXML: C{bool}
    @param inXML: A flag which, if set, indicates that the string should be
        quoted for use as an XML text node or as the value of an XML tag value.

    @param write: A callable which will be invoked with each C{str}
        produced by flattening C{root}.

    @return: A L{Deferred} which will be called back when C{root} has
        been completely flattened into C{write} or which will be errbacked if
        an unexpected exception occurs.
    """
    result = Deferred()
    state = flatten(request, write, root, inAttribute, inXML)
    _flattensome(state, write, _schedule, result)
    return result
