Преглед изворни кода

in progress: trying to get input and output working

frabbit пре 11 година
родитељ
комит
07c40c3d8e

+ 5 - 4
std/python/_std/Sys.hx

@@ -1,5 +1,6 @@
 import python.lib.Time;
-
+import sys.io.FileInput;
+import sys.io.FileOutput;
 
 @:coreApi
 class Sys {
@@ -111,15 +112,15 @@ class Sys {
 	}
 
 	public static function stdin() : haxe.io.Input {
-		return null;
+		return new FileInput(cast python.lib.Sys.stdin.buffer);
 	}
 
 	public static function stdout() : haxe.io.Output {
-		return null;
+		return new FileOutput(cast python.lib.Sys.stdout.buffer);
 	}
 
 	public static function stderr() : haxe.io.Output {
-		return null;
+		return new FileOutput(cast python.lib.Sys.stderr.buffer);
 	}
 
 	static function __init__():Void {

+ 13 - 3
std/python/_std/sys/io/File.hx

@@ -21,6 +21,8 @@
  */
 package sys.io;
 
+import sys.io.FileInput;
+
 /**
 	API for reading and writing to files.
 
@@ -58,15 +60,23 @@ class File {
 	}
 
 	public static function read( path : String, binary : Bool = true ) : FileInput {
-		return throw "not implemented";
+		var mode = if (binary) "rb" else "r";
+		var f:python.lib.io.RawIOBase = cast python.lib.Builtin.open(path, mode, -1);
+		return new FileInput(f);
 	}
 
 	public static function write( path : String, binary : Bool = true ) : FileOutput {
-		return throw "not implemented";
+		var mode = if (binary) "wb" else "w";
+		var f:python.lib.io.RawIOBase = cast python.lib.Builtin.open(path, mode, -1);
+
+		return new FileOutput(f);
 	}
 
 	public static function append( path : String, binary : Bool = true ) : FileOutput {
-		return throw "not implemented";
+		var mode = if (binary) "ab" else "a";
+		var f:python.lib.io.RawIOBase = cast python.lib.Builtin.open(path, mode, -1);
+
+		return new FileOutput(f);
 	}
 
 	public static function copy( srcPath : String, dstPath : String ) : Void

+ 14 - 0
std/python/_std/sys/io/FileInput.hx

@@ -0,0 +1,14 @@
+
+package sys.io;
+
+import python.io.NativeInput;
+import python.lib.io.RawIOBase;
+
+class FileInput extends NativeInput {
+	public function new (stream:RawIOBase) {
+		super(stream);
+	}
+
+
+
+}

+ 12 - 0
std/python/_std/sys/io/FileOutput.hx

@@ -0,0 +1,12 @@
+
+package sys.io;
+
+import python.lib.io.RawIOBase;
+
+class FileOutput extends python.io.NativeOutput {
+
+	public function new (stream:RawIOBase) {
+		super(stream);
+	}
+
+}

+ 26 - 0
std/python/_std/sys/io/Process.hx

@@ -0,0 +1,26 @@
+
+package sys.io;
+
+class Process {
+
+	var stdout(default,null) : haxe.io.Input;
+	var stderr(default,null) : haxe.io.Input;
+	var stdin(default,null) : haxe.io.Output;
+
+	function new( cmd : String, args : Array<String> ) : Void {
+		throw "not implemented";
+	}
+	function getPid() : Int {
+		return throw "not implemented";
+	}
+	function exitCode() : Int {
+		return throw "not implemented";
+	}
+	function close() : Void {
+		throw "not implemented";
+	}
+	function kill() : Void {
+		throw "not implemented";
+	}
+
+}

+ 75 - 0
std/python/io/NativeInput.hx

@@ -0,0 +1,75 @@
+
+package python.io;
+
+import haxe.io.Eof;
+import haxe.io.Input;
+
+import python.lib.io.RawIOBase;
+import python.lib.io.IOBase.SeekSet;
+
+
+class NativeInput extends Input{
+
+	var stream:RawIOBase;
+
+	public var canSeek(get_canSeek, null):Bool;
+
+	public function new (stream:RawIOBase) {
+		this.stream = stream;
+		if (!stream.readable()) throw "Write-only stream";
+	}
+
+	private function get_canSeek():Bool
+	{
+		return stream.seekable();
+	}
+
+	override public function close():Void
+	{
+		stream.close();
+	}
+
+	public function tell() : Int
+	{
+		return stream.tell();
+	}
+
+	override public function readByte():Int
+	{
+		var ret = cast stream.read(1);
+		if (ret == null) throw new Eof();
+		return ret;
+	}
+
+	public function seek( p : Int, pos : sys.io.FileSeek ) : Void
+	{
+		var pos = switch(pos)
+		{
+			case SeekBegin: SeekSet.SeekStart;
+			case SeekCur: SeekSet.SeekCur;
+			case SeekEnd: SeekSet.SeekEnd;
+		};
+		stream.seek(p, pos);
+	}
+
+	/*
+	override public function readBytes(s:Bytes, pos:Int, len:Int):Int
+	{
+		if( pos < 0 || len < 0 || pos + len > s.length )
+			throw Error.OutsideBounds;
+
+		var ret = stream.Read(s.getData(), pos, len);
+		if (ret == 0)
+			throw new Eof();
+		return ret;
+	}
+	*/
+
+	/*
+
+	public function eof() : Bool
+	{
+		return stream.Position == stream.Length;
+	}
+	*/
+}

+ 63 - 0
std/python/io/NativeOutput.hx

@@ -0,0 +1,63 @@
+
+package python.io;
+
+import haxe.io.Output;
+
+import python.lib.Builtin;
+import python.lib.io.IOBase;
+import python.lib.io.RawIOBase;
+
+class NativeOutput extends Output{
+
+	var stream:RawIOBase;
+
+	public var canSeek(get_canSeek, null):Bool;
+
+	public function new (stream:RawIOBase) {
+		this.stream = stream;
+		if (!stream.writable()) throw "Read only stream";
+	}
+
+	private function get_canSeek():Bool
+	{
+		return stream.seekable();
+	}
+
+	override public function close():Void
+	{
+		stream.close();
+	}
+
+	public function tell() : Int
+	{
+		return stream.tell();
+	}
+
+
+
+	public function seek( p : Int, pos : sys.io.FileSeek ) : Void
+	{
+		var pos = switch(pos)
+		{
+			case SeekBegin: SeekSet.SeekStart;
+			case SeekCur: SeekSet.SeekCur;
+			case SeekEnd: SeekSet.SeekEnd;
+		};
+		stream.seek(p, pos);
+	}
+
+	override public function prepare(nbytes:Int):Void
+	{
+		stream.truncate(nbytes);
+	}
+
+	override public function flush():Void
+	{
+		stream.flush();
+	}
+
+	override public function writeByte(c:Int):Void
+	{
+		stream.write(Builtin.bytearray([c]));
+	}
+}

+ 3 - 1
std/python/lib/Os.hx

@@ -28,7 +28,9 @@ extern class Stat {
 	@:optional public var st_creator:Int;
 	@:optional public var st_type:Int;
 }
-
+@:preCode(
+	python.Macros.importAs("os", "python.lib.Os")
+)
 extern class Os {
 
 	public static var environ : Dict<String, String>;

+ 2 - 1
std/python/lib/Sys.hx

@@ -1,5 +1,6 @@
 package python.lib;
 
+import python.lib.io.FileIO;
 import python.lib.io.RawIOBase;
 import python.lib.io.TextIOBase;
 import python.lib.Types;
@@ -17,7 +18,7 @@ extern class Sys {
 
 	public static var stdout(default, never):TextIOBase;
 	public static var stdin(default, never):TextIOBase;
-
+	public static var stderr(default, never):TextIOBase;
 
 	public static function getsizeof (t:Dynamic):Int;
 

+ 12 - 0
std/python/lib/io/BufferedIOBase.hx

@@ -0,0 +1,12 @@
+
+package python.lib.io;
+
+import python.lib.io.RawIOBase;
+
+extern class BufferedIOBase extends IOBase {
+
+	/* not always available */
+	public var raw:RawIOBase;
+
+
+}

+ 8 - 1
std/python/lib/io/IOBase.hx

@@ -1,6 +1,12 @@
 
 package python.lib.io;
 
+@:enum abstract SeekSet(Int) {
+	var SeekStart = 0;
+	var SeekCur = 1;
+	var SeekEnd = 2;
+}
+
 extern class IOBase {
 
 	public function close():Void;
@@ -13,5 +19,6 @@ extern class IOBase {
 	public function writable():Bool;
 	public function seekable():Bool;
 	public function fileno():Int;
-
+	public function seek(offset:Int, whence:SeekSet):Int;
+	public function truncate (size:Int):Int;
 }

+ 3 - 0
std/python/lib/io/TextIOBase.hx

@@ -1,6 +1,7 @@
 
 package python.lib.io;
 
+import python.lib.io.BufferedIOBase;
 import python.lib.io.IOBase;
 
 extern class TextIOBase extends IOBase {
@@ -10,4 +11,6 @@ extern class TextIOBase extends IOBase {
 	public function write (s:String):Int;
 
 	public function read (n:Int):String;
+
+	public var buffer:BufferedIOBase;
 }