Skip to content

Commit

Permalink
better handle different types of failure with block imports, in parti…
Browse files Browse the repository at this point in the history
…cular compile time errors, better diagnostics
  • Loading branch information
petercorke committed Aug 5, 2024
1 parent 03da672 commit 490dd74
Showing 1 changed file with 31 additions and 15 deletions.
46 changes: 31 additions & 15 deletions bdsim/run_sim.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from pathlib import Path
import sys
import importlib
import traceback as tb
import inspect
from collections import Counter, namedtuple
import argparse
Expand Down Expand Up @@ -212,7 +213,6 @@ def blockname(name):


class BDSimState:

"""
:ivar x: state vector
:vartype x: np.ndarray
Expand Down Expand Up @@ -1168,7 +1168,11 @@ def indent(s):
block = namedtuple("block", "name, cls, path")

if toolboxes:
packages = ["bdsim", "roboticstoolbox", "machinevisiontoolbox"]
packages = [
"bdsim",
"roboticstoolbox",
"machinevisiontoolbox",
]
else:
packages = ["bdsim"]
env = os.getenv("BDSIMPATH")
Expand All @@ -1182,20 +1186,32 @@ def indent(s):
for package in packages:
try:
spec = importlib.util.find_spec(".blocks", package=package)
if spec is None:
print(f"package {package} has no blocks module")
continue
pkg = spec.loader.load_module()
except ModuleNotFoundError:
print(f"package {package} not found")
except ModuleNotFoundError as err:
print(
f"package {package} not loaded: not found, not a proper package, no blocks module"
)
continue
except ImportError:
print(f"package {package} load error, continuing")
import textwrap

print(textwrap.indent(traceback.format_exc(), " "))
if spec is None:
print(f"package {package} not found or has no blocks module")
continue

try:
pkg = spec.loader.load_module()
except Exception as err:
print(f"package {package} contains a compile error")
exc = sys.exception()
print(fg("red"))
tb.print_exception(exc, limit=-4)
print(attr(0))
continue
# except ImportError:
# print(f"package {package} load error, continuing")
# import textwrap

# print(textwrap.indent(traceback.format_exc(), " "))
# continue

moduledict = {}

for name, value in pkg.__dict__.items():
Expand Down Expand Up @@ -1246,9 +1262,9 @@ def indent(s):

# create a dict for the block with metadata
block_info = {}
block_info[
"path"
] = pkg.__path__ # path to folder holding block definition
block_info["path"] = (
pkg.__path__
) # path to folder holding block definition
block_info["classname"] = name
block_info["blockname"] = blockname(name)

Expand Down

0 comments on commit 490dd74

Please sign in to comment.