Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def content(self):
content = self._content
if content is not None:
if isinstance(content, types.escape):
return types.escape((types.value(self.content_symbol),))
return types.parts((types.value(self.content_symbol),))
def __init__(self, parts, variable=None):
if not isinstance(parts, types.parts):
parts = types.parts((parts,))
self.parts = parts
self.variable = variable
stream.symbol_mapping.update(value.symbol_mapping)
if isinstance(value, types.template):
value = types.value(value % symbols)
if isinstance(value, types.value):
stream.write("%s = %s" % (variable, value))
elif isinstance(value, types.join):
parts = []
_v_count = 0
for part in value:
if isinstance(part, types.expression):
stream.symbol_mapping.update(part.symbol_mapping)
if isinstance(part, types.template):
part = types.value(part % symbols)
if isinstance(part, (types.parts, types.join)):
_v = stream.save()
assign = Assign(part, _v)
assign.begin(stream)
assign.end(stream)
_v_count +=1
parts.append(_v)
elif isinstance(part, types.value):
parts.append(part)
elif isinstance(part, unicode):
if stream.encoding:
parts.append(repr(part.encode(stream.encoding)))
else:
parts.append(repr(part))
elif isinstance(part, str):
parts.append(repr(part))
else:
value.label = expr
parts.append(value)
translator = self
i = j + 1
value = translator.translate("", escape)
if value is not None:
value.label = ""
parts.append(value)
if len(parts) == 1:
return parts[0]
return types.parts(parts)
... def translate(self, string, escape=None):
... if string: return types.value(string)
>>> tales = MockExpressionTranslator().tales
>>> tales('a')
value('a')
>>> tales('a|b')
parts(value('a'), value('b'))
"""
string = string.replace('\n', '').strip()
if not string:
return types.parts()
parts = []
# default translator is ``self``
translator = self
i = j = 0
while i < len(string):
match = self.re_pragma.match(string[i:])
if match is not None:
pragma = match.group('pragma')
new_translator = self.pragma(pragma)
if new_translator is not None:
translator = new_translator
i += match.end()
if translator.recursive:
match = self.re_pragma.match(string[i:])
if match is not None:
pragma = match.group('pragma')
new_translator = self.pragma(pragma)
if new_translator is not None:
translator = new_translator
i += match.end()
if translator.recursive:
value = translator.tales(string[i:])
if parts:
if isinstance(value, types.parts):
parts.extend(value)
else:
parts.append(value)
return types.parts(parts)
return value
continue
j = string.find('|', j + 1)
if j == -1:
j = len(string)
expr = string[i:j]
try:
value = translator.translate(expr, escape)
except SyntaxError:
if j < len(string):
continue
raise
# default translator is ``self``
translator = self
i = j = 0
while i < len(string):
match = self.re_pragma.match(string[i:])
if match is not None:
pragma = match.group('pragma')
new_translator = self.pragma(pragma)
if new_translator is not None:
translator = new_translator
i += match.end()
if translator.recursive:
value = translator.tales(string[i:])
if parts:
if isinstance(value, types.parts):
parts.extend(value)
else:
parts.append(value)
return types.parts(parts)
return value
continue
j = string.find('|', j + 1)
if j == -1:
j = len(string)
expr = string[i:j]
try:
value = translator.translate(expr, escape)