From 95aade31bcfff1a52246b6c31a14c36be935a909 Mon Sep 17 00:00:00 2001 From: Sander Land Date: Sun, 27 Dec 2020 21:41:26 +0100 Subject: [PATCH] debug print less, alt-tab --- katrain/__main__.py | 36 ++++++++++++++++++++++++++---------- katrain/core/engine.py | 4 ++-- katrain/core/utils.py | 13 +++++++++++++ 3 files changed, 41 insertions(+), 12 deletions(-) diff --git a/katrain/__main__.py b/katrain/__main__.py index 3eeecd4..03fc4f8 100644 --- a/katrain/__main__.py +++ b/katrain/__main__.py @@ -43,6 +43,7 @@ import traceback from queue import Queue import urllib3 import webbrowser +import time from kivy.base import ExceptionHandler, ExceptionManager from kivy.app import App @@ -113,9 +114,8 @@ class KaTrainGui(Screen, KaTrainBase): self.idle_analysis = False self.message_queue = Queue() - self._keyboard = Window.request_keyboard(None, self, "") - self._keyboard.bind(on_key_down=self._on_keyboard_down, on_key_up=self._on_keyboard_up) - Clock.schedule_interval(self.animate_pondering, 0.1) + self.last_key_down = None + self.last_focus_event = 0 def log(self, message, level=OUTPUT_INFO): super().log(message, level) @@ -162,6 +162,14 @@ class KaTrainGui(Screen, KaTrainBase): else: self._do_new_game() + Clock.schedule_interval(self.animate_pondering, 0.1) + Window.request_keyboard(None, self, "").bind(on_key_down=self._on_keyboard_down, on_key_up=self._on_keyboard_up) + + def set_focus_event(*args): + self.last_focus_event = time.time() + + MDApp.get_running_app().root_window.bind(focus=set_focus_event) + def update_gui(self, cn, redraw_board=False): # Handle prisoners and next player display prisoners = self.game.prisoner_count @@ -536,6 +544,7 @@ class KaTrainGui(Screen, KaTrainBase): return first_child if isinstance(first_child, Popup) else None def _on_keyboard_down(self, _keyboard, keycode, _text, modifiers): + self.last_key_down = keycode ctrl_pressed = "ctrl" in modifiers if self.controls.note.focus: return # when making notes, don't allow keyboard shortcuts @@ -552,11 +561,8 @@ class KaTrainGui(Screen, KaTrainBase): else: return shift_pressed = "shift" in modifiers - alt_pressed = "alt" in modifiers shortcuts = self.shortcuts - if keycode[1] == "tab" and not alt_pressed: - self.play_mode.switch_ui_mode() - elif keycode[1] == "spacebar": + if keycode[1] == "spacebar": self.toggle_continuous_analysis() elif keycode[1] == "k": self.board_gui.toggle_coordinates() @@ -615,10 +621,21 @@ class KaTrainGui(Screen, KaTrainBase): self.log(f"wrote profiling results to {filename}", OUTPUT_ERROR) def _on_keyboard_up(self, _keyboard, keycode): - if self.controls.note.focus or self.popup_open: - return # when making notes, don't allow keyboard shortcuts + if keycode[1] in ["alt", "tab"]: + Clock.schedule_once(lambda *_args: self._single_key_action(keycode), 0.05) + + def _single_key_action(self, keycode): + if ( + self.controls.note.focus + or self.popup_open + or keycode != self.last_key_down + or time.time() - self.last_focus_event < 0.2 # this is here to prevent alt-tab from firing alt or tab + ): + return if keycode[1] == "alt": self.nav_drawer.set_state("toggle") + elif keycode[1] == "tab": + self.play_mode.switch_ui_mode() class KaTrainApp(MDApp): @@ -659,7 +676,6 @@ class KaTrainApp(MDApp): Window.bind(on_request_close=self.on_request_close) Window.bind(on_dropfile=lambda win, file: self.gui.load_sgf_file(file.decode("utf8"))) - self.gui = KaTrainGui() Builder.load_file(popup_kv_file) return self.gui diff --git a/katrain/core/engine.py b/katrain/core/engine.py index 1c37b13..edd70f4 100644 --- a/katrain/core/engine.py +++ b/katrain/core/engine.py @@ -15,7 +15,7 @@ from katrain.core.constants import OUTPUT_DEBUG, OUTPUT_ERROR, OUTPUT_EXTRA_DEBU from katrain.core.game_node import GameNode from katrain.core.lang import i18n from katrain.core.sgf_parser import Move -from katrain.core.utils import find_package_resource +from katrain.core.utils import find_package_resource, json_truncate_arrays class EngineDiedException(Exception): @@ -219,7 +219,7 @@ class KataGoEngine: f"[{time_taken:.1f}][{query_id}][{'....' if partial_result else 'done'}] KataGo analysis received: {len(analysis.get('moveInfos',[]))} candidate moves, {analysis['rootInfo']['visits'] if results_exist else 'n/a'} visits", OUTPUT_DEBUG, ) - self.katrain.log(line, OUTPUT_EXTRA_DEBUG) + self.katrain.log(json_truncate_arrays(analysis), OUTPUT_EXTRA_DEBUG) try: if callback and results_exist: callback(analysis, partial_result) diff --git a/katrain/core/utils.py b/katrain/core/utils.py index 9bd5738..0665961 100644 --- a/katrain/core/utils.py +++ b/katrain/core/utils.py @@ -72,3 +72,16 @@ def format_visits(n): if n < 1e6: return f"{n/1000:.0f}k" return f"{n/1e6:.0f}k" + + +def json_truncate_arrays(data, lim=20): + if isinstance(data, list): + if data and isinstance(data[0], dict): + return [json_truncate_arrays(d) for d in data] + if len(data) > lim: + data = [f"{len(data)} x {type(data[0]).__name__}"] + return data + elif isinstance(data, dict): + return {k: json_truncate_arrays(v) for k, v in data.items()} + else: + return data