Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Super basic twill processor. Mostly just a CP of the django processor…
…. 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
1 parent
2c41f7b
commit 53f317d
Showing
2 changed files
with
101 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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 |