Commit 2174ab50 authored by VIGNET Pierre's avatar VIGNET Pierre
Browse files

Ensure the creation of directories given to argparse

parent 08a118f8
......@@ -42,8 +42,7 @@ def solutions_search(args):
# Module import
import solution_search
params = args_to_param(args)
solution_search.solutions_search(params) # !
solution_search.solutions_search(args) # !
def solutions_sort(args):
......@@ -53,8 +52,7 @@ def solutions_sort(args):
# Module import
import solution_sort
params = args_to_param(args)
solution_sort.solutions_sort(params['path'])
solution_sort.solutions_sort(args['path'])
def solutions_2_graph(args):
......@@ -67,11 +65,10 @@ def solutions_2_graph(args):
# Module import
import solution_sort
params = args_to_param(args)
solution_sort.solutions_2_graph(
params['output'],
params['chart_file'],
params['path']
args['output'],
args['chart_file'],
args['path']
)
......@@ -86,12 +83,11 @@ def solutions_2_json(args):
# Module import
import solution_sort
params = args_to_param(args)
solution_sort.solutions_2_json(
params['output'],
params['chart_file'],
params['path'],
conditions=not params['no_conditions'], # Reverse the param...
args['output'],
args['chart_file'],
args['path'],
conditions=not args['no_conditions'], # Reverse the param...
)
......@@ -106,11 +102,10 @@ def json_2_interaction_graph(args):
# Module import
import interaction_graph
params = args_to_param(args)
interaction_graph.json_2_interaction_graph(
params['output'],
params['molecules_of_interest'],
params['path'],
args['output'],
args['molecules_of_interest'],
args['path'],
)
......@@ -124,11 +119,10 @@ def solutions_2_common_graph(args):
# Module import
import solution_sort
params = args_to_param(args)
solution_sort.solutions_2_common_graph(
params['output'],
params['chart_file'],
params['path']
args['output'],
args['chart_file'],
args['path']
)
......@@ -139,12 +133,11 @@ def solutions_2_occcurrences_matrix(args):
# Module import
import solution_sort
params = args_to_param(args)
solution_sort.solutions_2_occcurrences_matrix(
params['output'],
params['chart_file'],
params['path'],
transposed=params['transpose_csv']
args['output'],
args['chart_file'],
args['path'],
transposed=args['transpose_csv']
)
......@@ -157,8 +150,7 @@ def identifiers_mapping(args):
# Module import
import solution_repr
params = args_to_param(args)
solution_repr.identifiers_mapping(**params)
solution_repr.identifiers_mapping(**args)
def model_comparison(args):
......@@ -170,15 +162,14 @@ def model_comparison(args):
# Module import
import solution_repr
params = args_to_param(args)
params['output'] = params['output'] if params['output'][-1] == '/' \
else params['output'] + '/'
args['output'] = args['output'] if args['output'][-1] == '/' \
else args['output'] + '/'
solution_repr.graph_isomorph_test(
params['model_file_1'],
params['model_file_2'],
params['output'],
params['graphs'],
params['json'],
args['model_file_1'],
args['model_file_2'],
args['output'],
args['graphs'],
args['json'],
)
......@@ -189,11 +180,9 @@ def model_info(args):
# Module import
import solution_repr
params = args_to_param(args)
params['output_dir'] = params['output'] if params['output'][-1] == '/' \
else params['output'] + '/'
solution_repr.model_info(**params)
args['output_dir'] = args['output'] if args['output'][-1] == '/' \
else args['output'] + '/'
solution_repr.model_info(**args)
def model_graph(args):
......@@ -205,10 +194,9 @@ def model_graph(args):
# Module import
import solution_repr
params = args_to_param(args)
params['output_dir'] = params['output'] if params['output'][-1] == '/' \
else params['output'] + '/'
solution_repr.model_graph(**params)
args['output_dir'] = args['output'] if args['output'][-1] == '/' \
else args['output'] + '/'
solution_repr.model_graph(**args)
def merge_macs(args):
......@@ -216,9 +204,8 @@ def merge_macs(args):
# Module import
import solution_merge
params = args_to_param(args)
solution_merge.merge_macs_to_csv(params['solutions_directory'],
params['output'])
solution_merge.merge_macs_to_csv(args['solutions_directory'],
args['output'])
def args_to_param(args):
......@@ -226,6 +213,18 @@ def args_to_param(args):
return {k: v for k, v in vars(args).items() if k != 'func'}
def ensure_dir_presence(path):
"""Test write mode and eventually create the given directory"""
if not os.path.isdir(path):
LOGGER.info("Creation of the directory <{}>".format(path))
os.mkdir(path)
if not os.access(path, os.W_OK):
LOGGER.error("<{}> is not writable".format(path))
exit()
class ReadableFile(argparse.Action):
"""
http://stackoverflow.com/questions/11415570/directory-path-types-with-argparse
......@@ -235,18 +234,14 @@ class ReadableFile(argparse.Action):
prospective_file = values
if not os.path.isfile(prospective_file):
raise argparse.ArgumentTypeError(
"readable_file:{0} is not a valid path".format(
prospective_file)
)
LOGGER.error("readable_file:<{}> is not a valid file".format(prospective_file))
exit()
if os.access(prospective_file, os.R_OK):
setattr(namespace, self.dest, prospective_file)
else:
raise argparse.ArgumentTypeError(
"readable_file:{0} is not a readable file".format(
prospective_file)
)
LOGGER.error("readable_file:<{}> is not a readable file".format(prospective_file))
exit()
class ReadableDir(argparse.Action):
......@@ -258,18 +253,14 @@ class ReadableDir(argparse.Action):
prospective_dir = values
if not os.path.isdir(prospective_dir):
raise argparse.ArgumentTypeError(
"readable_dir:{0} is not a valid path".format(
prospective_dir)
)
LOGGER.error("readable_dir:<{}> is not a valid path".format(prospective_dir))
exit()
if os.access(prospective_dir, os.R_OK):
setattr(namespace, self.dest, prospective_dir)
else:
raise argparse.ArgumentTypeError(
"readable_dir:{0} is not a readable dir".format(
prospective_dir)
)
LOGGER.error("readable_dir:<{}> is not a readable dir".format(prospective_dir))
exit()
def main():
......@@ -494,7 +485,7 @@ def main():
parser_model_comparison.set_defaults(func=model_comparison)
# subparser: Model infos
# subparser: Model info
# 1 model
parser_model_info = subparsers.add_parser(
'model_info',
......@@ -568,5 +559,13 @@ def main():
# Set log level
cm.log_level(vars(args)['verbose'])
# launch associated command
args.func(args)
# Take argparse arguments and put them in a standard dict
params = args_to_param(args)
# Ultimate check of the presence of the directories
know_directory_variables = ("output", "path", "solutions_directory")
[ensure_dir_presence(params[var]) for var in know_directory_variables
if params.get(var, None)]
# Launch associated command
args.func(params)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment