Sys.hx 2.8 KB

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