Commits

Anonymous committed e2311ff

First commit

  • Participants

Comments (0)

Files changed (6)

File INSTALL

Empty file added.

File LICENSE

Empty file added.

File README

Empty file added.

File txlearnbot/__init__.py

Empty file added.

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/txlearnbot.xml

+<?xml version="1.0" encoding="UTF-8"?>
+<interface>
+  <requires lib="gtk+" version="2.24"/>
+  <!-- interface-naming-policy project-wide -->
+  <object class="GtkTextBuffer" id="textbuffer1"/>
+  <object class="GtkWindow" id="window1">
+    <property name="can_focus">False</property>
+    <property name="default_width">300</property>
+    <property name="default_height">200</property>
+    <signal name="destroy" handler="closeWindow" swapped="no"/>
+    <child>
+      <object class="GtkVBox" id="vbox1">
+        <property name="visible">True</property>
+        <property name="can_focus">False</property>
+        <child>
+          <object class="GtkToolbar" id="toolbar1">
+            <property name="visible">True</property>
+            <property name="can_focus">False</property>
+            <child>
+              <object class="GtkToolButton" id="runCode">
+                <property name="visible">True</property>
+                <property name="can_focus">False</property>
+                <property name="use_action_appearance">False</property>
+                <property name="label" translatable="yes">Play</property>
+                <property name="use_underline">True</property>
+                <property name="stock_id">gtk-media-play</property>
+                <signal name="clicked" handler="runCode" swapped="no"/>
+              </object>
+              <packing>
+                <property name="expand">False</property>
+                <property name="homogeneous">True</property>
+              </packing>
+            </child>
+          </object>
+          <packing>
+            <property name="expand">False</property>
+            <property name="fill">True</property>
+            <property name="position">0</property>
+          </packing>
+        </child>
+        <child>
+          <object class="GtkTextView" id="textview1">
+            <property name="visible">True</property>
+            <property name="can_focus">True</property>
+            <property name="buffer">textbuffer1</property>
+          </object>
+          <packing>
+            <property name="expand">True</property>
+            <property name="fill">True</property>
+            <property name="position">1</property>
+          </packing>
+        </child>
+        <child>
+          <object class="GtkDrawingArea" id="drawingarea1">
+            <property name="visible">True</property>
+            <property name="can_focus">False</property>
+          </object>
+          <packing>
+            <property name="expand">True</property>
+            <property name="fill">True</property>
+            <property name="position">2</property>
+          </packing>
+        </child>
+      </object>
+    </child>
+  </object>
+</interface>