1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
|
/*
* 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.
*/
import python.lib.Time;
import python.lib.Os;
import sys.io.FileInput;
import sys.io.FileOutput;
@:coreApi
class Sys {
static var environ:haxe.ds.StringMap<String> = {
environ = new haxe.ds.StringMap();
var env = Os.environ;
for (key in env.keys()) {
environ.set(key, env.get(key, null));
}
environ;
}
public static function time():Float {
return Time.time();
}
public static function exit(code:Int):Void {
python.lib.Sys.exit(code);
}
public static function print(v:Dynamic):Void {
python.Lib.print(v);
}
public static function println(v:Dynamic):Void {
python.Lib.println(v);
}
public static function args() : Array<String> {
var argv = python.lib.Sys.argv;
return argv.slice(1);
}
public static function getEnv( s : String ) : String {
return environ.get(s);
}
public static function putEnv( s : String, v : String ) : Void {
python.lib.Os.putenv(s, v);
environ.set(s, v);
}
public static function environment() : Map<String,String> {
return environ;
}
public static function sleep( seconds : Float ) : Void {
python.lib.Time.sleep(seconds);
}
public static function setTimeLocale( loc : String ) : Bool {
return false;
}
public static function getCwd() : String {
return python.lib.Os.getcwd();
}
public static function setCwd( s : String ) : Void {
python.lib.Os.chdir(s);
}
public static function systemName() : String {
return switch (python.lib.Sys.platform) {
case x if (StringTools.startsWith(x, "linux")):
"Linux";
case "darwin": "Mac";
case "win32" | "cygwin" : "Windows";
case _ :
throw "not supported platform";
}
}
public static function command( cmd : String, ?args : Array<String> ) : Int {
var args = args == null ? [cmd] : [cmd].concat(args);
return python.lib.Subprocess.call(args);
}
public static function cpuTime() : Float {
return python.lib.Time.clock();
}
public static function executablePath() : String {
return python.lib.Sys.argv[0];
}
public static function getChar( echo : Bool ) : Int {
var ch = switch (systemName()) {
case "Linux" | "Mac":
var fd = python.lib.Sys.stdin.fileno();
var old = python.lib.Termios.tcgetattr(fd);
var restore = python.lib.Termios.tcsetattr.bind(fd, python.lib.Termios.TCSADRAIN, old);
try {
python.lib.Tty.setraw(fd);
var x = python.lib.Sys.stdin.read(1);
restore();
x.charCodeAt(0);
} catch (e:Dynamic) {
restore();
throw e;
}
case "Windows":
python.lib.Msvcrt.getch().decode("utf-8").charCodeAt(0);
case x :
throw "platform " + x + " not supported";
}
if (echo) {
python.Lib.print(String.fromCharCode(ch));
}
return ch;
}
public static function stdin() : haxe.io.Input {
return python.io.IoTools.createFileInputFromText(python.lib.Sys.stdin);
}
public static function stdout() : haxe.io.Output {
return python.io.IoTools.createFileOutputFromText(python.lib.Sys.stdout);
}
public static function stderr() : haxe.io.Output {
return python.io.IoTools.createFileOutputFromText(python.lib.Sys.stderr);
}
}
|