Sys.hx 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 "";
  44. }
  45. public static function command( cmd : String, ?args : Array<String> ) : Int {
  46. var args = args == null ? [cmd] : [cmd].concat(args);
  47. return python.lib.Subprocess.call(args);
  48. }
  49. public static function cpuTime() : Float {
  50. return 0.0;
  51. }
  52. public static function executablePath() : String {
  53. return python.lib.Sys.argv[0];
  54. }
  55. public static function getChar( echo : Bool ) : Int {
  56. var ch = switch (python.lib.Sys.platform) {
  57. case x if (StringTools.startsWith(x, "linux") || x =="darwin"):
  58. var fd = python.lib.Sys.stdin.fileno();
  59. var old = python.lib.Termios.tcgetattr(fd);
  60. var restore = python.lib.Termios.tcsetattr.bind(fd, python.lib.Termios.TCSADRAIN, old);
  61. try {
  62. python.lib.Tty.setraw(fd);
  63. var x = python.lib.Sys.stdin.read(1);
  64. restore();
  65. x.charCodeAt(0);
  66. } catch (e:Dynamic) {
  67. restore();
  68. throw e;
  69. }
  70. case "win32" | "cygwin":
  71. python.lib.Msvcrt.getch();
  72. case x :
  73. throw "platform " + x + " not supported";
  74. }
  75. return ch;
  76. }
  77. public static function stdin() : haxe.io.Input {
  78. return null;
  79. }
  80. public static function stdout() : haxe.io.Output {
  81. return null;
  82. }
  83. public static function stderr() : haxe.io.Output {
  84. return null;
  85. }
  86. static function __init__():Void {
  87. environ = new haxe.ds.StringMap();
  88. var env = python.lib.Os.environ;
  89. for (key in env.keys()) {
  90. environ.set(key, env.get(key, null));
  91. }
  92. }
  93. }