Skip to content

Commit

Permalink
Merge pull request #24 from ibm-watson-data-lab/david-gateway-work
Browse files Browse the repository at this point in the history
#14 url parameters
  • Loading branch information
David Taieb authored Jan 28, 2018
2 parents 7be7976 + e3d4cd9 commit 886806d
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 8 deletions.
11 changes: 10 additions & 1 deletion pixiegateway/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,18 @@ def get(self, *args, **kwargs):
managed_client = yield self.session.get_managed_client(self, pixieapp_def, True)
if pixieapp_def is not None:
yield pixieapp_def.warmup(managed_client)
query = self.request.query.strip()
metadata = dict(
[tuple(parts) if len(parts)>1 else (parts[0], "")
for group in query.split("&")
for parts in [group.split("=")]
]
) if query else None
print("path is {}".format(metadata))
code = pixieapp_def.get_run_code(
self.session,
self.session.get_pixieapp_run_id(self, pixieapp_def)
self.session.get_pixieapp_run_id(self, pixieapp_def),
app_metadata = metadata
)
else:
instance_name = self.session.getInstanceName(clazz)
Expand Down
32 changes: 28 additions & 4 deletions pixiegateway/notebookMgr.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,9 +268,9 @@ def warmup(self, managed_client):
raise exc
raise gen.Return(warmup_future)

def get_run_code(self, session, run_id):
def get_run_code(self, session, run_id, app_metadata = None):
pars = ast.parse(self.run_code)
vl = RewriteGlobals(get_symbol_table(pars), session.namespace)
vl = RewriteGlobals(get_symbol_table(pars), session.namespace, app_metadata)
vl.visit(pars)
run_code = """
from pixiedust.display.app import pixieapp
Expand Down Expand Up @@ -334,9 +334,10 @@ def generic_visit(self, node):
pass

class RewriteGlobals(ast.NodeTransformer):
def __init__(self, symbols, namespace):
def __init__(self, symbols, namespace, app_metadata = None):
self.symbols = symbols
self.namespace = namespace
self.app_metadata = app_metadata
self.level = 0
self.localTables = []
self.pixieApp = None
Expand Down Expand Up @@ -405,13 +406,16 @@ def assign_namespace(self, root):
setup_def = [func for func in root.body if isinstance(func, ast.FunctionDef) and func.name == 'setup']
setup_def = None if len(setup_def) == 0 else setup_def[0]
if setup_def is None:
body = [self.get_assign_node(self.namespace)]
if self.app_metadata:
body.append(self.get_call_append_metadata_node(self.app_metadata))
setup_node = ast.FunctionDef(
name='setup',
args=ast.arguments(
args=[ast.arg(arg='self', annotation=None) if six.PY3 else ast.Name(id='self', ctx=ast.Param())],
vararg=None, kwarg=None, defaults=[]
),
body=[self.get_assign_node(self.namespace)],
body=body,
decorator_list=[]
)
root.body.append(setup_node)
Expand All @@ -421,13 +425,33 @@ def assign_namespace(self, root):
]
if len(assign_node) == 0:
setup_def.body.append(self.get_assign_node(self.namespace))
if self.app_metadata:
setup_def.body.append(self.get_call_append_metadata_node(self.app_metadata))

def has_target(self, targets, name):
for target in targets:
if isinstance(target, ast.Attribute) and target.attr == name:
return True
return False

def get_call_append_metadata_node(self, metadata):
return ast.Expr(value= ast.Call(
func=ast.Attribute(
value=ast.Name(id='self', ctx=ast.Load()),
attr='append_metadata',
ctx=ast.Load()
),
args=[
ast.Dict(
keys=[ ast.Str(s=key) for key in metadata.keys()],
values=[ ast.Str(s=key) for key in metadata.values()]
)
],
keywords=[],
starargs=None,
kwargs=None
))

def get_assign_node(self, namespace):
return ast.Assign(
targets=[
Expand Down
10 changes: 7 additions & 3 deletions pixiegateway/tests/test_rewrite.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,10 @@ class TestApp""" + classdef + """:
def setup(self):
self.contents = [ns_var1, ns_var2]
self.__pd_gateway_namespace__ = 'ns_'
self.append_metadata({'key1': 'value1'})
TestApp().run()
"""
""",
'metadata': {'key1':'value1'}
},{
"src":"""
from pixiedust.display.app import *
Expand All @@ -116,8 +118,10 @@ class TestAppWithoutSetup""" + classdef + """:
pass
def setup(self):
self.__pd_gateway_namespace__ = 'ns_'
self.append_metadata({'key1': 'value1'})
TestApp().run()
"""
""",
'metadata': {'key1':'value1'}
}
]

Expand All @@ -130,5 +134,5 @@ def compare_multiline(src, target):
def test_rewrite():
for code in code_map:
symbols = get_symbol_table(ast_parse( code['src'].strip() ) )
rewrite_code = astunparse.unparse( RewriteGlobals(symbols, "ns_").visit(ast_parse(code['src'])) )
rewrite_code = astunparse.unparse( RewriteGlobals(symbols, "ns_", code.get('metadata', None)).visit(ast_parse(code['src'])) )
compare_multiline(code["target"].strip(), [l for l in rewrite_code.split('\n') if l.strip() != ""])

0 comments on commit 886806d

Please sign in to comment.