kicad/scripting/python_scripting.cpp

133 lines
4.0 KiB
C++
Raw Normal View History

/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2012 NBEE Embedded Systems, Miguel Angel Ajo <miguelangel@nbee.es>
* Copyright (C) 1992-2012 KiCad Developers, see AUTHORS.txt for contributors.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, you may find one here:
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* or you may search the http://www.gnu.org website for the version 2 license,
* or you may write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
/**
* @file python_scripting.cpp
* @brief methods to add scripting capabilities inside pcbnew
*/
#include <python_scripting.h>
2012-06-27 21:19:19 +00:00
#include <stdlib.h>
#include <string.h>
/* init functions defined by swig */
2012-06-27 21:19:19 +00:00
extern "C" void init_kicad( void );
2012-06-27 21:19:19 +00:00
extern "C" void init_pcbnew( void );
2012-06-27 21:19:19 +00:00
#define EXTRA_PYTHON_MODULES 2 // this is the number of python
// modules that we want to add into the list
/* python inittab that links module names to module init functions
* we will rebuild it to include the original python modules plus
2012-06-27 21:19:19 +00:00
* our own ones
*/
2012-06-27 21:19:19 +00:00
struct _inittab *SwigImportInittab;
static int SwigNumModules = 0;
/* Add a name + initfuction to our SwigImportInittab */
2012-06-27 21:19:19 +00:00
static void swigAddModule( const char* name, void (* initfunc)() )
{
2012-06-27 21:19:19 +00:00
SwigImportInittab[SwigNumModules].name = (char*) name;
SwigImportInittab[SwigNumModules].initfunc = initfunc;
SwigNumModules++;
SwigImportInittab[SwigNumModules].name = (char*) 0;
SwigImportInittab[SwigNumModules].initfunc = 0;
}
2012-06-27 21:19:19 +00:00
/* Add the builting python modules */
2012-06-27 21:19:19 +00:00
static void swigAddBuiltin()
{
2012-06-27 21:19:19 +00:00
int i = 0;
/* discover the length of the pyimport inittab */
while( PyImport_Inittab[i].name ) i++;
/* allocate memory for the python module table */
2012-06-27 21:19:19 +00:00
SwigImportInittab = (struct _inittab*) malloc(
sizeof(struct _inittab)*(i+EXTRA_PYTHON_MODULES));
/* copy all pre-existing python modules into our newly created table */
i=0;
2012-06-27 21:19:19 +00:00
while( PyImport_Inittab[i].name )
{
swigAddModule( PyImport_Inittab[i].name, PyImport_Inittab[i].initfunc );
i++;
}
}
2012-06-27 21:19:19 +00:00
/* Function swigAddModules
* adds the internal modules we offer to the python scripting, so they will be
* available to the scripts we run.
*
*/
static void swigAddModules()
{
2012-06-27 21:19:19 +00:00
swigAddModule( "_pcbnew", init_pcbnew );
// finally it seems better to include all in just one module
// but in case we needed to include any other modules,
// it must be done like this:
// swigAddModule("_kicad",init_kicad);
}
2012-06-27 21:19:19 +00:00
/* Function swigSwitchPythonBuiltin
* switches python module table to our built one .
*
*/
static void swigSwitchPythonBuiltin()
{
2012-06-27 21:19:19 +00:00
PyImport_Inittab = SwigImportInittab;
}
2012-06-27 21:19:19 +00:00
/* Function pcbnewInitPythonScripting
* Initializes all the python environment and publish our interface inside it
*
*/
void pcbnewInitPythonScripting()
{
2012-06-27 21:19:19 +00:00
swigAddBuiltin(); // add builtin functions
swigAddModules(); // add our own modules
swigSwitchPythonBuiltin(); // switch the python builtin modules to our new list
2012-06-27 21:19:19 +00:00
Py_Initialize(); // call the python library to get it initialized
2012-06-27 21:19:19 +00:00
// load pcbnew inside python, and load all the user plugins, TODO: add system wide plugins
2012-06-27 21:19:19 +00:00
PyRun_SimpleString( "import sys\n"
"sys.path.append(\".\")\n"
"import pcbnew\n"
"pcbnew.LoadPlugins()"
);
}