Skip to content

Commit

Permalink
working on debug messages and understanding Dougs logging mechanism. …
Browse files Browse the repository at this point in the history
…Try lazy formatting
  • Loading branch information
quintijn committed Mar 6, 2024
1 parent 1577ce3 commit 4a446e0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 72 deletions.
8 changes: 6 additions & 2 deletions src/unimacro/UnimacroGrammars/_folders.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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']
Expand Down
84 changes: 14 additions & 70 deletions src/unimacro/_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,17 @@
# 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
import filecmp
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
Expand All @@ -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()



Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 = """<trace> 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
Expand All @@ -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!
Expand Down Expand Up @@ -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'):
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 4a446e0

Please sign in to comment.