Commit 4bfcce31 authored by Iliya Manolov's avatar Iliya Manolov Committed by Ivan Tyagov

Shortened the warnings on importing new modules in Jupyter to just one

@Tyagov @kirr 

When someone imported a module in Jupyter they would get a warning for every module they imported like:

    WARNING: Your imported from the module numpy without...
    WARNING: Your imported from the module datetime without...
    ...

This fix shortens those to just one warning at the end of output like so:

    WARNING: You imported from the modules numpy, datetime, ... without ...



/reviewed-on !278
parent ba3646b8
...@@ -298,6 +298,20 @@ def Base_runJupyterCode(self, jupyter_code, old_notebook_context): ...@@ -298,6 +298,20 @@ def Base_runJupyterCode(self, jupyter_code, old_notebook_context):
print_fixer = PrintFixer() print_fixer = PrintFixer()
environment_collector = EnvironmentParser() environment_collector = EnvironmentParser()
ast_node = import_fixer.visit(ast_node) ast_node = import_fixer.visit(ast_node)
# Whenever we have new imports we need to warn the user about the
# environment
if (import_fixer.warning_module_names != []):
warning = ("print '"
"WARNING: You imported from the modules %s without "
"using the environment object, which is not recomended. "
"Your import was automatically converted to use such method. "
"The setup functions were named as *module*_setup. "
"'") % (', '.join(import_fixer.warning_module_names))
tree = ast.parse(warning)
tree.body[0].lineno = ast_node.body[-1].lineno+5
ast_node.body.append(tree.body[0])
ast_node = print_fixer.visit(ast_node) ast_node = print_fixer.visit(ast_node)
ast.fix_missing_locations(ast_node) ast.fix_missing_locations(ast_node)
...@@ -759,6 +773,7 @@ class ImportFixer(ast.NodeTransformer): ...@@ -759,6 +773,7 @@ class ImportFixer(ast.NodeTransformer):
def __init__(self): def __init__(self):
self.import_func_dict = {} self.import_func_dict = {}
self.warning_module_names = []
def visit_FunctionDef(self, node): def visit_FunctionDef(self, node):
""" """
...@@ -878,8 +893,8 @@ class ImportFixer(ast.NodeTransformer): ...@@ -878,8 +893,8 @@ class ImportFixer(ast.NodeTransformer):
empty_function.body = [node, return_dict] empty_function.body = [node, return_dict]
environment_set = self.newEnvironmentSetCall("%s_setup" %result_name) environment_set = self.newEnvironmentSetCall("%s_setup" %result_name)
warning = self.newImportWarningCall(root_module_name, result_name) self.newImportWarningCall(root_module_name, result_name)
return [empty_function, environment_set, warning] return [empty_function, environment_set]
else: else:
return node return node
...@@ -914,18 +929,10 @@ class ImportFixer(ast.NodeTransformer): ...@@ -914,18 +929,10 @@ class ImportFixer(ast.NodeTransformer):
def newImportWarningCall(self, module_name, function_name): def newImportWarningCall(self, module_name, function_name):
""" """
Return an AST.Expr representanting a print statement with a warning to an Adds a new module to the warning to the user about the importing of new
user about the import of a module named `module_name` and instructs him modules.
on how to fix it.
""" """
warning = ("print '" self.warning_module_names.append(module_name)
"WARNING: Your imported from the module %s without "
"using the environment object, which is not recomended. "
"Your import was automatically converted to use such method."
"The setup function was named as: %s_setup.\\n"
"'") % (module_name, function_name)
tree = ast.parse(warning)
return tree.body[0]
def renderAsHtml(self, renderable_object): def renderAsHtml(self, renderable_object):
......
...@@ -882,7 +882,7 @@ print dig ...@@ -882,7 +882,7 @@ print dig
self.assertEquals(result, True) self.assertEquals(result, True)
def testNPArrayPrint(self): def testNPArrayPrint(self):
self.login('dev_user') self.login('dev_user')
import_code = ''' import_code = '''
import numpy as np import numpy as np
...@@ -896,7 +896,6 @@ import numpy as np ...@@ -896,7 +896,6 @@ import numpy as np
result = json.loads(result) result = json.loads(result)
self.assertEquals(result['status'], 'ok') self.assertEquals(result['status'], 'ok')
jupyter_code = ''' jupyter_code = '''
print np.random.rand(256, 256, 256) print np.random.rand(256, 256, 256)
''' '''
...@@ -923,4 +922,42 @@ print np.random.randint(low = 2 ** 63 - 1, size = (256, 256, 256), dtype = 'int6 ...@@ -923,4 +922,42 @@ print np.random.randint(low = 2 ** 63 - 1, size = (256, 256, 256), dtype = 'int6
result = json.loads(result) result = json.loads(result)
self.assertEquals(result['status'], 'ok') self.assertEquals(result['status'], 'ok')
def testImportWarning(self):
'''
This test checks the warning output for imports in Jupyter.
'''
self.login('dev_user')
import_code = '''
import numpy as np
import matplotlib.pyplot as plt
import datetime
'''
reference = 'Test.Notebook.EnvironmentObject.Errors.Import'
result = self.portal.Base_executeJupyter(
reference=reference,
python_expression=import_code
)
self.tic()
expected_result = (u'WARNING: You imported from the modules numpy'
u', matplotlib.pyplot, datetime without using the environment'
u' object, which is not recomended. Your import was'
u' automatically converted to use such method. The setup'
u' functions were named as *module*_setup.')
result = json.loads(result)
self.assertEquals(result['status'], 'ok')
self.assertEquals(result['code_result'].strip(), expected_result)
jupyter_code = '''
print np.array([1, 2, 3])
'''
result = self.portal.Base_executeJupyter(
reference=reference,
python_expression=jupyter_code
)
self.tic()
result = json.loads(result)
self.assertEquals(result['status'], 'ok')
self.assertEquals(result['code_result'].strip(), u'[1 2 3]')
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment