Sys.hx 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import python.lib.Time;
  2. import sys.io.FileInput;
  3. import sys.io.FileOutput;
  4. @:coreApi
  5. class Sys {
  6. static var environ:haxe.ds.StringMap<String>;
  7. public static function time():Float {
  8. return Time.time();
  9. }
  10. public static function exit(code:Int):Void {
  11. python.lib.Sys.exit(code);
  12. }
  13. public static function print(v:Dynamic):Void {
  14. python.Lib.print(v);
  15. }
  16. public static function println(v:Dynamic):Void {
  17. python.Lib.println(v);
  18. }
  19. public static function args() : Array<String> {
  20. var argv = python.lib.Sys.argv;
  21. return argv.slice(1);
  22. }
  23. public static function getEnv( s : String ) : String {
  24. return environ.get(s);
  25. }
  26. public static function putEnv( s : String, v : String ) : Void {
  27. python.lib.Os.putenv(s, v);
  28. environ.set(s, v);
  29. }
  30. public static function environment() : haxe.ds.StringMap<String> {
  31. return environ;
  32. }
  33. public static function sleep( seconds : Float ) : Void {
  34. python.lib.Time.sleep(seconds);
  35. }
  36. public static function setTimeLocale( loc : String ) : Bool {
  37. return false;
  38. }
  39. public static function getCwd() : String {
  40. return python.lib.Os.getcwd();
  41. }
  42. public static function setCwd( s : String ) : Void {
  43. python.lib.Os.chdir(s);
  44. }
  45. public static function systemName() : String {
  46. return switch (python.lib.Sys.platform) {
  47. case x if (StringTools.startsWith(x, "linux")):
  48. "Linux";
  49. case "darwin": "Mac";
  50. case "win32" | "cygwin" : "Windows";
  51. case _ :
  52. throw "not supported platform";
  53. }
  54. }
  55. public static function command( cmd : String, ?args : Array<String> ) : Int {
  56. var args = args == null ? [cmd] : [cmd].concat(args);
  57. return python.lib.Subprocess.call(args);
  58. }
  59. public static function cpuTime() : Float {
  60. return python.lib.Time.clock();
  61. }
  62. public static function executablePath() : String {
  63. return python.lib.Sys.argv[0];
  64. }
  65. public static function getChar( echo : Bool ) : Int {
  66. var ch = switch (systemName()) {
  67. case "Linux" | "Mac":
  68. var fd = python.lib.Sys.stdin.fileno();
  69. var old = python.lib.Termios.tcgetattr(fd);
  70. var restore = python.lib.Termios.tcsetattr.bind(fd, python.lib.Termios.TCSADRAIN, old);
  71. try {
  72. python.lib.Tty.setraw(fd);
  73. var x = python.lib.Sys.stdin.read(1);
  74. restore();
  75. x.charCodeAt(0);
  76. } catch (e:Dynamic) {
  77. restore();
  78. throw e;
  79. }
  80. case "Windows":
  81. python.lib.Msvcrt.getch().decode("utf-8").charCodeAt(0);
  82. case x :
  83. throw "platform " + x + " not supported";
  84. }
  85. if (echo) {
  86. python.Lib.print(String.fromCharCode(ch));
  87. }
  88. return ch;
  89. }
  90. public static function stdin() : haxe.io.Input {
  91. return new FileInput(cast python.lib.Sys.stdin.buffer);
  92. }
  93. public static function stdout() : haxe.io.Output {
  94. return new FileOutput(cast python.lib.Sys.stdout.buffer);
  95. }
  96. public static function stderr() : haxe.io.Output {
  97. return new FileOutput(cast python.lib.Sys.stderr.buffer);
  98. }
  99. static function __init__():Void {
  100. environ = new haxe.ds.StringMap();
  101. var env = python.lib.Os.environ;
  102. for (key in env.keys()) {
  103. environ.set(key, env.get(key, null));
  104. }
  105. }
  106. }