Skip to content

Commit

Permalink
Merge pull request #225 from unihd-cag/219-block-script
Browse files Browse the repository at this point in the history
Implement the block flag for the direct mode
  • Loading branch information
TM90 authored Jul 13, 2023
2 parents e23a521 + 8fe52e9 commit 0b31532
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 6 deletions.
10 changes: 10 additions & 0 deletions docs/usage/quickstart.rst
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,13 @@ And you can pass CLI arguments to the script

If used with the above code, this will print "geGetEditCellView()" to *stdout* and
"cell view: 1234" to *stderr*.

.. warning::

Running a python script with ``pyRunScript`` does **not wait** until the script
is finished. If you rely on side effects the script causes, you can block until
the script is finished.

.. code-block:: lisp
pyRunScript "pathToScript.py" ?block t
8 changes: 5 additions & 3 deletions skillbridge/server/python_server.il
Original file line number Diff line number Diff line change
Expand Up @@ -218,10 +218,12 @@ let((_filename _baseName _moduleFolder _logDirectory _executable _unbound)
pyStartServer.logName = lsprintf("%s/skillbridge_skill.log" _logDirectory)

putd('pyRunScript nil)
defun(pyRunScript (script @key (python "python") (args nil) "ttl")
let((command)
defun(pyRunScript (script @key (python "python") (args nil) (block nil) "ttlg")
let((command process)
command = lsprintf("%s %s %s" python script buildString(args))
ipcBeginProcess(command "" '__pyOnData '__pyOnError '__pyOnFinishScript pyRunScript.logName)
process = ipcBeginProcess(command "" '__pyOnData '__pyOnError '__pyOnFinishScript pyRunScript.logName)

if(block then ipcWait(process) else process)
)
)

Expand Down
13 changes: 13 additions & 0 deletions tests/script.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from ast import literal_eval
from sys import argv
from time import sleep

from skillbridge import Symbol, Workspace

ws = Workspace.open(direct=True)

_, variable_name, value, delay = argv

sleep(float(delay))

ws['set'](Symbol(variable_name), literal_eval(value))
27 changes: 24 additions & 3 deletions tests/test_integration.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
from pathlib import Path
from time import sleep
from warnings import warn

from _pytest.python_api import raises
from pytest import fixture, skip
from pytest import fixture, raises, skip

from skillbridge import LazyList, RemoteObject, RemoteTable, SkillCode, Symbol, Workspace
from skillbridge import LazyList, RemoteObject, RemoteTable, SkillCode, Symbol, Var, Workspace

here = Path(__file__).parent


@fixture(scope='module')
Expand Down Expand Up @@ -214,3 +217,21 @@ def test_table_getattr_is_equivalent_to_symbol_lookup(ws: Workspace) -> None:

def test_nil_t_nil_is_not_a_disembodied_property_list(ws: Workspace) -> None:
assert ws["cdr"]([0, None, True, None]) == [None, True, None]


def test_run_script_does_not_block(ws: Workspace) -> None:
variable = 'skillbridge_script_args'
ws['set'](Symbol(variable), 0)
assert ws['pyRunScript'](str(here / 'script.py'), args=(variable, '42', '0.25'))

assert ws['plus'](Var(variable), 1) == 1
sleep(1.0)
assert ws['plus'](Var(variable), 1) == 43


def test_run_script_blocks_when_requested(ws: Workspace) -> None:
variable = 'skillbridge_script_args'
ws['set'](Symbol(variable), 0)
assert ws['pyRunScript'](str(here / 'script.py'), args=(variable, '42', '0.25'), block=True)

assert ws['plus'](Var(variable), 1) == 43

0 comments on commit 0b31532

Please sign in to comment.