Skip to content

Commit

Permalink
Merge pull request #226 from kikocorreoso/update_ipy
Browse files Browse the repository at this point in the history
Update ipy_id option functionality of brython function
  • Loading branch information
PierreQuentel committed Jul 1, 2015
2 parents 1311ddb + 5bbd062 commit 0409a85
Showing 1 changed file with 153 additions and 97 deletions.
250 changes: 153 additions & 97 deletions www/src/py2js.js
Original file line number Diff line number Diff line change
Expand Up @@ -6948,103 +6948,159 @@ function brython(options){

// Get all scripts with type = text/python or text/python3 and run them

var first_script = true, module_name
for(var $i=0;$i<$elts.length;$i++){
var $elt = $elts[$i]
if($elt.type=="text/python"||$elt.type==="text/python3"){

if($elt.id){module_name=$elt.id}
else if(first_script){module_name='__main__'; first_script=false}
else{module_name = '__main__'+$B.UUID()}

// Get Python source code
var $src = null
if($elt.src){
// format <script type="text/python" src="python_script.py">
// get source code by an Ajax call
if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
var $xmlhttp=new XMLHttpRequest();
}else{// code for IE6, IE5
var $xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
$xmlhttp.onreadystatechange = function(){
var state = this.readyState
if(state===4){
$src = $xmlhttp.responseText
}
}
$xmlhttp.open('GET',$elt.src,false)
$xmlhttp.send()
if($xmlhttp.status != 200){
var msg = "can't open file '"+$elt.src
msg += "': No such file or directory"
console.log(msg)
return
}
$B.$py_module_path[module_name]=$elt.src
var $src_elts = $elt.src.split('/')
$src_elts.pop()
var $src_path = $src_elts.join('/')
if ($B.path.indexOf($src_path) == -1) {
// insert in first position : folder /Lib with built-in modules
// should be the last used when importing scripts
$B.path.splice(0,0,$src_path)
}
}else{
// Get source code inside the script element
var $src = ($elt.innerHTML || $elt.textContent)
$B.$py_module_path[module_name] = $href
}

try{
// Conversion of Python source code to Javascript

var $root = $B.py2js($src,module_name,module_name,'__builtins__')
//earney
var $js = $root.to_js()
if($B.debug>1) console.log($js)

if ($B.async_enabled) {
$js = $B.execution_object.source_conversion($js)

//console.log($js)
eval($js)
} else {
// Run resulting Javascript
eval($js)
}

}catch($err){
if($B.debug>1){
console.log($err)
for(var attr in $err){
console.log(attr+' : ', $err[attr])
}
}

// If the error was not caught by the Python runtime, build an
// instance of a Python exception
if($err.$py_error===undefined){
console.log('Javascript error', $err)
//console.log($js)
//for(var attr in $err){console.log(attr+': '+$err[attr])}
$err=_b_.RuntimeError($err+'')
}

// Print the error traceback on the standard error stream
var $trace = _b_.getattr($err,'info')+'\n'+$err.__name__+
': ' +$err.args
try{
_b_.getattr($B.stderr,'write')($trace)
}catch(print_exc_err){
console.log($trace)
}
// Throw the error to stop execution
throw $err
}

}
}
var first_script = true, module_name;
if(options.ipy_id!==undefined){
module_name='__main__';
var $src = "";
$B.$py_module_path[module_name] = $href;
for(var $i=0;$i<$elts.length;$i++){
var $elt = $elts[$i];
$src += ($elt.innerHTML || $elt.textContent);
}
try{
// Conversion of Python source code to Javascript

var $root = $B.py2js($src,module_name,module_name,'__builtins__')
//earney
var $js = $root.to_js()
if($B.debug>1) console.log($js)

if ($B.async_enabled) {
$js = $B.execution_object.source_conversion($js)

//console.log($js)
eval($js)
} else {
// Run resulting Javascript
eval($js)
}

}catch($err){
if($B.debug>1){
console.log($err)
for(var attr in $err){
console.log(attr+' : ', $err[attr])
}
}

// If the error was not caught by the Python runtime, build an
// instance of a Python exception
if($err.$py_error===undefined){
console.log('Javascript error', $err)
//console.log($js)
//for(var attr in $err){console.log(attr+': '+$err[attr])}
$err=_b_.RuntimeError($err+'')
}

// Print the error traceback on the standard error stream
var $trace = _b_.getattr($err,'info')+'\n'+$err.__name__+
': ' +$err.args
try{
_b_.getattr($B.stderr,'write')($trace)
}catch(print_exc_err){
console.log($trace)
}
// Throw the error to stop execution
throw $err
}
}else{
for(var $i=0;$i<$elts.length;$i++){
var $elt = $elts[$i]
if($elt.type=="text/python"||$elt.type==="text/python3"){

if($elt.id){module_name=$elt.id}
else if(first_script){module_name='__main__'; first_script=false}
else{module_name = '__main__'+$B.UUID()}

// Get Python source code
var $src = null
if($elt.src){
// format <script type="text/python" src="python_script.py">
// get source code by an Ajax call
if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
var $xmlhttp=new XMLHttpRequest();
}else{// code for IE6, IE5
var $xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
$xmlhttp.onreadystatechange = function(){
var state = this.readyState
if(state===4){
$src = $xmlhttp.responseText
}
}
$xmlhttp.open('GET',$elt.src,false)
$xmlhttp.send()
if($xmlhttp.status != 200){
var msg = "can't open file '"+$elt.src
msg += "': No such file or directory"
console.log(msg)
return
}
$B.$py_module_path[module_name]=$elt.src
var $src_elts = $elt.src.split('/')
$src_elts.pop()
var $src_path = $src_elts.join('/')
if ($B.path.indexOf($src_path) == -1) {
// insert in first position : folder /Lib with built-in modules
// should be the last used when importing scripts
$B.path.splice(0,0,$src_path)
}
}else{
// Get source code inside the script element
var $src = ($elt.innerHTML || $elt.textContent)
$B.$py_module_path[module_name] = $href
}

try{
// Conversion of Python source code to Javascript

var $root = $B.py2js($src,module_name,module_name,'__builtins__')
//earney
var $js = $root.to_js()
if($B.debug>1) console.log($js)

if ($B.async_enabled) {
$js = $B.execution_object.source_conversion($js)

//console.log($js)
eval($js)
} else {
// Run resulting Javascript
eval($js)
}

}catch($err){
if($B.debug>1){
console.log($err)
for(var attr in $err){
console.log(attr+' : ', $err[attr])
}
}

// If the error was not caught by the Python runtime, build an
// instance of a Python exception
if($err.$py_error===undefined){
console.log('Javascript error', $err)
//console.log($js)
//for(var attr in $err){console.log(attr+': '+$err[attr])}
$err=_b_.RuntimeError($err+'')
}

// Print the error traceback on the standard error stream
var $trace = _b_.getattr($err,'info')+'\n'+$err.__name__+
': ' +$err.args
try{
_b_.getattr($B.stderr,'write')($trace)
}catch(print_exc_err){
console.log($trace)
}
// Throw the error to stop execution
throw $err
}

}
}
}

/* Uncomment to check the names added in global Javascript namespace
var kk1 = Object.keys(window)
Expand Down

0 comments on commit 0409a85

Please sign in to comment.