Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added autoprint feature #52

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
76 changes: 75 additions & 1 deletion jupyter_micropython_kernel/kernel.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from ipykernel.kernelbase import Kernel
import IPython

import logging, sys, time, os, re
import logging, sys, time, os, re, ast, traceback
import serial, socket, serial.tools.list_ports, select
import websocket # only for WebSocketConnectionClosedException
from . import deviceconnector
Expand Down Expand Up @@ -84,6 +84,46 @@
ap_writefilepc.add_argument('--append', '-a', action='store_true')
ap_writefilepc.add_argument('--execute', '-x', action='store_true')
ap_writefilepc.add_argument('destinationfilename', type=str)


def add_print(expr):
"""
surrounds an expression A with a print call that does not print if A is None:
print((lambda expr: expr if expr is not None else '')(A), end='')
"""
return ast.Expr(
value=ast.Call(
func=ast.Name(
id='print',
ctx=ast.Load()
),
args=[ast.Call(
func=ast.Lambda(
args=ast.arguments(
posonlyargs=[],
args=[ast.arg(arg='expr')],
kwonlyargs=[],
kw_defaults=[],
defaults=[]
),
body=ast.IfExp(
test=ast.Compare(
left=ast.Name(
id='expr',
ctx=ast.Load()
),
ops=[ast.IsNot()],
comparators=[ast.Constant(value=None)]
),
body=ast.Name(id='expr', ctx=ast.Load()),
orelse=ast.Constant(value='')
)
),
args=[expr],
keywords=[])],
keywords=[ast.keyword(arg='end', value=ast.Constant(value=''))]
)
)


def parseap(ap, percentstringargs1):
Expand Down Expand Up @@ -141,6 +181,8 @@ def __init__(self, **kwargs):
self.srescapturedoutputfile = None # used by %capture command
self.srescapturedlinecount = 0
self.srescapturedlasttime = 0 # to control the frequency of capturing reported

self.autoprint = False # surround last ast.Node with print() if ast.Expression


def interpretpercentline(self, percentline, cellcontents):
Expand Down Expand Up @@ -456,10 +498,42 @@ def sendtofile(filename, contents):
return cellcontents # allows for repeat %sendtofile in same cell


if percentcommand == "%setautoprint":
self.autoprint = True
return cellcontents.strip() and cellcontents or None


if percentcommand == "%setnoautoprint":
self.autoprint = False
return cellcontents.strip() and cellcontents or None


self.sres("Unrecognized percentline {}\n".format([percentline]), 31)
return cellcontents

def runnormalcell(self, cellcontents, bsuppressendcode):
if self.autoprint:
try:
parsed = ast.parse(cellcontents)

if isinstance(parsed.body[-1], ast.Expr):
parsed.body[-1] = add_print(parsed.body[-1])

cellcontents = ast.unparse(parsed)

except SyntaxError as e:
self.sres(
'A syntax error was found in cell. Code not sent to device.\n' +
'If you feel that micropython should accept the cell content ' +
'turn off autoprint.\n\nError was:\n',
n04count=1
)
self.sres(
traceback.format_exc(),
n04count=1
)
return

cmdlines = cellcontents.splitlines(True)
r = self.dc.workingserialreadall()
if r:
Expand Down