Skip to content

Instantly share code, notes, and snippets.

@ghutchis
Created October 1, 2023 23:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ghutchis/859ad99ce82db4af5f5ba31ebb2aac18 to your computer and use it in GitHub Desktop.
Save ghutchis/859ad99ce82db4af5f5ba31ebb2aac18 to your computer and use it in GitHub Desktop.
Avogadro Script Command Tester
#!/usr/bin/env python
import sys
import json
import subprocess
# example molecule for testing
water = {
"chemicalJson": 1,
"atoms": {
"coords": {
"3d": [0.617532, -0.027246, 0.481009,
0.156663, 0.031752, -0.362419,
-0.774185, -0.004516, -0.11859],
},
"elements": {"number": [1, 8, 1]}
},
"bonds": {"connections": {"index": [1, 2, 1, 0]},
"order": [1, 1]},
}
if __name__ == "__main__":
# get the name of a python script to test
script = sys.argv[1]
# check the name of the script
name_output = subprocess.check_output([sys.executable, script, "--display-name"])
print("Name: %s" % name_output)
# check that it outputs a menu
menu_output = subprocess.check_output([sys.executable, script, "--menu-path"])
print("Menu: %s" % menu_output)
# run the script to get the options
option_output = subprocess.check_output([sys.executable, script, "--print-options"])
# options = a json document
options = json.loads(option_output)
test = {}
if "userOptions" in options:
# add the keys from userOptions
for key in options["userOptions"]:
test[key] = options["userOptions"][key]["default"]
print(len(test))
test["cjson"] = water
# run the script to get the results
input_bytes = json.dumps(test).encode("utf-8")
result_output = subprocess.check_output([sys.executable, script, "--run-command"], input=input_bytes)
print(result_output)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment