blueloveTH 3 years ago
parent
commit
d954e74188
2 changed files with 27 additions and 0 deletions
  1. 26 0
      src/builtins.h
  2. 1 0
      src/pocketpy.h

+ 26 - 0
src/builtins.h

@@ -282,6 +282,32 @@ def open(path, mode='r'):
     return FileIO(path, mode)
 )";
 
+const char* __OS_CODE = R"(
+def listdir(path):
+  assert type(path) is str
+  return jsonrpc("os.listdir", [path])
+
+def mkdir(path):
+  assert type(path) is str
+  return jsonrpc("os.mkdir", [path])
+
+def rmdir(path):
+  assert type(path) is str
+  return jsonrpc("os.rmdir", [path])
+
+def remove(path):
+  assert type(path) is str
+  return jsonrpc("os.remove", [path])
+
+path = object()
+
+def __path4exists(path):
+  assert type(path) is str
+  return jsonrpc("os.path.exists", [path])
+path.exists = __path4exists
+del __path4exists
+)";
+
 const char* __RANDOM_CODE = R"(
 import time as _time
 

+ 1 - 0
src/pocketpy.h

@@ -769,6 +769,7 @@ extern "C" {
         if(code == nullptr) exit(1);
         vm->_exec(code, vm->builtins, {});
         pkpy_vm_add_module(vm, "random", __RANDOM_CODE);
+        pkpy_vm_add_module(vm, "os", __OS_CODE);
     }
 
     __EXPORT