LOCALE_IO toggle implementation for locale switches on scripting; code cleanups to comply with kicad coding style policy

This commit is contained in:
Miguel Angel Ajo
2013-03-15 17:35:24 +01:00
parent cefd3cd5e2
commit 155ea57c36
12 changed files with 449 additions and 450 deletions
+60 -52
View File
@@ -54,18 +54,18 @@ extern "C" void init_pcbnew( void );
* our own ones
*/
struct _inittab *SwigImportInittab;
static int SwigNumModules = 0;
struct _inittab* SwigImportInittab;
static int SwigNumModules = 0;
/* Add a name + initfuction to our SwigImportInittab */
static void swigAddModule( const char* name, void (* initfunc)() )
{
SwigImportInittab[SwigNumModules].name = (char*) name;
SwigImportInittab[SwigNumModules].name = (char*) name;
SwigImportInittab[SwigNumModules].initfunc = initfunc;
SwigNumModules++;
SwigImportInittab[SwigNumModules].name = (char*) 0;
SwigImportInittab[SwigNumModules].name = (char*) 0;
SwigImportInittab[SwigNumModules].initfunc = 0;
}
@@ -81,11 +81,12 @@ static void swigAddBuiltin()
i++;
/* allocate memory for the python module table */
SwigImportInittab = (struct _inittab*) malloc(
sizeof(struct _inittab)*(i+EXTRA_PYTHON_MODULES));
SwigImportInittab = (struct _inittab*) malloc(
sizeof(struct _inittab) * (i + EXTRA_PYTHON_MODULES) );
/* copy all pre-existing python modules into our newly created table */
i=0;
i = 0;
while( PyImport_Inittab[i].name )
{
swigAddModule( PyImport_Inittab[i].name, PyImport_Inittab[i].initfunc );
@@ -107,9 +108,10 @@ static void swigAddModules()
// 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);
// swigAddModule( "_kicad", init_kicad );
}
/* Function swigSwitchPythonBuiltin
* switches python module table to our built one .
*
@@ -120,6 +122,7 @@ static void swigSwitchPythonBuiltin()
PyImport_Inittab = SwigImportInittab;
}
/* Function pcbnewInitPythonScripting
* Initializes all the python environment and publish our interface inside it
* initializes all the wxpython interface, and returns the python thread control structure
@@ -142,9 +145,9 @@ bool pcbnewInitPythonScripting()
// Load the wxPython core API. Imports the wx._core_ module and sets a
// local pointer to a function table located there. The pointer is used
// internally by the rest of the API functions.
if( ! wxPyCoreAPI_IMPORT() )
if( !wxPyCoreAPI_IMPORT() )
{
wxLogError(wxT("***** Error importing the wxPython API! *****"));
wxLogError( wxT( "***** Error importing the wxPython API! *****" ) );
PyErr_Print();
Py_Finalize();
return false;
@@ -160,7 +163,7 @@ bool pcbnewInitPythonScripting()
#endif
{
PyLOCK lock;
PyLOCK lock;
PyRun_SimpleString( "import sys\n"
"sys.path.append(\".\")\n"
@@ -172,10 +175,11 @@ bool pcbnewInitPythonScripting()
return true;
}
void pcbnewFinishPythonScripting()
{
#ifdef KICAD_SCRIPTING_WXPYTHON
wxPyEndAllowThreads(g_PythonMainTState);
wxPyEndAllowThreads( g_PythonMainTState );
#endif
Py_Finalize();
}
@@ -189,54 +193,55 @@ void RedirectStdio()
// redirects Python's stdout and stderr to a window that will popup
// only on demand when something is printed, like a traceback.
const char* python_redirect =
"import sys\n\
import wx\n\
output = wx.PyOnDemandOutputWindow()\n\
c sys.stderr = output\n";
"import sys\n"
"import wx\n"
"output = wx.PyOnDemandOutputWindow()\n"
"sys.stderr = output\n";
PyLOCK lock;
PyLOCK lock;
PyRun_SimpleString( python_redirect );
}
wxWindow* CreatePythonShellWindow(wxWindow* parent)
wxWindow* CreatePythonShellWindow( wxWindow* parent )
{
const char* pycrust_panel = "\
import wx\n\
from wx.py import shell, version\n\
\n\
class PyCrustPanel(wx.Panel):\n\
\tdef __init__(self, parent):\n\
\t\twx.Panel.__init__(self, parent, -1, style=wx.SUNKEN_BORDER)\n\
\t\t\n\
\t\t\n\
\t\tintro = \"Welcome To PyCrust %s - KiCAD Python Shell\" % version.VERSION\n\
\t\tpycrust = shell.Shell(self, -1, introText=intro)\n\
\t\t\n\
\t\tsizer = wx.BoxSizer(wx.VERTICAL)\n\n\
\t\tsizer.Add(pycrust, 1, wx.EXPAND|wx.BOTTOM|wx.LEFT|wx.RIGHT, 10)\n\n\
\t\tself.SetSizer(sizer)\n\n\
\n\
def makeWindow(parent):\n\
win = PyCrustPanel(parent)\n\
return win\n\
";
const char* pycrust_panel =
"import wx\n"
"from wx.py import shell, version\n"
"\n"
"class PyCrustPanel(wx.Panel):\n"
"\tdef __init__(self, parent):\n"
"\t\twx.Panel.__init__(self, parent, -1, style=wx.SUNKEN_BORDER)\n"
"\t\t\n"
"\t\t\n"
"\t\tintro = \"Welcome To PyCrust %s - KiCAD Python Shell\" % version.VERSION\n"
"\t\tpycrust = shell.Shell(self, -1, introText=intro)\n"
"\t\t\n"
"\t\tsizer = wx.BoxSizer(wx.VERTICAL)\n\n"
"\t\tsizer.Add(pycrust, 1, wx.EXPAND|wx.BOTTOM|wx.LEFT|wx.RIGHT, 10)\n\n"
"\t\tself.SetSizer(sizer)\n\n"
"\n"
"def makeWindow(parent):\n"
" win = PyCrustPanel(parent)\n"
" return win\n"
"\n";
wxWindow* window = NULL;
PyObject* result;
wxWindow* window = NULL;
PyObject* result;
// As always, first grab the GIL
PyLOCK lock;
PyLOCK lock;
// Now make a dictionary to serve as the global namespace when the code is
// executed. Put a reference to the builtins module in it.
PyObject* globals = PyDict_New();
PyObject* builtins = PyImport_ImportModule( "__builtin__" );
PyObject* globals = PyDict_New();
PyObject* builtins = PyImport_ImportModule( "__builtin__" );
PyDict_SetItemString( globals, "__builtins__", builtins );
Py_DECREF(builtins);
Py_DECREF( builtins );
// Execute the code to make the makeWindow function we defined above
result = PyRun_String( pycrust_panel, Py_file_input, globals, globals );
@@ -247,7 +252,8 @@ def makeWindow(parent):\n\
PyErr_Print();
return NULL;
}
Py_DECREF(result);
Py_DECREF( result );
// Now there should be an object named 'makeWindow' in the dictionary that
// we can grab a pointer to:
@@ -258,10 +264,10 @@ def makeWindow(parent):\n\
// use of another wxPython API to take a wxWindows object and build a
// wxPython object that wraps it.
PyObject* arg = wxPyMake_wxObject( parent, false );
PyObject* arg = wxPyMake_wxObject( parent, false );
wxASSERT( arg != NULL );
PyObject* tuple = PyTuple_New( 1 );
PyObject* tuple = PyTuple_New( 1 );
PyTuple_SET_ITEM( tuple, 0, arg );
result = PyEval_CallObject( func, tuple );
@@ -273,11 +279,11 @@ def makeWindow(parent):\n\
{
// Otherwise, get the returned window out of Python-land and
// into C++-ville...
bool success = wxPyConvertSwigPtr(result, (void**)&window, _T("wxWindow") );
(void)success;
bool success = wxPyConvertSwigPtr( result, (void**) &window, _T( "wxWindow" ) );
(void) success;
wxASSERT_MSG(success, _T("Returned object was not a wxWindow!") );
Py_DECREF(result);
wxASSERT_MSG( success, _T( "Returned object was not a wxWindow!" ) );
Py_DECREF( result );
}
// Release the python objects we still have
@@ -286,4 +292,6 @@ def makeWindow(parent):\n\
return window;
}
#endif