Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 22 additions & 8 deletions src/dxtbx/model/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -794,19 +794,33 @@ def as_json(
):
"""Dump experiment list as json"""

# Find the module that called this function for the history
stack = inspect.stack()
this_module = inspect.getmodule(stack[0].frame)
# Find the first caller outside of this module
this_module = sys.modules[__name__] # The module containing as_json()

caller_module_name = "Unknown"
for f in stack[1:]:
module = inspect.getmodule(f.frame)
if module != this_module and module is not None:
caller_module_name = module.__name__
caller_module = None
frame_depth = 1
while True:
try:
frame = sys._getframe(frame_depth)
module = sys.modules.get(frame.f_globals.get("__name__"))
if module is not None and module != this_module:
caller_module = module
caller_module_name = module.__name__
break
frame_depth += 1
except ValueError: # No more frames
break

# If that module was called directly, look up via file path
if caller_module_name == "__main__":
caller_module_name = os.path.splitext(os.path.basename(module.__file__))[0]
if (
hasattr(caller_module, "__file__")
and caller_module.__file__ is not None
):
caller_module_name = os.path.splitext(
os.path.basename(caller_module.__file__)
)[0]

# Look up the dispatcher name for the caller module
lookup = {
Expand Down
Loading