diff options
Diffstat (limited to 'inputs')
-rwxr-xr-x | inputs/HTMLInput.py | 29 | ||||
-rw-r--r-- | inputs/OSCInput.py | 4 | ||||
-rw-r--r-- | inputs/PygameInput.py | 5 | ||||
-rw-r--r-- | inputs/RandomLocs.py | 32 | ||||
-rw-r--r-- | inputs/TCPInput.py | 14 |
5 files changed, 64 insertions, 20 deletions
diff --git a/inputs/HTMLInput.py b/inputs/HTMLInput.py new file mode 100755 index 0000000..9697a32 --- /dev/null +++ b/inputs/HTMLInput.py @@ -0,0 +1,29 @@ +from operationscore.Input import * +import urllib, re + +""" +HTML Input, which takes 2 arguments: +- 'Src': a URL to a web page, and +- 'Regex': a Regex to parse data out of the web page. +The input parses the source code of the web page according to the regex, and processes the parsed regex groups. +""" +class HTMLInput(Input): + def inputInit(self): + self.src = self.argDict['Src'] + self.regex = self.argDict['Regex'] + + def getHTML(self): + self.sock = urllib.urlopen(self.src); + self.html = self.sock.read() + self.sock.close() + + def sensingLoop(self): + self.getHTML() + self.dataList = [] + + pattern = re.compile(self.regex) + matchObj = pattern.search(self.html) + self.dataList = matchObj.groups() + + self.respond(self.dataList) + diff --git a/inputs/OSCInput.py b/inputs/OSCInput.py index f867fb5..ba1e035 100644 --- a/inputs/OSCInput.py +++ b/inputs/OSCInput.py @@ -7,8 +7,8 @@ class OSCInput(Input): def inputInit(self): HOST = '' # Symbolic name meaning all available interfaces PORT = self['Port'] # Arbitrary non-privileged port - self.server = liblo.Server(PORT) - self.server.add_method(None,None, self.fallback) + self.server = liblo.Server(PORT) + self.server.add_method(None,None, self.fallback) # except liblo.ServerError, err: # main_log.error(str(err)) diff --git a/inputs/PygameInput.py b/inputs/PygameInput.py index 399a77e..414adf3 100644 --- a/inputs/PygameInput.py +++ b/inputs/PygameInput.py @@ -23,7 +23,10 @@ class PygameInput(Input): if event.key == 27: self.die() if self['Keyboard']: - self.respond({'Key': event.key}) + try: + self.respond({'Key': event.key, 'KeyChar': chr(event.key)}) + except: + self.respond({'Key': event.key}) return else: pygame.event.post(event) diff --git a/inputs/RandomLocs.py b/inputs/RandomLocs.py index 2719981..f4182cf 100644 --- a/inputs/RandomLocs.py +++ b/inputs/RandomLocs.py @@ -1,16 +1,16 @@ -import util.TimeOps as clock -import random -import util.Geo as Geo -import util.Strings as Strings -from operationscore.Input import * -class RandomLocs(Input): - """RandomLocs is an Input that generates RandomLocations at a preset time interval. Just a - prototype, some assembly required.""" - - def inputInit(self): - self['LastEvent'] = clock.time() - def sensingLoop(self): #TODO: move to params - currentTime = clock.time() - if currentTime - self['LastEvent'] > 2000: - self.respond({Strings.LOCATION: Geo.randomLoc((50,50))}) - self['LastEvent'] = currentTime +import util.TimeOps as clock
+import random
+import util.Geo as Geo
+import util.Strings as Strings
+from operationscore.Input import *
+class RandomLocs(Input):
+ """RandomLocs is an Input that generates RandomLocations at a preset but randomly changing time interval. Just a
+ prototype, some assembly required."""
+
+ def inputInit(self):
+ self['LastEvent'] = clock.time()
+ def sensingLoop(self): #TODO: move to params
+ currentTime = clock.time()
+ if currentTime - self['LastEvent'] > 200+500*random.random():
+ self.respond({Strings.LOCATION: Geo.randomLoc((200,200))})
+ self['LastEvent'] = currentTime
diff --git a/inputs/TCPInput.py b/inputs/TCPInput.py index 2d8ab4d..e565649 100644 --- a/inputs/TCPInput.py +++ b/inputs/TCPInput.py @@ -4,6 +4,7 @@ from operationscore.Input import * import socket, json, time import logging as main_log import string +from select import select class TCPInput(Input): """TCPInput is a input to receive input on a TCP port. In its current incarnation, it parses @@ -20,9 +21,20 @@ class TCPInput(Input): self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) self.sock.bind((self.HOST, self.PORT)) self.sock.listen(1) - (self.conn, self.address) = self.sock.accept() + + isreadable=select([self.sock],[],[], 0)[0] + self.conn = None + if isreadable: + (self.conn, self.address) = self.sock.accept() def sensingLoop(self): + if self.conn == None: + isreadable=select([self.sock],[],[], 0)[0] + if isreadable: + (self.conn, self.address) = self.sock.accept() + else: + return + data = self.conn.recv(self.BUFFER_SIZE) main_log.debug('Incoming data', data) |