2828import logging .handlers
2929from logging .handlers import WatchedFileHandler
3030import os
31+ import re
3132
3233import grpc
3334import tqdm
@@ -102,7 +103,7 @@ def set_console_log_level(self, log_level):
102103 Parameters
103104 ----------
104105 log_level : str, default: "error"
105- Level of logging. Options are "critical" "debug", "error", "info", and "warning".
106+ Level of logging. Options are "critical", "debug", "error", "info", and "warning".
106107 """
107108 self .__log_console_handler .setLevel (WorkbenchClient .__to_python_log_level (log_level ))
108109
@@ -135,15 +136,18 @@ def reset_log_file(self):
135136 __log_file_handler = None
136137 __log_console_handler = None
137138
138- def run_script_string (self , script_string , log_level = "error" ):
139+ def run_script_string (self , script_string , args = None , log_level = "error" ):
139140 """Run a script as given in the input string on the server.
140141
141142 Parameters
142143 ----------
143144 script_string : str
144145 String containing the content of the script to run.
146+ args : dictionary of script variable names and values
147+ Variables in the script specified as $$varname%%50%% will be converted to variable
148+ values or use the default value - 50 in the example.
145149 log_level : str, default: "error"
146- Level of logging. Options are "critical" "debug", "error", "info", and "warning".
150+ Level of logging. Options are "critical", "debug", "error", "info", and "warning".
147151
148152 Returns
149153 -------
@@ -162,8 +166,22 @@ def run_script_string(self, script_string, log_level="error"):
162166 """
163167 if not self ._is_connected ():
164168 logging .error ("Workbench client is not yet connected to a server." )
169+ return None
170+ updated_script_string = script_string
171+ if args and len (args ) > 0 :
172+ if any (not re .match (r"^\w+$" , arg ) for arg in args .keys ()):
173+ logging .error ("script argument name contains illegal character." )
174+ return None
175+ for arg_name in args :
176+ updated_script_string = re .sub (
177+ r"\$\$" + arg_name + r"%%((?!%%).)*%%" ,
178+ str (args [arg_name ]),
179+ updated_script_string ,
180+ )
181+ updated_script_string = re .sub (r"\$\$\w+%%(((?!%%).)*)%%" , r"\1" , updated_script_string )
165182 request = wb .RunScriptRequest (
166- content = script_string , log_level = WorkbenchClient .__to_server_log_level (log_level )
183+ content = updated_script_string ,
184+ log_level = WorkbenchClient .__to_server_log_level (log_level ),
167185 )
168186 for response in self .stub .RunScript (request ):
169187 if response .log and response .log .messages and len (response .log .messages ) > 0 :
@@ -177,16 +195,19 @@ def run_script_string(self, script_string, log_level="error"):
177195 logging .info ("The script has finisished." )
178196 return json .loads (response .result .result )
179197
180- def run_script_file (self , script_file_name , log_level = "error" ):
198+ def run_script_file (self , script_file_name , args = None , log_level = "error" ):
181199 """Run a script file on the server.
182200
183201 Parameters
184202 ----------
185203 script_file_name : str
186204 Name of the script file to run. The script file should be located in the client
187205 working directory
206+ args : dictionary of script variable names and values
207+ Variables in the script specified as $$varname%%50%% will be converted to variable
208+ values or use the default value - 50 in the example.
188209 log_level : str, default: "error"
189- Level of logging. Options are "critical" "debug", "error", "info", and "warning".
210+ Level of logging. Options are "critical", "debug", "error", "info", and "warning".
190211
191212 Returns
192213 -------
@@ -195,10 +216,11 @@ def run_script_file(self, script_file_name, log_level="error"):
195216 """
196217 if not self ._is_connected ():
197218 logging .error ("Workbench client is not yet connected to a server" )
219+ return None
198220 script_path = os .path .join (self .workdir , script_file_name )
199221 with open (script_path , encoding = "utf-8-sig" ) as sf :
200222 script_string = sf .read ()
201- return self .run_script_string (script_string , log_level )
223+ return self .run_script_string (script_string , args , log_level )
202224
203225 def upload_file (self , * file_list , show_progress = True ):
204226 """Upload one or more files from the client to the server.
@@ -218,6 +240,7 @@ def upload_file(self, *file_list, show_progress=True):
218240 """
219241 if not self ._is_connected ():
220242 logging .error ("Workbench client is not yet connected to a server." )
243+ return
221244 requested = []
222245 for file_pattern in file_list :
223246 if "*" in file_pattern or "?" in file_pattern :
@@ -289,6 +312,7 @@ def upload_file_from_example_repo(self, relative_file_path, show_progress=True):
289312 """
290313 if not self ._is_connected ():
291314 logging .error ("Workbench client is not yet connected to a server." )
315+ return
292316 downloaded = ExampleData .download (relative_file_path , self .workdir )
293317 self .upload_file (downloaded , show_progress = show_progress )
294318
@@ -315,6 +339,7 @@ def download_file(self, file_name, show_progress=True, target_dir=None):
315339 """
316340 if not self ._is_connected ():
317341 logging .error ("Workbench client is not yet connected to a server." )
342+ return None
318343 request = wb .DownloadFileRequest (file_name = file_name )
319344 file_name = file_name .replace ("*" , "_" ).replace ("?" , "_" )
320345 td = target_dir
@@ -406,7 +431,7 @@ def __to_server_log_level(log_level):
406431 Parameters
407432 ----------
408433 log_level : str
409- Level of logging. Options are "critical" "debug", "error", "info", and "warning".
434+ Level of logging. Options are "critical", "debug", "error", "info", and "warning".
410435
411436 Returns
412437 -------
0 commit comments