1313import os
1414import re
1515import shutil
16+ import sys
1617import whitebox
1718import urllib .request
1819from zipfile import ZipFile
@@ -96,7 +97,8 @@ def generate_tool_template(tool):
9697 lines .append ("class {}(object):\n " .format (tool ["name" ]))
9798 lines .append (" def __init__(self):\n " )
9899 lines .append (' self.label = "{}"\n ' .format (tool ["label" ]))
99- lines .append (' self.description = "{}"\n ' .format (tool ["description" ]))
100+ lines .append (' self.description = "{}"\n ' .format (
101+ tool ["description" ]))
100102 lines .append (' self.category = "{}"\n \n ' .format (tool ["category" ]))
101103 lines .append (" def getParameterInfo(self):\n " )
102104 # Loop through parameters
@@ -120,7 +122,7 @@ def generate_tool_template(tool):
120122 lines .append (" param.clearMessage()\n " )
121123 lines .append (" return\n \n " )
122124 lines .append (" def execute(self, parameters, messages):\n " )
123- # Access parameters throught parameters[x].valueAsText
125+ # Access parameters through parameters[x].valueAsText
124126 lines .append (define_execute (tool ["parameters" ]))
125127 # redirect standard output to tool dialogue
126128 lines .append (" old_stdout = sys.stdout\n " )
@@ -130,7 +132,8 @@ def generate_tool_template(tool):
130132 # line = ' wbt.{}({})\n'.format(to_snakecase(tool['name']), ', '.join(tool['parameters']).replace(", class,", ", cls,"))
131133 line = " wbt.{}({})\n " .format (
132134 to_snakecase (tool ["name" ]),
133- ", " .join (tool_params ).replace (", class=class," , ", cls=cls," ).replace ("input=input" , "i=i" ),
135+ ", " .join (tool_params ).replace (", class=class," ,
136+ ", cls=cls," ).replace ("input=input" , "i=i" ),
134137 )
135138
136139 # Deal with name conflict with reserved Python functions (and, or, not)
@@ -176,7 +179,8 @@ def define_tool_params(params):
176179 if data_type ["data_type" ] == '"DERasterDataset"' and direction == "Output" :
177180 data_type ["data_type" ] = '"DEFile"'
178181 data_type ["data_filter" ] = '["tif"]'
179- parameter_type = "Required" # if a filter is used, the parameter must be changed to required.
182+ # if a filter is used, the parameter must be changed to required.
183+ parameter_type = "Required"
180184 elif data_type ["data_type" ] == '"DERasterDataset"' and direction == "Input" :
181185 data_type ["data_type" ] = '"GPRasterLayer"'
182186 elif data_type ["data_type" ] == '"DEShapefile"' and direction == "Input" :
@@ -193,14 +197,18 @@ def define_tool_params(params):
193197 lines .append (" {} = arcpy.Parameter(\n " .format (param ))
194198 lines .append (' displayName="{}",\n ' .format (items ["name" ]))
195199 lines .append (' name="{}",\n ' .format (param ))
196- lines .append (" datatype={},\n " .format (data_type ["data_type" ]))
197- lines .append (' parameterType="{}",\n ' .format (parameter_type ))
200+ lines .append (" datatype={},\n " .format (
201+ data_type ["data_type" ]))
202+ lines .append (
203+ ' parameterType="{}",\n ' .format (parameter_type ))
198204 lines .append (' direction="{}")\n ' .format (direction ))
199205
200206 if data_type ["multi_value" ]:
201207 lines .append (" {}.multiValue = True\n " .format (param ))
202208
203209 if len (data_type ["dependency_field" ]) > 0 :
210+ if data_type ["dependency_field" ] == "input" :
211+ data_type ["dependency_field" ] = "i"
204212 lines .append (
205213 " {}.parameterDependencies = [{}.name]\n " .format (
206214 param , data_type ["dependency_field" ]
@@ -209,7 +217,8 @@ def define_tool_params(params):
209217
210218 if data_type ["data_filter" ] != "[]" :
211219 if data_type ["filter_type" ] == '"ValueList"' :
212- lines .append (' {}.filter.type = "ValueList"\n ' .format (param ))
220+ lines .append (
221+ ' {}.filter.type = "ValueList"\n ' .format (param ))
213222
214223 if (
215224 data_type ["data_filter" ]
@@ -231,7 +240,8 @@ def define_tool_params(params):
231240 items ["default_value" ] = True
232241
233242 lines .append (
234- " {}.value = '{}'\n \n " .format (param , items ["default_value" ])
243+ " {}.value = '{}'\n \n " .format (
244+ param , items ["default_value" ])
235245 )
236246 else :
237247 lines .append ("\n " )
@@ -281,7 +291,8 @@ def define_execute(params):
281291 param = "i"
282292
283293 # deal with multi-value inputs
284- lines .append (" {} = parameters[{}].valueAsText\n " .format (param , index ))
294+ lines .append (
295+ " {} = parameters[{}].valueAsText\n " .format (param , index ))
285296 if data_type ["multi_value" ]:
286297 lines .append (" if {} is not None:\n " .format (param ))
287298 lines .append (' items = {}.split(";")\n ' .format (param ))
@@ -290,22 +301,27 @@ def define_execute(params):
290301 lines .append (
291302 " items_path.append(arcpy.Describe(item).catalogPath)\n "
292303 )
293- lines .append (' {} = ";".join(items_path)\n ' .format (param ))
304+ lines .append (
305+ ' {} = ";".join(items_path)\n ' .format (param ))
294306
295307 if param_type in inputRasVec :
296308 # lines.append(' desc = arcpy.Describe({})\n'.format(param))
297309 # lines.append(' {} = desc.catalogPath\n'.format(param))
298310 # if param_type == "Optional":
299311 lines .append (" if {} is not None:\n " .format (param ))
300- lines .append (" desc = arcpy.Describe({})\n " .format (param ))
312+ lines .append (
313+ " desc = arcpy.Describe({})\n " .format (param ))
301314 lines .append (" {} = desc.catalogPath\n " .format (param ))
302315 elif param_type == {"ExistingFileOrFloat" : "Raster" }:
303316 lines .append (" if {} is not None:\n " .format (param ))
304317 lines .append (" try:\n " )
305- lines .append (" {} = str(float({}))\n " .format (param , param ))
318+ lines .append (
319+ " {} = str(float({}))\n " .format (param , param ))
306320 lines .append (" except:\n " )
307- lines .append (" desc = arcpy.Describe({})\n " .format (param ))
308- lines .append (" {} = desc.catalogPath\n " .format (param ))
321+ lines .append (
322+ " desc = arcpy.Describe({})\n " .format (param ))
323+ lines .append (
324+ " {} = desc.catalogPath\n " .format (param ))
309325
310326 # lines.append(' if ({} is not None) and {}.isnumeric() == False:\n'.format(param, param))
311327
@@ -438,7 +454,7 @@ def get_github_tag(tool_name, category):
438454
439455def get_book_url (tool_name , category ):
440456 """
441- Get link to WhiteboxTools User Mannual
457+ Get link to WhiteboxTools User Manual
442458 """
443459 prefix = "https://www.whiteboxgeo.com/manual/wbt_book/available_tools"
444460 url = "{}/{}.html#{}" .format (prefix , category , tool_name )
@@ -451,7 +467,8 @@ def get_book_tag(tool_name, category):
451467 """
452468 prefix = "https://www.whiteboxgeo.com/manual/wbt_book/available_tools"
453469 url = "{}/{}.html#{}" .format (prefix , category , tool_name )
454- html_tag = "<a href='{}' target='_blank'>WhiteboxTools User Manual</a>" .format (url )
470+ html_tag = "<a href='{}' target='_blank'>WhiteboxTools User Manual</a>" .format (
471+ url )
455472 return html_tag
456473
457474
@@ -531,8 +548,31 @@ def get_book_tag(tool_name, category):
531548with open (wbt_py ) as f :
532549 lines = f .readlines ()
533550 for line in lines :
551+ if line .strip () == "import urllib.request" :
552+ line = ""
553+ if line .strip () == "from subprocess import CalledProcessError, Popen, PIPE, STDOUT" :
554+ line = """
555+ from subprocess import CalledProcessError, Popen, PIPE, STDOUT
556+ if sys.version_info.major == 2:
557+ import urllib2 as urlopen
558+ else:
559+ import urllib.request as urlopen
560+ """
534561 if 'f"={toolname}"' in line :
535562 line = ' args.append("={}".format(toolname))'
563+ if 'f"Warning: Unrecognized extension ext_name {ext_name}' in line :
564+ line = ' print("Warning: Unrecognized extension ext_name {}. Installing the GTE instead...".format(ext_name))\n '
565+ if line .strip () == "for entry in os.scandir(f'./{unzipped_dir_name}'):" :
566+ line = " for entry in os.scandir('./{}'.format(unzipped_dir_name)):\n "
567+ if line .strip () == "new_path = entry.path.replace(f'{unzipped_dir_name}', 'plugins')" :
568+ line = " new_path = entry.path.replace('{}'.format(unzipped_dir_name), 'plugins')\n "
569+ if line .strip () == "if os.path.exists(f'./{unzipped_dir_name}'):" :
570+ line = " if os.path.exists('./{}'.format(unzipped_dir_name)):\n "
571+ if line .strip () == "shutil.rmtree(f'./{unzipped_dir_name}')" :
572+ line = " shutil.rmtree('./{}'.format(unzipped_dir_name))\n "
573+ if "urllib.request" in line :
574+ line = line .replace ("urllib.request" , "urlopen" )
575+
536576 outlines .append (line )
537577
538578with open (wbt_py , "w" ) as f :
0 commit comments