# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"^[\s]*@wb_export[\s]+def ([\w]+)\(jsonstr\):[\s\S]*?\"\"\"({\"in\":.*)\"\"\"([\s\S]*?)((@|$)[\s\S]*)$"
test_str = ("@wb_export\n"
"def SimpleFunction(jsonstr):\n"
" #global fd\n"
" #os.write(fd, \"wb_pre\\n\")\n"
" \n"
" # Definition of Input and Output Parameters (name and type)\n"
" \"\"\"{\"in\": {\"param\":\"string\"}, \"out\": {\"return\":\"string\"}}\"\"\"\n"
" # /!\\ Do not edit here !\n"
" # Edit function SimpleFunctionImpl() in \"common\" part\n"
" \n"
" import json,os,sys,datetime\n"
" inputData = json.loads(jsonstr)\n"
" \n"
" try:\n"
" outputData = SimpleFunctionImpl(inputData[\"param\"])\n"
" fd = os.open(\"../log/custom_success.log\", os.O_APPEND|os.O_CREAT|os.O_RDWR)\n"
" os.write(fd, \"%s In : %s / Out : %s\\n\" % (datetime.datetime.now().strftime(\"[%Y-%m-%d %H:%M:%S.%f]\"), jsonstr, json.dumps(outputData)))\n"
" os.close(fd)\n"
" return json.dumps(outputData)\n"
" except Exception as e:\n"
" fd = os.open(\"../log/custom_error.log\", os.O_APPEND|os.O_CREAT|os.O_RDWR)\n"
" os.write(fd, \"%s INTERNAL ERROR: Custom code uncatched exception in VCO::SimpleFunction().\\n\\tIn : %s\\n\\tMessage: %s\\n\" % (datetime.datetime.now().strftime(\"[%Y-%m-%d %H:%M:%S.%f]\"), jsonstr, str(e)))\n"
" os.close(fd)\n"
" return json.dumps({\"error\":\"INTERNAL ERROR: Custom code uncatched exception in VCO::SimpleFunction.\\n\\tMessage: %s\" % str(e)})\n"
" except:\n"
" fd = os.open(\"../log/custom_error.log\", os.O_APPEND|os.O_CREAT|os.O_RDWR)\n"
" os.write(fd, \"%s INTERNAL ERROR: Custom code uncatched system exception in VCO::SimpleFunction().\\n\" % (datetime.datetime.now().strftime(\"[%Y-%m-%d %H:%M:%S.%f]\")))\n"
" os.close(fd)\n"
" return json.dumps({\"error\":\"INTERNAL ERROR: Custom code uncatched system exception in VCO::SimpleFunction().\"})\n"
" # End of SimpleFunction - VCO WB Custom Code Editor\n"
"@wb_export\n"
"def SimpleFunction2(jsonstr):\n"
" #global fd\n"
" #os.write(fd, \"wb_pre\\n\")\n"
" \n"
" # Definition of Input and Output Parameters (name and type)\n"
" \"\"\"{\"in\": {\"param\":\"string\"}, \"out\": {\"return\":\"string\"}}\"\"\"\n"
" # /!\\ Do not edit here !\n"
" # Edit function SimpleFunctionImpl() in \"common\" part\n"
" \n"
" return {\"return\": \"OK2\"}\n"
" \n"
" \n"
" \n"
" ")
matches = re.search(regex, test_str, re.IGNORECASE)
if matches:
print ("Match was found at {start}-{end}: {match}".format(start = matches.start(), end = matches.end(), match = matches.group()))
for groupNum in range(0, len(matches.groups())):
groupNum = groupNum + 1
print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = matches.start(groupNum), end = matches.end(groupNum), group = matches.group(groupNum)))
# Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Python, please visit: https://docs.python.org/3/library/re.html