Skip to content

Commit

Permalink
natlinkconfig programs now use logging (#65)
Browse files Browse the repository at this point in the history
* natlinkstatus.py: entered getUnimacroGrammarsDirectory
* multiple changes in natlinkstatus.py and natlinkconfigfunctions.py, letting eg unimacro setup be better
* add 2 tests execscriptest.py is ok, buttonclicktest.py fails. The cause is playEvents. (Dragon 16)
* the buttonclicktest.py shows the bug when natlink.playEvents is called.
* also enter dragonfly (dragonfly2) in the config procedure, including testing for upgrades..
* add a few changes to nsformat.py (a\\determiner and I\\pronoun, 
  a word with \\ (properties) can also have length 2 when splitted into wordList.
Logging:
* use pythong logging module to report output of pip etc.
* command line uses logging.
* logging to cli.

Loader.py:
* adapt load_or_reload_module function with force_load, so it works from Unimacro (_control.py grammar).

---------
Co-authored-by: Quintijn Hoogenboom <[email protected]>
  • Loading branch information
dougransom and quintijn authored Feb 21, 2024
1 parent 0e6e95f commit 852f7c6
Show file tree
Hide file tree
Showing 12 changed files with 324 additions and 108 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ dependencies= [
"pysimplegui>=4.60.3",
"pydebugstring",
"dtactions>=1.5.7",
"platformdirs >= 4.2.0"
]
classifiers=[
"Development Status :: 4 - Beta",
Expand Down
5 changes: 5 additions & 0 deletions src/natlinkcore/configure/foo.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
ERROR: Could not find a version that satisfies the requirement foobar32 (from versions: none)
ERROR: No matching distribution found for foobar32

[notice] A new release of pip available: 22.3.1 -> 24.0
[notice] To update, run: python.exe -m pip install --upgrade pip
14 changes: 14 additions & 0 deletions src/natlinkcore/configure/loggert.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import logging
import sys
handler = logging.StreamHandler(sys.stdout)
handler.setLevel(logging.DEBUG)
#formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
#handler.setFormatter(formatter)

root = logging.getLogger()
root.addHandler(handler)
root.setLevel(logging.DEBUG)
logging.debug("A")
logging.info("B")
logging.log(logging.DEBUG,"C")
print("\nD E")
21 changes: 20 additions & 1 deletion src/natlinkcore/configure/natlinkconfig_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,25 @@
import os
import os.path
from natlinkcore.configure import extensions_and_folders

from platformdirs import user_log_dir
from pathlib import Path
from natlinkcore.configure import natlinkconfigfunctions
import logging
appname="natlink"
logdir = Path(user_log_dir(appname=appname,ensure_exists=True))
logfilename=logdir/f"cli_log.txt"
file_handler = logging.FileHandler(logfilename)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
file_handler.setFormatter(formatter)
logfile_logger = logging.getLogger()

handler = logging.StreamHandler(sys.stdout)
handler.setLevel(logging.DEBUG)
logfile_logger.addHandler(handler)

file_handler.setLevel(logging.DEBUG)
logfile_logger.addHandler(file_handler)
logfile_logger.setLevel(logging.DEBUG)

def _main(Options=None):
"""Catch the options and perform the resulting command line functions
Expand All @@ -17,6 +34,8 @@ def _main(Options=None):
etc., usage above...
"""


cli = CLI()
cli.Config = natlinkconfigfunctions.NatlinkConfig()
shortOptions = "DVNOHKaAiIxXbBuqe"
Expand Down
65 changes: 62 additions & 3 deletions src/natlinkcore/configure/natlinkconfig_gui.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,26 @@
#pylint:disable=W0621, W0703
#pylint:disable=W0621, W0703, W0603
import sys
import platform

import PySimpleGUI as sg
import logging
from platformdirs import user_log_dir
from pathlib import Path
appname="natlink"
logdir = Path(user_log_dir(appname=appname,ensure_exists=True))
logfilename=logdir/"config_gui_log.txt"
file_handler = logging.FileHandler(logfilename)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
file_handler.setFormatter(formatter)
logfile_logger = logging.getLogger()

#always leave at debug. So we have this if there is ever a problem.
file_handler.setLevel(logging.DEBUG)
logfile_logger.addHandler(file_handler)
logfile_logger.setLevel(logging.DEBUG)



# https://www.pysimplegui.org/en/latest/
from natlinkcore.configure.natlinkconfigfunctions import NatlinkConfig
from natlinkcore import natlinkstatus
Expand All @@ -14,6 +32,8 @@
Config = NatlinkConfig()
Status = natlinkstatus.NatlinkStatus()



SYMBOL_UP = '▲'
SYMBOL_DOWN = '▼'

Expand Down Expand Up @@ -47,6 +67,8 @@ def collapse(layout, key, visible):
[sg.T('Natlink GUI Output')],
[sg.Output(size=(40,10), echo_stdout_stderr=True, expand_x=True, key='-OUTPUT-')]]



#### Main UI Layout ####
layout = [[sg.T('Environment:', font='bold'), sg.T(f'Windows OS: {osVersion.major}, Build: {osVersion.build}'), sg.T(f'Python: {pyVersion}'), sg.T(f'Dragon Version: {Status.getDNSVersion()}')],
#### Projects Checkbox ####
Expand All @@ -61,6 +83,23 @@ def collapse(layout, key, visible):

window = sg.Window('Natlink configuration GUI', layout, enable_close_attempted_event=True)

#this is for the GUI logging.
#set the level on this corresponding to the natlink levels later.
#This must be created after the window is created, or nothing gets logged.

#and we don't add the handler until just before the window is read the first time
#because a write during this period will cause a problem.

handler = logging.StreamHandler(sys.stdout)
handler.setLevel(logging.DEBUG)
try:
ll=Config.config_get("settings","log_level")
if ll != "Debug":
handler.setLevel(logging.INFO)
except Exception as ee:
logging.debug(f"{__file__} failed to get log_level {ee} ")


def ThreadIsRunning():
global Thread_Running
Thread_Running = not Thread_Running
Expand All @@ -69,6 +108,13 @@ def ThreadIsRunning():
# Natlink
def SetNatlinkLoggingOutput(values, event):
Config.setLogging(values['Set_Logging_Natlink'])
#also if natlink is not debug level, use info for logging in this application also.
try:
ll=Config.config_get("settings","log_level")
if ll != "Debug":
handler.setLevel(logging.INFO)
except Exception as ee:
logging.debug(f"{__file__} failed to get log_level {ee} ")

# Dragonfly2
def Dragonfly2UserDir(values, event):
Expand Down Expand Up @@ -112,11 +158,24 @@ def OpenNatlinkConfig(values, event):
unimacro_dispatch = {'Set_UserDir_Unimacro': UnimacroUserDir, 'Clear_UserDir_Unimacro': UnimacroUserDir}
autohotkey_dispatch = {'Set_Exe_Ahk': AhkExeDir, 'Clear_Exe_Ahk': AhkExeDir, 'Set_ScriptsDir_Ahk': AhkUserDir,'Clear_ScriptsDir_Ahk': AhkUserDir}


#### Event Loop ####
try:
#we want to set the logger up just before we call window.read()
#because if something is logged before the first window.read() there will be a failure.



#this would be a good spot to change the logging level, based on the natlink logging level
#note the natlink logging level doesn't correspond one to one with the logging module levels.
logfile_logger.addHandler(handler)

handler.setLevel(logging.DEBUG)
#do not change the logger level from debug
#change it in the handler. Because a file handler logs everything.
while True:
event, values = window.read()
if (event == '-WINDOW CLOSE ATTEMPTED-' or event == 'Exit') and not Thread_Running:
if (event in ['-WINDOW CLOSE ATTEMPTED-', 'Exit']) and not Thread_Running:
break
# Hidden Columns logic
# TODO: if project is enabled, update the project state to enabled.
Expand All @@ -140,7 +199,7 @@ def OpenNatlinkConfig(values, event):
Thread_Running = not Thread_Running

elif Thread_Running:
choice = sg.popup(f'Please Wait: Pip install is in progress', keep_on_top=True, custom_text=('Wait','Force Close'))
choice = sg.popup('Please Wait: Pip install is in progress', keep_on_top=True, custom_text=('Wait','Force Close'))
if choice == 'Force Close':
break

Expand Down
Loading

0 comments on commit 852f7c6

Please sign in to comment.