Adapted client code to use new findTool... api and fixed test cases.

This commit is contained in:
Markus Lampert
2021-01-15 19:07:22 -08:00
parent 495732ae6c
commit fccc7e6875
3 changed files with 27 additions and 27 deletions

View File

@@ -54,11 +54,9 @@ PathLog.setLevel(PathLog.Level.INFO, PathLog.thisModule())
def translate(context, text, disambig=None):
return PySide.QtCore.QCoreApplication.translate(context, text, disambig)
def _findToolFile(name, containerFile, typ, dbg=_DebugFindTool):
def _findToolFile(name, containerFile, typ):
PathLog.track(name)
if os.path.exists(name): # absolute reference
if dbg:
PathLog.debug("Found {} at {}".format(typ, name))
return name
if containerFile:
@@ -88,22 +86,24 @@ def _findToolFile(name, containerFile, typ, dbg=_DebugFindTool):
return None
def _findShape(name, path=None):
'''_findShape(name, path) ... search for name, full and partially starting with path in known shape directories'''
def findToolShape(name, path=None):
'''findToolShape(name, path) ... search for name, if relative path look in path'''
return _findToolFile(name, path, 'Shape')
def findBit(name, path=None):
def findToolBit(name, path=None):
'''findToolBit(name, path) ... search for name, if relative path look in path'''
PathLog.track(name)
if name.endswith('.fctb'):
return _findToolFile(name, path, 'Bit')
return _findToolFile("{}.fctb".format(name), path, 'Bit')
def findLibrary(name, path=None, dbg=False):
def findToolLibrary(name, path=None):
'''findToolLibrary(name, path) ... search for name, if relative path look in path'''
if name.endswith('.fctl'):
return _findToolFile(name, path, 'Library', dbg)
return _findToolFile("{}.fctl".format(name), path, 'Library', dbg)
return _findToolFile(name, path, 'Library')
return _findToolFile("{}.fctl".format(name), path, 'Library')
def _findRelativePath(path, typ):
@@ -222,7 +222,7 @@ class ToolBit(object):
doc = FreeCAD.getDocument(d)
break
if doc is None:
p = _findShape(p)
p = findToolShape(p, path if path else obj.File)
if not path and p != obj.BitShape:
obj.BitShape = p
PathLog.debug("ToolBit {} using shape file: {}".format(obj.Label, p))
@@ -330,7 +330,7 @@ class ToolBit(object):
def getBitThumbnail(self, obj):
if obj.BitShape:
path = _findShape(obj.BitShape)
path = findToolShape(obj.BitShape)
if path:
with open(path, 'rb') as fd:
try:

View File

@@ -143,7 +143,7 @@ class ModelFactory(object):
for toolBit in library['tools']:
try:
nr = toolBit['nr']
bit = PathToolBit.findBit(toolBit['path'])
bit = PathToolBit.findToolBit(toolBit['path'], path)
if bit:
PathLog.track(bit)
tool = PathToolBit.Declaration(bit)

View File

@@ -44,66 +44,66 @@ class TestPathToolBit(PathTestUtils.PathTestBase):
def test00(self):
'''Find a tool shape from file name'''
path = PathToolBit._findShape('endmill.fcstd')
path = PathToolBit.findToolShape('endmill.fcstd')
self.assertIsNot(path, None)
self.assertNotEqual(path, 'endmill.fcstd')
def test01(self):
'''Not find a relative path shape if not stored in default location'''
path = PathToolBit._findShape(TestToolShapeName)
path = PathToolBit.findToolShape(TestToolShapeName)
self.assertIsNone(path)
def test02(self):
'''Find a relative path shape if it's local to a bit path'''
path = PathToolBit._findShape(TestToolShapeName, testToolBit())
path = PathToolBit.findToolShape(TestToolShapeName, testToolBit())
self.assertIsNot(path, None)
self.assertEqual(path, testToolShape())
def test03(self):
'''Not find a tool shape from an invalid absolute path.'''
path = PathToolBit._findShape(testToolShape(TestInvalidDir))
path = PathToolBit.findToolShape(testToolShape(TestInvalidDir))
self.assertIsNone(path)
def test04(self):
'''Find a tool shape from a valid absolute path.'''
path = PathToolBit._findShape(testToolShape())
path = PathToolBit.findToolShape(testToolShape())
self.assertIsNot(path, None)
self.assertEqual(path, testToolShape())
def test10(self):
'''Find a tool bit from file name'''
path = PathToolBit.findBit('5mm_Endmill.fctb')
path = PathToolBit.findToolBit('5mm_Endmill.fctb')
self.assertIsNot(path, None)
self.assertNotEqual(path, '5mm_Endmill.fctb')
def test11(self):
'''Not find a relative path bit if not stored in default location'''
path = PathToolBit.findBit(TestToolBitName)
path = PathToolBit.findToolBit(TestToolBitName)
self.assertIsNone(path)
def test12(self):
'''Find a relative path bit if it's local to a library path'''
path = PathToolBit.findBit(TestToolBitName, testToolLibrary())
path = PathToolBit.findToolBit(TestToolBitName, testToolLibrary())
self.assertIsNot(path, None)
self.assertEqual(path, testToolBit())
def test13(self):
'''Not find a tool bit from an invalid absolute path.'''
path = PathToolBit.findBit(testToolBit(TestInvalidDir))
path = PathToolBit.findToolBit(testToolBit(TestInvalidDir))
self.assertIsNone(path)
def test14(self):
'''Find a tool bit from a valid absolute path.'''
path = PathToolBit.findBit(testToolBit())
path = PathToolBit.findToolBit(testToolBit())
self.assertIsNot(path, None)
self.assertEqual(path, testToolBit())
@@ -111,14 +111,14 @@ class TestPathToolBit(PathTestUtils.PathTestBase):
def test20(self):
'''Find a tool library from file name'''
path = PathToolBit.findBit('5mm_Endmill.fctb')
path = PathToolBit.findToolLibrary('Default.fctl')
self.assertIsNot(path, None)
self.assertNotEqual(path, '5mm_Endmill.fctb')
self.assertNotEqual(path, 'Default.fctl')
def test21(self):
'''Not find a relative path library if not stored in default location'''
path = PathToolBit.findBit(TestToolBitName)
path = PathToolBit.findToolLibrary(TestToolLibraryName)
self.assertIsNone(path)
@@ -130,13 +130,13 @@ class TestPathToolBit(PathTestUtils.PathTestBase):
def test23(self):
'''Not find a tool library from an invalid absolute path.'''
path = PathToolBit.findBit(testToolBit(TestInvalidDir))
path = PathToolBit.findToolLibrary(testToolLibrary(TestInvalidDir))
self.assertIsNone(path)
def test24(self):
'''Find a tool library from a valid absolute path.'''
path = PathToolBit.findBit(testToolBit())
path = PathToolBit.findToolBit(testToolBit())
self.assertIsNot(path, None)
self.assertEqual(path, testToolBit())