Commit 3fa7dd84 authored by Ivan Tyagov's avatar Ivan Tyagov

Try to import module before it is added to environment this way if user tries...

Try to import module before it is added to environment this way if user tries to import non existent module Exception is immediately raised and doesn't block next Jupyter cell execution.
parent caddd8ce
......@@ -15,6 +15,7 @@ import json
import transaction
import Acquisition
import astor
from Products.ERP5Type.Log import log
def Base_executeJupyter(self, python_expression=None, reference=None, \
title=None, request_reference=False, **kw):
......@@ -668,9 +669,32 @@ class ImportFixer(ast.NodeTransformer):
with the user context.
"""
module_name = node.names[0].name
test_import_string = None
if getattr(node, "module", None) is not None:
# case when 'from <module_name> import <something>'
test_import_string = "from %s import %s" %(node.module, module_name)
# XXX: handle sub case when "from <module_name> import *"
#if module_name == '*':
# module_name = '%s_ALL' %(node.module)
#else:
# module_name = '%s_%s' %(node.module, module_name)
if getattr(node.names[0], 'asname'):
# case when 'import <module_name> as <name>'
module_name = node.names[0].asname
test_import_string = "import %s as %s" %(node.names[0].name, module_name)
if test_import_string is None:
# case 'import <module_name>
test_import_string = "import %s" %node.names[0].name
#log('%s : %s' %(module_name, test_import_string))
if not self.import_func_dict.get(module_name):
# try to import module before it is added to environment
# this way if user tries to import non existent module Exception
# is immediately raised and doesn't block next Jupyter cell execution
exec(test_import_string)
empty_function = self.newEmptyFunction("%s_setup" % module_name)
return_dict = self.newReturnDict(module_name)
empty_function.body = [node, return_dict]
......@@ -692,7 +716,7 @@ class ImportFixer(ast.NodeTransformer):
"""
Return an AST.Expr representing a returned dict with one single key named
`'module_name'` (as string) which returns the variable `module_name` (as
exoression).
expression).
"""
return_dict = "return {'%s': %s}" % (module_name, module_name)
return ast.parse(return_dict).body[0]
......
......@@ -46,11 +46,13 @@
<key> <string>text_content_warning_message</string> </key>
<value>
<tuple>
<string>W:301, 10: Use of exec (exec-used)</string>
<string>W:347, 10: Use of exec (exec-used)</string>
<string>W:359, 10: Use of exec (exec-used)</string>
<string>W:443, 6: No exception type(s) specified (bare-except)</string>
<string>W:899, 2: Redefining name \'IFrame\' from outer scope (line 4) (redefined-outer-name)</string>
<string>W:302, 10: Use of exec (exec-used)</string>
<string>W:348, 10: Use of exec (exec-used)</string>
<string>W:360, 10: Use of exec (exec-used)</string>
<string>W:444, 6: No exception type(s) specified (bare-except)</string>
<string>W:697, 6: Use of exec (exec-used)</string>
<string>W:923, 2: Redefining name \'IFrame\' from outer scope (line 4) (redefined-outer-name)</string>
<string>W: 18, 0: Unused log imported from Products.ERP5Type.Log (unused-import)</string>
</tuple>
</value>
</item>
......
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