Skip to content

Commit

Permalink
feat: fix function annotation & code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanbesler committed May 23, 2022
2 parents f535440 + 7eb2cec commit 881e0f8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 22 deletions.
33 changes: 13 additions & 20 deletions pytwingrind/pytwingrind/prepare.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import re
import logging
import pickle
import copy
from pytwingrind import common

def create_hash(fb, method, hashes):
Expand Down Expand Up @@ -46,12 +45,12 @@ def add_guards(filepath, fb_name, hashes):
ncallables = 0

# add guards to functions
functions = re.findall(r'<POU(.*?)Name="(.*?)"(.*?) FUNCTION (.*?)<ST><!\[CDATA\[(.*?)\]\]><\/ST>', src, re.S | re.M | re.UNICODE)
functions = re.findall(r'<POU(.*?)Name="(.*?)"(.*?)FUNCTION (.*?)<ST><!\[CDATA\[(.*?)\]\]><\/ST>', src, re.S | re.M | re.UNICODE)
if functions:
for m in functions:
function_name = m[1]
body = m[4]
old_body = copy.deepcopy(body)
old_body = body
hash = create_hash(fb_name, function_name, hashes)

body = '''{tag}Twingrind.Profiler.Push({hash});{tag}\n'''.format(hash=hash, tag=common.profiler_tag) + body
Expand All @@ -63,26 +62,24 @@ def add_guards(filepath, fb_name, hashes):
nearly += i # two guards are always added
ncallables += 1

src = src.replace(r'<POU{spacer0}Name="{function_name}"{spacer2} FUNCTION {spacer3}<ST><![CDATA[{body}]]></ST>'.format(spacer0=m[0],
src = src.replace(r'<POU{spacer0}Name="{function_name}"{spacer2}FUNCTION {spacer3}<ST><![CDATA[{body}]]></ST>'.format(spacer0=m[0],
function_name=function_name,
spacer2=m[2],
spacer3=m[3],
body=old_body,
fb=fb_name),
r'<POU{spacer0}Name="{function_name}"{spacer2} FUNCTION {spacer3}<ST><![CDATA[{body}]]></ST>'.format(spacer0=m[0],
body=old_body),
r'<POU{spacer0}Name="{function_name}"{spacer2}FUNCTION {spacer3}<ST><![CDATA[{body}]]></ST>'.format(spacer0=m[0],
function_name=function_name,
spacer2=m[2],
spacer3=m[3],
body=body,
fb=fb_name))
body=body))

# add guards to programs
programs = re.findall(r'<POU(.*?)Name="(.*?)"(.*?)PROGRAM(.*?)<ST><!\[CDATA\[(.*?)\]\]><\/ST>', src, re.S | re.M | re.UNICODE)
if programs:
for m in programs:
prg_name = m[1]
body = m[4]
old_body = copy.deepcopy(body)
old_body = body
hash = create_hash(fb_name, prg_name, hashes)

body = '''{tag}Twingrind.Profiler.Push({hash});{tag}\n'''.format(hash=hash, tag=common.profiler_tag) + body
Expand All @@ -98,22 +95,20 @@ def add_guards(filepath, fb_name, hashes):
prg_name=prg_name,
spacer2=m[2],
spacer3=m[3],
body=old_body,
fb=fb_name),
body=old_body),
r'<POU{spacer0}Name="{prg_name}"{spacer2}PROGRAM{spacer3}<ST><![CDATA[{body}]]></ST>'.format(spacer0=m[0],
prg_name=prg_name,
spacer2=m[2],
spacer3=m[3],
body=body,
fb=fb_name))
body=body))

# add guards to function blocks
functionblocks = re.findall(r'<POU(.*?)Name="(.*?)"(.*?)FUNCTION_BLOCK(.*?)<ST><!\[CDATA\[(.*?)\]\]><\/ST>', src, re.S | re.M | re.UNICODE)
if functionblocks:
for m in functionblocks:
functionblock_name = m[1]
body = m[4]
old_body = copy.deepcopy(body)
old_body = body
hash = create_hash(fb_name, functionblock_name, hashes)

body = '''{tag}Twingrind.Profiler.Push({hash});{tag}\n'''.format(hash=hash, tag=common.profiler_tag) + body
Expand All @@ -129,14 +124,12 @@ def add_guards(filepath, fb_name, hashes):
functionblock_name=functionblock_name,
spacer2=m[2],
spacer3=m[3],
body=old_body,
fb=fb_name),
body=old_body),
r'<POU{spacer0}Name="{functionblock_name}"{spacer2}FUNCTION_BLOCK{spacer3}<ST><![CDATA[{body}]]></ST>'.format(spacer0=m[0],
functionblock_name=functionblock_name,
spacer2=m[2],
spacer3=m[3],
body=body,
fb=fb_name))
body=body))

# add guards to all methods
methods = re.findall(r'<Method(.*?)Name="(.*?)"(.*?)<ST><!\[CDATA\[(.*?)\]\]><\/ST>', src, re.S | re.M | re.UNICODE)
Expand All @@ -147,7 +140,7 @@ def add_guards(filepath, fb_name, hashes):

method_name = m[1]
body = m[3]
old_body = copy.deepcopy(body)
old_body = body
hash = create_hash(fb_name, method_name, hashes)

body = '''{tag}Twingrind.Profiler.Push({hash});{tag}\n'''.format(hash=hash, tag=common.profiler_tag) + body
Expand Down
3 changes: 1 addition & 2 deletions pytwingrind/pytwingrind/reconstruct.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import os
import logging
import glob
import pickle
import inspect
import networkx
Expand Down Expand Up @@ -73,7 +72,7 @@ def ch(x, y): return x + '::' + y if len(y) > 0 else x
if selfcost < 0 and depth == 0:
selfcost = 1000000000
elif selfcost < 0:
raise Expection('selfcost < 0')
raise Exception('selfcost < 0')

# write header information
if depth == 0:
Expand Down

0 comments on commit 881e0f8

Please sign in to comment.