Sys.hx 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. trace(python.lib.Sys.platform);
  57. var ch = switch (python.lib.Sys.platform) {
  58. case "linux" | "darwin":
  59. var fd = python.lib.Sys.stdin.fileno();
  60. var old = python.lib.Termios.tcgetattr(fd);
  61. var restore = python.lib.Termios.tcsetattr.bind(fd, python.lib.Termios.TCSADRAIN, old);
  62. try {
  63. python.lib.Tty.setraw(fd);
  64. var ch = python.lib.Sys.stdin.read(1);
  65. restore();
  66. ch;
  67. } catch (e:Dynamic) {
  68. trace("error" + e);
  69. restore();
  70. String.fromCharCode(0);
  71. }
  72. case "win32" | "cygwin":
  73. python.lib.Msvrt.getch();
  74. case x :
  75. throw "platform " + x + " not supported";
  76. }
  77. return ch.charCodeAt(0);
  78. }
  79. public static function stdin() : haxe.io.Input {
  80. return null;
  81. }
  82. public static function stdout() : haxe.io.Output {
  83. return null;
  84. }
  85. public static function stderr() : haxe.io.Output {
  86. return null;
  87. }
  88. static function __init__():Void {
  89. environ = new haxe.ds.StringMap();
  90. var env = python.lib.Os.environ;
  91. for (key in env.keys()) {
  92. environ.set(key, env.get(key, null));
  93. }
  94. }
  95. }