Skip to content
Snippets Groups Projects
Commit 7c60f71c authored by George Nachman's avatar George Nachman Committed by GitHub
Browse files

Merge pull request #326 from grimreaper/eax/it/it2ent

Update API docs and python examples to use more modern conventions: print as a function, virtualenv as an option.
parents 6b1bf27e 6d17e6f4
No related branches found
No related tags found
No related merge requests found
# iTerm2 API
This is an example of how to use the iTerm2 API in Python. It sets up a
websocket connection, sends an RPC to subscribe to notifications about changes
to the current username, hostname, or working directory. It prints incoming
Loading
Loading
@@ -7,17 +9,25 @@ notifications.
 
First, install the dependencies.
 
### Using virtualenvwrapper
```
mkvirtualenv iterm2api
pip install websocket-client
pip install protobuf
```
### Using system python
It is also possible to install the dependencies as system libraries.
```
sudo /usr/bin/python -m pip install websocket-client
sudo /usr/bin/python -m pip install protobuf
```
 
Then `cd api/examples/python` and then `./iterm2.py` to run the program.
 
This silly way of running pip is needed if you have more than one version of
python installed.
Now you can run `iTerm2/api/examples/python`.
 
## Writing your own app
Loading
Loading
#!/usr/bin/python
# This is python 2.7 on macOS 10.12.
 
from __future__ import print_function
import api_pb2
import sys
import thread
Loading
Loading
@@ -11,7 +13,7 @@ callbacks = []
 
def list_sessions(ws, argv):
def callback(response):
print str(response)
print(str(response))
ws.close()
request = api_pb2.Request()
request.list_sessions_request.SetInParent()
Loading
Loading
@@ -19,15 +21,15 @@ def list_sessions(ws, argv):
 
def send_text(ws, argv):
def callback(response):
print str(response)
print(str(response))
ws.close()
request = api_pb2.Request()
request.send_text_request.text = argv[2]
SendRPC(ws, request, callback)
 
def handle_location_change_notification(location_change_notification):
print "Location changed"
print str(location_change_notification)
print("Location changed")
print(str(location_change_notification))
 
def SendRPC(ws, message, callback):
ws.send(message.SerializeToString(), opcode=websocket.ABNF.OPCODE_BINARY)
Loading
Loading
@@ -39,14 +41,14 @@ def handle_notification(notification):
 
def handle_notification_response(response):
if not response.HasField('notification_response'):
print "Malformed notification response"
print str(response)
print("Malformed notification response")
print(str(response))
return
if response.notification_response.status != api_pb2.NotificationResponse.OK:
print "Bad status in notification response"
print str(response)
print("Bad status in notification response")
print(str(response))
return
print "Notifcation response ok"
print("Notifcation response ok")
 
def on_message(ws, message):
response = api_pb2.Response()
Loading
Loading
@@ -60,19 +62,19 @@ def on_message(ws, message):
callback(response)
 
def on_error(ws, error):
print "Error: " + str(error)
print("Error: " + str(error))
 
def on_close(ws):
print "Connection closed"
print("Connection closed")
 
def main(argv):
commands = { "list-sessions": list_sessions,
"send-text": send_text }
if len(argv) < 2:
print "Not enough arguments"
print("Not enough arguments")
return -1
if argv[1] not in commands:
print "Unrecognized command " + argv[1]
print("Unrecognized command " + argv[1])
return -2
 
def on_open(ws):
Loading
Loading
#!/usr/bin/python
# This is python 2.7 on macOS 10.12.
 
from __future__ import print_function
import api_pb2
import thread
import time
Loading
Loading
@@ -9,8 +11,8 @@ import websocket
callbacks = []
 
def handle_location_change_notification(location_change_notification):
print "Location changed"
print str(location_change_notification)
print("Location changed")
print(str(location_change_notification))
 
def SendRPC(ws, message, callback):
ws.send(message.SerializeToString(), opcode=websocket.ABNF.OPCODE_BINARY)
Loading
Loading
@@ -22,14 +24,14 @@ def handle_notification(notification):
 
def handle_notification_response(response):
if not response.HasField('notification_response'):
print "Malformed notification response"
print str(response)
print("Malformed notification response")
print(str(response))
return
if response.notification_response.status != api_pb2.NotificationResponse.OK:
print "Bad status in notification response"
print str(response)
print("Bad status in notification response")
print(str(response))
return
print "Notifcation response ok"
print("Notifcation response ok")
 
def on_message(ws, message):
response = api_pb2.Response()
Loading
Loading
@@ -43,19 +45,19 @@ def on_message(ws, message):
callback(response)
 
def on_error(ws, error):
print "Error: " + str(error)
print("Error: " + str(error))
 
def on_close(ws):
print "Connection closed"
print("Connection closed")
 
def on_open(ws):
print "Connection opened"
print("Connection opened")
request = api_pb2.Request()
request.notification_request.subscribe = True
request.notification_request.notification_type = api_pb2.NOTIFY_ON_LOCATION_CHANGE
SendRPC(ws, request, handle_notification_response)
 
if __name__ == "__main__":
def main():
websocket.enableTrace(True)
ws = websocket.WebSocketApp("ws://localhost:1912/",
on_message = on_message,
Loading
Loading
@@ -65,3 +67,5 @@ if __name__ == "__main__":
ws.on_open = on_open
ws.run_forever()
 
if __name__ == "__main__":
main()
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