Skip to content
This repository was archived by the owner on Oct 21, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 deletions py-src/ltipy.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import pickle
import json


def noop():
pass

Expand All @@ -18,6 +19,7 @@ def noop():
connected = noop
disconnected = noop


def km_from_string(s=''):
"""create kernel manager from IPKernelApp string
such as '--shell=47378 --iopub=39859 --stdin=36778 --hb=52668' for IPython 0.11
Expand Down Expand Up @@ -56,7 +58,6 @@ def km_from_string(s=''):
respond(None, "python.client.error.ipython-version", None)
return


s = s.replace('--existing', '')

if '--profile' in s:
Expand All @@ -74,15 +75,18 @@ def km_from_string(s=''):
send = kc.shell_channel.execute
return km


def _extract_traceback(traceback):
# strip ANSI color controls
strip = re.compile('\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]')
return [strip.sub('', t) for t in traceback]


def msgId(m):
if 'parent_header' in m and 'msg_id' in m['parent_header']:
return m['parent_header']['msg_id']


def normalize(m):
mid = msgId(m)
content = m['content']
Expand All @@ -108,11 +112,13 @@ def normalize(m):
content['mid'] = mid
return content


def msgs():
global kc
msgs = kc.iopub_channel.get_msgs()
return [normalize(i) for i in msgs]


def handleMsg(m):
if not m['mid']:
return
Expand Down Expand Up @@ -143,7 +149,8 @@ def handleMsg(m):
res = pickle.loads(m['data'][8:])
except:
pass
respond(None, "clients.raise-on-object", [res["meta"]["obj"], "editor.eval.python.watch", res])
respond(None, "clients.raise-on-object",
[res["meta"]["obj"], "editor.eval.python.watch", res])
else:
command = 'editor.eval.python.print'
ret['file'] = orig['path']
Expand All @@ -159,13 +166,15 @@ def handleMsg(m):
if command:
respond(orig["client"], command, ret)


def msgloop():
global ipy
while not ipy.returncode and not ltmain.stopped():
for m in msgs():
handleMsg(m)
time.sleep(0.01)


def initIPy(s):
try:
cur = km_from_string(s)
Expand All @@ -175,15 +184,19 @@ def initIPy(s):
disconnected()

loc = os.path.dirname(__file__)
send("import sys\nsys.path.append('" + loc.replace('\\','\\\\') + "')\nimport lttools")
send("import sys\nsys.path.append('" +
loc.replace('\\', '\\\\') + "')\nimport lttools")
connected()
msgloop()
disconnected()
except:
disconnected()


def setNs(path):
send("import lttools\nlttools.switch_ns('" + path.replace('\\', '\\\\') + "')")
send(
"import lttools\nlttools.switch_ns('" + path.replace('\\', '\\\\') + "')")


def IPyOutput(l):
m = re.search('--existing (.*\.json)', l)
Expand All @@ -192,6 +205,7 @@ def IPyOutput(l):
if re.search('ImportError: IPython.zmq', l):
respond(None, "python.client.error.pyzmq", None)


def listenIPy():
global ipy
while True:
Expand All @@ -202,6 +216,7 @@ def listenIPy():
break
IPyOutput(next_line)


def startIPy(opts):
global ipy
global respond
Expand All @@ -219,7 +234,8 @@ def startIPy(opts):
cmd = 'bin/ipython'
else:
cmd = 'ipython'
ipy = subprocess.Popen([cmd, 'kernel', '--pylab=inline'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, env=os.environ)
ipy = subprocess.Popen([cmd, 'kernel', '--pylab=inline'],
stdout=subprocess.PIPE, stderr=subprocess.STDOUT, env=os.environ)
# Start a thread listening to stdout
t = threading.Thread(target=listenIPy)
t.start()
Expand All @@ -228,6 +244,7 @@ def startIPy(opts):
disconnected()
return None


def killIPy():
global ipy
try:
Expand All @@ -238,6 +255,7 @@ def killIPy():
except:
pass


def request(cur):
id = send(cur["code"])
requests[id] = cur
Loading