[KSS-checkins] r50246 - kukit/kss.base/branches/protocol-data-types/kss/base

jvloothuis at codespeak.net jvloothuis at codespeak.net
Wed Jan 2 00:25:30 CET 2008


Author: jvloothuis
Date: Wed Jan  2 00:25:28 2008
New Revision: 50246

Modified:
   kukit/kss.base/branches/protocol-data-types/kss/base/commands.py
Log:

Ported support for global commands

Changed the parameter types so that they have a specific `node`
method. This is method used with a try except block in place of an
instance type check (which makes the code more duck-typing aware).


Modified: kukit/kss.base/branches/protocol-data-types/kss/base/commands.py
==============================================================================
--- kukit/kss.base/branches/protocol-data-types/kss/base/commands.py	(original)
+++ kukit/kss.base/branches/protocol-data-types/kss/base/commands.py	Wed Jan  2 00:25:28 2008
@@ -8,9 +8,10 @@
 
 kss_response_footer = '</commands></kukit>'
 
-
 kss_command_start = '<command selector=%(selector)s name=%(action)s selectorType=%(selector_type)s>'
 
+kss_command_start_global = '<command name=%(action)s>'
+
 kss_command_end = '</command>'
 
 kss_param = '<param name=%(name)s>%(value)s</param>'
@@ -19,7 +20,8 @@
     def __init__(self, value):
         self.value = value
 
-    def __str__(self):
+    def node(self):
+        '''Return the XML node representation of this object'''
         return '<![CDATA[%s]]>' % self.value.replace(']]>', ']]&gt;')
 
     def __repr__(self):
@@ -49,26 +51,32 @@
     def render(self):
         output = [kss_response_header]
         for action, selector, options in self.commands:
-            try:
-                selector_type = selector.type
-                selector = selector.value
-            except AttributeError:
-                # It is probably a string or unicode object so we can
-                # let the client decide the default selector
-                selector_type = ''
-
-            output.append(kss_command_start % dict(
-                    selector=quoteattr(selector),
-                    selector_type=quoteattr(selector_type),
-                    action=quoteattr(action)))
+            if selector is None:
+                output.append(kss_command_start_global % dict(
+                        action=quoteattr(action)))
+            else:
+                try:
+                    selector_type = selector.type
+                    selector = selector.value
+                except AttributeError:
+                    # It is probably a string or unicode object so we
+                    # can let the client decide the default selector
+                    selector_type = ''
+
+                output.append(kss_command_start % dict(
+                        selector=quoteattr(selector),
+                        selector_type=quoteattr(selector_type),
+                        action=quoteattr(action)))
                 
             for name, value in options.items():
-                if isinstance(value, basestring):
-                    value = escape(value)
-                else:
-                    value = str(value)
+                try:
+                    node = value.node()
+                except AttributeError:
+                    # If it the value does not explicitly convert to a
+                    # node make it a text node
+                    node = escape(value)
                 output.append(kss_param % dict(
-                    name=quoteattr(name), value=value))
+                    name=quoteattr(name), value=node))
             output.append(kss_command_end)
         output.append(kss_response_footer)
         return ''.join(output)


More information about the Kukit-checkins mailing list