Ver Fonte

add very basic File operations (Bytes are still missing)

frabbit há 11 anos atrás
pai
commit
29d6d6f584

+ 70 - 0
std/python/_std/sys/io/File.hx

@@ -0,0 +1,70 @@
+/*
+ * Copyright (C)2005-2012 Haxe Foundation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+package sys.io;
+
+/**
+	API for reading and writing to files.
+
+	See `sys.FileSystem` for the complementary file system API.
+**/
+@:coreApi
+class File {
+
+	public static function getContent( path : String ) : String
+	{
+		var f = python.lib.Builtin.open(path, "r", -1, "utf-8");
+		var content = f.read(-1);
+		f.close();
+		return content;
+	}
+
+	public static function saveContent( path : String, content : String ) : Void {
+		var f = python.lib.Builtin.open(path, "w", -1, "utf-8");
+		f.write(content);
+		f.close();
+	}
+
+	public static function getBytes( path : String ) : haxe.io.Bytes {
+		return throw "not implemented";
+	}
+
+	public static function saveBytes( path : String, bytes : haxe.io.Bytes ) : Void {
+		throw "not implemented";
+	}
+
+	public static function read( path : String, binary : Bool = true ) : FileInput {
+		return throw "not implemented";
+	}
+
+	public static function write( path : String, binary : Bool = true ) : FileOutput {
+		return throw "not implemented";
+	}
+
+	public static function append( path : String, binary : Bool = true ) : FileOutput {
+		return throw "not implemented";
+	}
+
+	public static function copy( srcPath : String, dstPath : String ) : Void
+	{
+		return python.lib.ShUtil.copy(srcPath, dstPath);
+	}
+}

+ 16 - 16
std/python/lib/Builtin.hx

@@ -11,7 +11,7 @@ import python.lib.Types;
 @:native("_hx_builtin")
 extern class Builtin {
 
-	
+
 
 	@:overload(function (f:Int):Int {})
 	public static function abs(x:Float):Float;
@@ -24,14 +24,14 @@ extern class Builtin {
 	public static function callable(x:Dynamic):Bool;
 
 
-	
+
 
 	@:overload(function (obj:Dynamic, f:Tuple<Dynamic>):Bool {})
 	public static function isinstance(obj:Dynamic, cl:Class<Dynamic>):Bool;
 
 	public static function hasattr(obj:Dynamic, attr:String):Bool;
 	public static function getattr(obj:Dynamic, attr:String):Dynamic;
-	
+
 	@:overload(function (f:Set<Dynamic>):Int {})
 	@:overload(function (f:StringBuf):Int {})
 	@:overload(function (f:Array<Dynamic>):Int {})
@@ -41,11 +41,11 @@ extern class Builtin {
 	@:overload(function (f:Tuple<Dynamic>):Int {})
 	public static function len(x:String):Int;
 
-	public static function open(file:String, mode:String, ?encoding:String = null, ?errors : String, ?newline:String, ?closefd:Bool, ?opener:String->Int->FileDescriptor):TextIOBase;
+	public static function open(file:String, mode:String, ?buffering:Int = -1, ?encoding:String = null, ?errors : String, ?newline:String, ?closefd:Bool, ?opener:String->Int->FileDescriptor):TextIOBase;
 
 	//public static function divmod():Void;
 	//public static function input():Void;
-	
+
 	//public static function staticmethod():Void;
 	//public static function enumerate():Void;
 
@@ -55,35 +55,35 @@ extern class Builtin {
 		return untyped __field__(Builtin, "str")(o);
 	}
 	//public static function eval():Void;
-	
+
 	//public static function pow():Void;
 	//public static function sum():Void;
 	//public static function basestring():Void;
 	//public static function execfile():Void;
-	
+
 	public static inline function print(o:Dynamic):Void {
 		return untyped __field__(Builtin, "print")(o);
 	}
-	
+
 	//public static function super():Void;
 	//public static function bin():Void;
 	//public static function file():Void;
 	public static function iter<X>(d:DictView<X>):PyIterator<X>;
 	//public static function property():Void;
-	
+
 
 
 	@:overload(function <X>():Tuple<X> {})
 	public static function tuple<X>(a:Array<X>):Tuple<X>;
 
-	
-	
-	
+
+
+
 	//public static function range():Void;
 	//public static function type():Void;
 	//public static function bytearray():Void;
 	//public static function float():Void;
-	
+
 	@:overload(function <T>(f:Array<T>):Array<T> {})
 	@:overload(function (f:String):Array<String> {})
 	@:overload(function <G>(f:Tuple<G>):Array<G> {})
@@ -92,7 +92,7 @@ extern class Builtin {
 	public static function filter<A>(f:A->Bool, i:Choice<Array<A>, PyIterable<A>>):PyIterator<A>;
 	//public static function raw_input():Void;
 	//public static function unichr():Void;
-	
+
 	//public static function format():Void;
 	//public static function locals():Void;
 	//public static function reduce():Void;
@@ -103,7 +103,7 @@ extern class Builtin {
 	//public static function reload():Void;
 	//public static function vars():Void;
 	//public static function classmethod():Void;
-	
+
 	public static function map<A,B>(fn:A->B, it:PyIterable<A>):PyIterator<B>;
 	//public static function repr():Void;
 	//public static function xrange():Void;
@@ -114,7 +114,7 @@ extern class Builtin {
 	//public static function reversed():Void;
 	//public static function zip():Void;
 	//public static function compile():Void;
-	
+
 	//public static function memoryview():Void;
 	public static function round(f:Float):Int;
 	//public static function __import__():Void;

+ 7 - 1
std/python/lib/ShUtil.hx

@@ -1,12 +1,18 @@
 
 package python.lib;
 
-import python.lib.Types.BaseException;
+import python.lib.Types;
+
 
 extern class ShUtil {
 
 	public static function rmtree(path:String, ?ignore_errors:Bool=false, ?onerror:BaseException->Void):Void;
 
+	public static function copyfile (src:String, dst:String):Void;
+
+	public static function copy (src:String, dst:String):Void;
+	public static function copy2 (src:String, dst:String):Void;
+
 	static function __init__ ():Void {
 		Macros.importAs("shutil", "python.lib.ShUtil");
 	}

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

@@ -8,4 +8,6 @@ extern class TextIOBase extends IOBase {
 	public var encoding:String;
 
 	public function write (s:String):Int;
+
+	public function read (n:Int):String;
 }

+ 0 - 1
tests/unit/TestPython.hx

@@ -1,4 +1,3 @@
-
 package unit;
 
 class TestPython extends Test {