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

Make the Python API library into a proper package.

parent 7feedbd8
No related branches found
No related tags found
No related merge requests found
Showing
with 94 additions and 45 deletions
Loading
Loading
@@ -21,3 +21,14 @@ iTerm2.xcodeproj/xcuserdata/*
ColorPicker/ColorPicker.xcodeproj/project.xcworkspace/xcuserdata/
ColorPicker/ColorPicker.xcodeproj/xcuserdata/
iTerm2.xcodeproj/project.xcworkspace/contents.xcworkspacedata
# Compiled python modules.
*.pyc
*.py-e
# Setuptools distribution folder.
api/library/python/iterm2/dist
# Python egg metadata, regenerated from source files by setuptools.
api/library/python/iterm2/*.egg-info
include README.rst
iTerm2
------
iTerm2 3.1 has a new scripting API. It uses Google Protocol Buffers to allow programs written in any language to communicate with iTerm2. Most users will prefer an interface in a high-level language. This library offers a Python interface to iTerm2. Please note that Prefs > Advanced > Enable websocket API server must be enabled in iTerm2 for this library to work.
from .hierarchy import Hierarchy
from .notifications import NewSessionSubscription, TerminateSessionSubscription, KeystrokeSubscription, LayoutChangeSubscription, wait
from .session import Session
from .tab import Tab
from .window import Window
Loading
Loading
@@ -3,19 +3,19 @@
 
from __future__ import print_function
import api_pb2
from it2global import get_socket, wait
import it2notifications
import it2session
import it2socket
import it2tab
import it2window
from sharedstate import get_socket, wait
import notifications
import session
import socket
import tab
import window
import logging
 
class Synchronizer(object):
def __init__(self):
it2notifications.NewSessionSubscription(lambda notification: self._refresh())
it2notifications.TerminateSessionSubscription(lambda notification: self._refresh())
it2notifications.LayoutChangeSubscription(self._layoutDidChange)
notifications.NewSessionSubscription(lambda notification: self._refresh())
notifications.TerminateSessionSubscription(lambda notification: self._refresh())
notifications.LayoutChangeSubscription(self._layoutDidChange)
 
self.value = None
self._refresh()
Loading
Loading
@@ -26,7 +26,7 @@ class Synchronizer(object):
 
def _layoutDidChange(self, notification):
logging.debug("Layout did change")
self.future = it2socket.Future()
self.future = socket.Future()
self.future.callback(notification.list_sessions_response)
 
def get(self):
Loading
Loading
@@ -55,18 +55,18 @@ class Hierarchy(object):
 
def parse(self, response):
windows = []
for window in response.windows:
for w in response.windows:
tabs = []
for tab in window.tabs:
for t in w.tabs:
sessions = []
for session in tab.sessions:
sessions.append(it2session.Session(session.uniqueIdentifier))
tabs.append(it2tab.Tab(tab.tab_id, sessions))
windows.append(it2window.Window(window.window_id, tabs))
for s in t.sessions:
sessions.append(session.Session(s.uniqueIdentifier))
tabs.append(tab.Tab(t.tab_id, sessions))
windows.append(window.Window(w.window_id, tabs))
self.windows = windows
 
def create_window(self, profile=None, command=None):
return it2window.FutureWindow(get_socket().request_create_tab(
return window.FutureWindow(get_socket().request_create_tab(
profile=profile, window=None, index=None, command=command))
 
def __repr__(self):
Loading
Loading
Loading
Loading
@@ -4,11 +4,11 @@
from __future__ import print_function
 
import api_pb2
from it2global import get_socket, wait, register_notification_handler
from sharedstate import get_socket, wait, register_notification_handler
import dispatchq
import it2session
import it2socket
import it2tab
import session
import socket
import tab
import logging
import threading
import time
Loading
Loading
@@ -105,6 +105,6 @@ def quick_wait():
n = _dispatch_queue.wait(0)
 
register_notification_handler(_dispatch_handle_notification)
it2socket.add_idle_observer(quick_wait)
socket.add_idle_observer(quick_wait)
 
 
from asyncws import AsyncWebsocketApp
from .asyncws import AsyncWebsocketApp
 
import api_pb2
import logging
Loading
Loading
Loading
Loading
@@ -4,8 +4,8 @@
from __future__ import print_function
 
import api_pb2
from it2global import get_socket, wait
import it2socket
from sharedstate import get_socket, wait
import socket
import logging
 
class TextSender(object):
Loading
Loading
@@ -52,7 +52,7 @@ class FutureSession(AbstractSession):
 
def create_inner(response):
return get_socket().request_send_text(self.get_session_id(), text)
sendTextFuture = it2socket.DependentFuture(self.future, create_inner)
sendTextFuture = socket.DependentFuture(self.future, create_inner)
return TextSender(sendTextFuture)
 
def split_pane(self, vertical=False, before=False, profile=None):
Loading
Loading
@@ -62,7 +62,7 @@ class FutureSession(AbstractSession):
def create_inner(response):
return get_socket().request_split_pane(
session=self.get_session_id(), vertical=vertical, before=before, profile=profile)
createSessionFuture = it2socket.DependentFuture(self.future, create_inner)
createSessionFuture = socket.DependentFuture(self.future, create_inner)
return FutureSession(createSessionFuture);
 
def _get_session(self):
Loading
Loading
import it2socket
import socket
 
_socket = None
_notification_handlers = []
Loading
Loading
@@ -13,7 +13,7 @@ def _notification_handler(notification):
def get_socket():
global _socket
if _socket is None:
_socket = it2socket.Connection()
_socket = socket.Connection()
_socket.connect(_notification_handler)
return _socket
 
Loading
Loading
Loading
Loading
@@ -3,10 +3,10 @@
 
from __future__ import print_function
 
from it2global import get_socket, wait
from sharedstate import get_socket, wait
import api_pb2
import it2session
import it2socket
import session
import socket
import logging
 
class AbstractTab(object):
Loading
Loading
@@ -21,8 +21,8 @@ class AbstractTab(object):
 
def pretty_str(self, indent=""):
s = indent + "Tab id=%s\n" % self.get_tab_id()
for session in self.get_sessions():
s += session.pretty_str(indent=indent + " ")
for j in self.get_sessions():
s += j.pretty_str(indent=indent + " ")
return s
 
class FutureTab(AbstractTab):
Loading
Loading
@@ -56,7 +56,7 @@ class FutureTab(AbstractTab):
def _parse(self, response):
self.status = response.status
if self.status == api_pb2.CreateTabResponse.OK:
self.tab = Tab(response.tab_id, [ it2session.Session(response.session_id) ])
self.tab = Tab(response.tab_id, [ session.Session(response.session_id) ])
 
class Tab(AbstractTab):
def __init__(self, tab_id, sessions):
Loading
Loading
Loading
Loading
@@ -3,11 +3,11 @@
 
from __future__ import print_function
 
from it2global import get_socket, wait
import it2session
import it2socket
from sharedstate import get_socket, wait
import session
import socket
import api_pb2
import it2tab
import tab
 
class AbstractWindow(object):
def __repr__(self):
Loading
Loading
@@ -53,8 +53,8 @@ class FutureWindow(AbstractWindow):
def create_inner(response):
return get_socket().request_create_tab(
profile=profile, window=self.get_window_id(), index=index, command=command)
createTabFuture = it2socket.DependentFuture(self.future, create_inner)
return it2tab.FutureTab(createTabFuture);
createTabFuture = socket.DependentFuture(self.future, create_inner)
return tab.FutureTab(createTabFuture);
 
def get_status(self):
self._parse_if_needed()
Loading
Loading
@@ -72,9 +72,9 @@ class FutureWindow(AbstractWindow):
def _parse(self, response):
self.status = response.status
if self.status == api_pb2.CreateTabResponse.OK:
session = it2session.Session(response.session_id)
tab = it2tab.Tab(response.tab_id, [ session ])
self.window = Window(response.window_id, [ tab ])
s = session.Session(response.session_id)
t = tab.Tab(response.tab_id, [ s ])
self.window = Window(response.window_id, [ t ])
 
class Window(AbstractWindow):
def __init__(self, window_id, tabs):
Loading
Loading
@@ -91,7 +91,7 @@ class Window(AbstractWindow):
return self.tabs
 
def create_tab(self, profile=None, command=None, index=None):
return it2tab.FutureTab(get_socket().request_create_tab(
return tab.FutureTab(get_socket().request_create_tab(
profile=profile, window=self.window_id, index=index, command=command))
 
 
from setuptools import setup
def readme():
with open('README.rst') as f:
return f.read()
setup(name='iterm2',
version='0.1',
description='Python interface to iTerm2\'s scripting API',
long_description=readme(),
classifiers=[
'Development Status :: 3 - Alpha',
'License :: OSI Approved :: GNU General Public License v3 or later (GPLv3+)',
'Programming Language :: Python :: 2.7',
],
url='http://github.com/gnachman/iTerm2',
author='George Nachman',
author_email='gnachman@gmail.com',
license='GPLv3',
packages=['iterm2'],
install_requires=[
'protobuf',
'websocket-client',
],
include_package_data=True,
zip_safe=False)
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