(ast, file_path, package_lua, lua_path=None)
Evaluate require() statements in a Lua AST.

    See the picotool README.md for a complete description of the intended
    behavior of require().

    This function is called recursively on require()'d files.

    Args:
        ast: A lua.Lua object.
        file_path: A path to the file containing the Lua code for the AST.
        package_lua: The master dict of require() strings to Lua ASTs.
        lua_path: The Lua load path. If None, uses: ?;?.lua;?.p8;?.p8.png

    Raises:
        FileNotFoundError.
Evaluate require() statements in a Lua AST.
Evaluate require () statements in a Lua AST .
def _evaluate_require(ast, file_path, package_lua, lua_path=None):
    """Evaluate require() statements in a Lua AST.

    See the picotool README.md for a complete description of the intended
    behavior of require().

    This function is called recursively on require()'d files.

    Args:
        ast: A lua.Lua object.
        file_path: A path to the file containing the Lua code for the AST.
        package_lua: The master dict of require() strings to Lua ASTs.
        lua_path: The Lua load path. If None, uses: ?;?.lua;?.p8;?.p8.png

    Raises:
        FileNotFoundError.
    """
    walker = RequireWalker(ast.tokens, ast.root)
    for (require_path, use_game_loop, require_token) in walker.walk():
        require_path_str = require_path.decode(encoding='utf-8')

        # Disallow chars that select files outside of the load path.
        if b'./' in require_path or require_path.startswith(b'/'):
            raise LuaBuildError(
                'require() filename cannot contain "./" or "../" or start '
                'with "/"', require_token)

        if require_path not in package_lua:
            reqd_filepath = _locate_require_file(
                require_path_str, file_path, lua_path=lua_path)
            if reqd_filepath is None:
                raise LuaBuildError(
                    'require() file {} not found; used load path {}'.format(require_path_str, lua_path),  # noqa: E501
                    require_token)

            # TODO: support loading required code from .p8 and .p8.png files.
            with open(reqd_filepath, 'rb') as infh:
                reqd_lua = lua.Lua.from_lines(
                    infh, version=game.DEFAULT_VERSION)

            # TODO: Technically the use_game_loop option needs to be part of
            # the package key because it results in a different package than
            # that without the option. As is, the first require() the parser
            # encounters determines the option. (This is not necessarily the
            # first require() the Lua interpreter encounters.)

            if not use_game_loop:
                reqd_lua.root.stats[:] = [
                    s for s in reqd_lua.root.stats
                    if not isinstance(s, parser.StatFunction) or
                    s.funcname.namepath[0].value not in GAME_LOOP_FUNCTION_NAMES]  # noqa: E501
                reqd_lua.reparse(writer_cls=lua.LuaASTEchoWriter)

            package_lua[require_path] = reqd_lua
            _evaluate_require(reqd_lua, reqd_filepath,
                              package_lua, lua_path=lua_path)
def _evaluate_require ( ast , file_path , package_lua , lua_path = None ) : walker = RequireWalker ( ast . tokens , ast . root ) for ( require_path , use_game_loop , require_token ) in walker . walk ( ) : require_path_str = require_path . decode ( encoding = 'utf-8' ) # Disallow chars that select files outside of the load path. if b'./' in require_path or require_path . startswith ( b'/' ) : raise LuaBuildError ( 'require() filename cannot contain "./" or "../" or start ' 'with "/"' , require_token ) if require_path not in package_lua : reqd_filepath = _locate_require_file ( require_path_str , file_path , lua_path = lua_path ) if reqd_filepath is None : raise LuaBuildError ( 'require() file {} not found; used load path {}' . format ( require_path_str , lua_path ) , # noqa: E501 require_token ) # TODO: support loading required code from .p8 and .p8.png files. with open ( reqd_filepath , 'rb' ) as infh : reqd_lua = lua . Lua . from_lines ( infh , version = game . DEFAULT_VERSION ) # TODO: Technically the use_game_loop option needs to be part of # the package key because it results in a different package than # that without the option. As is, the first require() the parser # encounters determines the option. (This is not necessarily the # first require() the Lua interpreter encounters.) if not use_game_loop : reqd_lua . root . stats [ : ] = [ s for s in reqd_lua . root . stats if not isinstance ( s , parser . StatFunction ) or s . funcname . namepath [ 0 ] . value not in GAME_LOOP_FUNCTION_NAMES ] # noqa: E501 reqd_lua . reparse ( writer_cls = lua . LuaASTEchoWriter ) package_lua [ require_path ] = reqd_lua _evaluate_require ( reqd_lua , reqd_filepath , package_lua , lua_path = lua_path )