Author: jvloothuis
Date: Sun May 13 15:35:14 2007
New Revision: 43308
Modified:
kukit/kss.commands/trunk/kss/commands/README.txt
kukit/kss.commands/trunk/kss/commands/commands.py
Log:
Modified: kukit/kss.commands/trunk/kss/commands/README.txt
==============================================================================
--- kukit/kss.commands/trunk/kss/commands/README.txt (original)
+++ kukit/kss.commands/trunk/kss/commands/README.txt Sun May 13 15:35:14 2007
@@ -33,7 +33,31 @@
>>> commands.add('someOtherAction', CSS('#otherid'), arg='some arg')
+Note that all data passed will be validated. Passing in invalid data will
+raise a ValueError.
+
+ >>> commands.add('brokenAction', CSS('#id'), arg='<h1>invalid')
+ Traceback (most recent call last):
+ ...
+ ValueError: Argument with name: arg is not valid xml: <h1>invalid
+
+The validation method can also be called directly. It will only raise and
+exeption on data which has tags that are not closed properly. Data does not
+need be valid XML (text nodes may procede any tags).
+
+ >>> commands.validate_parameters(dict(html='valid data'))
+ >>> commands.validate_parameters(dict(html='valid <strong>data</strong>'))
+ >>> commands.validate_parameters(dict(html='invalid <strong>'))
+ Traceback (most recent call last):
+ ...
+ ValueError: Argument with name: html is not valid xml: invalid <strong>
+
Now that we have a few commands we can render them.
>>> commands.render()
'...some value...some arg...'
+
+Command sets
+============
+
+
Modified: kukit/kss.commands/trunk/kss/commands/commands.py
==============================================================================
--- kukit/kss.commands/trunk/kss/commands/commands.py (original)
+++ kukit/kss.commands/trunk/kss/commands/commands.py Sun May 13 15:35:14 2007
@@ -1,4 +1,5 @@
from xml.sax.saxutils import escape, quoteattr
+from xml.parsers.expat import ParserCreate, ExpatError
kss_response_header = '''<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:kukit="http://www.kukit.org/commands/1.0"><body><kukit:commands>'''
@@ -17,8 +18,19 @@
self.commands = []
def add(self, action, selector, **kwargs):
+ self.validate_parameters(kwargs)
self.commands.append((action, selector, kwargs))
+ def validate_parameters(self, parameters):
+ for name, value in parameters.items():
+ parser = ParserCreate()
+ try:
+ parser.Parse('<kss>%s</kss>' % value)
+ except ExpatError:
+ raise ValueError(
+ 'Argument with name: %s is not valid xml: %s' % (
+ name, value))
+
def render(self):
output = [kss_response_header]
for action, selector, options in self.commands:
_______________________________________________
Kukit-checkins mailing list
[email protected]
http://codespeak.net/mailman/listinfo/kukit-checkins