| 1 |
#!/usr/bin/env python |
| 2 |
|
| 3 |
import argparse |
| 4 |
import os |
| 5 |
import re |
| 6 |
import sys |
| 7 |
import shlex |
| 8 |
from pprint import pprint |
| 9 |
|
| 10 |
def trim(s): |
| 11 |
""" |
| 12 |
All repetition of any kind of space is replaced by a single space |
| 13 |
and remove trailing space at beginning or end. |
| 14 |
""" |
| 15 |
# regex to replace all space groups by a single space |
| 16 |
# use split() to remove trailing space at beginning/end |
| 17 |
return re.sub(r'\s+', ' ', s).strip() |
| 18 |
|
| 19 |
|
| 20 |
def quotesForStrings(valueStr): |
| 21 |
""" |
| 22 |
Return the input string with quotes if it cannot be cast into another builtin type. |
| 23 |
""" |
| 24 |
v = valueStr |
| 25 |
try: |
| 26 |
int(valueStr) |
| 27 |
except ValueError: |
| 28 |
try: |
| 29 |
float(valueStr) |
| 30 |
except ValueError: |
| 31 |
if "'" in valueStr: |
| 32 |
v = f"'''{valueStr}'''" |
| 33 |
else: |
| 34 |
v = f"'{valueStr}'" |
| 35 |
return v |
| 36 |
|
| 37 |
def convertToLabel(name): |
| 38 |
camelCaseToLabel = re.sub('()([A-Z][a-z]*?)', r'\1 \2', name) |
| 39 |
snakeToLabel = ' '.join(word.capitalize() for word in camelCaseToLabel.split('_')) |
| 40 |
snakeToLabel = ' '.join(word.capitalize() for word in snakeToLabel.split(' ')) |
| 41 |
return snakeToLabel |
| 42 |
|
| 43 |
def is_int(s): |
| 44 |
try: |
| 45 |
int(s) |
| 46 |
return True |
| 47 |
except ValueError: |
| 48 |
return False |
| 49 |
|
| 50 |
def is_float(s): |
| 51 |
try: |
| 52 |
float(s) |
| 53 |
return True |
| 54 |
except ValueError: |
| 55 |
return False |
| 56 |
|
| 57 |
|
| 58 |
parser = argparse.ArgumentParser(description='Create a new Node Type') |
| 59 |
parser.add_argument('node', metavar='NODE_NAME', type=str, |
| 60 |
help='New node name') |
| 61 |
parser.add_argument('bin', metavar='CMDLINE', type=str, |
| 62 |
default=None, |
| 63 |
help='Input executable') |
| 64 |
parser.add_argument('--output', metavar='DIR', type=str, |
| 65 |
default=os.path.dirname(__file__), |
| 66 |
help='Output plugin folder') |
| 67 |
parser.add_argument('--parser', metavar='PARSER', type=str, |
| 68 |
default='boost', |
| 69 |
help='Select the parser adapted for your command line: {boost,cmdLineLib,basic}.') |
| 70 |
parser.add_argument("--force", help="Allows to overwrite the output plugin file.", |
| 71 |
action="store_true") |
| 72 |
|
| 73 |
args = parser.parse_args() |
| 74 |
|
| 75 |
inputCmdLineDoc = None |
| 76 |
soft = "{nodeType}" |
| 77 |
if args.bin: |
| 78 |
soft = args.bin |
| 79 |
import subprocess |
| 80 |
proc = subprocess.Popen(args=shlex.split(args.bin) + ['--help'], stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 81 |
stdout, stderr = proc.communicate() |
| 82 |
inputCmdLineDoc = stdout if stdout else stderr |
| 83 |
elif sys.stdin.isatty(): |
| 84 |
inputCmdLineDoc = ''.join([line for line in sys.stdin]) |
| 85 |
|
| 86 |
if not inputCmdLineDoc: |
| 87 |
print('No input documentation.') |
| 88 |
print(f'Usage: YOUR_COMMAND --help | {os.path.splitext(__file__)[0]}') |
| 89 |
sys.exit(-1) |
| 90 |
|
| 91 |
|
| 92 |
fileStr = '''import sys |
| 93 |
from meshroom.core import desc |
| 94 |
|
| 95 |
|
| 96 |
class __COMMANDNAME__(desc.CommandLineNode): |
| 97 |
commandLine = '__SOFT__ {allParams}' |
| 98 |
'''.replace('__COMMANDNAME__', args.node).replace('__SOFT__', soft) |
| 99 |
|
| 100 |
|
| 101 |
print(inputCmdLineDoc) |
| 102 |
|
| 103 |
args_re = None |
| 104 |
if args.parser == 'boost': |
| 105 |
args_re = re.compile( |
| 106 |
r'^\s+' # space(s) |
| 107 |
r'(?:-(?P<argShortName>\w+)\|?)?' # potential argument short name |
| 108 |
r'\s*\[?' # potential '[' |
| 109 |
r'\s*--(?P<argLongName>\w+)' # argument long name |
| 110 |
r'(?:\s*\])?' # potential ']' |
| 111 |
r'(?:\s+(?P<arg>\w+)?)?' # potential arg |
| 112 |
r'(?:\s+\(\=(?P<defaultValue>.+)\))?' # potential default value |
| 113 |
r'\s+(?P<descriptionFirst>.*?)\n' # end of the line |
| 114 |
r'(?P<descriptionNext>(?:\s+[^-\s].+?\n)*)' # next documentation lines |
| 115 |
, re.MULTILINE) |
| 116 |
elif args.parser == 'cmdLineLib': |
| 117 |
args_re = re.compile( |
| 118 |
'^' |
| 119 |
r'\[' # '[' |
| 120 |
r'-(?P<argShortName>\w+)' # argument short name |
| 121 |
r'\|' |
| 122 |
r'--(?P<argLongName>\w+)' # argument long name |
| 123 |
r'(?:\s+(?P<arg>\w+)?)?' # potential arg |
| 124 |
r'\]' # ']' |
| 125 |
r'()' # no default value |
| 126 |
r'(?P<descriptionFirst>.*?)?\n' # end of the line |
| 127 |
r'(?P<descriptionNext>(?:[^\[\w].+?\n)*)' # next documentation lines |
| 128 |
, re.MULTILINE) |
| 129 |
elif args.parser == 'basic': |
| 130 |
args_re = re.compile(r'()--(?P<argLongName>\w+)()()()()') |
| 131 |
else: |
| 132 |
print(f'Error: Unknown input parser "{args.parser}"') |
| 133 |
sys.exit(-1) |
| 134 |
|
| 135 |
choiceValues1_re = re.compile(r'\* (?P<value>\w+):') |
| 136 |
choiceValues2_re = re.compile(r'\((?P<value>.+?)\)') |
| 137 |
choiceValues3_re = re.compile(r'\{(?P<value>.+?)\}') |
| 138 |
|
| 139 |
cmdLineArgs = args_re.findall(inputCmdLineDoc.decode('utf-8')) |
| 140 |
|
| 141 |
print('='*80) |
| 142 |
pprint(cmdLineArgs) |
| 143 |
|
| 144 |
outputNodeStr = '' |
| 145 |
inputNodeStr = '' |
| 146 |
|
| 147 |
for cmdLineArg in cmdLineArgs: |
| 148 |
shortName = cmdLineArg[0] |
| 149 |
longName = cmdLineArg[1] |
| 150 |
if longName == 'help': |
| 151 |
continue # skip help argument |
| 152 |
|
| 153 |
arg = cmdLineArg[2] |
| 154 |
value = cmdLineArg[3] |
| 155 |
descLines = cmdLineArg[4:] |
| 156 |
description = ''.join(descLines).strip() |
| 157 |
if description.endswith(':'): |
| 158 |
# If documentation is multiple lines and the last line ends with ':', |
| 159 |
# we remove this last line as it is probably the title of the next group of options |
| 160 |
description = '\n'.join(description.split('\n')[:-1]) |
| 161 |
description = trim(description) |
| 162 |
|
| 163 |
values = choiceValues1_re.findall(description) |
| 164 |
if not values: |
| 165 |
possibleLists = choiceValues2_re.findall(description) + choiceValues3_re.findall(description) |
| 166 |
for possibleList in possibleLists: |
| 167 |
candidate = possibleList.split(',') |
| 168 |
if len(candidate) > 1: |
| 169 |
values = [trim(v) for v in candidate] |
| 170 |
|
| 171 |
cmdLineArgLower = ' '.join([shortName, longName, arg, value, description]).lower() |
| 172 |
namesLower = ' '.join([shortName, longName]).lower() |
| 173 |
isBool = (arg == '' and value == '') |
| 174 |
isFile = 'path' in cmdLineArgLower or 'folder' in cmdLineArgLower or 'file' in cmdLineArgLower |
| 175 |
isChoice = bool(values) |
| 176 |
isOutput = 'output' in cmdLineArgLower or 'out' in namesLower |
| 177 |
isInt = is_int(value) |
| 178 |
isFloat = is_float(value) |
| 179 |
|
| 180 |
argStr = None |
| 181 |
if isBool: |
| 182 |
argStr = """ |
| 183 |
desc.BoolParam( |
| 184 |
name='{name}', |
| 185 |
label='{label}', |
| 186 |
description='''{description}''', |
| 187 |
value={value}, |
| 188 |
),""".format( |
| 189 |
name=longName, |
| 190 |
label=convertToLabel(longName), |
| 191 |
description=description, |
| 192 |
value=quotesForStrings(value), |
| 193 |
arg=arg, |
| 194 |
) |
| 195 |
elif isFile: |
| 196 |
argStr = """ |
| 197 |
desc.File( |
| 198 |
name='{name}', |
| 199 |
label='{label}', |
| 200 |
description='''{description}''', |
| 201 |
value={value}, |
| 202 |
),""".format( |
| 203 |
name=longName, |
| 204 |
label=convertToLabel(longName), |
| 205 |
description=description, |
| 206 |
value=quotesForStrings(value), |
| 207 |
arg=arg, |
| 208 |
) |
| 209 |
elif isChoice: |
| 210 |
argStr = """ |
| 211 |
desc.ChoiceParam( |
| 212 |
name='{name}', |
| 213 |
label='{label}', |
| 214 |
description='''{description}''', |
| 215 |
value={value}, |
| 216 |
values={values}, |
| 217 |
exclusive={exclusive}, |
| 218 |
),""".format( |
| 219 |
name=longName, |
| 220 |
label=convertToLabel(longName), |
| 221 |
description=description, |
| 222 |
value=quotesForStrings(value), |
| 223 |
values=values, |
| 224 |
exclusive=True, |
| 225 |
) |
| 226 |
elif isInt: |
| 227 |
argStr = """ |
| 228 |
desc.IntParam( |
| 229 |
name='{name}', |
| 230 |
label='{label}', |
| 231 |
description='''{description}''', |
| 232 |
value={value}, |
| 233 |
range={range}, |
| 234 |
),""".format( |
| 235 |
name=longName, |
| 236 |
label=convertToLabel(longName), |
| 237 |
description=description, |
| 238 |
value=value, |
| 239 |
range='(-sys.maxsize, sys.maxsize, 1)', |
| 240 |
) |
| 241 |
elif isFloat: |
| 242 |
argStr = """ |
| 243 |
desc.FloatParam( |
| 244 |
name='{name}', |
| 245 |
label='{label}', |
| 246 |
description='''{description}''', |
| 247 |
value={value}, |
| 248 |
range={range}, |
| 249 |
),""".format( |
| 250 |
name=longName, |
| 251 |
label=convertToLabel(longName), |
| 252 |
description=description, |
| 253 |
value=value, |
| 254 |
range='''(-float('inf'), float('inf'), 0.01)''', |
| 255 |
) |
| 256 |
else: |
| 257 |
argStr = """ |
| 258 |
desc.StringParam( |
| 259 |
name='{name}', |
| 260 |
label='{label}', |
| 261 |
description='''{description}''', |
| 262 |
value={value}, |
| 263 |
),""".format( |
| 264 |
name=longName, |
| 265 |
label=convertToLabel(longName), |
| 266 |
description=description, |
| 267 |
value=quotesForStrings(value), |
| 268 |
range=range, |
| 269 |
) |
| 270 |
if isOutput: |
| 271 |
outputNodeStr += argStr |
| 272 |
else: |
| 273 |
inputNodeStr += argStr |
| 274 |
|
| 275 |
|
| 276 |
fileStr += """ |
| 277 |
inputs = [""" + inputNodeStr + """ |
| 278 |
] |
| 279 |
|
| 280 |
outputs = [""" + outputNodeStr + """ |
| 281 |
] |
| 282 |
""" |
| 283 |
|
| 284 |
outputFilepath = os.path.join(args.output, args.node + '.py') |
| 285 |
|
| 286 |
if not args.force and os.path.exists(outputFilepath): |
| 287 |
print(f'Plugin "{args.node}" already exists "{outputFilepath}".') |
| 288 |
sys.exit(-1) |
| 289 |
|
| 290 |
with open(outputFilepath, 'w') as pluginFile: |
| 291 |
pluginFile.write(fileStr) |
| 292 |
|
| 293 |
print(f'New node exported to: "{outputFilepath}"') |