I'm using the troposhere library and I'm trying to combine two string objects which have Join:
from troposphere import Join
str1 = Join('', ["""
sed -i -e '/hostname/s/=.*/=example.com/' /tmp/file.app
\n"""])
and
str2 = Join('', ["""
sed -i -e '/IP/s/=.*/=192.168.100.100/' /tmp/file.app
\n"""])
I've tried to combine them using:
str3 = str1 + str2
and
str1 += str2
But unfortunately I'm getting the following error:
TypeError: unsupported operand type(s) for +: 'Join' and 'Join'
You could just apply string concatenation before creating Joins :
from troposphere import Join
str1 = """
sed -i -e '/hostname/s/=.*/=example.com/' /tmp/file.app
\n"""
str2 = """
sed -i -e '/IP/s/=.*/=192.168.100.100/' /tmp/file.app
\n"""
str3 = str1.strip()+str2
join1, join2, join3 = [Join('', [cmd]) for cmd in (str1, str2, str3)]
print join3.data
# {'Fn::Join': ['', ["sed -i -e '/hostname/s/=.*/=example.com/' /tmp/file.app\nsed -i -e '/IP/s/=.*/=192.168.100.100/' /tmp/file.app\n\n"]]}
Here's the definition of Join
class :
class Join(AWSHelperFn):
def __init__(self, delimiter, values):
validate_delimiter(delimiter)
self.data = {'Fn::Join': [delimiter, values]}
To define join_a + join_b
, you could use :
from troposphere import Join
def add_joins(join_a, join_b):
delimiter = join_a.data['Fn::Join'][0]
str_a = join_a.data['Fn::Join'][1][0]
str_b = join_b.data['Fn::Join'][1][0]
return Join(delimiter, [str_a.strip() + str_b])
Join.__add__ = add_joins
str1 = """
sed -i -e '/hostname/s/=.*/=example.com/' /tmp/file.app
\n"""
str2 = """
sed -i -e '/IP/s/=.*/=192.168.100.100/' /tmp/file.app
\n"""
join1 = Join('', [str1])
join2 = Join('', [str2])
print (join1 + join2).data
# {'Fn::Join': ['', ["sed -i -e '/hostname/s/=.*/=example.com/' /tmp/file.app\nsed -i -e '/IP/s/=.*/=192.168.100.100/' /tmp/file.app\n\n"]]}