From 3790ebb127d9ab65e5fbd2a1573198c3ede2cf9d Mon Sep 17 00:00:00 2001 From: Mike Collier Date: Wed, 15 Apr 2026 10:49:52 -0400 Subject: [PATCH 1/2] Fix enhanced CLI explicit-donor and writeout handling --- molSimplify/__main__.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/molSimplify/__main__.py b/molSimplify/__main__.py index 73bf1c2e..946171d1 100644 --- a/molSimplify/__main__.py +++ b/molSimplify/__main__.py @@ -268,12 +268,14 @@ def _parse_usercatoms(s: str): fixed_ligand_list = [] pydentate_bool = pargs.pydentate if pydentate_bool: - from pydentate import pydentate_lite + pydentate_lite = None i = 0 for ligand in ligand_list: if ligand[1] is None: print(f"Missing coordinating atoms for ligand {ligands[i]}. \n Using pydentate prediction...") try: + if pydentate_lite is None: + from pydentate import pydentate_lite pydentate_results = pydentate_lite.pydentate_lite(ligands[i]) catoms = pydentate_results[1] from molSimplify.Classes import mol2D @@ -357,7 +359,7 @@ def _parse_usercatoms(s: str): for length in ANN_bondl: bondl.append(length[1]) - mol = enforce_metal_ligand_distances_and_optimize(mol, bondl, backbone_core_indices) + mol, _, _ = enforce_metal_ligand_distances_and_optimize(mol, bondl, backbone_core_indices) # -------------------- auto-build run name -------------------- import re, hashlib From 48a8da1f7aa87218293845d3793c6ad0e96b9b25 Mon Sep 17 00:00:00 2001 From: Mike Collier Date: Wed, 15 Apr 2026 10:49:58 -0400 Subject: [PATCH 2/2] Add CLI regression test for explicit usercatoms path --- tests/test_cli.py | 57 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/tests/test_cli.py b/tests/test_cli.py index 7ffb9dba..13cfe113 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -1,4 +1,8 @@ import pytest +import sys +import types +import builtins +import matplotlib.pyplot as plt from molSimplify.__main__ import main @@ -11,3 +15,56 @@ def test_help_no_error(): with pytest.raises(SystemExit) as exc_info: main(args=["--help"]) #assert exc_info.value.code == 0 + + +def test_main_does_not_import_pydentate_with_explicit_usercatoms(monkeypatch, tmp_path): + class DummyMol: + def writexyz(self, path): + with open(path, "w") as handle: + handle.write("dummy xyz\n") + + def writemol2_bodict(self, ignore_dummy_atoms=False, write_bond_orders=True, return_string=False, output_file=None): + with open(output_file, "w") as handle: + handle.write("dummy mol2\n") + + class DummyFig: + def savefig(self, path, dpi=300): + with open(path, "w") as handle: + handle.write("dummy fig\n") + + fake_enhanced = types.ModuleType("molSimplify.Scripts.enhanced_structgen") + fake_enhanced.create_ligand_list = lambda userligand_list, usercatoms_list=None, occupancy_list=None, isomer_list=None: [ + ("lig", [0, 7], 3, None) + ] + fake_enhanced.generate_complex = lambda *args, **kwargs: (DummyMol(), [], {}, DummyFig(), [], []) + fake_enhanced.enhanced_init_ANN = lambda *args, **kwargs: None + fake_enhanced.enforce_metal_ligand_distances_and_optimize = ( + lambda mol, bondl, backbone_core_indices: (mol, None, None) + ) + + fake_functionality = types.ModuleType("molSimplify.Scripts.enhanced_structgen_functionality") + fake_functionality.check_badjob = lambda mol: (False, True) + + monkeypatch.setitem(sys.modules, "molSimplify.Scripts.enhanced_structgen", fake_enhanced) + monkeypatch.setitem(sys.modules, "molSimplify.Scripts.enhanced_structgen_functionality", fake_functionality) + monkeypatch.setattr(plt, "close", lambda *args, **kwargs: None) + + real_import = builtins.__import__ + + def guarded_import(name, globals=None, locals=None, fromlist=(), level=0): + if name == "pydentate": + raise AssertionError("pydentate should not be imported when explicit usercatoms are provided") + return real_import(name, globals, locals, fromlist, level) + + monkeypatch.setattr(builtins, "__import__", guarded_import) + + main(args=[ + "--ligand", "NCCCOCCN", + "--usercatoms", "[0,7]", + "--occupancy", "3", + "--metal", "Fe", + "--ox", "2", + "--spin", "1", + "--geometry", "octahedral", + "--run-dir", str(tmp_path), + ])