2
0

Sys.hx 2.8 KB

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