class action:
def __init__(self, Target):
self.Target = Target
def __call__(self,url,**namespace):
import string
if not hasattr(string,"Template"): # Python 2.4 or above
raise SyntaxError,"Unable to handle this syntax for " + \
"string substitution. Python version must be 2.4 or above"
# apply translation and Python string substitution
abs_url = urlparse.urljoin(self.script_url,url)
target = self.Target(self.Target.handler,abs_url)
templateDef = open(target.name).read()
include_pattern = r"""
\@(?:
(?P<escaped>\@) | # Escape sequence of two delimiters
\[(?P<id>\$[a-z_][[a-z0-9_]*)\] | # delimiter and a bracketed identifier
\[(?P<url>.*?[^\\])\] | # delimiter and a bracketed url
(?P<invalid>) # Other ill-formed delimiter exprs
)
"""
def include_replace(mo):
if mo.group('escaped') is not None:
return mo.group(0)
url = ''
if mo.group('id') is not None:
id = str(mo.group('id'))
url = string.Template(id).safe_substitute(namespace)
# if id has no value, don't attempt to open the url.
if not url or url==id:
return ''
if mo.group('url') is not None:
url = str(mo.group('url'))
incl_file = urlparse.urljoin(abs_url, url)
if url:
target = Target(self.handler,incl_file)
inclusion = open(target.name).read()
return inclusion
if mo.group('invalid') is not None:
return mo.group(0)
include_re = re.compile(include_pattern, re.IGNORECASE | re.VERBOSE)
templateDef = include_re.sub(include_replace, templateDef)
templateDef = string.Template(templateDef).safe_substitute(namespace)
translate_pattern = r"""
\#(?:
(?P<escaped>\#) | # Escape sequence of two delimiters
\[(?P<string>.*?[^\\])\] | # delimiter and a bracketed string to translate
(?P<invalid>) # Other ill-formed delimiter exprs
)
"""
def trans_replace(mo):
if mo.group('escaped') is not None:
return mo.group(0)
if mo.group('string') is not None:
val = str(mo.group('string')).replace('\]', ']')
return self.translation(val)
if mo.group('invalid') is not None:
return mo.group(0)
trans_re = re.compile(translate_pattern, re.IGNORECASE | re.VERBOSE)
data = trans_re.sub(trans_replace, templateDef)
return data
|