# HG changeset patch # User Tero Marttila # Date 1401722801 -10800 # Node ID 2179a4e28aba0d3aaf9e46b849869f0e82a3bd40 # Parent 61925fb4947e7cabb14178efcfdb8a6875bd4275 qmsk.dmx.control: switch to readline() based __iter__, with __call__(poll=True) and **opts diff -r 61925fb4947e -r 2179a4e28aba qmsk/dmx/control.py --- a/qmsk/dmx/control.py Fri May 02 00:10:14 2014 +0300 +++ b/qmsk/dmx/control.py Mon Jun 02 18:26:41 2014 +0300 @@ -45,6 +45,10 @@ self.io.read(1) def _arg (self, arg) : + """ + Format given value as a protocol argument. + """ + if isinstance(arg, str) : value, = arg value = ord(value) @@ -58,7 +62,23 @@ else : raise ValueError(value) - def __call__ (self, cmd, *args) : + def __iter__ (self) : + """ + Read command responses back. + """ + + while True: + yield self.io.readline() + + def __call__ (self, cmd, *args, **opts) : + """ + + [ [...]] + """ + + # XXX: + poll = opts.pop('poll', True) + out = cmd + ' ' + ' '.join(self._arg(arg) for arg in args) + '\r' log.info("%s", out) @@ -66,15 +86,17 @@ self.io.write(out) self.io.flush() - ret = self.io.read(len(out)) + if poll: + for ret in self: + break - if '!' in ret : - raise DMXCommandError(cmd=out, out=ret) + if '!' in ret : + raise DMXCommandError(cmd=out, out=ret) - elif '?' in ret : - raise DMXUnknownCommandError(cmd=cmd) + elif '?' in ret : + raise DMXUnknownCommandError(cmd=cmd) - def clear (self) : + def clear (self, **opts) : """ Set dmx = [ ] @@ -82,46 +104,50 @@ For most lights, this seems to be equivalent to losing the DMX signal, and they retain their old state. """ - self('c') + self('c', **opts) - def zero (self) : + def zero (self, **opts) : """ Set dmx = [0, ...] Uses the maximum DMX packet length available. """ - self('z') + self('z', **opts) - def out (self, *values) : + def out (self, *values, **opts) : """ Set dmx = (value, ...) """ - self('o', *values) + self('o', *values, **opts) - def set (self, start, *values) : + def set (self, start, *values, **opts) : """ Set dmx[start:] = value """ - self('s', start, *values) + self('s', start, *values, **opts) - def fill (self, start, end, *values) : + def fill (self, start, end, *values, **opts) : """ Set dmx[start:end] to repetitions of (value, ...) """ - self('f', start, end, *values) + self('f', start, end, *values, **opts) - def range (self, start, stop, step, value) : + def range (self, start, stop, step, value, **opts) : """ Set dmx[start:end:step] = value """ - self('r', start, stop, step, value) + self('r', start, stop, step, value, **opts) def __setitem__ (self, index, value) : + """ + Magic indexing. + """ + if isinstance(value, collections.Sequence) : values = tuple(value) else :