Skip to content

Commit

Permalink
Deal with tempfile on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcCote committed May 17, 2024
1 parent c1ccc6c commit dbc2c18
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 9 deletions.
Binary file modified scienceworld/scienceworld.jar
Binary file not shown.
12 changes: 7 additions & 5 deletions scienceworld/scienceworld.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import logging
import tempfile
from collections import OrderedDict
from os.path import join as pjoin

from py4j.java_gateway import JavaGateway, GatewayParameters, launch_gateway, CallbackServerParameters

Expand Down Expand Up @@ -80,7 +81,7 @@ def __init__(self, taskName: str = None, serverPath: str = None, envStepLimit: i
# By default, set that the gold path was not generated unless the user asked for it
self.goldPathGenerated = False

self._obj_tree_tempfile = tempfile.NamedTemporaryFile()
self._obj_tree_tempdir = tempfile.TemporaryDirectory()

# Ask the simulator to load an environment from a script
def load(self, taskName: str, variationIdx: int = 0, simplificationStr: str = "",
Expand Down Expand Up @@ -276,14 +277,15 @@ def get_task_description(self) -> str:

# Get the current game's task description
def getObjectTree(self):
msg = self.server.getObjectTree(self._obj_tree_tempfile.name)
msg = self.server.getObjectTree(self._obj_tree_tempdir.name)
if msg:
# Game is not initialized.
raise RuntimeError(msg)

self._obj_tree_tempfile.file.seek(0)
payload = self._obj_tree_tempfile.file.read()
return json.loads(payload)
with open(pjoin(self._obj_tree_tempdir.name, 'objectTree.json')) as f:
payload = json.load(f)

return payload

#
# History
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package scienceworld.runtime.pythonapi

import java.io.PrintWriter
import java.io.{File, FileOutputStream,PrintWriter}

import main.scala.scienceworld.runtime.SimplifierProcessor
import scienceworld.environments.EnvironmentMaker
Expand Down Expand Up @@ -325,13 +325,13 @@ class PythonInterface() {
agentInterface.get.getTaskDescription()
}

def getObjectTree(path:String = ""):String = {
def getObjectTree(folderPath:String = ""):String = {
if (agentInterface.isEmpty) return ERROR_MESSAGE_UNINITIALIZED

val objTree = agentInterface.get.universe.toJSON()
if (path == "") return objTree
if (folderPath == "") return objTree

val pw = new PrintWriter(path)
var pw = new PrintWriter(folderPath + "/objectTree.json");
pw.print(objTree)
pw.close()
return ""
Expand Down

0 comments on commit dbc2c18

Please sign in to comment.