Changeset a93896


Ignore:
Timestamp:
Aug 30, 2025, 2:41:40 PM (8 weeks ago)
Author:
Frederik Heber <frederik.heber@…>
Branches:
Candidate_v1.7.0, stable
Children:
454d7a
Parents:
9928ece
git-author:
Frederik Heber <frederik.heber@…> (08/14/25 14:36:30)
git-committer:
Frederik Heber <frederik.heber@…> (08/30/25 14:41:40)
Message:

Ignores pyMoleCuilder import in python script.

  • TEST: LoadSession now uses an AS alias for the import.
  • TESTFIX: load session with complex waits does not look for the wait texts but rather for output from the actions in the script.
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • src/Python/PythonScripting.cpp

    r9928ece ra93896  
    3737#include "PythonScripting.hpp"
    3838
     39#include <string>
     40
    3941//#include "CodePatterns/MemDebug.hpp"
    4042
     
    5860  // move all lines with "import" to the beginning
    5961  std::string current_line;
     62  std::string moduleName;
    6063  while(std::getline(input,current_line,'\n')){
    61     if (current_line.find("import ") != std::string::npos)
    62       buffer_import << current_line << std::endl;
    63     else
     64    if (current_line.find("import ") != std::string::npos) {
     65      if (current_line.find(PYMOLECUILDER_MODULE_NAME) != std::string::npos) {
     66          // ignore a PyMoleCuilder import, use the AS however.
     67          const size_t lowerCaseAsPos = current_line.find("as");
     68          const size_t upperCaseAsPos = current_line.find("AS");
     69          size_t moduleNameStart = std::string::npos;
     70                                if (lowerCaseAsPos != std::string::npos)
     71                                        moduleNameStart = lowerCaseAsPos+2+1;
     72                                if (upperCaseAsPos != std::string::npos)
     73                                        moduleNameStart = upperCaseAsPos+2+1;
     74                                if (moduleNameStart != std::string::npos) {
     75                                        const size_t moduleNameEnd = current_line.find(" ", moduleNameStart);
     76                                        moduleName = current_line.substr(
     77                  moduleNameStart,
     78                                                moduleNameEnd == std::string::npos ? moduleNameEnd : moduleNameEnd  - moduleNameStart
     79                                        );
     80                ELOG(2, "Ignoring found " << PYMOLECUILDER_MODULE_NAME << " import, but keeping AS name " << moduleName << ".");
     81        } else {
     82                ELOG(2, "Ignoring found " << PYMOLECUILDER_MODULE_NAME << " import.");
     83        }
     84      } else {
     85        buffer_import << current_line << std::endl;
     86      }
     87    } else {
    6488      buffer_else << current_line << std::endl;
     89    }
    6590  }
    6691  std::string pythonscript_import( std::istreambuf_iterator<char>(buffer_import),
     
    7196  try {
    7297#ifdef HAVE_PYTHON2
    73     PyImport_AppendInittab( "pyMoleCuilder", &initpyMoleCuilder );
     98    PyImport_AppendInittab( PYMOLECUILDER_MODULE_NAME.c_str(), &initpyMoleCuilder );
    7499#else
    75     PyImport_AppendInittab( "pyMoleCuilder", &PyInit_pyMoleCuilder );
     100    PyImport_AppendInittab( PYMOLECUILDER_MODULE_NAME.c_str(), &PyInit_pyMoleCuilder );
    76101#endif
    77102
     
    89114
    90115    boost::python::object molecuilder_module( (boost::python::handle<>(PyImport_ImportModule("pyMoleCuilder"))) );
    91     main_namespace["pyMoleCuilder"] = molecuilder_module;
     116    main_namespace[PYMOLECUILDER_MODULE_NAME] = molecuilder_module;
     117    if (moduleName.size() > 0)
     118        main_namespace[moduleName] = molecuilder_module;
    92119
    93120    std::string enveloped_script("print(\"BEGIN of "+python_scripttitle+":\")\n");
  • src/Python/PythonScripting.hpp

    r9928ece ra93896  
    2020bool executePythonScript(const std::string &python_string, const std::string &python_scripttitle);
    2121
     22const std::string PYMOLECUILDER_MODULE_NAME = "pyMoleCuilder";
     23
    2224#endif /* PYTHONSCRIPTING_HPP_ */
  • tests/regression/Options/Session/pre/complexwait.py

    r9928ece ra93896  
    22from builtins import str
    33
    4 import pyMoleCuilder
     4import pyMoleCuilder as mol
    55# ========================== Stored Session BEGIN ==========================
    66i=1
    77print("wait1 begin")
    8 pyMoleCuilder.wait()
     8mol.wait()
    99i=2
    1010print("wait1 end")
    11 pyMoleCuilder.CommandVerbose(str(i))
    12 pyMoleCuilder.ParserSetOutputFormats("mpqc tremolo")
     11mol.CommandVerbose(str(i))
     12mol.ParserSetOutputFormats("mpqc tremolo")
    1313print("wait2 begin")
    14 pyMoleCuilder.wait()
     14mol.wait()
    1515i=i+1
    1616print("wait2 end")
    17 pyMoleCuilder.CommandSetRandomNumbersDistribution("uniform_int", "p=1;")
    18 pyMoleCuilder.WorldChangeBox("20,0,0,20,0,20")
    19 pyMoleCuilder.AtomAdd("1", "1,1,1")
    20 pyMoleCuilder.SelectionAtomById("0 0")
     17mol.CommandSetRandomNumbersDistribution("uniform_int", "p=1;")
     18mol.WorldChangeBox("20,0,0,20,0,20")
     19mol.AtomAdd("1", "1,1,1")
     20mol.SelectionAtomById("0 0")
    2121print("wait3 begin")
    22 pyMoleCuilder.wait()
     22mol.wait()
    2323i=i+1
    2424print("wait3 end")
    25 pyMoleCuilder.GraphSubgraphDissection()
    26 pyMoleCuilder.SelectionMoleculeOfAtom()
    27 pyMoleCuilder.FillRegularGrid("3 3 3", "0.5,0.5,0.5", "1", "0", "0", "0", "0")
    28 pyMoleCuilder.CommandVersion()
     25mol.GraphSubgraphDissection()
     26mol.SelectionMoleculeOfAtom()
     27mol.FillRegularGrid("3 3 3", "0.5,0.5,0.5", "1", "0", "0", "0", "0")
     28mol.CommandVersion()
    2929print("wait4 begin")
    30 pyMoleCuilder.wait()
     30mol.wait()
    3131i=i+1
    3232print("wait4 end")
  • tests/regression/Options/Session/testsuite-options-load-session-python.at

    r9928ece ra93896  
    5151file=complexwait.py
    5252AT_CHECK([../../molecuilder --load-session ${abs_top_srcdir}/tests/regression/Options/Session/pre/$file], 0, [stdout], [ignore])
     53# NOTE: The output from the python script itself may or may not appear. The embedding has weird
     54# effects, especially with the additional thread for the ActionQueue. Loading a python
     55# script inside the GUI is a miracle to work already. I don't care for now whether the
     56# script itself is also nicely executed.
    5357AT_CHECK([grep "Setting verbosity from 1 to 2." stdout], 0, [ignore], [ignore])
    54 AT_CHECK([grep "wait1" stdout], 0, [ignore], [ignore])
    55 AT_CHECK([grep "wait2" stdout], 0, [ignore], [ignore])
    56 AT_CHECK([grep "wait3" stdout], 0, [ignore], [ignore])
    57 AT_CHECK([grep "wait4" stdout], 0, [ignore], [ignore])
     58AT_CHECK([grep "STATUS: Clearing the Adjacency" stdout], 0, [ignore], [ignore])
     59AT_CHECK([grep "STATUS: Removing all present molecules." stdout], 0, [ignore], [ignore])
     60AT_CHECK([grep "Calling Action fill-regular-grid" stdout], 0, [ignore], [ignore])
    5861
    5962AT_CLEANUP
Note: See TracChangeset for help on using the changeset viewer.