import os
import re

PIPENAME = '/tmp/cheese_plate'

def create_fifo(pipename):
    if not os.path.exists(pipename):
        os.mkfifo(pipename)
    fd = os.open(pipename, os.O_RDWR|os.O_NONBLOCK)
    fifo = os.fdopen(fd, 'w')
    return fifo

def cmd_repl(fifo, data):
    fifo = create_fifo(PIPENAME)
    data = sanitize_cmd(data)
    if data != "":
        fifo.write('%s\n' % (data))
        fifo.flush()

def sanitize_cmd(cmd):
    """
    command has to be
    move (angle, distance)
        angle = -180 to 180 in degrees
        distance = distance in centimeters. floats > 0
    pen [action]
         action = 'up', 'down'

    reset
    """
    cmd = cmd.strip()
    if cmd[0:5] == 'reset':
        return cmd[0:5]
    elif cmd[0:3] == 'pen':
        if cmd[3:].strip() in ['up', 'down']:
            return cmd[0:3] + ' ' + cmd[3:].strip()
        else:
            return ""
    elif cmd[0:4] == 'move':
        params = cmd[4:].strip()
        try:
            if params[0] == '(' and params[-1] == ')' and params.index(",") >= 0:
                (degree_str, distance_str) = params[1:-1].split(",")
                distance = float(distance_str)
                degree = float(degree_str)
                # degree has to be -180.0 to 180
                if degree > 180 or degree < -180:
                    return ""
                # distance has to  > 0
                elif distance <= 0:
                    return ""
                else:
                    return "move (%s, %s)" %(str(degree), str(distance))
        except ValueError as err:
            return ""
        else:
            return ""
    else:
        return ""

@@ -300,20 +300,20 @@
 ME)%0A    
-data
+cmds
  = sanit
@@ -334,21 +334,23 @@
     
-if data != %22%22
+for cmd in cmds
 :%0A  
@@ -376,20 +376,19 @@
 s%5Cn' %25 (
-data
+cmd
 ))%0A     
@@ -421,16 +421,22 @@
 ize_cmd(
+input_
 cmd):%0A  
@@ -647,24 +647,137 @@
 set%0A    %22%22%22%0A
+    results = %5B%5D%0A    print(input_cmd)%0A    cmds = input_cmd.split('%5C%5Cn')%0A    print(cmds)%0A    for cmd in cmds:%0A    
     cmd = cm
@@ -782,24 +782,28 @@
 cmd.strip()%0A
+    
     if cmd%5B0
@@ -830,23 +830,35 @@
     
-return 
+    results.append(
 cmd%5B0:5%5D
 %0A   
@@ -853,17 +853,22 @@
 cmd%5B0:5%5D
-%0A
+)%0A    
     elif
@@ -887,16 +887,20 @@
  'pen':%0A
+    
         
@@ -949,23 +949,35 @@
         
-return 
+    results.append(
 cmd%5B0:3%5D
@@ -1004,45 +1004,14 @@
 ip()
+)
 %0A    
-    else:%0A            return %22%22%0A
     
@@ -1035,16 +1035,20 @@
 'move':%0A
+    
         
@@ -1084,13 +1084,21 @@
     
+    
 try:%0A
+    
     
@@ -1191,16 +1191,20 @@
         
+    
 (degree_
@@ -1256,32 +1256,36 @@
                 
+    
 distance = float
@@ -1315,16 +1315,20 @@
         
+    
 degree =
@@ -1362,16 +1362,20 @@
         
+    
 # degree
@@ -1399,16 +1399,20 @@
  to 180%0A
+    
         
@@ -1469,34 +1469,34 @@
             
-return %22%22%0A
+    pass%0A 
             
@@ -1499,16 +1499,19 @@
         
+   
 # distan
@@ -1521,24 +1521,28 @@
 has to  %3E 0%0A
+    
             
@@ -1581,33 +1581,32 @@
             
-return %22%22
+    pass
 %0A           
@@ -1602,32 +1602,36 @@
                 
+    
 else:%0A          
@@ -1640,23 +1640,35 @@
         
-return 
+    results.append(
 %22move (%25
@@ -1704,17 +1704,22 @@
 stance))
-%0A
+)%0A    
         
@@ -1760,26 +1760,27 @@
     
-return %22%22%0A
+    pass%0A  
         
 else
@@ -1767,32 +1767,34 @@
  pass%0A          
+  
 else:%0A          
@@ -1795,26 +1795,29 @@
         
-return %22%22%0A
+    pass%0A    
     else
@@ -1830,14 +1830,31 @@
     
-return %22%22%0A
+    pass%0A    return results

add sanitation