api: Fix python3 support, clean input buffer on call

master
informatic 2018-06-16 08:25:42 +02:00
parent a2e403b1b6
commit f8f022f909
1 changed files with 48 additions and 18 deletions

View File

@ -1,10 +1,17 @@
import socket
import re
import logging
from pyparsing import nestedExpr, originalTextFor
tclparser = nestedExpr('{', '}')
scene_head_re = re.compile(r'Scene (\d+) active ([01]) WxH (\d+)x(\d+) at (\d+),(\d+) name (.*)')
scene_back_re = re.compile(r'- back : (\w+) (\d+) WxH (\d+)x(\d+) at ([\d.]+),([\d.]+) shape (\d+) place (\d+)')
scene_frame_re = re.compile(r'- frame (\d+) active (\d+) : (\d+)x(\d+) at (\d+),(\d+) source ([\w-]+),([\w-]+) id ([\d-]+),([\d-]+) shape (\d+),(\d+) place (\d+),(\d+)')
image_list_re = re.compile('^image load (\d+) <(.*)> (\d+)x(\d+) bit depth (\d+) type (.*) seqno (\d+)$')
feed_list_re = re.compile(r'^feed (\d+) : (.*) (.*) (.*) (\d+)x(\d+) (\d+),(\d+) (\d+)x(\d+) (\d+),(\d+) (\d+):(\d+) (\d+) (\d+) (\d+) <(.*)>$')
def parse_tcl(data):
return tclparser.parseString('{%s}' % (data,))[0].asList()
@ -16,10 +23,21 @@ class SnowmixClient(object):
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.settimeout(1.0)
self.sock.connect((host, port))
self.fd = self.sock.makefile('r')
self.fd = self.sock.makefile('rw')
self.version = self.fd.readline().split(' ')[2]
def flush_input(self):
self.sock.setblocking(0)
try:
self.fd.read()
except:
pass
self.sock.setblocking(1)
self.sock.settimeout(1.0)
def call(self, command, expect='MSG:'):
self.flush_input()
self.fd.write(command + '\r\n')
self.fd.flush()
while True:
@ -39,26 +57,38 @@ class SnowmixClient(object):
def scene_info(self, scene_id):
lines = list(self.call('tcl eval SceneList %d' % scene_id))
s = {'frames': []}
s = {'frames': {}}
_, s['active'], s['width'], s['height'], s['x'], s['y'], s['name'] = re.match(
r'Scene (\d+) active ([01]) WxH (\d+)x(\d+) at '
'(\d+),(\d+) name (.*)', lines[0]).groups()
bg_type, bg_id, _, _, _, _ ,_ ,_ = re.match(
r'- back : (\w+) (\d+) WxH (\d+)x(\d+) at '
'([\d.]+),([\d.]+) shape (\d+) place (\d+)', lines[1]).groups()
_, s['active'], s['width'], s['height'], s['x'], s['y'], s['name'] = \
scene_head_re.match(lines[0]).groups()
bg_type, bg_id, _, _, _, _ ,_ ,_ = \
scene_back_re.match(lines[1]).groups()
for l in lines[2:]:
pos = {}
front = {}
back = {}
frame = {'position': pos, 'back': back, 'front': front}
frame['id'], frame['active'], pos['width'], pos['height'], \
pos['x'], pos['y'], front['type'], back['type'], \
front['id'], back['id'], _, _, _, _ = re.match(
r'- frame (\d+) active (\d+) : (\d+)x(\d+) at (\d+),(\d+) '
'source ([\w-]+),([\w-]+) id ([\d-]+),([\d-]+) shape (\d+),(\d+) '
'place (\d+),(\d+)', l).groups()
s['frames'].append(frame)
front['id'], back['id'], _, _, _, _ = \
scene_frame_re.match(l).groups()
s['frames'][frame['id']] = frame
alpha = c.tcl('SceneAlpha %d' % scene_id)
s['alpha'] = alpha[0][0]
s['background_alpha'] = alpha[0][1]
s['text_alpha'] = alpha[0][2]
print(alpha)
for k, front_alpha, back_alpha in alpha[1:]:
s['frames'][k]['front']['alpha'] = front_alpha
s['frames'][k]['back']['alpha'] = back_alpha
alphalink = c.tcl('SceneAlphaLink %d' % scene_id)
s['alpha_background_link'] = alphalink[0][0]
s['alpha_text_link'] = alphalink[0][1]
for k, link in alphalink[1:]:
s['frames'][k]['alpha_link'] = link
return s
@ -68,7 +98,7 @@ class SnowmixClient(object):
img = {}
img['id'], img['source'], img['width'], img['height'], \
img['bit'], img['type'], img['seqno'] = \
re.match('^image load (\d+) <(.*)> (\d+)x(\d+) bit depth (\d+) type (.*) seqno (\d+)$', l).groups()
image_list_re.match(l).groups()
images.append(img)
return images
@ -80,16 +110,16 @@ class SnowmixClient(object):
f['id'], f['state'], f['live'], f['oneshot'], f['width'], f['height'], \
f['cutstartx'], f['cutstarty'], f['cutw'], f['cuth'], \
f['offsetx'], f['offsety'], f['fifo1'], f['fifo2'], \
f['good'], f['missed'], f['dropped'], f['name'] = re.match(
r'^feed (\d+) : (.*) (.*) (.*) (\d+)x(\d+) (\d+),(\d+) (\d+)x(\d+) (\d+),(\d+) (\d+):(\d+) (\d+) (\d+) (\d+) <(.*)>$', l).groups()
f['good'], f['missed'], f['dropped'], f['name'] = \
feed_list_re.match(l).groups()
feeds.append(f)
return feeds
if __name__ == "__main__":
c = SnowmixClient('10.8.0.95')
c = SnowmixClient('127.0.0.1')
for l in c.call('feed info', 'STAT:'):
print('Result: %s' % (l,))
print c.tcl('SceneAlpha 1')
print(c.tcl('SceneAlpha 1'))