aboutsummaryrefslogtreecommitdiff
path: root/inputs
diff options
context:
space:
mode:
Diffstat (limited to 'inputs')
-rwxr-xr-xinputs/HTMLInput.py29
-rw-r--r--inputs/OSCInput.py4
-rw-r--r--inputs/PygameInput.py40
-rw-r--r--inputs/RandomLocs.py29
-rw-r--r--inputs/TCPInput.py34
-rw-r--r--inputs/UDPInput.py10
6 files changed, 107 insertions, 39 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 27b82b0..480630c 100644
--- a/inputs/PygameInput.py
+++ b/inputs/PygameInput.py
@@ -6,18 +6,32 @@ from pygame.locals import *
#This class processes input from an already running pygame instance and passes
#it to the parent. This class requires an already running pygame instance.
class PygameInput(Input):
+ """PygameInput is an input tied to the PygameDisplay. Specify:
+ <FollowMouse>True</FollowMouse> to receive an input every frame specifying the current mouse
+ position.
+ <Keyboard>True</Keyboard> to grab keystrokes
+ <Clicks>True</Clicks> to grab clicks.
+
+ NB: If follow mouse is enabled, PygameInput will not return mouse and keypresses. You can, however,
+ instantiate other PygameInputs in the XML that will capture mouse and keypresses."""
def sensingLoop(self):
- #try:
- if self['FollowMouse']:
- self.respond({Strings.LOCATION: pygame.mouse.get_pos()})
- return
- for event in pygame.event.get():
- if event.type is KEYDOWN:
- if event.key == 27:
- self.die()
- self.respond({Strings.LOCATION: (5,5),'Key': event.key})
- if event.type is MOUSEBUTTONDOWN:
+ if self['FollowMouse']:
+ self.respond({Strings.LOCATION: pygame.mouse.get_pos()})
+ return
+ for event in pygame.event.get():
+ if event.type is KEYDOWN:
+ if event.key == 27:
+ self.die()
+ if self['Keyboard']:
+ try:
+ self.respond({'Key': event.key, 'KeyChar': chr(event.key)})
+ except:
+ self.respond({'Key': event.key})
+ return
+ else:
+ pygame.event.post(event)
+ if event.type is MOUSEBUTTONDOWN:
+ if self['Clicks']:
self.respond({Strings.LOCATION: pygame.mouse.get_pos()})
- #except:
- #raise Exception('Pygame not initialized. Pygame must be \
- #initialized.')
+ else:
+ pygame.event.post(event)
diff --git a/inputs/RandomLocs.py b/inputs/RandomLocs.py
index d1ce1c7..f4182cf 100644
--- a/inputs/RandomLocs.py
+++ b/inputs/RandomLocs.py
@@ -1,13 +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):
- 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 5bf06bf..e565649 100644
--- a/inputs/TCPInput.py
+++ b/inputs/TCPInput.py
@@ -3,7 +3,14 @@ import pdb
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
+ json data into python dicts. Warning: contains a bug where init will hang until it receives a
+ connection. Specify:
+ <Port> -- Port number to listen on."""
def inputInit(self):
self.HOST = '' # Symbolic name meaning all available interfaces
self.PORT = self.argDict['Port'] # Arbitrary non-privileged port
@@ -14,15 +21,28 @@ 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)
if not data or 'end' in data: # data end, close socket
main_log.debug('End in data')
+ print 'end of stream'
self.IS_RESPONDING = 0
+ self.conn.close()
self.sock.close()
if self.IS_RESPONDING == 1: # if 'responding', respond to the received data
@@ -30,20 +50,18 @@ class TCPInput(Input):
for datagroup in data.split('\n'):
if datagroup != None and datagroup != '':
dataDict = json.loads(datagroup)
- # socketDict = {'data':dataDict, 'address':self.address}
- socketDict = {Strings.LOCATION: (dataDict['x'], dataDict['y'])} # like PygameInput
- print 'input'
- self.respond(socketDict)
+ #if dataDict['type'] != 1:
+ #print dataDict
+ self.respond(dataDict)
except Exception as exp:
print str(exp)
else:
# if not 'responding', don't respond to data and restart socket
# * an incomplete hack for now. will be changed if same-type-multi-Input is implemented.
- time.sleep(1)
+
+ self.IS_RESPONDING = 1
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
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()
- self.IS_RESPONDING = 1
-
diff --git a/inputs/UDPInput.py b/inputs/UDPInput.py
index 7d5609e..83e2f77 100644
--- a/inputs/UDPInput.py
+++ b/inputs/UDPInput.py
@@ -1,13 +1,17 @@
from operationscore.Input import *
import socket
class UDPInput(Input):
+ """UDPInput is a barebones UDP Input class. It takes any data it receives and adds it to the
+ 'data' element of the response dict. It also notes the 'address'. Specify:
+ <Port> -- the Port to listen on."""
+
def inputInit(self):
HOST = '' # Symbolic name meaning all available interfaces
PORT = self.argDict['Port'] # Arbitrary non-privileged port
self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.sock.bind((HOST, PORT))
def sensingLoop(self):
- (data,address) = self.sock.recvfrom(1024)
- dataDict = {'data':data, 'address':address}
- self.respond(dataDict)
+ (data,address) = self.sock.recvfrom(1024)
+ dataDict = {'data':data, 'address':address}
+ self.respond(dataDict)