Sys.hx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * Copyright (C)2005-2015 Haxe Foundation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. */
  22. import python.lib.Time;
  23. import python.lib.Os;
  24. import sys.io.FileInput;
  25. import sys.io.FileOutput;
  26. @:coreApi
  27. class Sys {
  28. static var environ:haxe.ds.StringMap<String> = {
  29. environ = new haxe.ds.StringMap();
  30. var env = Os.environ;
  31. for (key in env.keys()) {
  32. environ.set(key, env.get(key, null));
  33. }
  34. environ;
  35. }
  36. public static function time():Float {
  37. return Time.time();
  38. }
  39. public static function exit(code:Int):Void {
  40. python.lib.Sys.exit(code);
  41. }
  42. public static function print(v:Dynamic):Void {
  43. python.Lib.print(v);
  44. }
  45. public static function println(v:Dynamic):Void {
  46. python.Lib.println(v);
  47. }
  48. public static function args() : Array<String> {
  49. var argv = python.lib.Sys.argv;
  50. return argv.slice(1);
  51. }
  52. public static function getEnv( s : String ) : String {
  53. return environ.get(s);
  54. }
  55. public static function putEnv( s : String, v : String ) : Void {
  56. python.lib.Os.putenv(s, v);
  57. environ.set(s, v);
  58. }
  59. public static function environment() : Map<String,String> {
  60. return environ;
  61. }
  62. public static function sleep( seconds : Float ) : Void {
  63. python.lib.Time.sleep(seconds);
  64. }
  65. public static function setTimeLocale( loc : String ) : Bool {
  66. return false;
  67. }
  68. public static function getCwd() : String {
  69. return python.lib.Os.getcwd();
  70. }
  71. public static function setCwd( s : String ) : Void {
  72. python.lib.Os.chdir(s);
  73. }
  74. public static function systemName() : String {
  75. return switch (python.lib.Sys.platform) {
  76. case x if (StringTools.startsWith(x, "linux")):
  77. "Linux";
  78. case "darwin": "Mac";
  79. case "win32" | "cygwin" : "Windows";
  80. case _ :
  81. throw "not supported platform";
  82. }
  83. }
  84. public static function command( cmd : String, ?args : Array<String> ) : Int {
  85. var args = args == null ? [cmd] : [cmd].concat(args);
  86. return python.lib.Subprocess.call(args);
  87. }
  88. public static function cpuTime() : Float {
  89. return python.lib.Time.clock();
  90. }
  91. public static function executablePath() : String {
  92. return python.lib.Sys.argv[0];
  93. }
  94. public static function getChar( echo : Bool ) : Int {
  95. var ch = switch (systemName()) {
  96. case "Linux" | "Mac":
  97. var fd = python.lib.Sys.stdin.fileno();
  98. var old = python.lib.Termios.tcgetattr(fd);
  99. var restore = python.lib.Termios.tcsetattr.bind(fd, python.lib.Termios.TCSADRAIN, old);
  100. try {
  101. python.lib.Tty.setraw(fd);
  102. var x = python.lib.Sys.stdin.read(1);
  103. restore();
  104. x.charCodeAt(0);
  105. } catch (e:Dynamic) {
  106. restore();
  107. throw e;
  108. }
  109. case "Windows":
  110. python.lib.Msvcrt.getch().decode("utf-8").charCodeAt(0);
  111. case x :
  112. throw "platform " + x + " not supported";
  113. }
  114. if (echo) {
  115. python.Lib.print(String.fromCharCode(ch));
  116. }
  117. return ch;
  118. }
  119. public static function stdin() : haxe.io.Input {
  120. return python.io.IoTools.createFileInputFromText(python.lib.Sys.stdin);
  121. }
  122. public static function stdout() : haxe.io.Output {
  123. return python.io.IoTools.createFileOutputFromText(python.lib.Sys.stdout);
  124. }
  125. public static function stderr() : haxe.io.Output {
  126. return python.io.IoTools.createFileOutputFromText(python.lib.Sys.stderr);
  127. }
  128. }