Changeset a93896 for src/Python


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.
Location:
src/Python
Files:
2 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_ */
Note: See TracChangeset for help on using the changeset viewer.