def generic_translate_python(src, lib, lang_import, highlight, pre_code='', post_code=''): ''' Translate Python code into Javascript and execute src: source code in editor lib: string - language specific lib (e.g. "library" in English, "biblio" in French) already imported in html file lang_import: something like "from reeborg_en import *" ''' globals_ = {} exec(lang_import, globals_) if highlight: temp_src, problem = insert_highlight_info(src) if hasattr(window.RUR, "__debug"): window.console.log(temp_src) if not problem: src = temp_src else: exec("RUR.ui.highlight('{}')".format(problem), globals_) window.jQuery("#highlight-impossible").show() src = pre_code + "\n" + src + "\n" + post_code exec(src, globals_) if lib in sys.modules: del sys.modules[lib]
def generic_translate_python(src, lib, lang_import, highlight): ''' Translate Python code into Javascript and execute src: source code in editor lib: language specific lib (e.g. my_lib in English, biblio in French) already imported in html file lang_import: something like "from reeborg_en import *" ''' # save initial state of lib initial_lib_dict = {} for key in lib.__dict__: initial_lib_dict[key] = lib.__dict__[key] exec(library.getValue(), lib.__dict__) exec(lang_import) if highlight: src = insert_highlight_info(src) exec(src) # remove added definitions new_keys = [] for key in lib.__dict__: if key not in initial_lib_dict: new_keys.append(key) else: lib.__dict__[key] = initial_lib_dict[key] for key in new_keys: del lib.__dict__[key]
def generic_translate_python(src, lib, lang_import, highlight, pre_code='', post_code=''): ''' Translate Python code into Javascript and execute src: source code in editor lib: string - language specific lib (e.g. "library" in English, "biblio" in French) already imported in html file lang_import: something like "from reeborg_en import *" ''' if lib in sys.modules: del sys.modules[lib] globals_ = {} globals_.update(globals()) globals_['dir_py'] = dir_py globals_['Help'] = Help src = transform(src) exec(lang_import, globals_) if highlight: temp_src, problem = insert_highlight_info(src) if not problem: src = temp_src else: exec("RUR.ui.highlight('{}')".format(problem), globals_) window.jQuery("#highlight-impossible").show() if hasattr(window.RUR, "__debug"): window.console.log("processed source:") window.console.log(src) src = "help=Help\n" + pre_code + "\n" + src + "\n" + post_code exec(src, globals_)
def generic_translate_python(src, lib, lang_import, highlight, var_watch, pre_code='', post_code=''): ''' Translate Python code into Javascript and execute src: source code in editor lib: string - language specific lib (e.g. "library" in English, "biblio" in French) already imported in html file lang_import: something like "from reeborg_en import *" ''' sys.stdout.write = __write sys.stderr.write = __write_err if lib in sys.modules: del sys.modules[lib] globals_ = {} globals_.update(globals()) globals_['dir_py'] = dir_py globals_['Help'] = Help globals_['_watch_'] = _watch_ globals_['_v_'] = None globals_['previous_watch_values'] = {} src = transform(src) exec(lang_import, globals_) # globals_['system_default_vars'] = set([key for key in globals_]) if highlight or var_watch: try: temp_src, problem = insert_highlight_info(src, highlight=highlight, var_watch=var_watch) if not problem: src = temp_src else: window.RUR.ui.highlight() window.jQuery("#highlight-impossible").show() except Exception as e: window.RUR.__python_error = e window.console.log("problem with hightlight:", e) return if hasattr(window.RUR, "__debug"): window.console.log("processed source:") window.console.log(src) # include v again to reset its value _v_ = "system_default_vars = set(locals().keys())\n" src = "help=Help\n" + pre_code + "\n" + _v_ + src + "\n" + post_code try: exec(src, globals_) except Exception as e: window.RUR.__python_error = e
def generic_translate_python(src, highlight, var_watch, pre_code='', post_code=''): ''' RUR.translate Python code into Javascript and execute src: source code in editor highlight: determines if the code will be highlighted as it is run var_watch: determines if some variable watch will take place pre_code: code included with world definition and prepended to user code post_code: code included with world definition and appended to user code ''' # lib: string - language specific lib # (e.g. "library" in English, "biblio" in French) # already imported in html file lib = window.RUR.library_name # lang_import: something like "from reeborg_en import *" lang_import = window.RUR.from_import sys.stdout.write = __write sys.stderr.write = __write_err if lib in sys.modules: del sys.modules[lib] globals_ = {} globals_.update(globals()) globals_['dir_py'] = dir_py globals_['Help'] = Help globals_['_watch_'] = _watch_ globals_['_v_'] = None globals_['previous_watch_values'] = {} src = transform(src) exec(lang_import, globals_) # globals_['system_default_vars'] = set([key for key in globals_]) if highlight or var_watch: try: temp_src, problem = insert_highlight_info(src, highlight=highlight, var_watch=var_watch) if not problem: src = temp_src else: window.RUR.toggle_highlight() window.jQuery("#highlight-impossible").show() except Exception as e: window.RUR.__python_error = e window.console.log("problem with hightlight:", e) return if hasattr(window.RUR, "__debug"): window.console.log("processed source:") window.console.log(src) # include v again to reset its value _v_ = "system_default_vars = set(locals().keys())\n" src = "help=Help\n" + pre_code + "\n" + _v_ + src + "\n" + post_code try: exec(src, globals_) except Exception as e: window.RUR.__python_error = e
def generic_translate_python(src, lib, lang_import, highlight, pre_code='', post_code=''): ''' Translate Python code into Javascript and execute src: source code in editor lib: string - language specific lib (e.g. "library" in English, "biblio" in French) already imported in html file lang_import: something like "from reeborg_en import *" ''' sys.stdout.write = __write sys.stderr.write = __write_err if lib in sys.modules: del sys.modules[lib] globals_ = {} globals_.update(globals()) globals_['dir_py'] = dir_py globals_['Help'] = Help globals_['__watch'] = __watch globals_['__v'] = None globals_['previous_watch_values'] = {} src = transform(src) exec(lang_import, globals_) # globals_['system_default_vars'] = set([key for key in globals_]) if highlight: try: temp_src, problem = insert_highlight_info(src) if not problem: src = temp_src else: window.RUR.ui.highlight() window.jQuery("#highlight-impossible").show() except Exception as e: window.RUR.__python_error = e window.console.log("problem with hightlight:", e) return if hasattr(window.RUR, "__debug"): window.console.log("processed source:") window.console.log(src) # include v again to reset its value __v = "system_default_vars = set(locals().keys())\n" src = "help=Help\n" + pre_code + "\n" + __v + src + "\n" + post_code try: exec(src, globals_) except Exception as e: window.RUR.__python_error = e
def generic_translate_python(src, lib, lang_import, highlight, pre_code='', post_code=''): ''' Translate Python code into Javascript and execute src: source code in editor lib: language specific lib (e.g. my_lib in English, biblio in French) already imported in html file lang_import: something like "from reeborg_en import *" ''' globals_ = {} # save initial state of lib initial_lib_dict = {} for key in lib.__dict__: initial_lib_dict[key] = lib.__dict__[key] exec(library.getValue(), lib.__dict__) exec(lang_import, globals_) if highlight: temp_src, success = insert_highlight_info(src) if success: src = temp_src else: exec("RUR.ui.highlight()", globals_) window.jQuery("#highlight-impossible").show() src = pre_code + "\n" + src + "\n" + post_code exec(src, globals_) # remove added definitions new_keys = [] for key in lib.__dict__: if key not in initial_lib_dict: new_keys.append(key) else: lib.__dict__[key] = initial_lib_dict[key] for key in new_keys: del lib.__dict__[key]
def test_del(self): result = highlight.insert_highlight_info(test_del) self.assertEqual(test_del_result, result)
def test_continue(self): result = highlight.insert_highlight_info(test_continue) self.assertEqual(test_continue_result, result)
def test_try_except_finally(self): result = highlight.insert_highlight_info(test_try_except_finally) self.assertEqual(test_try_except_finally_result, result)
def test_class(self): result = highlight.insert_highlight_info(test_class) self.assertEqual(test_class_result, result)
def test_if(self): result = highlight.insert_highlight_info(test_if) self.assertEqual(test_if_result, result)
def test_while(self): result, _ = highlight.insert_highlight_info(test_while) self.assertEqual(test_while_result, result)
def test_single_move(self): result = highlight.insert_highlight_info(test_single_move) self.assertEqual(test_single_move_result, result)
def test_for(self): result = highlight.insert_highlight_info(test_for) self.assertEqual(test_for_result, result)
def test_single_assignment(self): result = highlight.insert_highlight_info(test_single_assignment_1) self.assertEqual(test_single_assignment_1_result, result) result = highlight.insert_highlight_info(test_single_assignment_2) self.assertEqual(test_single_assignment_2_result, result)
def test_if_elif_else(self): result, _ = highlight.insert_highlight_info(test_if_elif_else) self.assertEqual(test_if_elif_else_result, result)
def test_raise(self): result = highlight.insert_highlight_info(test_raise) self.assertEqual(test_raise_result, result)
def test_break(self): # test comments as well result = highlight.insert_highlight_info(test_break) self.assertEqual(test_break_result, result)
def test_yield(self): result = highlight.insert_highlight_info(test_yield) self.assertEqual(test_yield_result, result)
def generic_translate_python(src, highlight=False, var_watch=False, pre_code='', post_code=''): ''' RUR.translate Python code into Javascript and execute src: source code in editor highlight: determines if the code will be highlighted as it is run var_watch: determines if some variable watch will take place pre_code: code included with world definition and prepended to user code post_code: code included with world definition and appended to user code ''' # lib: string - language specific lib # (e.g. "library" in English, "biblio" in French) # already imported in html file lib = window.RUR.library_name # lang_import: something like "from reeborg_en import *" lang_import = window.RUR.from_import sys.stdout.write = __write sys.stderr.write = __write_err if lib in sys.modules: del sys.modules[lib] globals_ = {} globals_.update(globals()) globals_['dir_py'] = dir_py globals_['Help'] = Help globals_['_watch_'] = _watch_ globals_['_v_'] = None globals_['previous_watch_values'] = {} src = transform(src) # sometimes, when copying from documentation displayed in the browsers # some nonbreaking spaces are inserted instead of regular spaces. # We make the assumption that nonbreaking spaces should never appear # in source code - which is not necessarily valid... if '\xa0' in src: src = src.replace('\xa0', ' ') window.console.warn("Some nonbreaking spaces were replaced in the Python code.") exec(lang_import, globals_) # globals_['system_default_vars'] = set([key for key in globals_]) if highlight or var_watch: try: temp_src, problem = insert_highlight_info(src, highlight=highlight, var_watch=var_watch) if not problem: src = temp_src else: window.RUR.toggle_highlight() window.jQuery("#highlight-impossible").show() except Exception as e: window.RUR.__python_error = e window.console.log("problem with hightlight:", e) return if hasattr(window.RUR, "__debug"): window.console.log("processed source:") window.console.log(src) # include v again to reset its value _v_ = "system_default_vars = set(locals().keys())\n" src = "help=Help\n" + pre_code + "\n" + _v_ + src + "\n" + post_code try: exec(src, globals_) except Exception as e: window.RUR.__python_error = e
def test_from_import(self): result = highlight.insert_highlight_info(test_from_import) self.assertEqual(test_from_import_result, result)
def test_return(self): result = highlight.insert_highlight_info(test_return) self.assertEqual(test_return_result, result)
def __generic_translate_python(src, highlight=False, var_watch=False, pre_code='', post_code=''): ''' RUR.translate Python code into Javascript and execute src: source code in editor highlight: determines if the code will be highlighted as it is run var_watch: determines if some variable watch will take place pre_code: code included with world definition and prepended to user code post_code: code included with world definition and appended to user code ''' from preprocess import transform # keeping out of global namespace from highlight import insert_highlight_info sys.stdout.write = __write sys.stderr.write = __write # reeborg_en and reeborg_fr define some attributes to window; these # could have been redefined when importing a different language version - # or, perhas even when running a Javascript version; so it # is important to ensure that they have their proper definition by forcing # a fresh import each time such a request is made via something like # from reeborg_en import * # Similarly, library or biblio's content might have changed by the user # since the program was run last time for mod in ["reeborg_en", "reeborg_fr", "library", "biblio", "extra"]: if mod in sys.modules: del sys.modules[mod] globals_ = {} globals_['__help'] = __help globals_['__watch'] = __watch globals_['__previous_watch_values'] = {} globals_['window'] = window globals_['console'] = console globals_['print_dir'] = print_dir src = transform(src) # sometimes, when copying from documentation displayed in the browsers # some nonbreaking spaces are inserted instead of regular spaces. # We make the assumption that nonbreaking spaces should never appear # in source code - which is not necessarily valid... if '\xa0' in src: src = src.replace('\xa0', ' ') window.console.warn( "Some nonbreaking spaces were replaced in the Python code.") # Notwithstanding what is writte above regarding fresh imports, # we simulate this here by doing a dict update, thus effectively using a # cached version of a previous import while ensuring that and # global ("window") definition is done properly. if window.RUR.from_import == "from reeborg_en import *": globals_.update(__REEBORG_EN) elif window.RUR.from_import == "from reeborg_fr import *": globals_.update(__REEBORG_FR) else: raise Exception("unknown import %s" % window.RUR.from_import) if highlight or var_watch: try: temp_src, problem = insert_highlight_info(src, highlight=highlight, var_watch=var_watch) if not problem: src = temp_src else: window.RUR.toggle_highlight() window.jQuery("#highlight-impossible").show() except Exception as e: window.RUR.__python_error = e window.console.log("problem with hightlight:", e) return if hasattr(window.RUR, "__debug"): window.console.log("processed source:") window.console.log(src) if var_watch: system_vars = "system_default_vars = set(locals().keys())\n" else: system_vars = "\n" src = "help=__help\n" + pre_code + "\n" + system_vars + src + "\n" + post_code try: exec(src, globals_) except Exception as e: window.RUR.__python_error = e
def test_global_nonlocal(self): result = highlight.insert_highlight_info(test_global_nonlocal) self.assertEqual(test_global_nonlocal_result, result)
def test_with(self): result = highlight.insert_highlight_info(test_with) self.assertEqual(test_with_result, result)