예제 #1
0
def _action_stack_to_python(block, tw, name="start"):
    """ Turn a stack of blocks into Python code
    name -- the name of the action stack (defaults to "start") """

    if isinstance(name, int):
        name = float(name)
    if not isinstance(name, basestring):
        name = str(name)

    # traverse the block stack and get the AST for every block
    ast_list = _walk_action_stack(block, tw.lc)
    if not ast_list or not isinstance(ast_list[-1], ast.Yield):
        ast_list.append(ast_yield_true())
    action_stack_ast = ast.Module(body=ast_list)

    # serialize the ASTs into python code
    generated_code = codegen.to_source(action_stack_ast)

    # wrap the action stack setup code around everything
    name_id = _make_identifier(name)
    if name == "start":
        pre_preamble = _START_STACK_START_ADD
        for k in plugins_in_use:
            pre_preamble += "    global %s\n" % (k.lower(),)
            pre_preamble += "    %s = global_objects['%s']\n" % (k.lower(), k)
    else:
        pre_preamble = ""
    generated_code = _indent(generated_code, 1)
    if generated_code.endswith(linesep):
        newline = ""
    else:
        newline = linesep
    snippets = [
        _ACTION_STACK_START % (name_id),
        pre_preamble,
        _ACTION_STACK_PREAMBLE,
        generated_code,
        newline,
        _ACTION_STACK_END % (name, name_id),
    ]
    return "".join(snippets)
예제 #2
0
def _action_stack_to_python(block, tw, name='start'):
    """ Turn a stack of blocks into Python code
    name -- the name of the action stack (defaults to "start") """

    if isinstance(name, int):
        name = float(name)
    if not isinstance(name, basestring):
        name = str(name)

    # traverse the block stack and get the AST for every block
    ast_list = _walk_action_stack(block, tw.lc)
    if not ast_list or not isinstance(ast_list[-1], ast.Yield):
        ast_list.append(ast_yield_true())
    action_stack_ast = ast.Module(body=ast_list)

    # serialize the ASTs into python code
    generated_code = codegen.to_source(action_stack_ast)

    # wrap the action stack setup code around everything
    name_id = _make_identifier(name)
    if name == 'start':
        pre_preamble = _START_STACK_START_ADD
        for k in plugins_in_use:
            pre_preamble += '    global %s\n' % (k.lower(),)
            pre_preamble += "    %s = global_objects['%s']\n" % (k.lower(), k)
    else:
        pre_preamble = ''
    generated_code = _indent(generated_code, 1)
    if generated_code.endswith(linesep):
        newline = ''
    else:
        newline = linesep
    snippets = [_ACTION_STACK_START % (name_id),
                pre_preamble,
                _ACTION_STACK_PREAMBLE,
                generated_code,
                newline,
                _ACTION_STACK_END % (name, name_id)]
    return ''.join(snippets)