__import__ from online prototype based on open()
This commit is contained in:
parent
68e272eb55
commit
a0f5ad5f6b
@ -13,23 +13,25 @@ function awfull_get(url) {
|
||||
}
|
||||
|
||||
function transferFailed(evt) {
|
||||
console.log("callfs: An error occurred while transferring the file '"+window.currentTransfer+"'");
|
||||
console.log("awfull_get: An error occurred while transferring the file '"+window.currentTransfer+"'");
|
||||
window.currentTransferSize = -1 ;
|
||||
}
|
||||
|
||||
function transferCanceled(evt) {
|
||||
console.log("callfs: transfer '"+window.currentTransfer+"' has been canceled by the user.");
|
||||
console.log("awfull_get: transfer '"+window.currentTransfer+"' has been canceled by the user.");
|
||||
window.currentTransferSize = -1 ;
|
||||
}
|
||||
|
||||
var oReq = new XMLHttpRequest();
|
||||
|
||||
function transferComplete(evt) {
|
||||
if (oReq.status==404){
|
||||
console.log("callfs: File not found : "+ url );
|
||||
console.log("awfull_get: File not found : "+ url );
|
||||
window.currentTransferSize = -1 ;
|
||||
|
||||
} else {
|
||||
window.currentTransferSize = oReq.response.length;
|
||||
console.log("callfs: Transfer is complete saving : "+window.currentTransferSize);
|
||||
console.log("awfull_get: Transfer is complete saving : "+window.currentTransferSize);
|
||||
}
|
||||
}
|
||||
|
||||
@ -93,6 +95,10 @@ function wasm_file_open(url, cachefile){
|
||||
}
|
||||
|
||||
var ab = awfull_get(url)
|
||||
|
||||
// is file found and complete ?
|
||||
if (window.currentTransferSize<0)
|
||||
return -1
|
||||
var ret = ab.length
|
||||
|
||||
window.urls.id += 1
|
||||
@ -132,13 +138,19 @@ function wasm_file_exists(url, need_dot) {
|
||||
if (url.endswith('.mpy'))
|
||||
return -1
|
||||
|
||||
|
||||
// are we possibly doing folder checking ?
|
||||
if (need_dot) {
|
||||
|
||||
|
||||
// .mpy is blacklisted for now
|
||||
// so if it's not .py then it's a folder check.
|
||||
if (!url.endswith('.py')) {
|
||||
var found = -1
|
||||
|
||||
// TODO: gain 1 call if .py exists we can discard both __init__ and index checks
|
||||
// -> would need a path cache that is usefull anyway
|
||||
|
||||
// package search
|
||||
found = url_exists( url + '/__init__.py' , 2 )
|
||||
//console.log("wasm_([dir]/file)_exists ? :"+url+ ' --> ' + '/__init__.py => '+found)
|
||||
@ -163,3 +175,4 @@ function wasm_file_exists(url, need_dot) {
|
||||
return url_exists(url, 1)
|
||||
}
|
||||
|
||||
|
||||
|
||||
File diff suppressed because one or more lines are too long
99
ports/javascript/modules/imp.py
Normal file
99
ports/javascript/modules/imp.py
Normal file
@ -0,0 +1,99 @@
|
||||
import sys
|
||||
import builtins
|
||||
import types
|
||||
|
||||
# Deprecated since version 3.4: Use types.ModuleType instead.
|
||||
# but micropython aims toward full 3.4
|
||||
|
||||
# Return a new empty module object called name. This object is not inserted in sys.modules.
|
||||
def new_module(name):
|
||||
return types.ModuleType(name)
|
||||
|
||||
|
||||
|
||||
def load_module(module, *argv):
|
||||
m = new_module(module)
|
||||
if m:
|
||||
import embed
|
||||
file = '/assets/%s.py' % module
|
||||
mroot = module.split('.')[0]
|
||||
m = sys.modules[mroot]
|
||||
runf(file, module=embed.vars(m), patch='\n\n__file__=%r\n' % file )
|
||||
globals()[mroot] = m
|
||||
return m
|
||||
|
||||
try:
|
||||
vars
|
||||
except:
|
||||
#wasm port
|
||||
import embed
|
||||
builtins.vars = embed.vars
|
||||
|
||||
try:
|
||||
vars
|
||||
except:
|
||||
print(""" This could provide a workaround for :\r
|
||||
https://github.com/pmp-p/micropython-ports-wasm/issues/5\r
|
||||
use imp.load_module(modulename) to load modules from /assets/*.py
|
||||
""")
|
||||
raise ImportError("ERROR: this build has no vars() support at all")
|
||||
|
||||
|
||||
# keep the builtin function accessible in this module and from imp.__import__
|
||||
__import__ = __import__
|
||||
|
||||
def importer(name,*argv):
|
||||
global __import__
|
||||
if sys.modules.get(name,None) is None:
|
||||
print("import %s" % name,argv)
|
||||
try:
|
||||
return __import__(name,*argv)
|
||||
except ImportError:
|
||||
pass
|
||||
|
||||
|
||||
file = ':{0}.py'.format(name)
|
||||
print("trying to go online for",file)
|
||||
# todo open the file via open() or raise importerror
|
||||
try:
|
||||
code = open(file,'r').read()
|
||||
except:
|
||||
raise ImportError('module not found')
|
||||
|
||||
#build a empty module
|
||||
mod = types.ModuleType(name)
|
||||
|
||||
mod.__file__ = file
|
||||
|
||||
# compile module from cached file
|
||||
try:
|
||||
code = compile( code, file, 'exec')
|
||||
except Exception as e:
|
||||
sys.print_exception(e)
|
||||
raise
|
||||
|
||||
# execute it in its own empty namespace.
|
||||
ns = vars(mod)
|
||||
|
||||
try:
|
||||
exec( code, ns, ns)
|
||||
except Exception as e:
|
||||
sys.print_exception(e)
|
||||
raise
|
||||
|
||||
# though micropython would normally insert module before executing the whole body
|
||||
# do it after.
|
||||
sys.modules[name] = mod
|
||||
return mod
|
||||
|
||||
|
||||
|
||||
def reload(name):
|
||||
if sys.modules.get(name,None):
|
||||
del sys.modules[name]
|
||||
return importError(name)
|
||||
|
||||
|
||||
# install hook
|
||||
builtins.__import__ = importer
|
||||
print("__import__ is now", importer)
|
||||
0
ports/javascript/modules/imp_empty_pivot_module.py
Normal file
0
ports/javascript/modules/imp_empty_pivot_module.py
Normal file
21
ports/javascript/modules/types.py
Normal file
21
ports/javascript/modules/types.py
Normal file
@ -0,0 +1,21 @@
|
||||
import sys
|
||||
|
||||
|
||||
# not thread safe
|
||||
|
||||
def ModuleType(name):
|
||||
if sys.modules.get('name'):
|
||||
print("Error : module %s exists !"%name)
|
||||
return sys.modules[name]
|
||||
|
||||
# get a new fresh module
|
||||
import imp_empty_pivot_module as pivot
|
||||
# low risk, who would call his module like that ?
|
||||
del sys.modules['imp_empty_pivot_module']
|
||||
|
||||
#still unknown at this time
|
||||
del pivot.__file__
|
||||
|
||||
pivot.__name__ = name
|
||||
|
||||
return pivot
|
||||
@ -217,6 +217,7 @@ static_qstr_list = [
|
||||
"utf-8",
|
||||
"value",
|
||||
"values",
|
||||
"vars",
|
||||
"write",
|
||||
"zip",
|
||||
]
|
||||
|
||||
@ -134,6 +134,15 @@ STATIC mp_obj_t mp_builtin_callable(mp_obj_t o_in) {
|
||||
}
|
||||
MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_callable_obj, mp_builtin_callable);
|
||||
|
||||
#if MICROPY_PY_BUILTINS_EXECFILE
|
||||
STATIC mp_obj_t mp_builtin_vars(mp_obj_t o_in) {
|
||||
mp_obj_t* module_obj = (mp_obj_t*)o_in;
|
||||
mp_obj_dict_t *mod_globals = mp_obj_module_get_globals(module_obj);
|
||||
return mod_globals;
|
||||
}
|
||||
MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_vars_obj, mp_builtin_vars);
|
||||
#endif
|
||||
|
||||
STATIC mp_obj_t mp_builtin_chr(mp_obj_t o_in) {
|
||||
#if MICROPY_PY_BUILTINS_STR_UNICODE
|
||||
mp_uint_t c = mp_obj_get_int(o_in);
|
||||
@ -694,6 +703,9 @@ STATIC const mp_rom_map_elem_t mp_module_builtins_globals_table[] = {
|
||||
#endif
|
||||
#if MICROPY_PY_BUILTINS_EXECFILE
|
||||
{ MP_ROM_QSTR(MP_QSTR_execfile), MP_ROM_PTR(&mp_builtin_execfile_obj) },
|
||||
// because execfile is as non-standard as this vars implementation
|
||||
// move it when it is compliant.
|
||||
{ MP_ROM_QSTR(MP_QSTR_vars), MP_ROM_PTR(&mp_builtin_vars_obj) },
|
||||
#endif
|
||||
{ MP_ROM_QSTR(MP_QSTR_getattr), MP_ROM_PTR(&mp_builtin_getattr_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_setattr), MP_ROM_PTR(&mp_builtin_setattr_obj) },
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user