chessai.puzzle.parser
1import dataclasses 2import os 3import typing 4 5import edq.util.json 6 7import chessai.core.action 8import chessai.core.parser 9 10THIS_DIR: str = os.path.join(os.path.dirname(os.path.realpath(__file__))) 11PUZZLES_DIR: str = os.path.join(THIS_DIR, '..', 'resources', 'puzzles') 12 13PUZZLE_FILE_EXTENSION: str = '.json' 14 15MOVE_LINES_KEY: str = 'move_lines' 16 17@dataclasses.dataclass 18class PuzzleInfo: 19 """ A simple dataclass to unpack puzzle data from JSON. """ 20 21 fen: str 22 """ The starting FEN for the puzzle. """ 23 24 move_lines: list[list[str]] 25 """ The expected move lines to solve the puzzle. """ 26 27def parse_puzzle_from_string(text: str, **kwargs: typing.Any) -> chessai.core.parser.ParsedGameState: 28 """ Load a puzzle from a string, which is expected to be JSON data. """ 29 30 # Extract the JSON data into the puzzle info. 31 data_dict = edq.util.json.loads(text) 32 puzzle_info = PuzzleInfo(**data_dict) 33 34 # Use the default fen parsing. 35 parsed_gamestate = chessai.core.parser.load_fen_from_string(text = puzzle_info.fen, **kwargs) 36 37 move_lines: list[list[chessai.core.action.Action]] = [] 38 for line in puzzle_info.move_lines: 39 move_line: list[chessai.core.action.Action] = [] 40 for uci in line: 41 move_line.append(chessai.core.action.from_uci(uci)) 42 43 move_lines.append(move_line) 44 45 # Add the move line information. 46 parsed_gamestate.options[MOVE_LINES_KEY] = move_lines 47 48 return parsed_gamestate 49 50def parse_puzzle(data: str, 51 default_dir: str = PUZZLES_DIR, 52 default_extension: str = PUZZLE_FILE_EXTENSION, 53 string_parser: chessai.core.parser.GameStateStringParser = parse_puzzle_from_string, 54 accepts_raw_string: bool = False, 55 options: dict[str, typing.Any] | None = None, 56 **kwargs: typing.Any) -> chessai.core.parser.ParsedGameState: 57 """ 58 Parse a Puzzle GameState from a file path. 59 60 If the filepath does not exist, the default directory and file extension are added. 61 """ 62 63 return chessai.core.parser.load_from_path(data, 64 default_dir = default_dir, 65 default_extension = default_extension, 66 string_parser = string_parser, 67 **kwargs)
THIS_DIR: str =
'/home/runner/work/chessai/chessai/chessai/puzzle'
PUZZLES_DIR: str =
'/home/runner/work/chessai/chessai/chessai/puzzle/../resources/puzzles'
PUZZLE_FILE_EXTENSION: str =
'.json'
MOVE_LINES_KEY: str =
'move_lines'
@dataclasses.dataclass
class
PuzzleInfo:
18@dataclasses.dataclass 19class PuzzleInfo: 20 """ A simple dataclass to unpack puzzle data from JSON. """ 21 22 fen: str 23 """ The starting FEN for the puzzle. """ 24 25 move_lines: list[list[str]] 26 """ The expected move lines to solve the puzzle. """
A simple dataclass to unpack puzzle data from JSON.
28def parse_puzzle_from_string(text: str, **kwargs: typing.Any) -> chessai.core.parser.ParsedGameState: 29 """ Load a puzzle from a string, which is expected to be JSON data. """ 30 31 # Extract the JSON data into the puzzle info. 32 data_dict = edq.util.json.loads(text) 33 puzzle_info = PuzzleInfo(**data_dict) 34 35 # Use the default fen parsing. 36 parsed_gamestate = chessai.core.parser.load_fen_from_string(text = puzzle_info.fen, **kwargs) 37 38 move_lines: list[list[chessai.core.action.Action]] = [] 39 for line in puzzle_info.move_lines: 40 move_line: list[chessai.core.action.Action] = [] 41 for uci in line: 42 move_line.append(chessai.core.action.from_uci(uci)) 43 44 move_lines.append(move_line) 45 46 # Add the move line information. 47 parsed_gamestate.options[MOVE_LINES_KEY] = move_lines 48 49 return parsed_gamestate
Load a puzzle from a string, which is expected to be JSON data.
def
parse_puzzle( data: str, default_dir: str = '/home/runner/work/chessai/chessai/chessai/puzzle/../resources/puzzles', default_extension: str = '.json', string_parser: chessai.core.parser.GameStateStringParser = <function parse_puzzle_from_string>, accepts_raw_string: bool = False, options: dict[str, typing.Any] | None = None, **kwargs: Any) -> chessai.core.parser.ParsedGameState:
51def parse_puzzle(data: str, 52 default_dir: str = PUZZLES_DIR, 53 default_extension: str = PUZZLE_FILE_EXTENSION, 54 string_parser: chessai.core.parser.GameStateStringParser = parse_puzzle_from_string, 55 accepts_raw_string: bool = False, 56 options: dict[str, typing.Any] | None = None, 57 **kwargs: typing.Any) -> chessai.core.parser.ParsedGameState: 58 """ 59 Parse a Puzzle GameState from a file path. 60 61 If the filepath does not exist, the default directory and file extension are added. 62 """ 63 64 return chessai.core.parser.load_from_path(data, 65 default_dir = default_dir, 66 default_extension = default_extension, 67 string_parser = string_parser, 68 **kwargs)
Parse a Puzzle GameState from a file path.
If the filepath does not exist, the default directory and file extension are added.