Commit ca504041 authored by Romain Casati's avatar Romain Casati
Browse files

Better module extension managment.

parent 554f0c78
......@@ -69,6 +69,11 @@
*/
languageName () { throw new NotImplementedError("languageName"); };
/**
* Script (module) file extensions
*/
moduleExts () { throw new NotImplementedError("moduleExts"); };
/**
* Launch the kernel.
*/
......@@ -91,10 +96,6 @@
throw new NotImplementedError("putModule");
};
putRessource (filename, content) {
throw new NotImplementedError("putRessource");
};
userModules () { throw new NotImplementedError("userModules"); };
getFile (path) { throw new NotImplementedError("getFile"); };
......@@ -258,6 +259,19 @@
// simple trick that is enough for our purpose.
return JSON.parse(JSON.stringify(obj));
};
/**
* Put a ressource (file or module).
* Detection is based on extension.
*/
putRessource (filename, content) {
const ext = filename.split('.').pop();
if( this.moduleExts().includes(ext) ) {
return this.putModule(filename, content);
} else {
return this.putFile(filename, content);
}
};
/**
* Internal. Code evaluation after an eval.request event.
......
......@@ -23,6 +23,7 @@
language () { return "javascript"; };
languageName () { return "Javascript"; };
moduleExts () { return ['js']; };
async launch () { await this.pageLoad(); };
......@@ -74,6 +75,21 @@
more (source) { return false; };
complete (code) { return []; };
putFile (filename, content) {
console.error(`Fichier ${filename} not added since putFile has no mean in the JS context.`);
};
putModule (filename, content) {
const ext = filename.split('/').pop();
switch( ext ) {
case 'js':
this.eval(content);
break;
default:
throw {message: "Only '.js' files supported."};
}
};
}
return new Basthon();
......
......@@ -33,6 +33,7 @@
language () { return "python3"; };
languageName () { return "Python 3"; };
moduleExts () { return ['py']; };
/**
* What to do when loaded (private).
......@@ -133,19 +134,6 @@
userModules () {
return this.__kernel__.user_modules();
};
/**
* Put a ressource (file or *.py module) on the local (emulated)
* filesystem. Detection is based on extension.
*/
putRessource (filename, content) {
const ext = filename.split('.').pop();
if( ext === 'py' ) {
return this.putModule(filename, content);
} else {
return this.putFile(filename, content);
}
};
/**
* Download a file from the VFS.
......
......@@ -21,6 +21,7 @@
language () { return "sql"; };
languageName () { return "SQL"; };
moduleExts () { return ['sql', 'db', 'sqlite']; };
async launch () {
// avoid conflict with requirejs and use it when available.
......@@ -99,6 +100,26 @@
more (source) { return false; };
complete (code) { return []; };
putFile (filename, content) {
console.error(`Fichier ${filename} not added since putFile has no mean in the SQL context.`);
};
putModule (filename, content) {
const ext = filename.split('/').pop();
switch( ext ) {
case 'sql':
this.db.run(content);
break;
case 'sqlite':
case 'db':
if( this.db ) this.db.close();
this.db = new SQL.Database(content);
break;
default:
throw {message: "Only '.sql', '.db' and '.sqlite' files supported."};
}
};
}
return new Basthon();
......
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