Skip to content

Commit 024d600

Browse files
author
hartsantler
committed
fake os library for nodejs, builtin open, publish npm package 0.9.5.
1 parent 62df431 commit 024d600

6 files changed

Lines changed: 287 additions & 5 deletions

File tree

pythonjs/fakelibs/os.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
_fs = require('fs')
2+
_path = require('path')
3+
4+
5+
class _fake_path:
6+
def __init__(self):
7+
self.sep = _path.sep
8+
9+
def join(self, a, b):
10+
return _path.join( a, b )
11+
12+
def normpath(self, path):
13+
return _path.normalize( path )
14+
15+
def dirname(self, path):
16+
return _path.dirname( path )
17+
18+
def basename(self, path):
19+
return _path.basename( path )
20+
21+
def split(self, path):
22+
a = self.dirname(path)
23+
b = self.basename(path)
24+
return [a,b]
25+
26+
def exists(self, path): ## this is new - missing in Node v0.6.19
27+
return _fs.existsSync(path)
28+
29+
def abspath(self, path):
30+
return _path.resolve( path )
31+
32+
def expanduser(self, path):
33+
## assume that path starts with "~/"
34+
return self.join( process.env.HOME, path[2:] )
35+
36+
def isdir(self, path):
37+
if self.exists( path ):
38+
with javascript:
39+
stat = _fs.statSync( path )
40+
if stat:
41+
return stat.isDirectory()
42+
else:
43+
return False
44+
return False
45+
46+
def isfile(self, path):
47+
if self.exists( path ):
48+
with javascript:
49+
stat = _fs.statSync( path )
50+
if stat:
51+
return stat.isFile()
52+
else:
53+
return False
54+
return False
55+
56+
class _fake_os:
57+
def __init__(self):
58+
self.environ = process.env
59+
self.path = _fake_path()
60+
61+
def abort(self):
62+
process.abort()
63+
64+
def chrdir(self, path):
65+
process.chdir( path )
66+
67+
def getcwd(self):
68+
return process.cwd()
69+
70+
def getpid(self):
71+
return process.pid
72+
73+
def listdir(self, path):
74+
return _fs.readdirSync(path)
75+
76+
def mkdir(self, path):
77+
_fs.mkdirSync( path )
78+
79+
def stat(self, path):
80+
return _fs.statSync( path )
81+
82+
os = _fake_os()

pythonjs/package.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,18 @@
22
"author": "Brett Hartshorn <goatman.py@gmail.com>",
33
"name": "python-js",
44
"description": "python multi-translator: javascript, dart, coffee, lua, vis.js",
5-
"version": "0.9.4",
5+
"version": "0.9.5",
66
"license": "BSD-3-Clause",
77
"repository": {
88
"type": "git",
99
"url": "git://github.com/PythonJS/PythonJS.git"
1010
},
1111
"keywords": [
1212
"pythonjs",
13-
"python"
13+
"python",
14+
"dart",
15+
"lua",
16+
"transpiler"
1417
],
1518
"dependencies": {
1619
"workerjs": "*",
@@ -26,6 +29,7 @@
2629

2730
"files": [
2831
"lib",
32+
"fakelibs",
2933
"empythoned.js",
3034
"python-js",
3135
"pythonjs.js",

pythonjs/python-js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ empythoned.FS.createFolder(".","fakelibs",true,true);
6767
empythoned.FS.createLazyFile("./fakelibs", "tornado.py", "./fakelibs/tornado.py",
6868
true,false
6969
);
70+
empythoned.FS.createLazyFile("./fakelibs", "os.py", "./fakelibs/os.py",
71+
true,false
72+
);
7073

7174

7275
Python.initialize(

pythonjs/python_to_pythonjs.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,11 @@ def __init__(self, source=None, module=None, module_path=None, dart=False, coffe
190190
header = open( os.path.join(dirname, os.path.join('fakelibs', 'tornado.py')) ).read()
191191
source = header + '\n' + source
192192
self._source = source.splitlines()
193-
break
193+
elif line.strip().startswith('import os'):
194+
dirname = os.path.dirname(os.path.abspath(__file__))
195+
header = open( os.path.join(dirname, os.path.join('fakelibs', 'os.py')) ).read()
196+
source = header + '\n' + source
197+
self._source = source.splitlines()
194198

195199
tree = parse( source ) ## ast.parse
196200
self._generator_function_nodes = collect_generator_functions( tree )
@@ -285,7 +289,10 @@ def visit_Import(self, node):
285289
fallback to requirejs or if in webworker importScripts.
286290
some special modules from pythons stdlib can be faked here like:
287291
. threading
292+
293+
nodejs only:
288294
. tornado
295+
. os
289296
290297
'''
291298

@@ -294,7 +301,7 @@ def visit_Import(self, node):
294301
for alias in node.names:
295302
if alias.name in tornado:
296303
pass ## pythonjs/fakelibs/tornado.py
297-
elif alias.name == 'json':
304+
elif alias.name == 'json' or alias.name == 'os':
298305
pass ## part of builtins.py
299306
elif alias.name == 'threading':
300307
self._use_threading = True

pythonjs/pythonjs.js

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3535,6 +3535,144 @@ __array_to_ascii.types_signature = { };
35353535
__array_to_ascii.pythonscript_function = true;
35363536
__array_attrs.to_ascii = __array_to_ascii;
35373537
array = __create_class__("array", __array_parents, __array_attrs, __array_properties);
3538+
var file, __file_attrs, __file_parents;
3539+
__file_attrs = {};
3540+
__file_parents = [];
3541+
__file_properties = {};
3542+
__file___init__ = function(args, kwargs) {
3543+
var __sig__, __args__;
3544+
__sig__ = { kwargs:{},args:["self", "path", "flags"] };
3545+
if (args instanceof Array && ( Object.prototype.toString.call(kwargs) ) == "[object Object]" && ( arguments.length ) == 2) {
3546+
/*pass*/
3547+
} else {
3548+
args = Array.prototype.slice.call(arguments, 0, __sig__.args.length);
3549+
kwargs = {};
3550+
}
3551+
__args__ = __getargs__("__file___init__", __sig__, args, kwargs);
3552+
var self = __args__['self'];
3553+
var path = __args__['path'];
3554+
var flags = __args__['flags'];
3555+
self.path = path;
3556+
if (( flags ) == "rb") {
3557+
self.flags = "r";
3558+
self.binary = true;
3559+
} else {
3560+
if (( flags ) == "wb") {
3561+
self.flags = "w";
3562+
self.binary = true;
3563+
} else {
3564+
self.flags = flags;
3565+
self.binary = false;
3566+
}
3567+
}
3568+
self.flags = flags;
3569+
}
3570+
3571+
__file___init__.NAME = "__file___init__";
3572+
__file___init__.args_signature = ["self", "path", "flags"];
3573+
__file___init__.kwargs_signature = { };
3574+
__file___init__.types_signature = { };
3575+
__file___init__.pythonscript_function = true;
3576+
__file_attrs.__init__ = __file___init__;
3577+
__file_read = function(args, kwargs) {
3578+
var _fs, path;
3579+
var __sig__, __args__;
3580+
__sig__ = { kwargs:{"binary": false},args:["self", "binary"] };
3581+
if (args instanceof Array && ( Object.prototype.toString.call(kwargs) ) == "[object Object]" && ( arguments.length ) == 2) {
3582+
/*pass*/
3583+
} else {
3584+
args = Array.prototype.slice.call(arguments, 0, __sig__.args.length);
3585+
kwargs = {};
3586+
}
3587+
__args__ = __getargs__("__file_read", __sig__, args, kwargs);
3588+
var self = __args__['self'];
3589+
var binary = __args__['binary'];
3590+
_fs = __get__(require, "__call__")(["fs"], __NULL_OBJECT__);
3591+
path = self.path;
3592+
if (__test_if_true__(binary || self.binary)) {
3593+
return _fs.readFileSync(path);
3594+
} else {
3595+
return _fs.readFileSync(path, __jsdict([["encoding", "utf8"]]));
3596+
}
3597+
}
3598+
3599+
__file_read.NAME = "__file_read";
3600+
__file_read.args_signature = ["self", "binary"];
3601+
__file_read.kwargs_signature = { binary:false };
3602+
__file_read.types_signature = { binary:"False" };
3603+
__file_read.pythonscript_function = true;
3604+
__file_attrs.read = __file_read;
3605+
__file_write = function(args, kwargs) {
3606+
var _fs, path;
3607+
var __sig__, __args__;
3608+
__sig__ = { kwargs:{"binary": false},args:["self", "data", "binary"] };
3609+
if (args instanceof Array && ( Object.prototype.toString.call(kwargs) ) == "[object Object]" && ( arguments.length ) == 2) {
3610+
/*pass*/
3611+
} else {
3612+
args = Array.prototype.slice.call(arguments, 0, __sig__.args.length);
3613+
kwargs = {};
3614+
}
3615+
__args__ = __getargs__("__file_write", __sig__, args, kwargs);
3616+
var self = __args__['self'];
3617+
var data = __args__['data'];
3618+
var binary = __args__['binary'];
3619+
_fs = __get__(require, "__call__")(["fs"], __NULL_OBJECT__);
3620+
path = self.path;
3621+
if (__test_if_true__(binary || self.binary)) {
3622+
_fs.writeFileSync(path, data);
3623+
} else {
3624+
_fs.writeFileSync(path, data, __jsdict([["encoding", "utf8"]]));
3625+
}
3626+
}
3627+
3628+
__file_write.NAME = "__file_write";
3629+
__file_write.args_signature = ["self", "data", "binary"];
3630+
__file_write.kwargs_signature = { binary:false };
3631+
__file_write.types_signature = { binary:"False" };
3632+
__file_write.pythonscript_function = true;
3633+
__file_attrs.write = __file_write;
3634+
__file_close = function(args, kwargs) {
3635+
var __sig__, __args__;
3636+
__sig__ = { kwargs:{},args:["self"] };
3637+
if (args instanceof Array && ( Object.prototype.toString.call(kwargs) ) == "[object Object]" && ( arguments.length ) == 2) {
3638+
/*pass*/
3639+
} else {
3640+
args = Array.prototype.slice.call(arguments, 0, __sig__.args.length);
3641+
kwargs = {};
3642+
}
3643+
__args__ = __getargs__("__file_close", __sig__, args, kwargs);
3644+
var self = __args__['self'];
3645+
/*pass*/
3646+
}
3647+
3648+
__file_close.NAME = "__file_close";
3649+
__file_close.args_signature = ["self"];
3650+
__file_close.kwargs_signature = { };
3651+
__file_close.types_signature = { };
3652+
__file_close.pythonscript_function = true;
3653+
__file_attrs.close = __file_close;
3654+
file = __create_class__("file", __file_parents, __file_attrs, __file_properties);
3655+
open = function(args, kwargs) {
3656+
var __sig__, __args__;
3657+
__sig__ = { kwargs:{"mode": null},args:["path", "mode"] };
3658+
if (args instanceof Array && ( Object.prototype.toString.call(kwargs) ) == "[object Object]" && ( arguments.length ) == 2) {
3659+
/*pass*/
3660+
} else {
3661+
args = Array.prototype.slice.call(arguments, 0, __sig__.args.length);
3662+
kwargs = {};
3663+
}
3664+
__args__ = __getargs__("open", __sig__, args, kwargs);
3665+
var path = __args__['path'];
3666+
var mode = __args__['mode'];
3667+
return __get__(file, "__call__")([path, mode], __NULL_OBJECT__);
3668+
}
3669+
3670+
open.NAME = "open";
3671+
open.args_signature = ["path", "mode"];
3672+
open.kwargs_signature = { mode:null };
3673+
open.types_signature = { mode:"None" };
3674+
open.return_type = "file";
3675+
open.pythonscript_function = true;
35383676
json = __jsdict([["loads", (function (s) {return JSON.parse(s)})], ["dumps", (function (o) {return JSON.stringify(o)})]]);
35393677
__get_other_workers_with_shared_arg = function(worker, ob) {
35403678
var a, other, args;

pythonjs/runtime/builtins.py

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1315,13 +1315,61 @@ def to_ascii(self):
13151315
return string
13161316

13171317

1318-
# JSON stuff
1318+
## file IO ##
1319+
class file:
1320+
'''
1321+
TODO, support multiple read/writes. Currently this just reads all data,
1322+
and writes all data.
1323+
'''
1324+
def __init__(self, path, flags):
1325+
self.path = path
1326+
1327+
if flags == 'rb':
1328+
self.flags = 'r'
1329+
self.binary = True
1330+
elif flags == 'wb':
1331+
self.flags = 'w'
1332+
self.binary = True
1333+
else:
1334+
self.flags = flags
1335+
self.binary = False
1336+
1337+
self.flags = flags
1338+
1339+
def read(self, binary=False):
1340+
_fs = require('fs')
1341+
path = self.path
1342+
with javascript:
1343+
if binary or self.binary:
1344+
return _fs.readFileSync( path )
1345+
else:
1346+
return _fs.readFileSync( path, {'encoding':'utf8'} )
1347+
1348+
def write(self, data, binary=False):
1349+
_fs = require('fs')
1350+
path = self.path
1351+
with javascript:
1352+
if binary or self.binary:
1353+
_fs.writeFileSync( path, data )
1354+
else:
1355+
_fs.writeFileSync( path, data, {'encoding':'utf8'} )
1356+
1357+
def close(self):
1358+
pass
1359+
1360+
def open( path, mode=None):
1361+
return file( path, mode )
1362+
13191363

13201364
with javascript:
1365+
1366+
## mini json library ##
13211367
json = {
13221368
'loads': lambda s: JSON.parse(s),
13231369
'dumps': lambda o: JSON.stringify(o)
13241370
}
1371+
1372+
13251373
def __get_other_workers_with_shared_arg( worker, ob ):
13261374
a = []
13271375
for b in threading.workers:

0 commit comments

Comments
 (0)