From e270aa04fac8086feddb8f95260011b7812d94f9 Mon Sep 17 00:00:00 2001 From: George Marchment <georgemarchment@yahoo.fr> Date: Tue, 18 Mar 2025 11:09:13 +0100 Subject: [PATCH] Update ternary operation extraction + put back --- src/code_.py | 20 ++++++++++++++------ src/nextflow_building_blocks.py | 3 +++ src/nextflow_file.py | 3 +++ src/workflow.py | 11 +++++++++++ 4 files changed, 31 insertions(+), 6 deletions(-) diff --git a/src/code_.py b/src/code_.py index 5296712..2cf99b4 100644 --- a/src/code_.py +++ b/src/code_.py @@ -53,24 +53,32 @@ class Code: code = code.replace(old, new) return code + def add_to_ternary_operation_dico(self, old, new): + self.origin.add_to_ternary_operation_dico(old, new) + #This methods rewrite ternary operation into "normal" conditions #variable = (condition) ? Expression2 : Expression3; def rewrite_ternary_operation_to_normal_condition(self, code): - pattern = r"(\w+) *\= *([^?\n]+) *\? *([^:\n]+) *\: *([^\n]+)\n" + pattern = r"(def)? *(\w+) *\= *([^?\n]+) *\? *([^:\n]+) *\: *([^\n]+)\n" to_replace = [] for match in re.finditer(pattern, code): - variable = match.group(1) - condition = match.group(2).strip() - exp1, exp2 = match.group(3).strip(), match.group(4).strip() + try: + def_variable = match.group(1) + except: + def_variable = "" + variable = match.group(2) + condition = match.group(3).strip() + exp1, exp2 = match.group(4).strip(), match.group(5).strip() old = match.group(0) - new = f"if ({condition}) {{\n{variable} = {exp1}\n}}\n\n" - new += f"if (!({condition})) {{\n{variable} = {exp2}\n}}\n\n" + new = f"if ({condition}) {{\n{def_variable} {variable} = {exp1}\n}}\n" + new += f"if (!({condition})) {{\n{def_variable} {variable} = {exp2}\n}}\n\n" #else {{\n{variable} = {exp2}\n}}\n" #Here we check that it's worked correctly -> that we have done a good parsing if(get_parenthese_count(condition)==0 and get_parenthese_count(exp1)==0 and get_parenthese_count(exp2)==0 and get_curly_count(condition)==0 and get_curly_count(exp1)==0 and get_curly_count(exp2)==0): to_replace.append((old, new)) for r in to_replace: old, new = r + self.add_to_ternary_operation_dico(old, new) code = code.replace(old, new) return code diff --git a/src/nextflow_building_blocks.py b/src/nextflow_building_blocks.py index 4f554df..5f024e5 100644 --- a/src/nextflow_building_blocks.py +++ b/src/nextflow_building_blocks.py @@ -18,6 +18,9 @@ class Nextflow_Building_Blocks: #AUXILIARY METHODS FOR ALL CLASSES #--------------------------------- + def add_to_ternary_operation_dico(self, old, new): + self.origin.add_to_ternary_operation_dico(old, new) + def get_code(self, get_OG = False): return self.code.get_code(get_OG = get_OG) diff --git a/src/nextflow_file.py b/src/nextflow_file.py index 0e1cbc1..ebdf62d 100644 --- a/src/nextflow_file.py +++ b/src/nextflow_file.py @@ -40,6 +40,9 @@ class Nextflow_File(Nextflow_Building_Blocks): #GENERAL #---------------------- + def add_to_ternary_operation_dico(self, old, new): + self.workflow.add_to_ternary_operation_dico(old, new) + def get_root_directory(self): return self.workflow.get_root_directory() diff --git a/src/workflow.py b/src/workflow.py index 9b2873d..277b1c7 100644 --- a/src/workflow.py +++ b/src/workflow.py @@ -76,6 +76,7 @@ class Workflow: self.name = name self.graph = None self.conditions_2_ignore = [] + self.ternary_operation_dico = {} OG_file = Nextflow_File(file, workflow = self, first_file = True) @@ -434,6 +435,14 @@ George Marchment, Bryan Brancotte, Marie Schmit, Frédéric Lemoine, Sarah Cohen + def add_to_ternary_operation_dico(self, old, new): + self.ternary_operation_dico[new] = old + + def put_back_old_ternary_operations(self, code, ternary_operation_dico): + for new in ternary_operation_dico: + old = ternary_operation_dico[new] + code = code.replace(new.strip(), old) + return code #TODO -> write tests for this method @@ -717,6 +726,7 @@ George Marchment, Bryan Brancotte, Marie Schmit, Frédéric Lemoine, Sarah Cohen def convert_workflow_2_user_view(self, relevant_processes = [], render_graphs = True): self.iniatilise_tab_processes_2_remove() self.graph.initialise(processes_2_remove = self.processes_2_remove) + ternary_operation_dico = self.ternary_operation_dico if(self.get_DSL()=="DSL1"): code = self.convert_to_DSL2() @@ -1134,6 +1144,7 @@ George Marchment, Bryan Brancotte, Marie Schmit, Frédéric Lemoine, Sarah Cohen #Putting || back + code = self.put_back_old_ternary_operations(code, ternary_operation_dico) code = code.replace("$OR$", "||") code = remove_extra_jumps(format_with_tabs(code)) f = open(self.get_output_dir()/ "debug" / "rewritten.nf", "w") -- GitLab