You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

107 lines
3.2 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 1992-2019 KiCad Developers, see AUTHORS.txt for contributors.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, you may find one here:
  18. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  19. * or you may search the http://www.gnu.org website for the version 2 license,
  20. * or you may write to the Free Software Foundation, Inc.,
  21. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  22. */
  23. #ifndef __PYTHON_SCRIPTING_H
  24. #define __PYTHON_SCRIPTING_H
  25. // undefs explained here: https://bugzilla.redhat.com/show_bug.cgi?id=427617
  26. #ifdef _POSIX_C_SOURCE
  27. #undef _POSIX_C_SOURCE
  28. #endif
  29. #ifdef _XOPEN_SOURCE
  30. #undef _XOPEN_SOURCE
  31. #endif
  32. #if defined(WIN32)
  33. #undef pid_t // wxWidgets defines this, python typedefs, result is a conflict
  34. #endif
  35. #undef HAVE_CLOCK_GETTIME // macro is defined in Python.h and causes redefine warning
  36. #include <Python.h>
  37. #undef HAVE_CLOCK_GETTIME
  38. #include <wx/window.h>
  39. #include <wx/string.h>
  40. #include <wx/arrstr.h>
  41. #include <kicommon.h>
  42. class KICOMMON_API SCRIPTING
  43. {
  44. public:
  45. SCRIPTING();
  46. ~SCRIPTING();
  47. /// We do not allow secondary creation of the scripting system
  48. SCRIPTING( SCRIPTING const& ) = delete;
  49. void operator= ( SCRIPTING const& ) = delete;
  50. static bool IsWxAvailable();
  51. static bool IsModuleLoaded( std::string& aModule );
  52. enum PATH_TYPE
  53. {
  54. STOCK,
  55. USER,
  56. THIRDPARTY
  57. };
  58. static wxString PyScriptingPath( PATH_TYPE aPathType = STOCK );
  59. static wxString PyPluginsPath( PATH_TYPE aPathType = STOCK );
  60. private:
  61. bool scriptingSetup();
  62. PyThreadState* m_python_thread_state;
  63. };
  64. /**
  65. * Set an environment variable in the current Python interpreter.
  66. *
  67. * @param aVar is the variable to set
  68. * @param aValue is the value to give it
  69. */
  70. KICOMMON_API void UpdatePythonEnvVar( const wxString& aVar, const wxString& aValue );
  71. KICOMMON_API void RedirectStdio();
  72. KICOMMON_API wxWindow* CreatePythonShellWindow( wxWindow* parent, const wxString& aFramenameId );
  73. KICOMMON_API bool InitPythonScripting( const char* aStockScriptingPath,
  74. const char* aUserScriptingPath );
  75. KICOMMON_API bool IsWxPythonLoaded();
  76. class KICOMMON_API PyLOCK
  77. {
  78. PyGILState_STATE gil_state;
  79. public:
  80. PyLOCK() { gil_state = PyGILState_Ensure(); }
  81. ~PyLOCK() { PyGILState_Release( gil_state ); }
  82. };
  83. KICOMMON_API wxString PyStringToWx( PyObject* str );
  84. KICOMMON_API wxArrayString PyArrayStringToWx( PyObject* arr );
  85. KICOMMON_API wxString PyErrStringWithTraceback();
  86. #endif // __PYTHON_SCRIPTING_H