summaryrefslogtreecommitdiff
path: root/host/proto
diff options
context:
space:
mode:
authorNicolas Schodet2009-02-09 20:05:30 +0100
committerNicolas Schodet2009-02-09 20:05:30 +0100
commit2e95e3a33bcb34aeec66551503c692c1cb80ab61 (patch)
tree6b763ee0d1bf458da0ced13f44535b42f3b8efb8 /host/proto
parent552b28da98a387d13aea7403a7e72732a913277d (diff)
* all python:
- changed tabs to spaces.
Diffstat (limited to 'host/proto')
-rw-r--r--host/proto/popen_io.py22
-rw-r--r--host/proto/proto.py228
-rw-r--r--host/proto/test/fio.py24
-rw-r--r--host/proto/test/interactive.py2
4 files changed, 138 insertions, 138 deletions
diff --git a/host/proto/popen_io.py b/host/proto/popen_io.py
index 235151d3..0f582802 100644
--- a/host/proto/popen_io.py
+++ b/host/proto/popen_io.py
@@ -29,22 +29,22 @@ class PopenIO:
convert from and to \r and \n."""
def __init__ (self, cmd):
- """Initialise and start the given commande line."""
- fout, fin = os.popen2 (cmd, 'b', 0)
- time.sleep (0.2)
- self.fin = fin
- self.fout = fout
+ """Initialise and start the given commande line."""
+ fout, fin = os.popen2 (cmd, 'b', 0)
+ time.sleep (0.2)
+ self.fin = fin
+ self.fout = fout
def read (self, *args):
- buf = self.fin.read (*args).replace ('\n', '\r')
- return buf
+ buf = self.fin.read (*args).replace ('\n', '\r')
+ return buf
def write (self, *args):
- return self.fout.write (*[i.replace ('\r', '\n') for i in args])
+ return self.fout.write (*[i.replace ('\r', '\n') for i in args])
def fileno (self):
- return self.fin.fileno ()
+ return self.fin.fileno ()
def close (self):
- self.fin.close ()
- self.fout.close ()
+ self.fin.close ()
+ self.fout.close ()
diff --git a/host/proto/proto.py b/host/proto/proto.py
index 9eb401e1..19dff792 100644
--- a/host/proto/proto.py
+++ b/host/proto/proto.py
@@ -32,149 +32,149 @@ ARG = 3
class Proto:
def __init__ (self, file, date, timeout, log = None):
- """Initialise and set file (serial port, pty, socket...), date
- function and timeout value.
-
- - file: open file connected to the slave device.
- - date: when called, should return the current time.
- - timeout: time after which retransmission is done.
- - log: if defined, will be called with a log string.
-
- """
- self.file = file
- self.date = date
- self.last_send = None
- self.timeout = timeout
- self.send_queue = [ ]
- self.state = START
- self.log = log
- self.handlers = { }
+ """Initialise and set file (serial port, pty, socket...), date
+ function and timeout value.
+
+ - file: open file connected to the slave device.
+ - date: when called, should return the current time.
+ - timeout: time after which retransmission is done.
+ - log: if defined, will be called with a log string.
+
+ """
+ self.file = file
+ self.date = date
+ self.last_send = None
+ self.timeout = timeout
+ self.send_queue = [ ]
+ self.state = START
+ self.log = log
+ self.handlers = { }
def send (self, *frame):
- """Queue a frame to send."""
- if not self.send_queue:
- self.last_send = None
- self.send_queue.append (Frame (*frame))
+ """Queue a frame to send."""
+ if not self.send_queue:
+ self.last_send = None
+ self.send_queue.append (Frame (*frame))
def read (self):
- """Read from file and receive frames."""
- for f in self.recv ():
- if self.log:
- self.log ('recv %s' % f)
- if self.send_queue and f == self.send_queue[0]:
- del self.send_queue[0]
- if self.send_queue:
- self.send_head ()
- else:
- self.dispatch (f)
+ """Read from file and receive frames."""
+ for f in self.recv ():
+ if self.log:
+ self.log ('recv %s' % f)
+ if self.send_queue and f == self.send_queue[0]:
+ del self.send_queue[0]
+ if self.send_queue:
+ self.send_head ()
+ else:
+ self.dispatch (f)
def sync (self):
- """Send frames, return True if all is sent."""
- if self.send_queue and (self.last_send is None
- or self.last_send + self.timeout < self.date ()):
- self.send_head ()
- return not self.send_queue
+ """Send frames, return True if all is sent."""
+ if self.send_queue and (self.last_send is None
+ or self.last_send + self.timeout < self.date ()):
+ self.send_head ()
+ return not self.send_queue
def wait (self, cond = None):
- """Wait forever or until cond () is True."""
- while not (self.sync () and (cond is not None or cond ())):
- fds = select.select ((self,), (), (), self.timeout)[0]
- for i in fds:
- assert i is self
- i.read ()
+ """Wait forever or until cond () is True."""
+ while not (self.sync () and (cond is not None or cond ())):
+ fds = select.select ((self,), (), (), self.timeout)[0]
+ for i in fds:
+ assert i is self
+ i.read ()
def register (self, command, fmt, handler):
- """Register a handler for the specified command and format. The
- handler will receive decoded arguments."""
- key = (command, struct.calcsize ('!' + fmt))
- assert key not in self.handlers
- self.handlers[key] = (handler, fmt)
+ """Register a handler for the specified command and format. The
+ handler will receive decoded arguments."""
+ key = (command, struct.calcsize ('!' + fmt))
+ assert key not in self.handlers
+ self.handlers[key] = (handler, fmt)
def fileno (self):
- """Return file descriptor, for use with select."""
- return self.file.fileno ()
+ """Return file descriptor, for use with select."""
+ return self.file.fileno ()
def send_head (self):
- """Send first frame from the send queue."""
- if self.log:
- self.log ('send %s' % self.send_queue[0])
- self.file.write (self.send_queue[0].data ())
- self.last_send = self.date ()
+ """Send first frame from the send queue."""
+ if self.log:
+ self.log ('send %s' % self.send_queue[0])
+ self.file.write (self.send_queue[0].data ())
+ self.last_send = self.date ()
def recv (self):
- """Receive a frame, used as a generator."""
- for c in self.file.read (1):
- if c == '!':
- self.state = BANG
- else:
- if self.state == START:
- pass
- elif self.state == BANG:
- if c.isalpha ():
- self.recv_command = c
- self.recv_args = ''
- self.state = CMD
- else:
- self.recv_error ()
- elif self.state == CMD:
- if c == '\r':
- f = Frame (self.recv_command)
- f.args = binascii.unhexlify (self.recv_args)
- yield f
- elif (c >= '0' and c <= '9') or (c >= 'a' and c <= 'f'):
- self.recv_args += c
- self.state = ARG
- else:
- self.recv_error ()
- else:
- assert self.state == ARG
- if (c >= '0' and c <= '9') or (c >= 'a' and c <= 'f'):
- self.recv_args += c
- self.state = CMD
- else:
- self.recv_error ()
+ """Receive a frame, used as a generator."""
+ for c in self.file.read (1):
+ if c == '!':
+ self.state = BANG
+ else:
+ if self.state == START:
+ pass
+ elif self.state == BANG:
+ if c.isalpha ():
+ self.recv_command = c
+ self.recv_args = ''
+ self.state = CMD
+ else:
+ self.recv_error ()
+ elif self.state == CMD:
+ if c == '\r':
+ f = Frame (self.recv_command)
+ f.args = binascii.unhexlify (self.recv_args)
+ yield f
+ elif (c >= '0' and c <= '9') or (c >= 'a' and c <= 'f'):
+ self.recv_args += c
+ self.state = ARG
+ else:
+ self.recv_error ()
+ else:
+ assert self.state == ARG
+ if (c >= '0' and c <= '9') or (c >= 'a' and c <= 'f'):
+ self.recv_args += c
+ self.state = CMD
+ else:
+ self.recv_error ()
def recv_error (self):
- """Handle reception errors."""
- self.state = START
- if self.log:
- self.log ('error')
- # Resend now.
- if self.send_queue:
- self.send_head ()
+ """Handle reception errors."""
+ self.state = START
+ if self.log:
+ self.log ('error')
+ # Resend now.
+ if self.send_queue:
+ self.send_head ()
def dispatch (self, frame):
- """Pass a received frame to the correct handler."""
- key = (frame.command, len (frame.args))
- if key in self.handlers:
- h = self.handlers[key]
- h[0] (*(frame.decode (h[1])))
+ """Pass a received frame to the correct handler."""
+ key = (frame.command, len (frame.args))
+ if key in self.handlers:
+ h = self.handlers[key]
+ h[0] (*(frame.decode (h[1])))
class Frame:
def __init__ (self, command = None, fmt = '', *args):
- """Initiliase a frame. If command is given, the frame is constructed
- using a struct.pack like fmt string."""
- if command:
- assert len (command) == 1 and command.isalpha ()
- self.command = command
- self.args = struct.pack ('!' + fmt, *args)
- else:
- self.command = None
- self.args = ''
+ """Initiliase a frame. If command is given, the frame is constructed
+ using a struct.pack like fmt string."""
+ if command:
+ assert len (command) == 1 and command.isalpha ()
+ self.command = command
+ self.args = struct.pack ('!' + fmt, *args)
+ else:
+ self.command = None
+ self.args = ''
def data (self):
- """Get a frame representation ready to send."""
- return '!' + self.command + binascii.hexlify (self.args) + '\r'
+ """Get a frame representation ready to send."""
+ return '!' + self.command + binascii.hexlify (self.args) + '\r'
def decode (self, fmt):
- """Decode using a struct.unpack like fmt string."""
- return struct.unpack ('!' + fmt, self.args)
+ """Decode using a struct.unpack like fmt string."""
+ return struct.unpack ('!' + fmt, self.args)
def __eq__ (self, other):
- """Compare for equality."""
- return self.command == other.command and self.args == other.args
+ """Compare for equality."""
+ return self.command == other.command and self.args == other.args
def __str__ (self):
- """Convert to string."""
- return '!' + self.command + binascii.hexlify (self.args)
+ """Convert to string."""
+ return '!' + self.command + binascii.hexlify (self.args)
diff --git a/host/proto/test/fio.py b/host/proto/test/fio.py
index 07646199..f520d41e 100644
--- a/host/proto/test/fio.py
+++ b/host/proto/test/fio.py
@@ -1,22 +1,22 @@
class IO:
def __init__ (self, fin = None, fout = None):
- if fin is None:
- import sys, tty
- self.fin = sys.stdin
- self.fout = sys.stdout
- tty.setcbreak (sys.stdin.fileno ())
- else:
- self.fin = fin
- self.fout = fout
+ if fin is None:
+ import sys, tty
+ self.fin = sys.stdin
+ self.fout = sys.stdout
+ tty.setcbreak (sys.stdin.fileno ())
+ else:
+ self.fin = fin
+ self.fout = fout
def read (self, *args):
- buf = self.fin.read (*args).replace ('\n', '\r')
- return buf
+ buf = self.fin.read (*args).replace ('\n', '\r')
+ return buf
def write (self, *args):
- return self.fout.write (*[i.replace ('\r', '\n') for i in args])
+ return self.fout.write (*[i.replace ('\r', '\n') for i in args])
def fileno (self):
- return self.fin.fileno ()
+ return self.fin.fileno ()
diff --git a/host/proto/test/interactive.py b/host/proto/test/interactive.py
index a8251f6d..d34d6d59 100644
--- a/host/proto/test/interactive.py
+++ b/host/proto/test/interactive.py
@@ -26,4 +26,4 @@ while True:
p.sync ()
fds = select.select ((p,), (), (), 0.1)[0]
for i in fds:
- i.read ()
+ i.read ()