Commit d52666e7 authored by Ayush Tiwari's avatar Ayush Tiwari

erp5_data_notebook bt5: Handle case for jupyter_code as comments , giving empty nodelist

parent e5184879
...@@ -58,7 +58,7 @@ def Base_compileJupyterCode(self, jupyter_code, old_local_variable_dict): ...@@ -58,7 +58,7 @@ def Base_compileJupyterCode(self, jupyter_code, old_local_variable_dict):
# Saving the initial globals dict so as to compare it after code execution # Saving the initial globals dict so as to compare it after code execution
globals_dict = globals() globals_dict = globals()
g['context'] = self g['context'] = self
result_string = None result_string = ''
ename, evalue, tb_list = None, None, None ename, evalue, tb_list = None, None, None
# Update globals dict and use it while running exec command # Update globals dict and use it while running exec command
g.update(old_local_variable_dict['variables']) g.update(old_local_variable_dict['variables'])
...@@ -69,9 +69,18 @@ def Base_compileJupyterCode(self, jupyter_code, old_local_variable_dict): ...@@ -69,9 +69,18 @@ def Base_compileJupyterCode(self, jupyter_code, old_local_variable_dict):
# TODO: This can be refactored by using client side error handling instead of # TODO: This can be refactored by using client side error handling instead of
# catching errors on server/erp5. # catching errors on server/erp5.
status = u'ok' status = u'ok'
local_variable_dict = old_local_variable_dict
# Execute only if jupyter_code is not empty # Execute only if jupyter_code is not empty
if jupyter_code: if jupyter_code:
# Create ast parse tree
ast_node = ast.parse(jupyter_code)
# Get the node list from the parsed tree
nodelist = ast_node.body
# Handle case for empty nodelist(in case of comments as jupyter_code)
if nodelist:
# Import all the modules from local_variable_dict['imports'] # Import all the modules from local_variable_dict['imports']
# While any execution, in locals() dict, a module is saved as: # While any execution, in locals() dict, a module is saved as:
# code : 'from os import path' # code : 'from os import path'
...@@ -81,11 +90,6 @@ def Base_compileJupyterCode(self, jupyter_code, old_local_variable_dict): ...@@ -81,11 +90,6 @@ def Base_compileJupyterCode(self, jupyter_code, old_local_variable_dict):
import_statement_code = 'import %s as %s'%(v, k) import_statement_code = 'import %s as %s'%(v, k)
exec(import_statement_code, g, g) exec(import_statement_code, g, g)
# Create ast parse tree
ast_node = ast.parse(jupyter_code)
# Get the node list from the parsed tree
nodelist = ast_node.body
# If the last node is instance of ast.Expr, set its interactivity as 'last' # If the last node is instance of ast.Expr, set its interactivity as 'last'
# This would be the case if the last node is expression # This would be the case if the last node is expression
if isinstance(nodelist[-1], ast.Expr): if isinstance(nodelist[-1], ast.Expr):
...@@ -125,13 +129,10 @@ def Base_compileJupyterCode(self, jupyter_code, old_local_variable_dict): ...@@ -125,13 +129,10 @@ def Base_compileJupyterCode(self, jupyter_code, old_local_variable_dict):
sys.stdout = old_stdout sys.stdout = old_stdout
result_string = result.getvalue() result_string = result.getvalue()
else:
result_string = jupyter_code
# Difference between the globals variable before and after exec/eval so that # Difference between the globals variable before and after exec/eval so that
# we don't have to save unnecessary variables in database which might or might # we don't have to save unnecessary variables in database which might or might
# not be picklabale # not be picklabale
local_variable_dict = old_local_variable_dict
local_variable_dict_new = {key: val for key, val in g.items() if key not in globals_dict.keys()} local_variable_dict_new = {key: val for key, val in g.items() if key not in globals_dict.keys()}
local_variable_dict['variables'].update(local_variable_dict_new) local_variable_dict['variables'].update(local_variable_dict_new)
......
...@@ -47,10 +47,10 @@ ...@@ -47,10 +47,10 @@
<value> <value>
<tuple> <tuple>
<string>W: 50, 2: Using the global statement (global-statement)</string> <string>W: 50, 2: Using the global statement (global-statement)</string>
<string>W: 82, 6: Use of exec (exec-used)</string> <string>W: 91, 8: Use of exec (exec-used)</string>
<string>W:111, 6: Use of exec (exec-used)</string> <string>W:115, 8: Use of exec (exec-used)</string>
<string>W:117, 6: Use of exec (exec-used)</string> <string>W:121, 8: Use of exec (exec-used)</string>
<string>W:212, 4: Using the global statement (global-statement)</string> <string>W:213, 4: Using the global statement (global-statement)</string>
</tuple> </tuple>
</value> </value>
</item> </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