Commit 2eccdf19 authored by Iliya Manolov's avatar Iliya Manolov Committed by Eteri

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 nexedi/erp5!278
parent e5af505b
......@@ -298,6 +298,20 @@ def Base_runJupyterCode(self, jupyter_code, old_notebook_context):
print_fixer = PrintFixer()
environment_collector = EnvironmentParser()
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.fix_missing_locations(ast_node)
......@@ -759,6 +773,7 @@ class ImportFixer(ast.NodeTransformer):
def __init__(self):
self.import_func_dict = {}
self.warning_module_names = []
def visit_FunctionDef(self, node):
"""
......@@ -878,8 +893,8 @@ class ImportFixer(ast.NodeTransformer):
empty_function.body = [node, return_dict]
environment_set = self.newEnvironmentSetCall("%s_setup" %result_name)
warning = self.newImportWarningCall(root_module_name, result_name)
return [empty_function, environment_set, warning]
self.newImportWarningCall(root_module_name, result_name)
return [empty_function, environment_set]
else:
return node
......@@ -914,18 +929,10 @@ class ImportFixer(ast.NodeTransformer):
def newImportWarningCall(self, module_name, function_name):
"""
Return an AST.Expr representanting a print statement with a warning to an
user about the import of a module named `module_name` and instructs him
on how to fix it.
Adds a new module to the warning to the user about the importing of new
modules.
"""
warning = ("print '"
"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]
self.warning_module_names.append(module_name)
def renderAsHtml(self, renderable_object):
......
......@@ -882,45 +882,82 @@ print dig
self.assertEquals(result, True)
def testNPArrayPrint(self):
self.login('dev_user')
import_code = '''
def testNPArrayPrint(self):
self.login('dev_user')
import_code = '''
import numpy as np
'''
reference = 'Test.Notebook.EnvironmentObject.Errors.NPArrayTest'
result = self.portal.Base_executeJupyter(
reference=reference,
python_expression=import_code
)
self.tic()
result = json.loads(result)
self.assertEquals(result['status'], 'ok')
jupyter_code = '''
reference = 'Test.Notebook.EnvironmentObject.Errors.NPArrayTest'
result = self.portal.Base_executeJupyter(
reference=reference,
python_expression=import_code
)
self.tic()
result = json.loads(result)
self.assertEquals(result['status'], 'ok')
jupyter_code = '''
print np.random.rand(256, 256, 256)
'''
result = self.portal.Base_executeJupyter(
reference=reference,
python_expression=jupyter_code
)
self.tic()
result = self.portal.Base_executeJupyter(
reference=reference,
python_expression=jupyter_code
)
self.tic()
result = json.loads(result)
self.assertEquals(result['status'], 'ok')
result = json.loads(result)
self.assertEquals(result['status'], 'ok')
jupyter_code = '''
jupyter_code = '''
print np.random.randint(low = 2 ** 63 - 1, size = (256, 256, 256), dtype = 'int64')
'''
result = self.portal.Base_executeJupyter(
reference=reference,
python_expression=jupyter_code
)
self.tic()
result = self.portal.Base_executeJupyter(
reference=reference,
python_expression=jupyter_code
)
self.tic()
result = json.loads(result)
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')
result = json.loads(result)
self.assertEquals(result['status'], 'ok')
self.assertEquals(result['code_result'].strip(), expected_result)
\ No newline at end of file
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