Commits

Clovis Fabricio  committed 1a74fee

refactoring part 1

  • Participants
  • Parent commits e2311ff

Comments (0)

Files changed (5)

File txlearnbot/amprobot.py

+from twisted.protocols import amp
+
+class Move(amp.Command):
+    arguments = [('steps', amp.Integer())]
+    response = [('total', amp.Integer())]
+
+class RobotProtocol(amp.AMP):
+    @Move.responder
+    def move(self, steps):
+        d = self.factory.robot.move(steps)
+        return d.addCallback(lambda t: {'total': int(t)})
+
+class RobotMoverFactory(protocol.Factory):
+    protocol = RobotProtocol
+    def __init__(self, robot):
+        self.robot = robot
+
+def startListening(reactor, robot, port=5234):
+    f = RobotMoverFactory(robot)
+    reactor.listenTCP(5234, RobotMoverFactory(self.bot))
+

File txlearnbot/gui.py

+import tempfile
+import shutil
+import os
+import sys
+
+import gtk
+from twisted.internet import gtk2reactor # for gtk-2.0
+gtk2reactor.install()
+
+from twisted.internet import reactor, protocol, defer
+from twisted.protocols import amp
+from txlearnbot import robot, amprobot
+
+class CodeRunner(protocol.ProcessProtocol):
+    def __init__(self, code):
+        self.code = code
+        self._path = tempfile.mkdtemp(prefix='coderunner')
+        fullname = os.path.join(self._path, 'txusercode.py')
+        shutil.copy('txlearnbotclient.py', self._path)
+        with open(self._fullname, 'w') as f:
+            f.write('def run(bot):')
+            for line in code.splitlines():
+                f.write('\t%s\n' % (line,))
+        self.running = None
+    
+    def run(self):
+        if not self.running:
+            filename = 'txlearnbotclient.py'
+            self.running = defer.Deferred()
+            reactor.spawnProcess(self, 
+                sys.executable, 
+                args=[sys.executable, filename],
+                env={'HOME': self._path}, 
+                path=self._path,
+                childFDs={0: 0, 1: 1, 2: 2})
+        return self.running
+    
+    def processEnded(status):
+        self.cleanup()
+        self.running.callback(status.value.exitCode)
+        self.running = None
+
+    def cleanup(self):
+        shutil.rmtree(self._path)
+
+class LearnIDE(object):
+    def __init__(self):
+        self.builder = gtk.Builder()
+        self.builder.add_from_file('txlearnbot.xml')
+        self.builder.connect_signals(self)
+
+        window = self.builder.get_object('window1')
+        window.show()
+
+        self.buffer = self.builder.get_object('textbuffer1')
+        self.bot = robot.Robot(self.builder.get_object('drawingarea1'))
+        amprobot.startListening(self.bot)
+
+    def on_runCode_clicked(self, widget, *ign):
+        code = CodeRunner(self.buffer.get_text(*self.buffer.get_bounds()))
+        d = code.run()
+        d.addBoth(lambda ign: widget.enable())
+
+    def on_window1_destroy(self, widget, *ign):
+        reactor.stop()
+
+def main():
+    p = LearnIDE()
+    reactor.run()
+
+if __name__ == '__main__':
+    main()
+

File txlearnbot/robot.py

+import gtk
+
+from twisted.internet import reactor, defer
+from twisted.protocols import amp
+
+def wait(time):
+    d = defer.Deferred()
+    reactor.callLater(time, d.callback, None)
+    return d
+
+class Robot(object):
+    def __init__(self, darea):
+        self._draw = darea
+        self.speed = 0.1
+        self.gc = darea.get_style().fg_gc[gtk.STATE_NORMAL]
+        self.x = 5
+        self.y = 5
+        self.draw()
+        self.direction = (1, 0)
+
+    def draw(self):
+        print self.x, self.y
+        self._draw.window.draw_point(self.gc, self.x, self.y)
+
+    @defer.inlineCallbacks
+    def move(self, steps, d=None):
+        for i in xrange(steps):
+            yield wait(self.speed)
+            dx, dy = self.direction
+            self.x += dx
+            self.y += dy
+            self.draw()
+        defer.returnValue(steps)
+
+class Move(amp.Command):
+    arguments = [('steps', amp.Integer())]
+    response = [('total', amp.Integer())]
+
+class RobotProtocol(amp.AMP):
+    @Move.responder
+    def move(self, steps):
+        return {'total': self.factory.robot.move(steps)}
+
+class RobotMoverFactory(protocol.Factory):
+    protocol = RobotProtocol
+    def __init__(self, robot):
+        self.robot = robot
+

File txlearnbot/txlearnbot.py

-import tempfile
-import shutil
-import os
-import sys
-
-import gtk
-from twisted.internet import gtk2reactor # for gtk-2.0
-gtk2reactor.install()
-
-from twisted.internet import reactor, protocol, defer
-from twisted.protocols import amp
-
-
-class Robot(object):
-    def __init__(self, darea):
-        self._draw = darea
-        self.speed = 0.1
-        self.gc = darea.get_style().fg_gc[gtk.STATE_NORMAL]
-        self.x = 5
-        self.y = 5
-        self.draw()
-        self.direction = (1, 0)
-
-    def draw(self):
-        print self.x, self.y
-        self._draw.window.draw_point(self.gc, self.x, self.y)
-
-    def move(self, steps, d=None):
-        if d is None:
-            d = defer.Deferred()
-        if steps:
-            reactor.callLater(self.speed, self.move, steps-1, d)
-        else:
-            d.callback(0)
-        dx, dy = self.direction
-        self.x += dx
-        self.y += dy
-        self.draw()
-
-class Move(amp.Command):
-    arguments = [('steps', amp.Integer())]
-    response = [('total', amp.Integer())]
-
-class RobotProtocol(amp.AMP):
-    @Move.responder
-    def move(self, steps):
-        return {'total': self.factory.robot.move(steps)}
-
-class RobotMoverFactory(protocol.Factory):
-    protocol = RobotProtocol
-    def __init__(self, robot):
-        self.robot = robot
-
-
-class CodeRunner(protocol.ProcessProtocol):
-    def __init__(self, code):
-        self.code = code
-        self._path = tempfile.mkdtemp(prefix='coderunner')
-        self._fullname = os.path.join(self._path, 'code.py')
-        shutil.copy('txlearnbotclient.py', self._path)
-        with open(self._fullname, 'w') as f:
-            f.write(code)
-        self.running = None
-    
-    def run(self):
-        if not self.running:
-            filename = 'txlearnbotclient.py'
-            self.running = defer.Deferred()
-            reactor.spawnProcess(self, 
-                sys.executable, 
-                args=[sys.executable, filename],
-                env={'HOME': self._path}, 
-                path=self._path,
-                childFDs={0: 0, 1: 1, 2: 2})
-        return self.running
-    
-    def processEnded(status):
-        self.cleanup()
-        self.running.callback(status.value.exitCode)
-        self.running = None
-
-    def cleanup(self):
-        shutil.rmtree(self._path)
-
-class LearnIDE(object):
-    def __init__(self):
-        self.builder = gtk.Builder()
-        self.builder.add_from_file('txlearnbot.xml')
-        self.builder.connect_signals(self)
-
-        window = self.builder.get_object('window1')
-        window.show()
-
-        self.buffer = self.builder.get_object('textbuffer1')
-        self.bot = Robot(self.builder.get_object('drawingarea1'))
-
-        reactor.listenTCP(5234, RobotMoverFactory(self.bot))
-
-
-    def runCode(self, widget, *ign):
-        code = CodeRunner(self.buffer.get_text(*self.buffer.get_bounds()))
-        d = code.run() #.addBoth(lambda ign: widget.enable())
-        print d
-
-    def closeWindow(self, widget, *ign):
-        reactor.stop()
-
-p = LearnIDE()
-
-
-
-reactor.run()
-

File txlearnbot/txlearnbotclient.py

+from twisted.internet import reactor, threads
+from twisted.internet.protocol import ClientCreator
+from twisted.protocols import amp
+
+class Move(amp.Command):
+    arguments = [('steps', amp.Integer())]
+    response = [('total', amp.Integer())]
+
+class Bot(object):
+    def __init__(self, protocol):
+        self.p = protocol
+
+    def move(self, steps):
+        result = threads.blockingCallFromThread(reactor, self.p.callRemote, Move, steps=steps)
+        return result['total']
+
+
+def connected(protocol):
+    bot = Bot(protocol)
+    import txusercode
+    reactor.callInThread(code.run, bot)
+
+ClientCreator(reactor, amp.AMP).connectTCP(
+    '127.0.0.1', 5234).addCallback(connected)
+
+reactor.run()