chessai.escort.parser

 1import os
 2import typing
 3
 4import chessai.chess.piece
 5import chessai.core.parser
 6import chessai.tour.parser
 7
 8THIS_DIR: str = os.path.join(os.path.dirname(os.path.realpath(__file__)))
 9ESCORT_DIR: str = os.path.join(THIS_DIR, '..', 'resources', 'escort')
10
11ESCORT_FILE_EXTENSION: str = '.json'
12
13def parse_escort(data: str,
14               default_dir: str = ESCORT_DIR,
15               default_extension: str = ESCORT_FILE_EXTENSION,
16               string_parser: chessai.core.parser.GameStateStringParser = chessai.tour.parser.parse_tour_from_string,
17               accepts_raw_string: bool = False,
18               options: dict[str, typing.Any] | None = None,
19               **kwargs: typing.Any) -> chessai.core.parser.ParsedGameState:
20    """
21    Parse an Escort GameState from a file path.
22
23    If the filepath does not exist, the default directory and file extension are added.
24    """
25
26    parsed_state = chessai.core.parser.parse_fen(data,
27            default_dir = default_dir,
28            default_extension = default_extension,
29            string_parser = string_parser,
30            accepts_raw_string = accepts_raw_string,
31            **kwargs)
32
33    # Escort games must at least one king on the agent's turn, which is always the starting turn.
34    for _, piece in parsed_state.pieces.items():
35        if not isinstance(piece, chessai.chess.piece.King):
36            continue
37
38        # Check that the king is on the agent's team.
39        if (piece.color != parsed_state.turn):
40            continue
41
42        return parsed_state
43
44    raise ValueError('Escort games must have at least one king for the agent')
THIS_DIR: str = '/home/runner/work/chessai/chessai/chessai/escort'
ESCORT_DIR: str = '/home/runner/work/chessai/chessai/chessai/escort/../resources/escort'
ESCORT_FILE_EXTENSION: str = '.json'
def parse_escort( data: str, default_dir: str = '/home/runner/work/chessai/chessai/chessai/escort/../resources/escort', default_extension: str = '.json', string_parser: chessai.core.parser.GameStateStringParser = <function parse_tour_from_string>, accepts_raw_string: bool = False, options: dict[str, typing.Any] | None = None, **kwargs: Any) -> chessai.core.parser.ParsedGameState:
14def parse_escort(data: str,
15               default_dir: str = ESCORT_DIR,
16               default_extension: str = ESCORT_FILE_EXTENSION,
17               string_parser: chessai.core.parser.GameStateStringParser = chessai.tour.parser.parse_tour_from_string,
18               accepts_raw_string: bool = False,
19               options: dict[str, typing.Any] | None = None,
20               **kwargs: typing.Any) -> chessai.core.parser.ParsedGameState:
21    """
22    Parse an Escort GameState from a file path.
23
24    If the filepath does not exist, the default directory and file extension are added.
25    """
26
27    parsed_state = chessai.core.parser.parse_fen(data,
28            default_dir = default_dir,
29            default_extension = default_extension,
30            string_parser = string_parser,
31            accepts_raw_string = accepts_raw_string,
32            **kwargs)
33
34    # Escort games must at least one king on the agent's turn, which is always the starting turn.
35    for _, piece in parsed_state.pieces.items():
36        if not isinstance(piece, chessai.chess.piece.King):
37            continue
38
39        # Check that the king is on the agent's team.
40        if (piece.color != parsed_state.turn):
41            continue
42
43        return parsed_state
44
45    raise ValueError('Escort games must have at least one king for the agent')

Parse an Escort GameState from a file path.

If the filepath does not exist, the default directory and file extension are added.