def test_group(self): p = Params() self.assertEqual(len(p), 0) p2 = Params() p.group('P2', p2) self.assertEqual(len(p), 1) self.assertEqual(p.get('P2'), p2)
def test_propagates_params_changes(self): p = Params() self.assertEqual(len(p), 0) p2 = Params() p.group('P2', p2) self.assertEqual(p.changeEvent._fireCount, 1) p2.int('foo') self.assertEqual(p.changeEvent._fireCount, 2)
def test_set_values(self): pars = Params() pars.int('count').set(1) pars.float('price').set(9.99) subpars = Params() subpars.bool('flag').set(True) pars.group('subgroup', subpars) set_values(pars, {'count': 5, 'price': 0.5, 'subgroup': {'flag': False}}) self.assertEqual(pars.get('count').val(), 5) self.assertEqual(pars.get('price').val(), 0.5) self.assertEqual(pars.get('subgroup').get('flag').val(), False)
def test_broadcast_incoming_value_changes(self): # params pars = Params() pars.string('name') pars.int('age') # server s = Server(pars) # remote r1 r1 = Remote() s.connect(r1) # remote r2 r2_value_log = [] def onval(path, val): print('--- on val {}'.format(path)) r2_value_log.append((path, val)) r2 = Remote() r2.outgoing.sendValueEvent += onval s.connect(r2) self.assertEqual(r2_value_log, []) # remote r1 send value change to server r1.incoming.valueEvent('/age', 41) # verify the change was broadcasted to r2 self.assertEqual(r2_value_log, [('/age', 41)])
def test_propagates_param_changes(self): p = Params() self.assertEqual(p.changeEvent._fireCount, 0) name = p.string('name') self.assertEqual(p.changeEvent._fireCount, 1) name.set('John') self.assertEqual(p.changeEvent._fireCount, 2)
def test_string(self): params = Params() param = params.string('name') self.assertEqual(param.type, 's') self.assertTrue(isinstance(param, Param)) param.set(4) self.assertEqual(param.val(), '4')
def test_schema_list_with_restrictions(self): pars = Params() pars.int('count', min=3, max=10).set(1) pars.float('price', min=0.0, max=1.0).set(9.99) self.assertEqual(schema_list(pars), [ {'path': '/count', 'type':'i', 'value':3, 'opts': {'min':3, 'max':10}}, {'path': '/price', 'type':'f', 'value':1.0, 'opts': {'min':0.0, 'max':1.0}}, ])
def test_float(self): params = Params() param = params.float('value') self.assertEqual(param.type, 'f') self.assertTrue(isinstance(param, Param)) param.set('4.81') self.assertEqual(param.val(), 4.81) param.set('zzz') self.assertEqual(param.val(), 4.81)
def test_disconnect_with_invalid_remote(self): # params pars = Params() pars.string('name').set('Abe') # server s = Server(pars) r = Remote() s.disconnect(r) s.disconnect(None)
def test_int(self): params = Params() param = params.int('age') self.assertEqual(param.type, 'i') self.assertTrue(isinstance(param, Param)) param.set('4') self.assertEqual(param.val(), 4) param.set('zzz') self.assertEqual(param.val(), 4)
def test_schema_list_with_values(self): pars = Params() pars.string('name').set('Moby Dick') pars.int('count').set(100) pars.float('price').set(9.99) pars.bool('soldout').set(False) details = Params() details.int('page_count').set(345) details.string('author').set('Herman Melville') pars.group('details', details) self.assertEqual(schema_list(pars), [ {'path': '/name', 'type':'s', 'value':'Moby Dick'}, {'path': '/count', 'type':'i', 'value':100}, {'path': '/price', 'type':'f', 'value':9.99}, {'path': '/soldout', 'type':'b', 'value':False}, {'path': '/details/page_count', 'type':'i','value':345}, {'path': '/details/author', 'type':'s','value':'Herman Melville'} ])
def test_schema_list(self): pars = Params() pars.string('name') pars.int('count') pars.float('price') pars.bool('soldout') details = Params() details.int('page_count') details.string('author') pars.group('details', details) self.assertEqual(schema_list(pars), [ {'path': '/name', 'type':'s'}, {'path': '/count', 'type':'i'}, {'path': '/price', 'type':'f'}, {'path': '/soldout', 'type':'b'}, {'path': '/details/page_count', 'type':'i'}, {'path': '/details/author', 'type':'s'} ])
def test_void(self): p = Params() exitparam = p.void('exit') self.assertEqual(exitparam.to_dict()['type'], 'v') exits = [] exitparam.onchange(exits.append) self.assertEqual(len(exits), 0) exitparam.set(None) self.assertEqual(len(exits), 1) exitparam.set('foo') self.assertEqual(len(exits), 2) exitparam.trigger() self.assertEqual(len(exits), 3)
def test_get_values(self): pars = Params() pars.int('count').set(1) pars.float('price').set(9.99) vals = get_values(pars) self.assertEqual(vals['count'], 1) self.assertEqual(vals['price'], 9.99) self.assertEqual(json.loads(json.dumps(vals))['count'], 1) self.assertEqual(json.loads(json.dumps(vals))['price'], 9.99) pars.get('count').set(100) self.assertEqual(get_values(pars)['count'], 100)
def test_void_argumentless_callback(self): p = Params() exitparam = p.void('exit') self.assertEqual(exitparam.to_dict()['type'], 'v') exits = [] def func(): print('func: {}'.format(len(exits))) exits.append('func') exitparam.ontrigger(func) self.assertEqual(len(exits), 0) exitparam.trigger() self.assertEqual(len(exits), 1) self.assertEqual(exits[-1], 'func')
async def main(self): # Params params = Params() self.imageParam = params.image('image') fps = params.float('fps', min=0.0, max=5.0) snap = params.void('snap') fps.set(0.0) snap.ontrigger(self.update) logger.info(f'Starting websocket server on port: {self.port}') wss = WebsocketServer(Server(params), host=self.host, port=self.port, start=False) await wss.start_async() logger.info(f'Starting webcam') self.cap = cv2.VideoCapture(0) nextTime = time.time() try: while True: t = time.time() hz = fps.val() if t >= nextTime and hz > 0.00001: self.update() nextTime = t + 1.0 / hz await asyncio.sleep(0.05) key = cv2.waitKey(10) & 0xFF if key == 27 or key == ord('q'): # escape or Q break if key == ord('s'): self.update() except KeyboardInterrupt: print("Received Ctrl+C... initiating exit") print('Stopping...') wss.stop()
def test_option_queueIncomingValuesUntilUpdate(self): # params pars = Params() pars.string('name').set('Abe') # server s = Server(pars, queueIncomingValuesUntilUpdate=True) # remote r1 = Remote() s.connect(r1) # remote sends value change event r1.incoming.valueEvent('/name', 'Bob') # incoming value NOT effectuated yet (operation queued) self.assertEqual(pars.get('name').val(), 'Abe') # process queued operations s.update() # incoming value effectuated self.assertEqual(pars.get('name').val(), 'Bob')
def test_disconnect(self): # params pars = Params() pars.string('name').set('Abe') # server s = Server(pars) # remote r1 = Remote() s.connect(r1) p1 = create_sync_params(r1) # before self.assertEqual(p1.get('name').val(), 'Abe') # action-1; param value change pars.get('name').set('Bob') # after-1; verify value change arrived at r1 self.assertEqual(p1.get('name').val(), 'Bob') # action-1.1; r1 send value change r1.incoming.valueEvent('/name', 'Cat') # after-1; verify value change was processed by server self.assertEqual(pars.get('name').val(), 'Cat') # ... and was sent back to r1 self.assertEqual(p1.get('name').val(), 'Cat') # action-2; param value changes AFTER r1 disconnects s.disconnect(r1) pars.get('name').set('Don') # after-2; verify value change did NOT arrive at r1 self.assertEqual(p1.get('name').val(), 'Cat') # action-2.1; r1 send value change r1.incoming.valueEvent('/name', 'Eve') # after-1; verify value change was NOT processed by server self.assertEqual(pars.get('name').val(), 'Don') # ... and was NOT sent back to r1 self.assertEqual(p1.get('name').val(), 'Cat')
def test_create_sync_params(self): # params pars = Params() pars.string('name') # server s = Server(pars) # remote r1 = Remote() s.connect(r1) p1 = create_sync_params(r1) # before self.assertEqual(len(p1), 1) self.assertIsNone(p1.get('ranking')) # mutation; a new parameter is added to pars pars.int('ranking') # after; verify the mutation is applied to r1's synced params self.assertEqual(len(p1), 2) self.assertIsNotNone(p1.get('ranking'))
def test_bool(self): params = Params() param = params.bool('checked') self.assertEqual(param.type, 'b') self.assertTrue(isinstance(param, Param)) param.set('true') self.assertEqual(param.val(), True) self.assertEqual(param.changeEvent._fireCount, 1) param.set('xxx') # will not change the value self.assertEqual(param.val(), True) self.assertEqual(param.changeEvent._fireCount, 1) param.set('false') self.assertEqual(param.val(), False) self.assertEqual(param.changeEvent._fireCount, 2) param.set('yyy') # will not change the value self.assertEqual(param.val(), False) self.assertEqual(param.changeEvent._fireCount, 2)
def setUp(self): self.params = params = Params() self.p1 = params.int('some_int') self.p1.set(0) self.wss = WebsocketServer(Server(self.params), start=False)
def test_schema_list_empty(self): pars = Params() self.assertEqual(schema_list(pars), [])
def test_get_path_with_invalid_path(self): pars = Params() pars.string('foo') self.assertIsNone(get_path(pars, '/bar'))
def test_osc_server_choreography(self): # # setup # # param params = Params() params.string("name") # server server = Server(params) # intercept all outgoing osc messages into send_log send_log = [] def capture(host, port, addr, args): send_log.append((host, port, addr, args)) # create osc server osc_server = OscServer(server, capture_sends=capture, listen=False) # # Client connects # # create fake incoming connect message osc_server.receive('/params/connect', ['127.0.0.1:8081']) # verify a connect confirmation was sent self.assertEqual(send_log, [('127.0.0.1', 8081, '/params/connect/confirm', (json.dumps(schema_list(params))))]) # # Client sends new value # self.assertIsNone(params.get('name').val()) send_log.clear() osc_server.receive('/params/value', ['/name', 'Fab']) # verify value got applied into our local params self.assertEqual(params.get('name').val(), 'Fab') # verify the value was broadcasted back to client self.assertEqual(send_log, [('127.0.0.1', 8081, '/params/value', ('/name', 'Fab'))]) # # Client sends invalid new value # send_log.clear() osc_server.receive('/params/value', ['/foo', 'bar']) # verify nothing sent out to client(s) self.assertEqual(len(send_log), 0) # # Schema change broadcasted to client # send_log.clear() params.int('age') self.assertEqual(send_log, [('127.0.0.1', 8081, '/params/schema', (json.dumps(schema_list(params))))]) # # Client requests schema # send_log.clear() osc_server.receive('/params/schema', ['192.168.1.2:8080']) # verify response self.assertEqual(send_log, [('192.168.1.2', 8080, '/params/schema', (json.dumps(schema_list(params))))]) # # Client disconnected by server # send_log.clear() # osc_server.receive('/params/disconnect', ('127.0.0.1:8081')) for r in server.connected_remotes: r.outgoing.send_disconnect() # server.disconnect(r) self.assertEqual(send_log, [('127.0.0.1', 8081, '/params/disconnect', ())])
default='info') opts, args = parser.parse_args() lvl = { 'debug': logging.DEBUG, 'info': logging.INFO, 'warning': logging.WARNING, 'error': logging.ERROR, 'critical': logging.CRITICAL }['debug' if opts.verbose else str(opts.verbosity).lower()] logging.basicConfig(level=lvl) return opts, args opts, args = parse_args() params = Params() params.string('name') params.float('score') params.int('level') params.bool('highest-score') http_server = HttpServer(Server(params)) try: while True: time.sleep(0.5) except KeyboardInterrupt: print("Received Ctrl+C... initiating exit") http_server.stop()
def test_get(self): params = Params() param = params.bool('check') self.assertEqual(params.get('check'), param) self.assertIsNone(params.get('foo'))