[core] String formatting of parameters with/without quotes to deal with spaces in strings

We have the problem of spaces in file paths, choices (like colorspace),
etc.
An empty list is not send to the command line.
An empty string is send to the command line as "".
Add new unit test to ensure it follows the expected rules.
This commit is contained in:
demoulinv 2023-11-10 09:57:25 +01:00 committed by Fabien Castan
parent 1141d44bce
commit a7fc167512
3 changed files with 98 additions and 7 deletions

View file

@ -305,15 +305,31 @@ class Attribute(BaseObject):
return self._value
def getEvalValue(self):
'''
Return the value. If it is a string, expressions will be evaluated.
'''
if isinstance(self.value, str):
return Template(self.value).safe_substitute(os.environ)
return self.value
def getValueStr(self):
'''
Return the value formatted as a string with quotes to deal with spaces.
If it is a string, expressions will be evaluated.
If it is an empty string, it will returns 2 quotes.
If it is an empty list, it will returns a really empty string.
If it is a list with one empty string element, it will returns 2 quotes.
'''
# ChoiceParam with multiple values should be combined
if isinstance(self.attributeDesc, desc.ChoiceParam) and not self.attributeDesc.exclusive:
# ensure value is a list as expected
assert(isinstance(self.value, Sequence) and not isinstance(self.value, str))
return self.attributeDesc.joinChar.join(self.getEvalValue())
if isinstance(self.attributeDesc, (desc.StringParam, desc.File)):
v = self.attributeDesc.joinChar.join(self.getEvalValue())
if v:
return '"{}"'.format(v)
return v
# String, File, single value Choice are based on strings and should includes quotes to deal with spaces
if isinstance(self.attributeDesc, (desc.StringParam, desc.File, desc.ChoiceParam)):
return '"{}"'.format(self.getEvalValue())
return str(self.getEvalValue())
@ -498,9 +514,8 @@ class ListAttribute(Attribute):
return [attr.getPrimitiveValue(exportDefault=exportDefault) for attr in self._value if not attr.isDefault]
def getValueStr(self):
if isinstance(self.value, ListModel):
return self.attributeDesc.joinChar.join([v.getValueStr() for v in self.value])
return super(ListAttribute, self).getValueStr()
assert(isinstance(self.value, ListModel))
return self.attributeDesc.joinChar.join([v.getValueStr() for v in self.value])
def updateInternals(self):
super(ListAttribute, self).updateInternals()