Sys.hx 3.0 KB

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