From f23bb59cd157b8b7f6aec7f14c637c006fbda394 Mon Sep 17 00:00:00 2001 From: jean-pierre charras Date: Sat, 18 Oct 2014 10:18:14 +0200 Subject: [PATCH] Python scripting: make UTF8 class accessible by python scripts. Add python method GetChars() to UTF8 class to get its char buffer. See scripts/test_kicad_plugin.py for example. --- include/utf8.h | 9 +++++++++ scripting/kicad.i | 27 +++++++++++++++++++++++++-- scripts/test_kicad_plugin.py | 14 +++++++++----- 3 files changed, 43 insertions(+), 7 deletions(-) diff --git a/include/utf8.h b/include/utf8.h index 6f618a3897..b4f6f9802b 100644 --- a/include/utf8.h +++ b/include/utf8.h @@ -76,6 +76,10 @@ public: { } + ~UTF8() // Needed mainly to build python wrapper + { + } + UTF8& operator=( const wxString& o ); UTF8& operator=( const std::string& o ) @@ -144,6 +148,11 @@ public: public: + uni_iter() // Needed only to build python wrapper, not used outside the wrapper + { + it = NULL; + } + uni_iter( const uni_iter& o ) { it = o.it; diff --git a/scripting/kicad.i b/scripting/kicad.i index c522b040e3..35c7f91790 100644 --- a/scripting/kicad.i +++ b/scripting/kicad.i @@ -53,6 +53,7 @@ %ignore operator <<; %ignore operator=; + /* headers/imports that must be included in the _wrapper.cpp at top */ %{ @@ -62,7 +63,7 @@ #include #include #include - #include + #include #include #include @@ -70,7 +71,6 @@ #include #include #include - %} /* all the wx wrappers for wxString, wxPoint, wxRect, wxChar .. */ @@ -110,7 +110,30 @@ /* std template mappings */ %template(intVector) std::vector; +%template(str_utf8_Map) std::map< std::string,UTF8 >; /* KiCad plugin handling */ %include "kicadplugins.i" +// ignore warning relative to operator = and operator ++: +#pragma SWIG nowarn=362,383 + +// Rename operators defined in utf8.h +%rename(utf8_to_charptr) operator char* () const; +%rename(utf8_to_wxstring) operator wxString () const; + +#include +%include + +%extend UTF8 +{ + const char* Cast_to_CChar() { return (self->c_str()); } + + %pythoncode + { + + def GetChars(self): + return self.Cast_to_CChar() + } +} + diff --git a/scripts/test_kicad_plugin.py b/scripts/test_kicad_plugin.py index fc2934cebb..0a1d253c85 100755 --- a/scripts/test_kicad_plugin.py +++ b/scripts/test_kicad_plugin.py @@ -20,8 +20,8 @@ import sys import os -lib_path1='/tmp/lib1.pretty' -lib_path2='/tmp/lib2.pretty' +lib_path1='f:/tmp/lib1.pretty' +lib_path2='f:/tmp/lib2.pretty' plugin = IO_MGR.PluginFind( IO_MGR.KICAD ) @@ -65,23 +65,27 @@ plugin.FootprintSave( lib_path1, module ) # create a disparity between the library's name ("footprint"), # and the module's internal useless name ("mine"). Module is officially named "footprint" now # but has (module mine ...) internally: -os.rename( '/tmp/lib2.pretty/mine.kicad_mod', '/tmp/lib2.pretty/footprint.kicad_mod' ) +os.rename( 'f:/tmp/lib2.pretty/mine.kicad_mod', 'f:/tmp/lib2.pretty/footprint.kicad_mod' ) footprint=plugin.FootprintLoad( lib_path2, 'footprint' ) fpid = footprint.GetFPID() +fpid.SetLibNickname( UTF8( 'mylib' ) ) +name = fpid.Format().GetChars() # Always after a FootprintLoad() the internal name should match the one used to load it. -print( "internal name should be 'footprint':", fpid.GetFootprintName() ) +print( "internal name should be 'footprint':", name ) # Verify that the same plugin instance can edge trigger on a lib_path change # for FootprintLoad() footprint=plugin.FootprintLoad( lib_path1, 'mine' ) fpid = footprint.GetFPID() +fpid.SetLibNickname( UTF8( 'other_mylib' ) ) +name = fpid.Format().GetChars() # Always after a FootprintLoad() the internal name should match the one used to load it. -print( "internal name should be 'mine':", fpid.GetFootprintName() ) +print( "internal name should be 'mine':", name ) # As of 3-Dec-2013 this test is passed by KICAD_PLUGIN and Wayne is owed an atta boy!