Skip to content
Snippets Groups Projects
Commit e0c11b77 authored by George Marchment's avatar George Marchment
Browse files

Updated function which trabsforms single line conditions into multu line -> to...

Updated function which trabsforms single line conditions into multu line -> to not take into account the single line condition in strings (e.g scripts)
parent 195e1020
No related branches found
No related tags found
No related merge requests found
Pipeline #14473 failed with stage
in 2 minutes and 8 seconds
...@@ -34,21 +34,88 @@ class Code: ...@@ -34,21 +34,88 @@ class Code:
#This methods turns a single line condition into a muli line conditions #This methods turns a single line condition into a muli line conditions
def turn_single_condition_into_multiline(self, code): def turn_single_condition_into_multiline(self, code):
pattern = r"(if *\()(.+)\n"
to_replace = [] to_replace = []
for match in re.finditer(pattern, code):
all = match.group(0) start = 0
extarcted = match.group(2).strip()
curly_count, parenthese_count = 0, 0
quote_single, quote_double = False, False
triple_single, triple_double = False, False
while(start<len(code)):
checked_triple = False
if(start+3<=len(code)):
if(code[start:start+3]=="'''" and not quote_single and not quote_double and not triple_single and not triple_double):
triple_single = True
start+=3
checked_triple = True
elif(code[start:start+3]=="'''" and not quote_single and not quote_double and triple_single and not triple_double):
triple_single = False
start+=3
checked_triple = True
if(code[start:start+3]=='"""' and not quote_single and not quote_double and not triple_single and not triple_double):
triple_double = True
start+=3
checked_triple = True
elif(code[start:start+3]=='"""' and not quote_single and not quote_double and not triple_single and triple_double):
triple_double = False
start+=3
checked_triple = True
if(extarcted!="" and extarcted[-1] not in ["{", "}"]): if(not checked_triple):
start, end = match.span(1) if(code[start]=="{" and not quote_single and not quote_double and not triple_single and not triple_double):
extracted_condition = get_code_until_parenthese_count(code=code[end:], val=-1) curly_count+=1
condition = extracted_condition[:-1] elif(code[start]=="}" and not quote_single and not quote_double and not triple_single and not triple_double):
body = extarcted.replace(extracted_condition.strip(), "") curly_count-=1
if(body!="" and body[0]!="{"):
new = f"if ({condition}) {{\n{body}\n}}\n" if(code[start]=="(" and not quote_single and not quote_double and not triple_single and not triple_double):
to_replace.append((all, new)) parenthese_count+=1
elif(code[start]==")" and not quote_single and not quote_double and not triple_single and not triple_double):
parenthese_count-=1
if(code[start]=="'" and not quote_single and not quote_double and not triple_single and not triple_double):
if(code[start-1]!="\\" or (code[start-1]=="\\" and code[start-2]=="\\")):
quote_single=True
elif(code[start]=="'" and quote_single and not quote_double and not triple_single and not triple_double):
if(code[start-1]!="\\" or (code[start-1]=="\\" and code[start-2]=="\\")):
quote_single=False
if(code[start]=='"' and not quote_single and not quote_double and not triple_single and not triple_double):
if(code[start-1]!="\\" or (code[start-1]=="\\" and code[start-2]=="\\")):
quote_double=True
elif(code[start]=='"' and not quote_single and quote_double and not triple_single and not triple_double):
if(code[start-1]!="\\" or (code[start-1]=="\\" and code[start-2]=="\\")):
quote_double=False
if(code[start:start+2]=="if" and [quote_single, quote_double, triple_single, triple_double]==[False, False, False, False]):
pattern = r"(if *\()(.+)\n"
temp_code = code[start:]
for match in re.finditer(pattern, temp_code):
if(match.span(0)[0]==0):
found_if_bloc = True
all = match.group(0)
extarcted = match.group(2).strip()
if(extarcted!="" and extarcted[-1] not in ["{", "}"]):
_, end_condition = match.span(1)
extracted_condition = get_code_until_parenthese_count(code=temp_code[end_condition:], val=-1)
condition = extracted_condition[:-1]
body = extarcted.replace(extracted_condition.strip(), "")
if(body!="" and body[0]!="{"):
new = f"if ({condition}) {{\n{body}\n}}\n"
to_replace.append((all, new))
start+=1
for r in to_replace: for r in to_replace:
old, new = r old, new = r
code = code.replace(old, new) code = code.replace(old, new)
......
...@@ -60,6 +60,10 @@ class Process(Nextflow_Building_Blocks): ...@@ -60,6 +60,10 @@ class Process(Nextflow_Building_Blocks):
else: else:
raise Exception("This shouldn't happen") raise Exception("This shouldn't happen")
def add_to_ternary_operation_dico(self, old, new):
self.nextflow_file.add_to_ternary_operation_dico(old, new)
def add_to_emits(self, emit): def add_to_emits(self, emit):
self.later_emits.append(emit) self.later_emits.append(emit)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment