Reverted back to using the spreadsheet Name for the expression, since it has less issues than using the Label.

This commit is contained in:
Markus Lampert
2017-10-10 22:05:50 -07:00
parent 46d120071e
commit dba05326ff
2 changed files with 44 additions and 20 deletions

View File

@@ -74,9 +74,9 @@ It's mostly a convencience wrapper around Spreadsheet.
def __init__(self, obj):
self.obj = obj
def setup(self, label=None):
def setup(self, label='SetupSheet'):
'''setup(label=None) ... initializes receiver with default values.'''
self.obj.SetupSheet = self.obj.Document.addObject('Spreadsheet::Sheet', 'SetupSheet')
self.obj.SetupSheet = self.obj.Document.addObject('Spreadsheet::Sheet', 'Spreadsheet')
if label:
self.obj.SetupSheet.Label = label
self.obj.SetupSheet.set('A2', translate('PathSetupSheet', 'Tool Rapid Speeds'))
@@ -127,12 +127,30 @@ It's mostly a convencience wrapper around Spreadsheet.
def expressionReference(self):
'''expressionReference() ... returns the string to be used in expressions'''
return self.obj.SetupSheet.Label
# Using the Name here and not the Label (both would be valid) because the Name 'fails early'.
#
# If there is a Name/Label conflict and an expression is bound to the Name we'll get an error
# on creation (Property not found). Not good, but at least there's some indication that
# something's afoul.
#
# If the expression is based on the Label everything works out nicely - until the document is
# saved and loaded from disk. The Labels change in order to avoid the Name/Label conflict
# but the expression stays the same. If the user's lucky the expression is broken because the
# conflicting object doesn't have the properties reference by the expressions. If the user is
# not so lucky those properties also exist in the other object, there is no indication that
# anything is wrong but the expressions will substitute the values from the wrong object.
#
# I prefer the question: "why do I get this error when I create ..." over "my cnc machine just
# rammed it's tool head into the table ..." or even "I saved my file and now it's corrupt..."
#
# https://forum.freecadweb.org/viewtopic.php?f=10&t=24839
# https://forum.freecadweb.org/viewtopic.php?f=10&t=24845
return self.obj.SetupSheet.Name
def encodeTemplateAttributes(self, attrs):
'''encodeTemplateAttributes(attrs) ... return a dictionary with all values encoded.'''
return _traverseTemplateAttributes(attrs, lambda value: value.replace(self.expressionReference(), self.TemplateReference))
return _traverseTemplateAttributes(attrs, lambda value: unicode(value.replace(self.expressionReference(), self.TemplateReference)))
def decodeTemplateAttributes(self, attrs):
'''decodeTemplateAttributes(attrs) ... expand template attributes to reference the receiver where applicable.'''
return _traverseTemplateAttributes(attrs, lambda value: value.replace(self.TemplateReference, self.expressionReference()))
return _traverseTemplateAttributes(attrs, lambda value: unicode(value.replace(self.TemplateReference, self.expressionReference())))

View File

@@ -81,7 +81,7 @@ class TestPathSetupSheet(PathTestBase):
ss = PathSetupSheet.SetupSheet(self.obj)
ss.setup('x')
self.assertEqual(ss.expressionReference(), 'x')
self.assertEqual(ss.expressionReference(), 'Spreadsheet')
self.assertEqual(str(ss.encodeTemplateAttributes({'00': 13.00})), "{'00': 13.0}")
self.assertEqual(str(ss.decodeTemplateAttributes({'00': 13.00})), "{'00': 13.0}")
@@ -92,14 +92,14 @@ class TestPathSetupSheet(PathTestBase):
ss.setup('x')
self.assertEqual(str(ss.encodeTemplateAttributes({'00': 'SetupSheet'})), "{'00': u'SetupSheet'}")
self.assertEqual(str(ss.encodeTemplateAttributes({'00': 'x'})), "{'00': u'${SetupSheet}'}")
self.assertEqual(str(ss.encodeTemplateAttributes({'00': 'x.y'})), "{'00': u'${SetupSheet}.y'}")
self.assertEqual(str(ss.encodeTemplateAttributes({'00': 'Spreadsheet'})), "{'00': u'${SetupSheet}'}")
self.assertEqual(str(ss.encodeTemplateAttributes({'00': 'Spreadsheet.y'})), "{'00': u'${SetupSheet}.y'}")
self.assertEqual(str(ss.encodeTemplateAttributes({'00': '${SetupSheet}'})), "{'00': u'${SetupSheet}'}")
self.assertEqual(str(ss.decodeTemplateAttributes({'00': 'SetupSheet'})), "{'00': u'SetupSheet'}")
self.assertEqual(str(ss.decodeTemplateAttributes({'00': '${SetupSheet}'})), "{'00': u'x'}")
self.assertEqual(str(ss.decodeTemplateAttributes({'00': '${SetupSheet}.y'})), "{'00': u'x.y'}")
self.assertEqual(str(ss.decodeTemplateAttributes({'00': '${SetupSheet}.y - ${SetupSheet}.z'})), "{'00': u'x.y - x.z'}")
self.assertEqual(str(ss.decodeTemplateAttributes({'00': '${SetupSheet}'})), "{'00': u'Spreadsheet'}")
self.assertEqual(str(ss.decodeTemplateAttributes({'00': '${SetupSheet}.y'})), "{'00': u'Spreadsheet.y'}")
self.assertEqual(str(ss.decodeTemplateAttributes({'00': '${SetupSheet}.y - ${SetupSheet}.z'})), "{'00': u'Spreadsheet.y - Spreadsheet.z'}")
def test12(self):
'''Verify template attributes encoding/decoding of dictionaries.'''
@@ -107,10 +107,10 @@ class TestPathSetupSheet(PathTestBase):
ss.setup('rrr')
self.assertEqual(str(ss.encodeTemplateAttributes({'00': {'01': 'SetupSheet'}})), "{'00': {'01': u'SetupSheet'}}")
self.assertEqual(str(ss.encodeTemplateAttributes({'00': {'01': 'rrr.y - rrr.z'}})), "{'00': {'01': u'${SetupSheet}.y - ${SetupSheet}.z'}}")
self.assertEqual(str(ss.encodeTemplateAttributes({'00': {'01': 'Spreadsheet.y - Spreadsheet.z'}})), "{'00': {'01': u'${SetupSheet}.y - ${SetupSheet}.z'}}")
self.assertEqual(str(ss.decodeTemplateAttributes({'00': {'01': 'SetupSheet'}})), "{'00': {'01': u'SetupSheet'}}")
self.assertEqual(str(ss.decodeTemplateAttributes({'00': {'01': '${SetupSheet}.y - ${SetupSheet}.z'}})), "{'00': {'01': u'rrr.y - rrr.z'}}")
self.assertEqual(str(ss.decodeTemplateAttributes({'00': {'01': '${SetupSheet}.y - ${SetupSheet}.z'}})), "{'00': {'01': u'Spreadsheet.y - Spreadsheet.z'}}")
def test13(self):
'''Verify template attributes encoding/decoding of lists.'''
@@ -118,9 +118,9 @@ class TestPathSetupSheet(PathTestBase):
ss.setup('hugo')
attrs = {}
attrs['00'] = 'x.hugo'
attrs['01'] = [{'10': 'hugo', '11': 'hugo.y'}, {'20': 'hugo'}]
attrs['02'] = [{'a': [{'b': 'hugo'}, {'c': 'hugo'}], 'b': [{'b': 'hugo'}]}]
attrs['00'] = 'x.Spreadsheet'
attrs['01'] = [{'10': 'Spreadsheet', '11': 'Spreadsheet.y'}, {'20': 'Spreadsheet'}]
attrs['02'] = [{'a': [{'b': 'Spreadsheet'}, {'c': 'Spreadsheet'}], 'b': [{'b': 'Spreadsheet'}]}]
encoded = ss.encodeTemplateAttributes(attrs)
self.assertEqual(encoded['00'], 'x.${SetupSheet}')
@@ -150,8 +150,14 @@ class TestPathSetupSheet(PathTestBase):
self.assertEqual(decoded['02'][0]['b'][0]['b'], attrs['02'][0]['b'][0]['b'])
# just to be safe ...
self.obj.SetupSheet.Label = 'xxx'
self.assertEqual(ss.expressionReference(), 'xxx')
dec = ss.decodeTemplateAttributes(encoded)
o2 = self.TestObject(self.doc)
s2 = PathSetupSheet.SetupSheet(o2)
s2.setup()
self.doc.recompute()
s2.setFromTemplate(ss.templateAttributes())
o2.SetupSheet.Label = 'xxx'
self.assertEqual(s2.expressionReference(), 'Spreadsheet001')
dec = s2.decodeTemplateAttributes(encoded)
# pick one
self.assertEqual(dec['01'][0]['11'], 'xxx.y')
self.assertEqual(dec['01'][0]['11'], 'Spreadsheet001.y')