diff --git a/.gitignore b/.gitignore index dc76fb3..f14b4f0 100644 --- a/.gitignore +++ b/.gitignore @@ -171,7 +171,7 @@ cython_debug/ *.dvi *.aux *.out -*.log +# *.log # redundant (See further up) *localoverrides.py @@ -182,3 +182,4 @@ cython_debug/ # ^ /.vscode/settings.json is ignored since it may have python.defaultInterpreterPath with differs depending on the specific machine. Recommended: place that setting in there ("PythonOlcbNode Folder" tab in VSCode settings) /build /doc/_autosummary +/examples/settings.json diff --git a/examples/example_cdi_access.py b/examples/example_cdi_access.py index fc6292f..e8d3f54 100644 --- a/examples/example_cdi_access.py +++ b/examples/example_cdi_access.py @@ -10,7 +10,13 @@ the address and port). Defaults to a hard-coded test address and port. ''' +# region same code as other examples +from examples_settings import Settings # do 1st to fix path if no pip install +settings = Settings() +if __name__ == "__main__": + settings.load_cli_args(docstring=__doc__) +# endregion same code as other examples from openlcb.canbus.tcpsocket import TcpSocket @@ -36,14 +42,6 @@ # farNodeID = "02.01.57.00.04.9C" # endregion moved to settings -# region same code as other examples -from examples_settings import Settings -settings = Settings() - -if __name__ == "__main__": - settings.load_cli_args(docstring=__doc__) -# endregion same code as other examples - s = TcpSocket() # s.settimeout(30) s.connect(settings['host'], settings['port']) diff --git a/examples/example_datagram_transfer.py b/examples/example_datagram_transfer.py index 10e6c02..50b2256 100644 --- a/examples/example_datagram_transfer.py +++ b/examples/example_datagram_transfer.py @@ -9,6 +9,14 @@ the address and port). Defaults to a hard-coded test address and port. ''' +# region same code as other examples +from examples_settings import Settings # do 1st to fix path if no pip install +settings = Settings() + +if __name__ == "__main__": + settings.load_cli_args(docstring=__doc__) +# endregion same code as other examples + import threading from openlcb.canbus.tcpsocket import TcpSocket @@ -28,15 +36,6 @@ # port = 12021 # endregion replaced by settings -# region same code as other examples -from examples_settings import Settings -settings = Settings() - -if __name__ == "__main__": - settings.load_cli_args(docstring=__doc__) -# endregion same code as other examples - - localNodeID = "05.01.01.01.03.01" farNodeID = "09.00.99.03.00.35" s = TcpSocket() diff --git a/examples/example_frame_interface.py b/examples/example_frame_interface.py index d5045fd..f73f798 100644 --- a/examples/example_frame_interface.py +++ b/examples/example_frame_interface.py @@ -10,6 +10,13 @@ the address and port). Defaults to a hard-coded test address and port. ''' +# region same code as other examples +from examples_settings import Settings # do 1st to fix path if no pip install +settings = Settings() + +if __name__ == "__main__": + settings.load_cli_args(docstring=__doc__) +# endregion same code as other examples from openlcb.canbus.tcpsocket import TcpSocket from openlcb.canbus.canphysicallayergridconnect import ( @@ -24,14 +31,6 @@ # port = 12021 # endregion replaced by settings -# region same code as other examples -from examples_settings import Settings -settings = Settings() - -if __name__ == "__main__": - settings.load_cli_args(docstring=__doc__) -# endregion same code as other examples - s = TcpSocket() # s.settimeout(30) s.connect(settings['host'], settings['port']) diff --git a/examples/example_memory_length_query.py b/examples/example_memory_length_query.py index c7b1aaa..f407679 100644 --- a/examples/example_memory_length_query.py +++ b/examples/example_memory_length_query.py @@ -9,7 +9,13 @@ the address and port). Defaults to a hard-coded test address and port. ''' +# region same code as other examples +from examples_settings import Settings # do 1st to fix path if no pip install +settings = Settings() +if __name__ == "__main__": + settings.load_cli_args(docstring=__doc__) +# endregion same code as other examples from openlcb.canbus.tcpsocket import TcpSocket @@ -37,14 +43,6 @@ farNodeID = "09.00.99.03.00.35" # endregion replaced by settings -# region same code as other examples -from examples_settings import Settings -settings = Settings() - -if __name__ == "__main__": - settings.load_cli_args(docstring=__doc__) -# endregion same code as other examples - s = TcpSocket() # s.settimeout(30) s.connect(settings['host'], settings['port']) diff --git a/examples/example_memory_transfer.py b/examples/example_memory_transfer.py index 63129cb..f437101 100644 --- a/examples/example_memory_transfer.py +++ b/examples/example_memory_transfer.py @@ -9,7 +9,13 @@ the address and port). Defaults to a hard-coded test address and port. ''' +# region same code as other examples +from examples_settings import Settings # do 1st to fix path if no pip install +settings = Settings() +if __name__ == "__main__": + settings.load_cli_args(docstring=__doc__) +# endregion same code as other examples from openlcb.canbus.tcpsocket import TcpSocket @@ -37,14 +43,6 @@ # farNodeID = "09.00.99.03.00.35" # endregion replaced by settings -# region same code as other examples -from examples_settings import Settings -settings = Settings() - -if __name__ == "__main__": - settings.load_cli_args(docstring=__doc__) -# endregion same code as other examples - s = TcpSocket() # s.settimeout(30) s.connect(settings['host'], settings['port']) diff --git a/examples/example_message_interface.py b/examples/example_message_interface.py index 0fae84a..af0aabe 100644 --- a/examples/example_message_interface.py +++ b/examples/example_message_interface.py @@ -11,6 +11,14 @@ the address and port). Defaults to a hard-coded test address and port. ''' +# region same code as other examples +from examples_settings import Settings # do 1st to fix path if no pip install +settings = Settings() + +if __name__ == "__main__": + settings.load_cli_args(docstring=__doc__) +# endregion same code as other examples + from openlcb.canbus.tcpsocket import TcpSocket from openlcb.canbus.canphysicallayergridconnect import ( @@ -28,14 +36,6 @@ # localNodeID = "05.01.01.01.03.01" # endregion replaced by settings -# region same code as other examples -from examples_settings import Settings -settings = Settings() - -if __name__ == "__main__": - settings.load_cli_args(docstring=__doc__) -# endregion same code as other examples - s = TcpSocket() # s.settimeout(30) s.connect(settings['host'], settings['port']) diff --git a/examples/example_node_implementation.py b/examples/example_node_implementation.py index 06622e9..167bee0 100644 --- a/examples/example_node_implementation.py +++ b/examples/example_node_implementation.py @@ -9,6 +9,14 @@ the address and port). Defaults to a hard-coded test address and port. ''' +# region same code as other examples +from examples_settings import Settings # do 1st to fix path if no pip install +settings = Settings() + +if __name__ == "__main__": + settings.load_cli_args(docstring=__doc__) +# endregion same code as other examples + from openlcb.canbus.tcpsocket import TcpSocket from openlcb.canbus.canphysicallayergridconnect import ( @@ -34,14 +42,6 @@ # farNodeID = "09.00.99.03.00.35" # endregion moved to settings -# region same code as other examples -from examples_settings import Settings -settings = Settings() - -if __name__ == "__main__": - settings.load_cli_args(docstring=__doc__) -# endregion same code as other examples - s = TcpSocket() # s.settimeout(30) s.connect(settings['host'], settings['port']) diff --git a/examples/example_remote_nodes.py b/examples/example_remote_nodes.py index cd368bb..003ae9c 100644 --- a/examples/example_remote_nodes.py +++ b/examples/example_remote_nodes.py @@ -10,6 +10,13 @@ the address and port). Defaults to a hard-coded test address and port. ''' +# region same code as other examples +from examples_settings import Settings # do 1st to fix path if no pip install +settings = Settings() + +if __name__ == "__main__": + settings.load_cli_args(docstring=__doc__) +# endregion same code as other examples from openlcb.canbus.canphysicallayergridconnect import ( CanPhysicalLayerGridConnect, @@ -41,20 +48,10 @@ # timeout = 0.5 # endregion replaced by settings -# region same code as other examples -from examples_settings import Settings -settings = Settings() - -if __name__ == "__main__": - settings.load_cli_args(docstring=__doc__) -# endregion same code as other examples - - s = TcpSocket() # s.settimeout(30) s.connect(settings['host'], settings['port']) - if settings['trace'] : print("RR, SR are raw socket interface receive and send;" " RL, SL are link (frame) interface") diff --git a/examples/example_string_interface.py b/examples/example_string_interface.py index 3991f2f..e1f7668 100644 --- a/examples/example_string_interface.py +++ b/examples/example_string_interface.py @@ -10,6 +10,13 @@ the address and port). Defaults to a hard-coded test address and port. ''' +# region same code as other examples +from examples_settings import Settings # do 1st to fix path if no pip install +settings = Settings() + +if __name__ == "__main__": + settings.load_cli_args(docstring=__doc__) +# endregion same code as other examples from openlcb.canbus.tcpsocket import TcpSocket @@ -19,15 +26,6 @@ # port = 12021 # endregion replaced by settings -# region same code as other examples -from examples_settings import Settings -settings = Settings() - -if __name__ == "__main__": - settings.load_cli_args(docstring=__doc__) -# endregion same code as other examples - - s = TcpSocket() # s.settimeout(30) s.connect(settings['host'], settings['port']) diff --git a/examples/example_string_serial_interface.py b/examples/example_string_serial_interface.py index b2d2b6d..c81e4ec 100644 --- a/examples/example_string_serial_interface.py +++ b/examples/example_string_serial_interface.py @@ -10,6 +10,13 @@ the address and port). Defaults to a hard-coded test address and port. ''' +# region same code as other examples +from examples_settings import Settings # do 1st to fix path if no pip install +settings = Settings() + +if __name__ == "__main__": + settings.load_cli_args(docstring=__doc__) +# endregion same code as other examples from openlcb.canbus.seriallink import SerialLink @@ -18,13 +25,6 @@ # device = "/dev/cu.usbmodemCC570001B1" # endregion replaced by settings -# region same code as other examples -from examples_settings import Settings -settings = Settings() - -if __name__ == "__main__": - settings.load_cli_args(docstring=__doc__) -# endregion same code as other examples s = SerialLink() s.connect(settings['device']) diff --git a/examples/example_tcp_message_interface.py b/examples/example_tcp_message_interface.py index 0252980..8b0cabf 100644 --- a/examples/example_tcp_message_interface.py +++ b/examples/example_tcp_message_interface.py @@ -11,6 +11,14 @@ the address and port). Defaults to a hard-coded test address and port. ''' +# region same code as other examples +from examples_settings import Settings # do 1st to fix path if no pip install +settings = Settings() + +if __name__ == "__main__": + settings.load_cli_args(docstring=__doc__) +# endregion same code as other examples + from openlcb.tcplink.tcpsocket import TcpSocket from openlcb.tcplink.tcplink import TcpLink @@ -25,14 +33,6 @@ # localNodeID = "05.01.01.01.03.01" # endregion moved to settings -# region same code as other examples -from examples_settings import Settings -settings = Settings() - -if __name__ == "__main__": - settings.load_cli_args(docstring=__doc__) -# endregion same code as other examples - s = TcpSocket() # s.settimeout(30) print("Using settings:") diff --git a/examples/examples_settings.py b/examples/examples_settings.py index a9ea7ab..1285cbc 100644 --- a/examples/examples_settings.py +++ b/examples/examples_settings.py @@ -8,6 +8,12 @@ import shutil import sys +REPO_DIR = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) +if os.path.isfile(os.path.join(REPO_DIR, "openlcb", "__init__.py")): + # User is running from the repo + # (generally true if using examples_settings) + sys.path.insert(0, REPO_DIR) + CONFIGS_DIR = os.path.dirname(os.path.realpath(__file__)) DEFAULT_SETTINGS = {