Skip to content

Commit

Permalink
DAP Working (#67)
Browse files Browse the repository at this point in the history
* was lost in the flow, reentered from natlinkcore repository

* entered getUnimacroGrammarsDirectory, and a lot of tidying up

* multiple changes in natlinkstatus.py and natlinkconfigfunctions.py, letting eg unimacro setup be better

add getUnimacroGrammarsDirectory

* add 2 tests execscriptest.py is ok, buttonclicktest.py fails. The cause is playEvents.

* 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.

* small change in nsformat.py (formatPassword)

* use pythong logging module to report output of pip etc.

* command line uses logging.

* logging to cli.

* tweak.

* updated

* renamed

* checkpoint

* some updates on logging, debug startup

* Removed documentation and obsolete natlinkpydebug.py.  Start DAP in loader of configured in natlink.ini.

---------

Co-authored-by: Quintijn Hoogenboom <[email protected]>
  • Loading branch information
dougransom and quintijn authored Feb 22, 2024
1 parent 852f7c6 commit 6ab6ced
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 222 deletions.
Binary file added documentation/add_configuration.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
196 changes: 0 additions & 196 deletions documentation/developers.rst

This file was deleted.

Binary file added documentation/run_and_debug.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ readme = "readme.md"
dependencies= [
"natlink>=5.3.4",
"pysimplegui>=4.60.3",
"pydebugstring",
"pydebugstring >= 1.0.0.1",
"dtactions>=1.5.7",
"platformdirs >= 4.2.0"
]
Expand All @@ -30,7 +30,7 @@ test = [
"pytest >=7.1.2",
]
dev = [
"pyenvutils","entry-point-inspector"
"pyenvutils","entry-point-inspector","build"
]

[tool.pytest.ini_options]
Expand Down
82 changes: 58 additions & 24 deletions src/natlinkcore/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import importlib.machinery
import importlib.util
import logging
from pydebugstring import outputDebugString,OutputDebugStringHandler
import os
import copy
import sys
Expand Down Expand Up @@ -630,45 +631,78 @@ def config_locations() -> Iterable[str]:
# choose between .natlink/natlink.ini in home or the fallback_directory:
return [join(home, config_sub_dir, natlink_inifile), fallback_config_file]

def startDap(config : NatlinkConfig) -> bool:
"""
Starts DAP (Debug Adapter Protocol) if there a DAP port specified in the config object.
returns True if the dap was started.
Natlink will startDap automatically if configured in the run method below.
If you need to start the DAP sooner, edit your code to make a call to startDap.
Similarly, if you want to start the DAP later, call startDap. You can call it from your grammar or
anywhere else.
"""

dap_started=False
logging.debug(f"testing dap , enabled {config.dap_enabled} port {config.dap_port}")
try:
logging.debug("Debugpy.configure ...")
debugpy.configure(python=f"{python_exec}")
logging.debug("Debugpy.listen ...")

debugpy.listen(config.dap_port)
dap_started=True

logging.debug(f"DAP Started on Port {config.dap_port} in {__file__}")
if config.dap_wait_for_debugger_attach_on_startup:
#use info level logging, the user will need to know as natlink and dragon will hang here.
#unti debuger is attached.
logging.info(f"waiting for debugger to attach using DAP in {__file__} ")
debugpy.wait_for_client()
return dap_started

except Exception as ee:
logging.info(f"""
Exception {ee} while starting DAP in {__file__}. Possible cause is incorrect python executable specified {python_exec}
""" )


def run() -> None:
logger = logging.getLogger('natlink')
default_logger=logging.getLogger()
dh = OutputDebugStringHandler()
sh=logging.StreamHandler(sys.stdout)
for h in [sh,dh]:
default_logger.addHandler(h)

default_logger.setLevel(logging.DEBUG)

logging.debug(f"{__file__} run()")
try:
# # TODO: remove this hack. As of October 2021, win32api does not load properly, except if
# # the package pywin32_system32 is explictly put on new dll_directory white-list
# pywin32_dir = os.path.join(sysconfig.get_path('platlib'), "pywin32_system32")
# if os.path.isdir(pywin32_dir):
# os.add_dll_directory(pywin32_dir)

#create a temporary logging handler, so we can log the startup of DAP

config = NatlinkConfig.from_first_found_file(config_locations())
dap_started=False
print(f"testing dap , enabled {config.dap_enabled} port {config.dap_port}")
try:
if config.dap_enabled:
print("Debugpy.configure ...")
debugpy.configure(python=f"{python_exec}")
print("Debugpy.listen ...")

debugpy.listen(config.dap_port)
dap_started=True

print(f"DAP Started on Port {config.dap_port} in {__file__}")
if config.dap_wait_for_debugger_attach_on_startup:
print(" waiting for debugger to attach")

except Exception as ee:
print(f"""
Exception {ee} while starting DAP in {__file__}. Possible cause is incorrect python executable specified {python_exec}
""" )

dap_started = config.dap_enabled and startDap(config)
logger=logging.getLogger("natlink")
logger.setLevel(logging.DEBUG)

main = NatlinkMain(logger, config)
main.setup_logger()
main.dap_started=dap_started
for h in [sh,dh]:
default_logger.removeHandler(h)

main.setup_logger()
print(f"Dap enabled: {config.dap_enabled} port: {config.dap_port} ")

logging.debug(f"Dap enabled: {config.dap_enabled} port: {config.dap_port} ")
main.start()
except Exception as exc:
print(f'Exception: "{exc}" in loader.run', file=sys.stderr)
print(traceback.format_exc())
logging.info(f'Exception: "{exc}" in loader.run', file=sys.stderr)
logging.info(traceback.format_exc())
raise Exception from exc

if __name__ == "__main__":
Expand Down

0 comments on commit 6ab6ced

Please sign in to comment.