From 8630cb0f258cc6461a008c21a4ba8a38f04bf25f Mon Sep 17 00:00:00 2001
From: VIGNET Pierre <pierre.vignet@irisa.fr>
Date: Mon, 2 Dec 2019 23:22:56 +0100
Subject: [PATCH] [cmd] Fix paths and trailing '/'

---
 command_line/cadbiom_cmd/cadbiom_cmd.py     | 34 +++++++++++++++++----
 command_line/cadbiom_cmd/solution_search.py |  4 +--
 command_line/cadbiom_cmd/solution_sort.py   | 10 ++----
 3 files changed, 32 insertions(+), 16 deletions(-)

diff --git a/command_line/cadbiom_cmd/cadbiom_cmd.py b/command_line/cadbiom_cmd/cadbiom_cmd.py
index 567d9b6..f1e5c6e 100644
--- a/command_line/cadbiom_cmd/cadbiom_cmd.py
+++ b/command_line/cadbiom_cmd/cadbiom_cmd.py
@@ -34,6 +34,25 @@ import cadbiom.commons as cm
 
 LOGGER = cm.logger()
 
+
+def check_output_dir(function):
+    """Decorator used process some arguments from argparse"""
+
+    def modified_func(*args, **kwargs):
+        """Fix paths in arguments (add trailing '/')"""
+        output_dir = args[0]['output']
+        output_dir = output_dir if output_dir[-1] == '/' else output_dir + '/'
+
+        # Compatibility for output and output_dir
+        args[0]['output'] = output_dir
+        args[0]['output_dir'] = output_dir
+
+        return function(*args, **kwargs)
+
+    return modified_func
+
+
+@check_output_dir
 def solutions_search(args):
     """Launch the search for Minimum Activation Conditions (MAC) for entities
     of interest.
@@ -56,6 +75,7 @@ def solutions_sort(args):
     solution_sort.solutions_sort(args['path'])
 
 
+@check_output_dir
 def solutions_2_graph(args):
     """Create GraphML formated files containing a representation of the
     trajectories for every solution in complete MAC files (*mac_complete files).
@@ -73,6 +93,7 @@ def solutions_2_graph(args):
     )
 
 
+@check_output_dir
 def solutions_2_json(args):
     """Create a JSON formated file containing all data from complete MAC files
     (*mac_complete files). The file will contain frontier places/boundaries
@@ -92,6 +113,7 @@ def solutions_2_json(args):
     )
 
 
+@check_output_dir
 def json_2_interaction_graph(args):
     """Make an interaction weighted graph based on the searched molecule of interest.
 
@@ -110,6 +132,7 @@ def json_2_interaction_graph(args):
     )
 
 
+@check_output_dir
 def solutions_2_common_graph(args):
     """Create a GraphML formated file containing a representation of **all**
     trajectories for **all** solutions in complete MAC files (*mac_complete files).
@@ -127,6 +150,7 @@ def solutions_2_common_graph(args):
     )
 
 
+@check_output_dir
 def solutions_2_occcurrences_matrix(args):
     """Create a matrix of occurrences counting entities in the solutions found in
     *mac.txt files in the given path.
@@ -154,6 +178,7 @@ def identifiers_mapping(args):
     solution_repr.identifiers_mapping(**args)
 
 
+@check_output_dir
 def model_comparison(args):
     """Isomorphism test.
 
@@ -163,8 +188,6 @@ def model_comparison(args):
 
     # Module import
     import solution_repr
-    args['output'] = args['output'] if args['output'][-1] == '/' \
-                        else args['output'] + '/'
     solution_repr.graph_isomorph_test(
         args['model_file_1'],
         args['model_file_2'],
@@ -174,6 +197,7 @@ def model_comparison(args):
     )
 
 
+@check_output_dir
 def model_info(args):
     """Provide several levels of information about the structure of the model
     and its places/entities.
@@ -181,11 +205,10 @@ def model_info(args):
 
     # Module import
     import solution_repr
-    args['output_dir'] = args['output'] if args['output'][-1] == '/' \
-                        else args['output'] + '/'
     solution_repr.model_info(**args)
 
 
+@check_output_dir
 def model_graph(args):
     """Information about the graph based on the model.
 
@@ -195,11 +218,10 @@ def model_graph(args):
 
     # Module import
     import solution_repr
-    args['output_dir'] = args['output'] if args['output'][-1] == '/' \
-                        else args['output'] + '/'
     solution_repr.model_graph(**args)
 
 
+@check_output_dir
 def merge_macs(args):
     """Merge solutions to a csv file."""
 
diff --git a/command_line/cadbiom_cmd/solution_search.py b/command_line/cadbiom_cmd/solution_search.py
index 39ecf6e..65f9f61 100644
--- a/command_line/cadbiom_cmd/solution_search.py
+++ b/command_line/cadbiom_cmd/solution_search.py
@@ -709,9 +709,7 @@ def compute_macs(params):
     model_filename = os.path.basename(os.path.splitext(params["chart_file"])[0])
 
     # FILES
-    # Add trailing '/' if not present
-    output = params["output"] if params["output"][-1] == "/" else params["output"] + "/"
-    mac_file_prefix = output + model_filename + "_" + params["final_prop"] + "_mac"
+    mac_file_prefix = params["output"] + model_filename + "_" + params["final_prop"] + "_mac"
     # mac_file
     mac_file          = mac_file_prefix + ".txt"
     # mac_step_file
diff --git a/command_line/cadbiom_cmd/solution_sort.py b/command_line/cadbiom_cmd/solution_sort.py
index f129f61..e795e20 100644
--- a/command_line/cadbiom_cmd/solution_sort.py
+++ b/command_line/cadbiom_cmd/solution_sort.py
@@ -204,8 +204,7 @@ def solutions_2_json(output_dir, model_file, path, conditions=True):
 
     elif os.path.isdir(path):
         # The given path is a directory
-        path = path if path[-1] == '/' \
-            else path + '/'
+        path = path if path[-1] == '/' else path + '/'
 
         # Decompilation of all files in the directory
         file_number = 0
@@ -258,8 +257,7 @@ def solutions_2_graph(output_dir, model_file, path):
 
     elif os.path.isdir(path):
         # The given path is a directory
-        path = path if path[-1] == '/' \
-            else path + '/'
+        path = path if path[-1] == '/' else path + '/'
 
         # Decompilation of all files in the directory
         file_number = 0
@@ -377,8 +375,7 @@ def solutions_2_common_graph(output_dir, model_file, path):
 
     elif os.path.isdir(path):
         # The given path is a directory
-        path = path if path[-1] == '/' \
-            else path + '/'
+        path = path if path[-1] == '/' else path + '/'
 
         # Decompilation of all files in the directory
         file_number = 0
@@ -454,7 +451,6 @@ def solutions_2_occcurrences_matrix(output_dir, model_file, path,
     assert os.path.isdir(path)
 
     path = path if path[-1] == '/' else path + '/'
-    output_dir = output_dir if output_dir[-1] == '/' else output_dir + '/'
 
     # Make matrix
     occurrence_matrix(output_dir, model_file, path)
-- 
GitLab