33import logging
44import os
55import pathlib
6+ import subprocess
67import typing as t
78
89logger = logging .getLogger (__name__ )
@@ -16,9 +17,10 @@ def optional_windows_and_pane(
1617 The function evaluates the 'if' condition specified in `workspace_dict` to determine inclusion:
1718 - If 'if' key is not present, it defaults to True.
1819 - If 'if' is a string or boolean, it's treated as a shell variable.
19- - 'if' can be a dictionary containing 'shell' or 'python' keys with valid expressions.
20- - 'shell' expressions are expanded and checked against true values ('y', 'yes', '1', 'on', 'true', 't').
21- - 'python' expressions are evaluated using `eval()`
20+ - 'if' can be a dictionary containing 'shell', 'shell_var' or 'python' keys with valid expressions.
21+ - 'shell_var' expressions are expanded and checked against true values ('y', 'yes', '1', 'on', 'true', 't').
22+ - 'shell' expressions are evaluated using subprocess
23+ - 'python' expressions are evaluated using `exec()`
2224
2325 Parameters
2426 ----------
@@ -35,18 +37,22 @@ def optional_windows_and_pane(
3537 if_cond = workspace_dict ["if" ]
3638 if isinstance (if_cond , (str , bool )):
3739 # treat this as shell variable
38- if_cond = {"shell " : if_cond }
39- if not isinstance (if_cond , dict ) or not ( "shell" in if_cond or "python" in if_cond ):
40+ if_cond = {"shell_var " : if_cond }
41+ if not isinstance (if_cond , dict ) or not any ( predicate in if_cond for predicate in ( "python" , "shell" , "shell_var" ) ):
4042 raise ValueError (f"if conditions does not contains valid expression: { if_cond } " )
43+ if "shell_var" in if_cond :
44+ if expandshell (str (if_cond ["shell_var" ])).lower () not in ("y" , "yes" , "1" , "on" , "true" , "t" ):
45+ return False
4146 if "shell" in if_cond :
42- if isinstance (if_cond ["shell" ], str ):
43- if expandshell (if_cond ["shell" ]).lower () not in ("y" , "yes" , "1" , "on" , "true" , "t" ):
44- return False
45- elif isinstance (if_cond ["shell" ], bool ):
46- if not if_cond ["shell" ]:
47- return False
47+ if subprocess .run (if_cond ["shell" ], shell = True ).returncode != 0 :
48+ return False
4849 if "python" in if_cond :
49- if if_cond ["python" ] and not eval (if_cond ["python" ]): # dangerous
50+ # assign the result of the last statement from the python snippet
51+ py_statements = if_cond ["python" ].split (";" )
52+ py_statements [- 1 ] = f"ret={ py_statements [- 1 ]} "
53+ locals = {}
54+ exec (";" .join (py_statements ), {}, locals )
55+ if not locals ['ret' ]:
5056 return False
5157 return True
5258
0 commit comments