Changeset 2cf12a for src/Python


Ignore:
Timestamp:
May 17, 2015, 12:00:52 PM (10 years ago)
Author:
Frederik Heber <heber@…>
Branches:
Action_Thermostats, Add_AtomRandomPerturbation, Add_FitFragmentPartialChargesAction, Add_RotateAroundBondAction, Add_SelectAtomByNameAction, Added_ParseSaveFragmentResults, AddingActions_SaveParseParticleParameters, Adding_Graph_to_ChangeBondActions, Adding_MD_integration_tests, Adding_ParticleName_to_Atom, Adding_StructOpt_integration_tests, AtomFragments, Automaking_mpqc_open, AutomationFragmentation_failures, Candidate_v1.5.4, Candidate_v1.6.0, Candidate_v1.6.1, ChangeBugEmailaddress, ChangingTestPorts, ChemicalSpaceEvaluator, CombiningParticlePotentialParsing, Combining_Subpackages, Debian_Package_split, Debian_package_split_molecuildergui_only, Disabling_MemDebug, Docu_Python_wait, EmpiricalPotential_contain_HomologyGraph, EmpiricalPotential_contain_HomologyGraph_documentation, Enable_parallel_make_install, Enhance_userguide, Enhanced_StructuralOptimization, Enhanced_StructuralOptimization_continued, Example_ManyWaysToTranslateAtom, Exclude_Hydrogens_annealWithBondGraph, FitPartialCharges_GlobalError, Fix_BoundInBox_CenterInBox_MoleculeActions, Fix_ChargeSampling_PBC, Fix_ChronosMutex, Fix_FitPartialCharges, Fix_FitPotential_needs_atomicnumbers, Fix_ForceAnnealing, Fix_IndependentFragmentGrids, Fix_ParseParticles, Fix_ParseParticles_split_forward_backward_Actions, Fix_PopActions, Fix_QtFragmentList_sorted_selection, Fix_Restrictedkeyset_FragmentMolecule, Fix_StatusMsg, Fix_StepWorldTime_single_argument, Fix_Verbose_Codepatterns, Fix_fitting_potentials, Fixes, ForceAnnealing_goodresults, ForceAnnealing_oldresults, ForceAnnealing_tocheck, ForceAnnealing_with_BondGraph, ForceAnnealing_with_BondGraph_continued, ForceAnnealing_with_BondGraph_continued_betteresults, ForceAnnealing_with_BondGraph_contraction-expansion, FragmentAction_writes_AtomFragments, FragmentMolecule_checks_bonddegrees, GeometryObjects, Gui_Fixes, Gui_displays_atomic_force_velocity, ImplicitCharges, IndependentFragmentGrids, IndependentFragmentGrids_IndividualZeroInstances, IndependentFragmentGrids_IntegrationTest, IndependentFragmentGrids_Sole_NN_Calculation, JobMarket_RobustOnKillsSegFaults, JobMarket_StableWorkerPool, JobMarket_unresolvable_hostname_fix, MoreRobust_FragmentAutomation, ODR_violation_mpqc_open, PartialCharges_OrthogonalSummation, PdbParser_setsAtomName, PythonUI_with_named_parameters, QtGui_reactivate_TimeChanged_changes, Recreated_GuiChecks, Rewrite_FitPartialCharges, RotateToPrincipalAxisSystem_UndoRedo, SaturateAtoms_findBestMatching, SaturateAtoms_singleDegree, StoppableMakroAction, Subpackage_CodePatterns, Subpackage_JobMarket, Subpackage_LinearAlgebra, Subpackage_levmar, Subpackage_mpqc_open, Subpackage_vmg, Switchable_LogView, ThirdParty_MPQC_rebuilt_buildsystem, TrajectoryDependenant_MaxOrder, TremoloParser_IncreasedPrecision, TremoloParser_MultipleTimesteps, TremoloParser_setsAtomName, Ubuntu_1604_changes, stable
Children:
3a4be0
Parents:
06993e
git-author:
Frederik Heber <heber@…> (05/17/15 07:33:08)
git-committer:
Frederik Heber <heber@…> (05/17/15 12:00:52)
Message:

Split executePythonScript into file and string script option.

  • as python script is internally a string anyway, this allows more freedom in creating or internally modifying python scripts.
Location:
src/Python
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • src/Python/PythonScripting.cpp

    r06993e r2cf12a  
    3939#include "CodePatterns/MemDebug.hpp"
    4040
    41 bool executePythonScript(const std::string &pythonfilename_string)
     41bool executePythonScriptFile(const boost::filesystem::path &pythonfilename)
    4242{
    43   boost::filesystem::path pythonfilename(pythonfilename_string);
    4443  if (exists(pythonfilename)) {
    4544    // parse in and execute the local and global config.py
     
    6160      std::string pythonscript( std::istreambuf_iterator<char>(pythonfile),
    6261          (std::istreambuf_iterator<char>()) );
    63       std::string enveloped_script("print \"BEGIN of "+pythonfilename_string+":\"\n");
     62      std::string enveloped_script("print \"BEGIN of "+pythonfilename.string()+":\"\n");
    6463      enveloped_script += pythonscript;
    65       enveloped_script += std::string("print \"END of "+pythonfilename_string+":\"\n");
     64      enveloped_script += std::string("print \"END of "+pythonfilename.string()+":\"\n");
    6665
    6766      boost::python::handle<> ignored(( PyRun_String( enveloped_script.c_str(),
     
    7877  return false;
    7978}
     79
     80bool executePythonScript(const std::string &python_string, const std::string &python_scripttitle)
     81{
     82  try {
     83    PyImport_AppendInittab( "pyMoleCuilder", &initpyMoleCuilder );
     84
     85    Py_Initialize();
     86
     87    boost::python::object main_module((
     88        boost::python::handle<>(boost::python::borrowed(PyImport_AddModule("__main__")))));
     89
     90    boost::python::object main_namespace = main_module.attr("__dict__");
     91
     92    boost::python::object molecuilder_module( (boost::python::handle<>(PyImport_ImportModule("pyMoleCuilder"))) );
     93    main_namespace["pyMoleCuilder"] = molecuilder_module;
     94
     95    std::string enveloped_script("print \"BEGIN of "+python_scripttitle+":\"\n");
     96    enveloped_script += python_string+std::string("\n");
     97    enveloped_script += std::string("print \"END of "+python_scripttitle+":\"\n");
     98
     99    boost::python::handle<> ignored(( PyRun_String( enveloped_script.c_str(),
     100        Py_file_input,
     101        main_namespace.ptr(),
     102        main_namespace.ptr() ) ));
     103
     104  } catch( boost::python::error_already_set ) {
     105    PyErr_Print();
     106    return false;
     107  }
     108  return true;
     109}
  • src/Python/PythonScripting.hpp

    r06993e r2cf12a  
    1515
    1616#include <string>
     17#include <boost/filesystem/path.hpp>
    1718
    18 bool executePythonScript(const std::string &pythonfilename_string);
     19bool executePythonScriptFile(const boost::filesystem::path &pythonfilename);
     20bool executePythonScript(const std::string &python_string, const std::string &python_scripttitle);
    1921
    2022#endif /* PYTHONSCRIPTING_HPP_ */
Note: See TracChangeset for help on using the changeset viewer.