Skip to content

Commit

Permalink
Fix sys.path for examples (in case running without a pip install of t…
Browse files Browse the repository at this point in the history
…he package).
  • Loading branch information
Poikilos committed Dec 5, 2024
1 parent b226a71 commit 376320a
Show file tree
Hide file tree
Showing 13 changed files with 86 additions and 92 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ cython_debug/
*.dvi
*.aux
*.out
*.log
# *.log # redundant (See further up)

*localoverrides.py

Expand All @@ -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
14 changes: 6 additions & 8 deletions examples/example_cdi_access.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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'])
Expand Down
17 changes: 8 additions & 9 deletions examples/example_datagram_transfer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand Down
15 changes: 7 additions & 8 deletions examples/example_frame_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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'])
Expand Down
14 changes: 6 additions & 8 deletions examples/example_memory_length_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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'])
Expand Down
14 changes: 6 additions & 8 deletions examples/example_memory_transfer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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'])
Expand Down
16 changes: 8 additions & 8 deletions examples/example_message_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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'])
Expand Down
16 changes: 8 additions & 8 deletions examples/example_node_implementation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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'])
Expand Down
17 changes: 7 additions & 10 deletions examples/example_remote_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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")
Expand Down
16 changes: 7 additions & 9 deletions examples/example_string_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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'])
Expand Down
14 changes: 7 additions & 7 deletions examples/example_string_serial_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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'])
Expand Down
16 changes: 8 additions & 8 deletions examples/example_tcp_message_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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:")
Expand Down
6 changes: 6 additions & 0 deletions examples/examples_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down

0 comments on commit 376320a

Please sign in to comment.