Skip to content

Commit 4375e2c

Browse files
committed
Use more robust method of checking for headless
1 parent 9624f74 commit 4375e2c

File tree

2 files changed

+11
-24
lines changed

2 files changed

+11
-24
lines changed

src/main/PythonExtension.scala

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,26 @@ import org.json4s.jackson.{ JsonMethods, Json4sScalaModule }
1313

1414
import org.nlogo.languagelibrary.Subprocess.path
1515
import org.nlogo.languagelibrary.Subprocess
16-
import org.nlogo.languagelibrary.config.{ Config, ConfigProperty, FileProperty, Menu, Platform }
16+
import org.nlogo.languagelibrary.config.{ Config, ConfigProperty, FileProperty, Menu }
1717

1818
import org.nlogo.api
1919
import org.nlogo.api._
2020
import org.nlogo.core.{ LogoList, Syntax }
21-
import org.nlogo.workspace.{ AbstractWorkspace, ExtensionManager => WorkspaceExtensionManager }
21+
import org.nlogo.workspace.AbstractWorkspace
2222

2323
import scala.collection.immutable.ArraySeq
2424

2525
object PythonExtension {
2626
val codeName = "py"
2727
val longName = "Python"
28-
val extLangBin = if (Platform.isWindows) { "python" } else { "python3" }
28+
val extLangBin = if (System.getProperty("os.name").toLowerCase.startsWith("win")) { "python" } else { "python3" }
2929

3030
private var _pythonProcess: Option[Subprocess] = None
3131

3232
var menu: Option[Menu] = None
3333
val config: Config = Config.createForPropertyFile(classOf[PythonExtension], PythonExtension.codeName)
3434

35-
var headless = GraphicsEnvironment.isHeadless || System.getProperty("org.nlogo.preferHeadless") == "true"
35+
var isHeadless = true
3636

3737
def pythonProcess: Subprocess = {
3838
_pythonProcess.getOrElse(throw new ExtensionException(
@@ -72,21 +72,9 @@ class PythonExtension extends api.DefaultClassManager {
7272
override def runOnce(em: ExtensionManager): Unit = {
7373
super.runOnce(em)
7474

75-
PythonExtension.headless = PythonExtension.headless | (em match {
76-
case wem: WorkspaceExtensionManager =>
77-
wem.workspace match {
78-
case aw: AbstractWorkspace if aw.isHeadless =>
79-
true
75+
PythonExtension.isHeadless = !em.workspaceContext.workspaceGUI
8076

81-
case _ =>
82-
false
83-
}
84-
85-
case _ =>
86-
false
87-
})
88-
89-
if (!PythonExtension.headless) {
77+
if (!PythonExtension.isHeadless) {
9078
val py2Message = s"It is recommended to use Python 3 if possible and enter its path above. If you must use Python 2, enter the path to its executable folder below."
9179
val py2Property = new FileProperty("python2", "python2", PythonExtension.config.get("python2").getOrElse(""), py2Message)
9280
PythonExtension.menu = Menu.create(em, PythonExtension.longName, PythonExtension.extLangBin, PythonExtension.config, Seq(py2Property))
@@ -97,7 +85,7 @@ class PythonExtension extends api.DefaultClassManager {
9785
super.unload(em)
9886
PythonExtension.killPython()
9987

100-
if (!PythonExtension.headless)
88+
if (!PythonExtension.isHeadless)
10189
PythonExtension.menu.foreach(_.unload())
10290
}
10391

@@ -114,7 +102,7 @@ object Using {
114102
object PythonSubprocess {
115103
def python2: Option[File] = {
116104
val maybePy2File = PythonExtension.config.get("python2").map( (dir) => {
117-
val bin = if (Platform.isWindows) { "python.exe" } else { "python2" }
105+
val bin = if (System.getProperty("os.name").toLowerCase.startsWith("win")) { "python.exe" } else { "python2" }
118106
val path = Paths.get(dir, bin)
119107
new File(path.toString)
120108
})
@@ -158,7 +146,7 @@ object SetupPython extends api.Command {
158146
PythonExtension.codeName, PythonExtension.longName,
159147
customMapper = Option(mapper))
160148

161-
if (!PythonExtension.headless)
149+
if (!PythonExtension.isHeadless)
162150
PythonExtension.menu.foreach(_.setup(PythonExtension.pythonProcess.evalStringified))
163151
} catch {
164152
case e: Exception =>

src/test/Tests.scala

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@
33
package org.nlogo.extensions.py
44

55
import java.io.File
6+
67
import org.nlogo.headless.TestLanguage
78

89
object Tests {
910
val testFileNames = Seq("tests.txt")
1011
val testFiles = testFileNames.map( (f) => (new File(f)).getCanonicalFile )
1112
}
1213

13-
class Tests extends TestLanguage(Tests.testFiles) {
14-
System.setProperty("org.nlogo.preferHeadless", "true")
15-
}
14+
class Tests extends TestLanguage(Tests.testFiles)

0 commit comments

Comments
 (0)