ezt: update bits for python3 (has_key, basestring, number types) (#36515)
This commit is contained in:
parent
d4840892b4
commit
1346c90386
|
@ -228,6 +228,7 @@ import string
|
|||
import re
|
||||
import os
|
||||
import cgi
|
||||
from django.utils import six
|
||||
from django.utils.six import StringIO as cStringIO
|
||||
|
||||
#
|
||||
|
@ -465,7 +466,7 @@ class Template:
|
|||
to the file object 'fp' and functions are called.
|
||||
"""
|
||||
for step in program:
|
||||
if isinstance(step, basestring):
|
||||
if isinstance(step, six.string_types):
|
||||
fp.write(step)
|
||||
else:
|
||||
step[0](step[1], fp, ctx)
|
||||
|
@ -533,7 +534,7 @@ class Template:
|
|||
((left_ref, right_ref), t_section, f_section) = args
|
||||
try:
|
||||
value = _get_value(right_ref, ctx)
|
||||
value = string.lower(str(_get_value(left_ref, ctx))) == string.lower(str(value))
|
||||
value = str(_get_value(left_ref, ctx)).lower() == str(value).lower()
|
||||
except UnknownReference:
|
||||
value = False
|
||||
self._do_if(value, t_section, f_section, fp, ctx)
|
||||
|
@ -615,12 +616,12 @@ def _prepare_ref(refname, for_names, file_args):
|
|||
### the 'argNNN' names
|
||||
if not rest:
|
||||
return start, start, [ ]
|
||||
refname = start + '.' + string.join(rest, '.')
|
||||
refname = start + '.' + '.'.join(rest)
|
||||
|
||||
if for_names:
|
||||
# From last to first part, check if this reference is part of a for loop
|
||||
for i in range(len(parts), 0, -1):
|
||||
name = string.join(parts[:i], '.')
|
||||
name = '.'.join(parts[:i])
|
||||
if name in for_names:
|
||||
return refname, name, parts[i:]
|
||||
|
||||
|
@ -640,10 +641,10 @@ def _get_value(value_ref, ctx):
|
|||
return start
|
||||
|
||||
# get the starting object
|
||||
if ctx.for_index.has_key(start):
|
||||
if start in ctx.for_index:
|
||||
list, idx = ctx.for_index[start]
|
||||
ob = list[idx]
|
||||
elif ctx.defines.has_key(start):
|
||||
elif start in ctx.defines:
|
||||
ob = ctx.defines[start]
|
||||
elif hasattr(ctx.data, start):
|
||||
ob = getattr(ctx.data, start)
|
||||
|
@ -664,7 +665,7 @@ def _get_value(value_ref, ctx):
|
|||
raise UnknownReference(refname)
|
||||
|
||||
# make sure we return a string instead of some various Python types
|
||||
if isinstance(ob, (int, long, float)):
|
||||
if isinstance(ob, six.integer_types + (float,)):
|
||||
return str(ob)
|
||||
if ob is None:
|
||||
return ''
|
||||
|
@ -686,7 +687,7 @@ def _write_value(valrefs, fp, ctx, format=lambda s: s):
|
|||
value = '[' + ' '.join([v[0] for v in valrefs]) + ']'
|
||||
fp.write(format(value))
|
||||
return
|
||||
args = map(lambda valref, ctx=ctx: _get_value_fallback(valref, ctx), valrefs[1:])
|
||||
args = list(map(lambda valref, ctx=ctx: _get_value_fallback(valref, ctx), valrefs[1:]))
|
||||
|
||||
# if the value has a 'read' attribute, then it is a stream: copy it
|
||||
if hasattr(value, 'read'):
|
||||
|
@ -700,10 +701,10 @@ def _write_value(valrefs, fp, ctx, format=lambda s: s):
|
|||
elif callable(value):
|
||||
if getattr(value, 'ezt_call_mode', None) == 'simple':
|
||||
# simple call mode, call with args and write the result
|
||||
fp.write(apply(value, args))
|
||||
fp.write(value(*args))
|
||||
else:
|
||||
# default, call with file pointer and extra args
|
||||
apply(value, [fp] + args)
|
||||
value(* [fp] + args)
|
||||
|
||||
# value is a substitution pattern
|
||||
elif args:
|
||||
|
|
Loading…
Reference in New Issue