Skip to content

Commit

Permalink
Super basic twill processor. Mostly just a CP of the django processor…
Browse files Browse the repository at this point in the history
…. This just outputs the URL and the code, punting on the ability to check for content. That is a hard problem.

A lot of the shared functions between this one and the Django processor should be abstracted to the base, or some utils functionality.
  • Loading branch information
ericholscher committed Aug 28, 2009
1 parent 2c41f7b commit 53f317d
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 1 deletion.
2 changes: 1 addition & 1 deletion test_utils/testmaker/processors/__init__.py
Expand Up @@ -18,7 +18,7 @@


TEST_PROCESSORS = { TEST_PROCESSORS = {
'django': 'test_utils.testmaker.processors.django_processor', 'django': 'test_utils.testmaker.processors.django_processor',
#'twill': 'test_utils.testmaker.processors.twill', 'twill': 'test_utils.testmaker.processors.twill_processor',
} }


_test_processors = {} _test_processors = {}
Expand Down
100 changes: 100 additions & 0 deletions test_utils/testmaker/processors/twill_processor.py
@@ -0,0 +1,100 @@
import time
import re
import base

from django.template import Template, Context
from django.utils.encoding import force_unicode
from django.utils.safestring import mark_safe

TEST_TEMPLATE = """go {{ path }}"""

STATUS_TEMPLATE = """code {{ status_code }}"""

#CONTEXT_TEMPLATE = '''find {{value}}'''
CONTEXT_TEMPLATE = ''

def safe_dict(dict):
new_dic = {}
for key,val in dict.iteritems():
new_dic[key] = mark_safe(val)
return new_dic

class Processor(base.Processer):
"""Processes the serialized data. Generally to create some sort of test cases"""

def __init__(self, name='django'):
super(Processor, self).__init__(name)

def save_request(self, request):
""" Actually write the request out to a file """
if self.shall_we_proceed(request):
self._log_request(request)

def save_response(self, request, response):
if self.shall_we_proceed(request):
self._log_status(response)
'''#TODO make this log sanely.
if response.context and response.status_code != 404:
context = self._get_context(response.context)
self._log_context(context)
#This is where template tag outputting would go
'''

def _log_request(self, request):
method = request.method.lower()
request_str = "'%s', {" % request.path
for dikt in request.REQUEST.dicts:
for arg in dikt:
request_str += "'%s': '%s', " % (arg, request.REQUEST[arg])
request_str += "}"

template = Template(TEST_TEMPLATE)
context = {
'path': request.path,
'time': base.slugify(time.time()),
'method': method,
'request_str': request_str,
}
context = Context(safe_dict(context))
self.log.info(template.render(context))

def _log_status(self, response):
template = Template(STATUS_TEMPLATE)
context = {
'status_code': response.status_code,
}
if response.status_code in [301, 302]:
context['location'] = response['Location']
context = Context(safe_dict(context))
self.log.info(template.render(context))

def _get_context(self, context_list):
#Ugly Hack. Needs to be a better way
if isinstance(context_list, list):
context_list = context_list[-1] #Last context rendered
ret = context_list.dicts[-1]
if ret == {}:
ret = context_list.dicts[0]
try:
return ret.copy()
except Exception, e:
return dict()

else:
return context_list

def _log_context(self, context):
template = Template(CONTEXT_TEMPLATE)
for var in context:
val = force_unicode(context[var])
con = {
'key': var,
'value': val,
}
con = Context(safe_dict(con))
try:
#Avoid memory addy's which will change.
if not re.search("0x\w+", val):
self.log.info(template.render(con))
except UnicodeDecodeError, e:
pass

0 comments on commit 53f317d

Please sign in to comment.