From 4a446e0ffc67d156df88b87b124a34d80b5ac302 Mon Sep 17 00:00:00 2001 From: Quintijn Hoogenboom Date: Wed, 6 Mar 2024 16:16:36 +0100 Subject: [PATCH] working on debug messages and understanding Dougs logging mechanism. Try lazy formatting --- src/unimacro/UnimacroGrammars/_folders.py | 8 ++- src/unimacro/_control.py | 84 ++++------------------- 2 files changed, 20 insertions(+), 72 deletions(-) diff --git a/src/unimacro/UnimacroGrammars/_folders.py b/src/unimacro/UnimacroGrammars/_folders.py index 4793810..48f1215 100644 --- a/src/unimacro/UnimacroGrammars/_folders.py +++ b/src/unimacro/UnimacroGrammars/_folders.py @@ -169,6 +169,7 @@ def initialize(self): self.catchRemember = "" self.inTimerRecentFolders = False self.prevDisplayRecentFolders = None # displaying recent folders list + self.prevActiveFolder = None self.subfoldersDict = {} self.subfilesDict = {} self.foldersSet = set() @@ -609,10 +610,13 @@ def getActiveFolder(self, hndle=None, className=None): elif className == '#32770': f = mess.getFolderFromDialog(hndle, className) if not f: + self.prevActiveFolder = f return None if os.path.isdir(f): nf = os.path.normpath(f) - self.debug("getActiveFolder: %s",nf) + if nf != self.prevActiveFolder: + self.debug("getActiveFolder: %s",nf) + self.prevActiveFolder = nf return nf self.warning(f'getActiveFolder, strange invalid path for folder: "{f}"' ) return None @@ -2436,7 +2440,7 @@ def unload(): # get hndle of a explore window (via _general "give window info") and try interactive # thisGrammar.catchTimerRecentFolders(132524, "CabinetWClass") - thisGrammar.getActiveFolder(198518) + thisGrammar.getActiveFolder(329491) # # Words = ['folder', 'dtactions'] diff --git a/src/unimacro/_control.py b/src/unimacro/_control.py index 0ca37f8..005214c 100644 --- a/src/unimacro/_control.py +++ b/src/unimacro/_control.py @@ -10,6 +10,7 @@ # Author: Bart Jan van Os, Version: 1.0, nov 1999 # starting new version Quintijn Hoogenboom, August 2003, for python3 2023 #pylint:disable=C0115, C0116, W0702, R0904, R0911, R0912, R0914, R0915, W0201, W0613, W0107, C0209, E0601, W0602, C0112 +#pylint:disable=R1735, W0703 #pylint:disable=E1101 import os @@ -17,6 +18,9 @@ import shutil import string from pathlib import Path +#a global logger for unimacro. perfectly reasonable to access by name instead. +import logging as l + import natlink from natlinkcore import loader from natlinkcore import natlinkstatus @@ -30,28 +34,22 @@ import sys #from unimacro.logger import ulogger -status = natlinkstatus.NatlinkStatus() -natlinkmain = loader.NatlinkMain() -#a global logger for unimacro. perfectly reasonable to access by name instead. -import logging as l #for some reason, importing amodule which does this doesn't work. Likely because natlinkmain must be started first for #this sublogger natlink.unimacro to work correctly. import unimacro as unimacro_l #bring in so we can add a variable ulogger to the namespace. ulogger : l.Logger = l.getLogger(unimacro_l.logname()) - -unimacro_l.__dict__['ulogger']=ulogger -ulogger.debug("natlink.unimacro logger available") - #Loggers can be created for any module, and they can propogate to the parent Logger, or not. #As an example, this module for the control grammar has its own child logger of unimacro. #Note an entry point has to be defined as well, in pyproject.toml, so Loggers for various natlink components can be discovered. +control_logger=l.getLogger(unimacro_l.control_logger_name()) - - -control_logger=l.getLogger(unimacro_l.control_logger_name()) +unimacro_l.__dict__['ulogger']=ulogger +ulogger.debug("natlink.unimacro logger available") +status = natlinkstatus.NatlinkStatus() +natlinkmain = loader.NatlinkMain() @@ -83,8 +81,8 @@ def natlink_loggers() ->dict: discovered_eps=meta.entry_points(group='dt.loggers') - ulogger.debug(f"Entry Points for natlink.loggers: {discovered_eps}") - loggers=dict() + ulogger.debug('Entry Points for natlink.loggers: %s', discovered_eps) + loggers = dict() for ep in discovered_eps: try: name=ep.name @@ -150,19 +148,6 @@ class UtilGrammar(ancestor): for rulename in gramRules: gramSpec.append(gramDict[rulename]) - - - ## extra: the trace rule: - ## TODO QH, switchoff for the moment, febr24 - if specials: - specials2 = specials[1:] # remove initial "|" (at show it is "| actions | 'spoken forms'", here it is - # "actions | 'spoken forms'" only, because gramnames etc are not implemented - # for tracing) - traceSpecial = """ exported = trace (("""+ specials2 +""") | - ((on|off| {tracecount})("""+ specials2 +""")) | - (("""+ specials2 +""") (on|off|{tracecount}))) ;""" - # gramSpec.append(traceSpecial) # add trace for actions of spoken forms (latter not implemented) - ulogger.info('check, correct _control?????') Mode = Normal LastMode = Normal @@ -173,15 +158,14 @@ def initialize(self): # temp set allResults to 0, disabling the messages trick: if not self.load(self.gramSpec, allResults=showAll): return + + print(f'loggers_names: {self.loggers_names}') self.setList('logmodulename',self.loggers_names) self.RegisterControlObject(self) self.emptyList('message') # at post load # allGramNames = self.getUnimacroGrammarNames() # self.setList('gramnames', allGramNames) - ## TODO QH switch on again if tracing is activated again - # self.setNumbersList('tracecount', tracecount) - self.activateAll() self.setMode(Normal) self.startExclusive = self.exclusive # exclusive state at start of recognition! @@ -286,42 +270,6 @@ def gotResults_checkalphabet(self,words,fullResults): ini.set(alph, letter, spoken) ini.writeIfChanged() - - def gotResults_trace(self,words,fullResults): - self.info('control, trace: %s'% words) - traceNumList = self.getNumbersFromSpoken(words) # returns a string or None - if traceNumList: - traceNum = int(traceNumList[0]) - else: - traceNum = None - - if self.hasCommon(words, 'actions'): - if self.hasCommon(words, 'show'): - actions.debugActionsShow() - elif self.hasCommon(words, 'off'): - actions.debugActions(0) - elif self.hasCommon(words, 'on'): - actions.debugActions(1) - elif traceNum: - actions.debugActions(traceNum) - else: - actions.debugActions(1) - elif self.hasCommon(words, 'spoken forms'): - self.info("no tracing possible for spoken forms") - - #def gotResults_voicecode(self,words,fullResults): - # """switch on if requirements are fulfilled - # - # voicecodeHome must exist - # emacs must be in foreground - # """ - # wxmed = os.path.join(voicecodeHome, 'mediator', 'wxmediator.py') - # if os.path.isfile(wxmed): - # commandLine = r"%spython.exe %s > D:\foo1.txt >> D:\foo2.txt"% (sys.prefix, wxmed) - # os.system(commandLine) - # else: - # print 'not a file: %s'% wxmed - def gotResults_switch(self,words,fullResults): #print 'control, switch: %s'% words if self.hasCommon(words, 'on'): @@ -409,14 +357,10 @@ def gotResults_setlogging(self,words, fullresults): logger=l.getLogger(logger_name) logger.setLevel(new_log_level) - - - -#Hide numbers def gotResults_loglevel(self,words,fullresults): + def gotResults_loglevel(self,words,fullresults): """ """ self.debug(f"gotResults_logging_level words: {words} fullResults: {fullresults}") - return def gotResults_showexclusive(self,words,fullResults):