Add util to convert string to pgn format
Browse files- src/util/pgn_util.py +23 -1
src/util/pgn_util.py
CHANGED
@@ -1,13 +1,35 @@
|
|
|
|
1 |
import logging
|
2 |
import chess.pgn
|
3 |
import tempfile
|
4 |
import os
|
5 |
import time
|
6 |
import glob
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
|
9 |
# Load game
|
10 |
-
def read_pgn(pgn_filepath):
|
11 |
with open(pgn_filepath) as f:
|
12 |
game = chess.pgn.read_game(f)
|
13 |
return game
|
|
|
1 |
+
import io
|
2 |
import logging
|
3 |
import chess.pgn
|
4 |
import tempfile
|
5 |
import os
|
6 |
import time
|
7 |
import glob
|
8 |
+
import gradio as gr
|
9 |
+
|
10 |
+
|
11 |
+
def extract_game(png_input):
|
12 |
+
if isinstance(png_input, io.IOBase):
|
13 |
+
file = png_input
|
14 |
+
elif isinstance(png_input, str):
|
15 |
+
file = io.StringIO(png_input)
|
16 |
+
elif isinstance(png_input, gr.utils.NamedString):
|
17 |
+
file = png_input.name
|
18 |
+
else:
|
19 |
+
raise ValueError("Input must be a string or a file-like object.")
|
20 |
+
|
21 |
+
game = read_pgn(file)
|
22 |
+
return game
|
23 |
+
|
24 |
+
|
25 |
+
def pgn_string_to_game(pgn_text: str) -> chess.pgn.Game:
|
26 |
+
pgn_io = io.StringIO(pgn_text)
|
27 |
+
game = chess.pgn.read_game(pgn_io)
|
28 |
+
return game
|
29 |
|
30 |
|
31 |
# Load game
|
32 |
+
def read_pgn(pgn_filepath) -> str:
|
33 |
with open(pgn_filepath) as f:
|
34 |
game = chess.pgn.read_game(f)
|
35 |
return game
|