From 5b787ffe30d365febaf230cb0a4f2646ebdc0189 Mon Sep 17 00:00:00 2001
From: George Marchment <georgemarchment@yahoo.fr>
Date: Wed, 19 Mar 2025 16:50:33 +0100
Subject: [PATCH] fixed a small bug + i've started to test if the replaces
 actaully work -> it's a good way to catch errors -> perhaps add them to the
 rest of the code

---
 src/call.py          | 31 ++++++++++++++++++++++++++++---
 src/main.py          |  4 ++--
 src/nextflow_file.py | 19 +++++++++++++++++++
 src/operation.py     | 20 ++++++++++++++++++++
 4 files changed, 69 insertions(+), 5 deletions(-)

diff --git a/src/call.py b/src/call.py
index 604564b..60b2052 100644
--- a/src/call.py
+++ b/src/call.py
@@ -52,7 +52,7 @@ class Call(Executor):
     def get_parameters(self):
         return self.parameters
 
-    def get_code(self, clean_pipe = False, get_OG=False, remove_emit_and_take = False):
+    def get_code(self, clean_pipe = False, get_OG=False, remove_emit_and_take = False, replace_calls = False):
         if(get_OG):
             if(self.OG_code==''):
                 return self.code.get_code()
@@ -83,16 +83,25 @@ class Call(Executor):
                 if(param.get_type()=="Call"):
                     #If it's not a function -> then we rewrite it
                     if(param.get_first_element_called().get_type()!="Function"):
+                        temp = code
                         code = replace_group1(code, fr"[^\w]({re.escape(param.get_code(get_OG=True))})", param_new_name)
+                        if(temp==code):
+                            raise Exception("This souldn't happen")
                         #code = code.replace(param.get_code(get_OG=True), param_new_name)
                         new_bit = param.simplify_code(new_name = param_new_name)
+                        temp = code
                         code = code.replace(tag_to_add, f"{tag_to_add}\n{new_bit}")
+                        if(temp==code):
+                            raise Exception("This souldn't happen")
 
                 #Case the param is an operation
                 elif(param.get_type()=="Operation"):
                     #If it's an artificial operation -> we don't need to do anything
                     if(not param.get_artificial_status()):
-                        code = replace_group1(code, fr"[^\w]({re.escape(param.get_code(get_OG=True))})", param_new_name)
+                        temp = code
+                        code = replace_group1(code, fr"[^\w]({re.escape(param.get_code(get_OG=True, replace_calls = False))})", param_new_name)
+                        if(temp==code):
+                            raise Exception("This souldn't happen")
                         #code = code.replace(param.get_code(get_OG=True), param_new_name)
                         lines = param.simplify_code().split('\n')
                         if(len(lines)==1):
@@ -100,21 +109,34 @@ class Call(Executor):
                         else:
                             head = '\n'.join(lines[:-1])
                             new_bit = f"{head}\n{param_new_name} = {lines[-1]}"
+                        temp = code
                         code = code.replace(tag_to_add, f"{tag_to_add}\n{new_bit}")
+                        if(temp==code):
+                            raise Exception("This souldn't happen")
                 
                 #Case Channel
                 elif(param.get_type()=="Channel"):
                     raise Exception("This shouldn't happen")
                     None
                 elif(param.get_type()=="Emitted"):
+                    temp = code
                     code = replace_group1(code, fr"[^\w]({re.escape(param.get_code(get_OG=True))})", param_new_name)
+                    if(temp==code):
+                        raise Exception("This souldn't happen")
                     #code = code.replace(param.get_code(get_OG=True), param_new_name)
                     new_bit = f"{param_new_name} = {param.simplify_code()}"
+                    temp = code
                     code = code.replace(tag_to_add, f"{tag_to_add}\n{new_bit}")
+                    if(temp==code):
+                        raise Exception("This souldn't happen")
                 else:
                     raise Exception("This shouldn't happen")
                 index+=1
-            return code.replace(tag_to_add, "").strip()
+            temp = code
+            code = code.replace(tag_to_add, "").strip()
+            if(temp==code):
+                raise Exception("This souldn't happen")
+            return code
 
         
     
@@ -148,7 +170,10 @@ class Call(Executor):
     def get_code_split_space(self, code):
         to_add_spaces = ['(', ')', '}', '{']
         for character in to_add_spaces:
+            temp = code
             code = code.replace(f'{character}', f' {character} ')
+            if(temp==code):
+                raise Exception("This shouldn't happen")
         return code.split()
 
     def analye_parameters(self, param):
diff --git a/src/main.py b/src/main.py
index 0cec5f5..63a052a 100644
--- a/src/main.py
+++ b/src/main.py
@@ -50,11 +50,11 @@ class Main(Nextflow_Building_Blocks):
         
         for exe in sorted_executor_2_length:
             if(exe.get_type()=="Call" or exe.get_type()=="Operation"):
-                old = exe.get_code(get_OG = True, remove_emit_and_take = True)
+                old = exe.get_code(get_OG = True, remove_emit_and_take = True, replace_calls = False)
                 new = exe.simplify_code()
                 if(new!=old):
                     temp = code
-                    code = code.replace(exe.get_code(get_OG = True), exe.simplify_code(), 1)
+                    code = code.replace(old, new, 1)
                     if(temp==code):
                         print(code)
                         print("- old", f'"{old}"')
diff --git a/src/nextflow_file.py b/src/nextflow_file.py
index 9329ac8..829999c 100644
--- a/src/nextflow_file.py
+++ b/src/nextflow_file.py
@@ -277,15 +277,30 @@ class Nextflow_File(Nextflow_Building_Blocks):
         #Code without processes
         code = self.get_code()
         for proecess in self.processes:
+            temp = code
             code = code.replace(proecess.get_code(), "")
+            if(temp==code):
+                raise Exception("This souldn't happen")
         for sub in self.subworkflows:
+            temp = code
             code = code.replace(sub.get_code(), "")
+            if(temp==code):
+                raise Exception("This souldn't happen")
         for fun in self.functions:
+            temp = code
             code = code.replace(fun.get_code(), "")
+            if(temp==code):
+                raise Exception("This souldn't happen")
         if(self.first_file and self.main!=None):
+            temp = code
             code = code.replace(self.main.get_code(), "")
+            if(temp==code):
+                raise Exception("This souldn't happen")
         for include in self.includes:
+            temp = code
             code = code.replace(include.get_code(), "")
+            if(temp==code):
+                raise Exception("This souldn't happen")
 
         from .root import Root
         self.root = Root(code=code, origin= self, modules_defined=self.get_modules_defined(), subworkflow_inputs = [])
@@ -317,7 +332,11 @@ class Nextflow_File(Nextflow_Building_Blocks):
                 #Code without processes
                 code = self.get_code()
                 for proecess in self.processes:
+                    temp = code
                     code = code.replace(proecess.get_code(), "")
+                    if(temp==code):
+                        raise Exception("This souldn't happen")
+                    
                 
                 #Extract includes
                 self.extract_includes()
diff --git a/src/operation.py b/src/operation.py
index 46fa2c1..1c83604 100644
--- a/src/operation.py
+++ b/src/operation.py
@@ -352,7 +352,10 @@ class Operation(Executor):
                         self.add_origin_emits(full_code, name_called, "")
                     else:
                         self.add_origin_emits(full_code, name_called, name_emitted)
+                    temp = operation
                     operation = operation.replace(full_code, "")
+                    if(temp==operation):
+                        raise Exception("This souldn't happen")
                     searching_for_emits = True
                     stop_pattern_search = True
                     break
@@ -381,9 +384,11 @@ class Operation(Executor):
                 def replacer(match):
                     return match.group(0).replace(match.group(0), match.group(1))
                 return re.sub(f"[^\w\s] *\(\s*({pattern})\s*\)", replacer, text)
+        
             operation = replace(operation)
 
 
+
         first_call = True
         for pattern in patterns:
 
@@ -458,7 +463,10 @@ class Operation(Executor):
                     if(operation_until_out==full_code[:full_code.find("out")]):
                         self.add_origin_emits(full_code, name_called, "")
                         case_operation_starts_with_emit = True
+                        temp = operation
                         operation = operation.replace(full_code, "")
+                        if(temp==operation):
+                            raise Exception("This souldn't happen")
 
 
 
@@ -699,7 +707,10 @@ class Operation(Executor):
   
         if(replace_calls):
             for call in self.calls:
+                temp = code
                 code = code.replace(self.calls[call].get_code(), str(call))
+                if(temp==code):
+                    raise Exception("This souldn't happen")
         
         #Remove "emit:" and "takes:" for the subworkklfow inputs and outputs
         if(remove_emit_and_take and code[:6] in ["emit: ", "take: "]):
@@ -744,7 +755,10 @@ class Operation(Executor):
         while(searching):
             searching= False
             for c in self.calls:
+                temp = text
                 text = text.replace(self.calls[c].get_code(), "")
+                if(temp==text):
+                    raise Exception("This souldn't happen")
             for match in re.finditer(pattern_call, text):
                 if(match.group(1) in to_call):
                     start, end = match.span(0)
@@ -960,10 +974,13 @@ class Operation(Executor):
         for origin in dico_origin_2_replace:
             if(len(dico_origin_2_replace[origin])<=1):
                 for val in dico_origin_2_replace[origin]:
+                    temp = code
                     if(type(val)==str):
                         code = code.replace(origin, val)
                     else:
                         code = code.replace(origin, val.simplify_code())
+                    if(temp==code):
+                        raise Exception("This souldn't happen")
             #Case there are mutiple origins then:
             else:
                 #For example 
@@ -997,7 +1014,10 @@ class Operation(Executor):
                         for val in values:
                             new_body+=f"\n{temporary_channel} = {val}"
                 #new_body+=temporary_channel
+                temp = code
                 code = code.replace(origin, temporary_channel)
+                if(temp==code):
+                    raise Exception("This souldn't happen")
                 temporary_index+=1
                 to_add.append(new_body)
             
-- 
GitLab