zero width space markers and base64 encoded data in sgf files are probably terrible ideas

This commit is contained in:
Sander Land committed 2020-11-30 13:18:23 +01:00
1 parent 71d19a2c4e
commit 8dcfc1cd59
26 files changed
+153 -43

No files matched your search

+51 -10
View File
@@ -2,10 +2,13 @@ import copy
import random
from typing import Dict, List, Optional, Tuple
from katrain.core.constants import HOMEPAGE, VERSION, SGF_COMMENTS_MARKER, SGF_SEPARATOR_MARKER, SGF_NOTE_MARKER, PROGRAM_NAME
from katrain.core.lang import i18n
from katrain.core.sgf_parser import Move, SGFNode
from katrain.core.utils import evaluation_class, var_to_grid
from katrain.gui.style import INFO_PV_COLOR
import base64
import gzip
class GameNode(SGFNode):
@@ -22,19 +25,56 @@ class GameNode(SGFNode):
self.time_used = 0
self.undo_threshold = random.random() # for fractional undos
self.end_state = None
self.analysis_loaded = False
self.clear_analysis()
def add_list_property(self, property: str, values: List):
if property == "KT":
try:
import bson
self.analysis = bson.loads(gzip.decompress(base64.standard_b64decode(values[0])))
self.analysis_loaded = True
except Exception as e:
print(e)
elif property == "C":
comments = [ # strip out all previously auto generated comments
c for v in values for c in v.split(SGF_SEPARATOR_MARKER)
]
self.note = "".join(c for c in comments if SGF_NOTE_MARKER in c)
super().add_list_property(
property,
[c for c in comments if c.strip() and SGF_COMMENTS_MARKER not in c and SGF_NOTE_MARKER not in c],
)
else:
return super().add_list_property(property, values)
def clear_analysis(self):
self.analysis_visits_requested = 0
self.analysis = {"moves": {}, "root": None, "completed": False}
def sgf_properties(self, save_comments_player=None, save_comments_class=None, eval_thresholds=None):
def sgf_properties(
self, save_comments_player=None, save_comments_class=None, eval_thresholds=None, save_analysis=False
):
properties = copy.copy(super().sgf_properties())
note = self.note.strip()
print(save_analysis, self.analysis_complete)
if save_analysis and self.analysis_complete:
try:
import bson
analysis = copy.deepcopy(self.analysis)
for movedict in analysis["moves"].values():
if "ownership" in movedict:
del movedict["ownership"]
properties["KT"] = [base64.standard_b64encode(gzip.compress(bson.dumps(analysis))).decode("utf-8")]
except Exception as e:
print(e)
if self.points_lost and save_comments_class is not None and eval_thresholds is not None:
show_class = save_comments_class[evaluation_class(self.points_lost, eval_thresholds)]
else:
show_class = False
comments = properties.get("C", [])
if (
self.parent
and self.parent.analysis_exists
@@ -50,18 +90,19 @@ class GameNode(SGFNode):
properties["SQ"] = best_sq
if top_x and "MA" not in properties:
properties["MA"] = [top_x]
comment = self.comment(sgf=True, interactive=False)
if comment:
properties["C"] = ["\n".join(properties.get("C", "")) + comment]
comments.append(self.comment(sgf=True, interactive=False))
if self.is_root:
properties["C"] = [
i18n._("SGF start message")
+ "\n"
+ "\n".join(properties.get("C", ""))
+ "\nSGF with review generated by KaTrain."
comments = [
i18n._("SGF start message") + SGF_COMMENTS_MARKER,
*comments,
f"SGF with generated by {PROGRAM_NAME} {VERSION}{SGF_COMMENTS_MARKER}.",
]
if note:
properties["C"] = ["\n".join(properties.get("C", "")) + f"\nNote: {self.note}"]
comments.append(f"\nNote: {self.note}{SGF_NOTE_MARKER}")
if comments:
properties["C"] = [SGF_SEPARATOR_MARKER.join(comments)]
elif "C" in properties:
del properties["C"]
return properties
@staticmethod