Skip to content

Instantly share code, notes, and snippets.

@formula1
Last active August 29, 2015 14:11
Show Gist options
  • Save formula1/b7677d575182be6386ba to your computer and use it in GitHub Desktop.
Save formula1/b7677d575182be6386ba to your computer and use it in GitHub Desktop.
Python to JS skeleton convert
//!/usr/bin/env node
var fs = require("fs");
var curclass = [];
var lastws = 0;
var curws = 0;
var multi = false;
var req = [];
var lastline = "";
function addclass(name){
curclass.unshift({
name:name,
ws:curws
});
}
function addreq(name){
if(req.indexOf(name) == -1){
req.push(name);
}
}
module.exports.walkdir = function walkdir(indir,outdir){
var result = [];
var stat = fs.statSync(indir);
if(!stat.isDir()){
if(/\.py$/.test(indir)){
transform(indir, outdir.substring(0,outdir.length-3)+".js");
console.log(indir+" was transformed into "+outdir);
result.push(indir);
}else{
console.log(indir+" is not a python script");
return false;
}
}else{
var files = fs.readdirSync(indir);
fs.mkdirSync(outdir);
for(var i = 0;i<files.length;i++){
if(/^\..*$/.test(files[i])) continue;
var tpath = path.join(indir,files[i]);
var topath = path.join(outdir,files[i]);
var check = walkdir(tpath,topath);
if(!check){
continue;
}
result = result.concat(check);
}
}
result.sort();
return result;
}
module.exports.transform = function transform(infile,outfile){
var file = fs.readFileSync(infile);
file = file.split("\n");
for(var i=0;i<file.length;i++){
file[i] = whitespaceparse(file[i]);
}
while(req.length > 0){
var name = req.shift()
file.unshift("var "+name+" = require(\""+name+"\");");
}
file = file.join("\n");
fs.writeFileSync(outfile,file);
}
module.exports.whitespaceparse = function whitespaceparse(line){
if(multi){
return patterns.multi.transform(patterns.multi.pattern.match(line));
}
var temp = lastline+line;
curws = /^(\t*)(.*)$/.match(line)[0].length;
if(curws < lastwhitespace){
temp = "}\n";
if(curclass.length > 0){
if(curws == curclass[0].ws){
curclass.shift();
}
}
return temp+whitespaceparse(line);
}
for(var i in patterns){
temp = patterns[i].transform(patterns[i].pattern.match(line));
}
lastws = curws;
if(/\\$/.test(temp)){
lastline = temp.substring(0,temp.length-2);
return "";
}else{
lastline = "";
return temp;
}
}
var patterns = {
main:{
pattern:/^if\s+__name__\s+==\s+"__main__":/,
transform:function(groups){
return "if(!module.parent) {";
}
},
usrbin:{
patters:/#\s*\!\/usr\/bin\/env\s+python/,
transform:function(groups){
return "//!/usr/bin/env node";
}
},
std:{
pattern:/^(.*)(\s+)(sys)\.(stdout|stderr)(.*)$/,
transform:function(groups){
return groups[0]+groups[1]+"process."+groups[3]+groups[4];
}
},
platform:{
pattern:/^(.*)(\s+)(sys.platfrom)(.*)$/,
transform:function(groups){
addreq("os");
return groups[0]+groups[1]+"os.platform()"+groups[3];
}
},
chdir:{
pattern:/^(.*)(\s+)(os.chdir)(.*)$/,
transform:function(groups){
return groups[0]+groups[1]+"process.chdir"+groups[3];
}
},
isdir:{
pattern:/^(.*)(\s+)(os.path.isdir)(\s*)\((.*)\)(.*)$/,
transform:function(groups){
return groups[0]+groups[1]+"fs.statSync("+groups[4]+").isDir() "+groups[5];
}
},
concat:{
pattern:/^(\s*)(\w+)(\.extend)(\s*)\((.*)\)(.*)$/,
transform:function(groups){
return groups[0]+groups[1]+" = "+groups[1]+".concat("+groups[4]+");"+groups[5];
}
},
push:{
pattern:/^(\s*)(\w+)(\.append)(\s*)\((.*)\)(.*)$/,
transform:function(groups){
return groups[0]+groups[1]+" = "+groups[1]+".push("+groups[4]+");"+groups[5];
}
},
ifstate:{
pattern:/^(\s*)(if)(.*)(\:)$/,
transform: function(groups){
return groups[0]+"if ("+groups[3]+" ){ ";
}
},
elifstate:{
pattern:/^(\s*)(elif)(.*)(\:)$/,
transform: function(groups){
return groups[0]+"else if ("+groups[3]+" ){ ";
}
},
elsestate:{
pattern:/^(\s*)(else)(\:)$/,
transform: function(groups){
return groups[0]+"else { ";
}
},
forloops:{
pattern:/^(\s*)(for)(.*)(\:)$/,
transform: function(groups){
return groups[0]+"for ( "+groups[2]+" ){ ";
}
},
classstate:{
pattern:/^(\s*)(class )(.*)\:$/,
transform:function(groups){
addclass(groups[2]);
}
},
functionstate:{
pattern:/^(\s*)(def)\s*(.*)\((.*)\)\:$/,
transform: function(groups){
var ret;
if(curclass.length > 0){
if(/^__init__/.test(groups[2])){
ret = groups[0]+"module.exports."+curclass[0].name+" = function "+curclass[0].name;
}else{
ret = groups[0]+curclass[0].name+".prototype."+groups[2]+" = function ";
}
}else{
ret = groups[0]+"module.exports."+groups[2]+" = function "+curclass[2].name;
}
ret += "("+groups[3]+") {";
return ret;
}
},
comment:{
pattern:/^(.*)#(.*)$/,
transform:function(groups){
return groups[0]+"//"+groups[1];
}
},
multi:{
pattern:/(.*)"""(.*)/,
transform:function(groups){
multi = !multi;
return groups[0]+(multi?"/*":"*/")+groups[1];
}
},
not:{
pattern:/(.*)(\s+)not(\s+)(.*)/,
transform:function(groups){
return groups[0]+" ! "+groups[3];
}
},
req:{
pattern:/^(\s+)import\s+(.*)$/,
transform: function(groups){
addreq(groups[2]);
}
}
}
if(!module.parent) {
var indir = process.env.in_dir || process.cwd();
var outdir = process.env.out_dir || path.resolve(process.cwd(), "compjs");
var boo = false;
while(boo){
try{
boo = !fs.statSync(outdir).isDir();
if(boo) outdir += "_";
}catch(e){
boo = false;
fs.mkdirSync(outdir);
}
}
walkdir(indir,outdir);
}
/*
* if a string contains `%` then we have to replace that and the next character with `j`
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment