CString GetDir(const CString& strFSpec) { CString strDir = GetDirNoSlash(strFSpec); if( strDir.Right(1) != _T("\\") ) strDir += _T("\\"); return strDir; }
BOOL CPyScriptAutoConfigDlg::InitPython() { // in case we've been here before, reset it now. if( m_pModule != 0 ) ResetPython(); // search in that file for the available function names Py_Initialize(); // next add the path to the sys.path CStringA strCmd; strCmd.Format("import sys\nsys.path.append('%s')", CT2A(GetDirNoSlash(m_strConverterFilespec))); PyRun_SimpleString(strCmd); // turn the filename into a Python object (Python import doesn't like .py extension) CString strFilespec = m_strConverterFilespec; if( !strFilespec.Right(3).CompareNoCase(_T(".py")) ) strFilespec = strFilespec.Left(strFilespec.GetLength() - 3); int nIndex = strFilespec.ReverseFind('\\'); strFilespec = strFilespec.Right(strFilespec.GetLength() - nIndex - 1); // get the module point by the name m_pModule = PyImport_ImportModule(CT2A(strFilespec)); if( m_pModule == 0 ) { CString strError; // first check to see if the Python reg keys exist. If not, then tell them they need a distribution CRegKey keyPythonInstallPath; if( keyPythonInstallPath.Open(HKEY_LOCAL_MACHINE, clpszPythonInstallPathKey, KEY_READ) != ERROR_SUCCESS ) strError = _T("It doesn't appear that you have a Python distribution installed. See the About tab for details of where to get one."); else strError.Format(_T("Unable to import script module '%s'! Is it locked? Does it have a syntax error? Is a Python distribution installed?"),strFilespec); MessageBox(strError); // gracefully disconnect from Python if( PyErr_Occurred() ) PyErr_Clear(); // without this, the Finalize normally throws a fatal exception. Py_Finalize(); return false; } m_pDictionary = PyModule_GetDict(m_pModule); return true; }