-
Notifications
You must be signed in to change notification settings - Fork 0
/
yaml_parser.py
54 lines (50 loc) · 1.63 KB
/
yaml_parser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#########################################################################################
#
#
# parse config.yaml file containing pipeline's parameters
#
#
# ---------------------------------------------------------------------------------------
#
# Example: python yaml_parser.py -o <n_transfo>
# About the license: see the file LICENSE
###################################################################
import yaml
import argparse
import os
import sys
def get_parser():
parser = argparse.ArgumentParser(
description='Parse config.yaml file csa-atrophy pipeline parameters to be usedd in shell scripts',
add_help=None,
formatter_class=argparse.RawTextHelpFormatter,
prog=os.path.basename(__file__).strip(".py")
)
mandatory = parser.add_argument_group("\nMANDATORY ARGUMENTS")
mandatory.add_argument(
"-o",
required=True,
help="parameters to output from config.yaml",
)
mandatory.add_argument(
"-i",
required=True,
help="input path to configuration of file",
)
optional = parser.add_argument_group("\nOPTIONAL ARGUMENTS")
optional.add_argument(
'-h',
help='Help',
nargs="*"
)
return parser
def main():
"""main function, reads and prints yaml config file parameters"""
# get parser elements
parser = get_parser()
arguments = parser.parse_args(args=None if sys.argv[0:] else ['--help'])
# Read yaml file
with open(arguments.i, 'r') as config_var:
data_loaded = yaml.safe_load(config_var)
# print yaml file parameters to be read by bash script
print(data_loaded[arguments.o])