Skip to content
Snippets Groups Projects
Commit 3cea1df0 authored by George Nachman's avatar George Nachman
Browse files

Improvements to the python scripting library.

Add NewSessionSubscription.

Add idle observers. Before entering a cond-wait on the main thread let each idle observer run. A notification handler is registered as an idle observer to handle incoming notifications.

Improve debug logging.
parent 334a69d6
No related branches found
No related tags found
No related merge requests found
Loading
Loading
@@ -65,10 +65,15 @@ class IdleDispatchQueue(AbstractDispatchQueue):
 
def wait(self, timeout=None):
start_time = time.time()
n = 0
if timeout is None:
self.cond.acquire()
while self._run_jobs_locked() == 0:
c = self._run_jobs_locked()
n += c
while c == 0:
self.cond.wait()
c = self._run_jobs_locked()
n += c
self.cond.release()
else:
end_time = start_time + timeout
Loading
Loading
@@ -82,4 +87,5 @@ class IdleDispatchQueue(AbstractDispatchQueue):
if n > 0 or now >= end_time:
break;
self.cond.release()
return n
 
Loading
Loading
@@ -37,6 +37,10 @@ class Subscription(object):
def handle(self, notification):
self.handler(notification)
 
class NewSessionSubscription(Subscription):
def __init__(self, handler):
Subscription.__init__(self, api_pb2.NOTIFY_ON_NEW_SESSION, None, handler)
class KeystrokeSubscription(Subscription):
def __init__(self, session_id, handler):
Subscription.__init__(self, api_pb2.NOTIFY_ON_KEYSTROKE, session_id, handler)
Loading
Loading
@@ -67,18 +71,21 @@ def _extract(notification):
 
def _dispatch_handle_notification(notification):
def _run_handlers():
print("Running handlers")
key, sub_notification = _extract(notification)
handlers = _subscriptions[key]
if handlers is not None:
for handler in handlers:
handler.handle(sub_notification)
print("Got a notification")
_dispatch_queue.dispatch_async(_run_handlers)
 
def wait(timeout=None):
_dispatch_queue.wait(timeout)
n = _dispatch_queue.wait(timeout)
return n
def quick_wait():
n = _dispatch_queue.wait(0)
 
register_notification_handler(_dispatch_handle_notification)
it2socket.add_idle_observer(quick_wait)
 
 
Loading
Loading
@@ -7,6 +7,11 @@ import api_pb2
import rpcsocket
import logging
 
_idle_observers = []
def add_idle_observer(observer):
_idle_observers.append(observer)
class Future(rpcsocket.SynchronousCallback):
def __init__(self, transform=None):
rpcsocket.SynchronousCallback.__init__(self)
Loading
Loading
@@ -19,6 +24,7 @@ class Future(rpcsocket.SynchronousCallback):
 
def get(self):
if self.transformed_response is None:
logging.debug("Waiting on future")
self.wait()
logging.debug("REALIZING %s" % str(self))
self.transformed_response = self.transform(self.response)
Loading
Loading
@@ -40,6 +46,11 @@ class Future(rpcsocket.SynchronousCallback):
logging.debug("Immediately run callback for watch for %s" % str(self))
callback(self.get())
 
def wait(self):
for o in _idle_observers:
o()
rpcsocket.SynchronousCallback.wait(self)
class DependentFuture(Future):
"""If you have a future A and you want to create future B, but B can't be
created yet because the information needed to make it doesn't exist yet, use
Loading
Loading
Loading
Loading
@@ -4,9 +4,10 @@
from __future__ import print_function
 
from it2global import get_socket, wait
import api_pb2
import it2session
import it2socket
import api_pb2
import logging
 
class AbstractTab(object):
def __repr__(self):
Loading
Loading
from asyncws import AsyncWebsocketApp
 
import api_pb2
import logging
import threading
import websocket
Loading
Loading
@@ -38,6 +39,7 @@ class RPCSocket(AsyncWebsocketApp):
def sync_send_rpc(self, message):
callback = SynchronousCallback()
def f():
logging.debug("Send request")
self.send(message, opcode=websocket.ABNF.OPCODE_BINARY)
self.dispatch_async(f)
self._append_callback(callback.callback)
Loading
Loading
@@ -48,6 +50,9 @@ class RPCSocket(AsyncWebsocketApp):
 
def async_send_rpc(self, message, callback):
def f():
request = api_pb2.Request()
request.ParseFromString(message)
logging.debug("SEND:\n" + str(request))
self.send(message, opcode=websocket.ABNF.OPCODE_BINARY)
self.dispatch_queue.dispatch_async(f)
self._append_callback(callback)
Loading
Loading
Loading
Loading
@@ -11,22 +11,37 @@ import it2socket
import logging
import time
 
import code, traceback, signal
def main():
# logging.basicConfig(level=logging.DEBUG)
#logging.basicConfig(level=logging.DEBUG)
logging.basicConfig()
def handle_new_session(notification):
print("New session created\n" + str(notification))
logging.debug("Register for new sessions")
it2notifications.NewSessionSubscription(handle_new_session)
logging.debug("Get hierarchy")
hierarchy = it2hierarchy.Hierarchy()
 
logging.debug("Create window")
window = hierarchy.create_window()
logging.debug("Create tab")
tab = window.create_tab()
logging.debug("Get sessions")
session = tab.get_sessions()[0]
logging.debug("Splitting pane")
s2 = session.split_pane(vertical=True).split_pane()
s2.send_text("Hello world").get_status()
 
def handle_keystroke(notification):
print("YOU PRESSED A DAMN KEY")
print("Keypress\n" + str(notification))
 
it2notifications.KeystrokeSubscription(s2.get_session_id(), handle_keystroke)
it2notifications.wait(5)
while True:
it2notifications.wait(1)
 
if __name__ == "__main__":
main()
Loading
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment