Footprints swig API: access shown text in fields

This commit is contained in:
qu1ck 2023-06-23 10:44:26 -07:00 committed by Jon Evans
parent 7779a01d48
commit ec94439df4
3 changed files with 23 additions and 12 deletions

View File

@ -30,7 +30,6 @@
%template(MAP_STRING_STRING) std::map<wxString, wxString>;
%rename(GetFieldsNative) FOOTPRINT::GetFields;
%feature("flatnested");
%include footprint.h
%feature("flatnested", "");
@ -59,18 +58,30 @@
# the object in the garbage collector
#
def GetFields(self):
""" Returns footprint fields map. """
fields = self.GetFieldsNative()
def GetFieldsText(self):
""" Returns footprint fields name to text map. """
fields = self.GetFields()
return {str(field.GetName()): str(field.GetText()) for field in fields}
def GetField(self, key):
""" Returns Field with a given key if it exists, throws KeyError otherwise. """
def GetFieldsShownText(self):
""" Returns footprint fields name to shown text map. """
fields = self.GetFields()
return {str(field.GetName()): str(field.GetShownText(False)) for field in fields}
def GetFieldText(self, key):
""" Returns Field text with a given key if it exists, throws KeyError otherwise. """
if self.HasFieldByName(key):
return self.GetFieldByName(key).GetText()
else:
raise KeyError("Field not found: " + key)
def GetFieldShownText(self, key):
""" Returns Field shown text with a given key if it exists, throws KeyError otherwise. """
if self.HasFieldByName(key):
return self.GetFieldByName(key).GetShownText(False)
else:
raise KeyError("Field not found: " + key)
def SetField(self, key, value):
if self.HasFieldByName(key):
self.GetFieldByName(key).SetText(value)

View File

@ -52,7 +52,7 @@ if( NOT (MSVC AND CMAKE_BUILD_TYPE STREQUAL "Debug") )
# but the kicad binaries are linked to the debug mode python
# Test that runs the QA tests through scripting
add_test(NAME qa_python
COMMAND ${PYTHON_EXECUTABLE} -m pytest ${CMAKE_CURRENT_SOURCE_DIR}/pcbnewswig ${PYTEST_ARGS_QACLI}
COMMAND ${PYTHON_EXECUTABLE} -m pytest ${CMAKE_CURRENT_SOURCE_DIR}/pcbnewswig ${PYTEST_ARGS_QAPYTHON}
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)

View File

@ -128,9 +128,9 @@ class TestBoardClass:
}
assert footprint.GetSheetfile() == 'custom_fields.kicad_sch'
assert footprint.GetSheetname() == ''
assert footprint.GetField('myfield') == 'myvalue'
assert footprint.HasField('myfield') == True
assert footprint.HasField('abcd') == False
assert footprint.GetFieldText('myfield') == 'myvalue'
assert footprint.HasField('myfield')
assert not footprint.HasField('abcd')
footprint.SetField('abcd', 'efgh')
assert footprint.HasField('abcd') == True
assert footprint.GetField('abcd') == 'efgh'
assert footprint.HasField('abcd')
assert footprint.GetFieldText('abcd') == 'efgh'