Example #1
0
def view_html(request):
    """This function expects to receive a context object, having
    a pipeline and an execution as attributes.
    Each pipeline element is either a resource or an action.
    While iterating through the pipeline, each time a resource is found
    it is pushed into the execution's stack. If an action is found it
    pops its parameters from the execution stack, optionally pushing
    its results on to the top of the stack.
    """
    # FIXME
    ##    import pdb; pdb.set_trace()
    uri = request.META['REQUEST_URI'][14:]  #9: strip-off '/web/actionlet'
    # preserve pipeline history
    # this may be needed is some action or resource
    # needs to look into the past
    pipeline = PipelineQueue(uri)
    # An Executor instance is needed to hold the
    # execution stack and index to which is the current
    # pipeline's stage
    config = pyisis.config.config
    # FIXME
    execution = Execution([Engine.collection])

    # prepare context object for actionlets
    context = StringIO()
    context.execution = execution
    context.pipeline = pipeline
    context.META = request.META
    params = None

    for index, (stage, params) in enumerate(pipeline):
        if stage.startswith(ACTION_SEP):
            # this is an action. Call it!
            action = getattr(action_registry, stage[1:])
            action(context, params)
        else:
            # This is a resource. Load it!
            # So far a resource string is a key for
            # a value inside the resource container object
            # placed at the top of the stack
            resource_container = execution.pop()
            key = stage
            new_resource = resource_container[key]
            execution.append(new_resource)
        execution.stage_index += 1

    # If something is left at the top of the stack
    # it will be added to the HTTP response
    try:
        resource = context.execution.pop()
        context.write(unicode(resource).encode(config.OUTPUT_ENCODING))
        # Handle encoding
    except IndexError:
        pass

    return render_to_response('actionlet.html', {
        'title': 'ISIS NBP Cell',
        'verbatim': context.getvalue()
    },
                              context_instance=RequestContext(request))