/usr/lib/python2.7/dist-packages/txosc/dispatch.py is in python-txosc 0.2.0-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 | #!/usr/bin/env python
# -*- test-case-name: txosc.test.test_dispatch -*-
# Copyright (c) 2009 Alexandre Quessy, Arjan Scherpenisse
# See LICENSE for details.
"""
OSC message address dispatching to callbacks
"""
import string
import math
import struct
import re
from txosc.osc import *
class AddressNode(object):
"""
A node in the tree of OSC addresses.
This node can be either a container branch or a leaf. An OSC address is a series of names separated by forward slash characters. ('/') We say that a node is a branch when it has one or more child nodes.
This class is provided so that the programmer can separate the handling of an address sub-tree in the OSC addresses. For example, an AddressNode can be added to a receiver in order to handle all the messages starting with "/egg/spam/". AddressNode classes can be nested.
@ivar _name: the name of this node.
@ivar _parent: the parent node.
"""
def __init__(self, name=None, parent=None):
"""
@type name: C{str}
@param parent: L{Receiver} or L{AddressNode}
"""
self._name = name
self._parent = parent
self._childNodes = {}
self._callbacks = set()
self._parent = None
self._wildcardNodes = set()
def removeCallbacks(self):
"""
Remove all callbacks from this node.
"""
self._callbacks = set()
self._checkRemove()
def setName(self, newname):
"""
Give this node a new name.
@type newname: C{str}
"""
if self._parent:
del self._parent._childNodes[self._name]
self._name = newname
if self._parent:
self._parent._childNodes[self._name] = self
def setParent(self, newparent):
"""
Reparent this node to another parent.
@param newparent: L{Receiver} or L{AddressNode}
"""
if self._parent:
del self._parent._childNodes[self._name]
self._parent._checkRemove()
self._parent = newparent
self._parent._childNodes[self._name] = self
# def getParent(self):
# """
# Returns the parent node or None.
# """
# return self._parent
#
# def getChildren(self):
# """
# Returns a set of children nodes.
# """
# return set(self._childNodes)
def _checkRemove(self):
if not self._parent:
return
if not self._callbacks and not self._childNodes:
del self._parent._childNodes[self._name]
self._parent._checkRemove()
def addNode(self, name, instance):
"""
Add a child node.
@type name: C{str}
@type instance: L{AddressNode}
"""
#FIXME: We should document the name.
# Is it /foo or foo?
# Does it redirect all messages prefixed with "/foo" to the child?
instance.setName(name)
instance.setParent(self)
def getName(self):
"""
Returns the name of this address node.
"""
return self._name
def match(self, pattern):
"""
Match a pattern to return a set of nodes.
@param pattern: A C{str} with an address pattern.
@return a C{set()} of matched AddressNode instances.
"""
path = self._patternPath(pattern)
if not len(path):
return set([self])
matchedNodes = set()
part = path[0]
if AddressNode.isWildcard(part):
for c in self._childNodes:
if AddressNode.matchesWildcard(c, part):
matchedNodes.add( self._childNodes[c] )
# FIXME - what if both the part and some of my childs have wildcards?
elif self._wildcardNodes:
matches = set()
for c in self._wildcardNodes:
if AddressNode.matchesWildcard(part, c):
matchedNodes.add( self._childNodes[c] )
break
if part in self._childNodes:
matchedNodes.add( self._childNodes[part] )
if not matchedNodes:
return matchedNodes
return reduce(lambda a, b: a.union(b), [n.match(path[1:]) for n in matchedNodes])
def addCallback(self, pattern, cb):
"""
Adds a callback for L{txosc.osc.Message} instances received for a given OSC path, relative to this node's address as its root.
In the OSC protocol, only leaf nodes can have callbacks, though this implementation allows also branch nodes to have callbacks.
@param path: OSC address in the form C{/egg/spam/ham}, or list C{['egg', 'spam', 'ham']}.
@type pattern: C{str} or C{list}.
@param cb: Callback that will receive L{Message} as an argument when received.
@type cb: Function or method.
@return: None
"""
path = self._patternPath(pattern)
if not len(path):
self._callbacks.add(cb)
else:
part = path[0]
if part not in self._childNodes:
if not AddressNode.isValidAddressPart(part):
raise ValueError("Invalid address part: '%s'" % part)
self.addNode(part, AddressNode())
if AddressNode.isWildcard(part):
self._wildcardNodes.add(part)
self._childNodes[part].addCallback(path[1:], cb)
def removeCallback(self, pattern, cb):
"""
Removes a callback for L{Message} instances received for a given OSC path.
@param path: OSC address in the form C{/egg/spam/ham}, or list C{['egg', 'spam', 'ham']}.
@type pattern: C{str} or C{list}.
@param cb: Callback that will receive L{txosc.osc.Message} as an argument when received.
@type cb: A callable object.
"""
path = self._patternPath(pattern)
if not len(path):
self._callbacks.remove(cb)
else:
part = path[0]
if part not in self._childNodes:
raise KeyError("No such address part: " + part)
self._childNodes[part].removeCallback(path[1:], cb)
if not self._childNodes[part]._callbacks and not self._childNodes[part]._childNodes:
# remove child
if part in self._wildcardNodes:
self._wildcardNodes.remove(part)
del self._childNodes[part]
@staticmethod
def isWildcard(name):
"""
Given a name, returns whether it contains wildcard characters.
"""
wildcardChars = set("*?[]{}")
return len(set(name).intersection(wildcardChars)) > 0
@staticmethod
def isValidAddressPart(part):
"""
Check whether the address part can be used as an L{AddressNode} name.
@rtype bool
"""
invalidChars = set(" #,/")
return len(set(part).intersection(invalidChars)) == 0
@staticmethod
def matchesWildcard(value, wildcard):
"""
Match a value to a wildcard.
"""
if value == wildcard and not AddressNode.isWildcard(wildcard):
return True
if wildcard == "*":
return True
wildcard = wildcard.replace("*", ".*")
wildcard = wildcard.replace("?", ".?")
wildcard = wildcard.replace("[!", "[^")
wildcard = wildcard.replace("(", "\(")
wildcard = wildcard.replace(")", "\)")
wildcard = wildcard.replace("|", "\|")
wildcard = wildcard.replace("{", "(")
wildcard = wildcard.replace("}", ")")
wildcard = wildcard.replace(",", "|")
wildcard = "^" + wildcard + "$"
try:
r = re.compile(wildcard)
return re.match(wildcard, value) is not None
except:
raise OscError("Invalid character in wildcard.")
def _patternPath(self, pattern):
"""
Given a OSC address path like /foo/bar, return a list of
['foo', 'bar']. Note that an OSC address always starts with a
slash. If a list is input, it is output directly.
@param pattern: A L{str} OSC address.
@return: A L{list} of L{str}. Each part of an OSC path.
"""
if type(pattern) == list:
return pattern
return pattern.split("/")[1:]
def removeCallbacksByPattern(self, pattern):
"""
Remove all callbacks with the given pattern.
@param pattern: The pattern to match the callbacks. When
ommited, removes all callbacks.
"""
raise NotImplementedError("Implement removeCallbacks")
def removeAllCallbacks(self):
"""
Remove all callbacks from this node.
"""
self._childNodes = {}
self._wildcardNodes = set()
self._callbacks = set()
self._checkRemove()
def matchCallbacks(self, message):
"""
Get all callbacks for a given message
"""
pattern = message.address
return self.getCallbacks(pattern)
def getCallbacks(self, pattern):
"""
Retrieve all callbacks which are bound to given
pattern. Returns a set() of callables.
@return: L{set} of callbables.
"""
path = self._patternPath(pattern)
nodes = self.match(path)
if not nodes:
return nodes
return reduce(lambda a, b: a.union(b), [n._callbacks for n in nodes])
class Receiver(AddressNode):
"""
Receive OSC elements (L{Bundle}s and L{Message}s) from the server
protocol and handles the matching and dispatching of these to the
registered callbacks.
Callbacks are stored in a tree-like structure, using L{AddressNode} objects.
"""
def dispatch(self, element, client):
"""
Dispatch an element to all matching callbacks.
Executes every callback matching the message address with
element as argument. The order in which the callbacks are
called is undefined.
@param element: A L{Message} or L{Bundle}.
@param client: Either a (host, port) tuple with the originator's address, or an instance of L{StreamBasedFactory} whose C{send()} method can be used to send a message back.
"""
if isinstance(element, Bundle):
messages = element.getMessages()
else:
messages = [element]
for m in messages:
matched = False
for c in self.getCallbacks(m.address):
c(m, client)
matched = True
if not matched:
self.fallback(m, client)
#TODO: add a addFallback or setFallback method
def fallback(self, message, client):
"""
The default fallback handler.
"""
from twisted.python import log
log.msg("Unhandled message from %s): %s" % (repr(client), str(message)))
def setFallback(self, fallback):
"""
Sets the fallback.
@param fallback: callable function or method.
"""
self.fallback = fallback
|