diff --git a/cfel_optarg.py b/cfel_optarg.py index 40ec42061321e1b21f2b6390b3d7459a284d5a35..885565a5c7b0b77f7c4ce5fe8cb86a0fde825fb3 100644 --- a/cfel_optarg.py +++ b/cfel_optarg.py @@ -28,6 +28,11 @@ configuration files. """ +import argparse +import os +import json + + def parse_parameters(config): """Sets correct types for parameter dictionaries. @@ -36,12 +41,18 @@ def parse_parameters(config): The parser tries to interpret each entry in the dictionary according to the following rules: - - If the entry starts and ends with a single quote, it is interpreted as a string. - - If the entry is the word None, without quotes, then the entry is interpreted as NoneType. - - If the entry is the word False, without quotes, then the entry is interpreted as a boolean False. - - If the entry is the word True, without quotes, then the entry is interpreted as a boolean True. - - If non of the previous options match the content of the entry, the parser tries to interpret the entry in order - as: + - If the entry starts and ends with a single quote or double quote, it is + interpreted as a string. + - If the entry starts and ends with a square bracket, it is interpreted as a list. + - If the entry starts and ends with a brace, it is interpreted as a dictionary. + - If the entry is the word None, without quotes, then the entry is + interpreted as NoneType. + - If the entry is the word False, without quotes, then the entry is + interpreted as a boolean False. + - If the entry is the word True, without quotes, then the entry is + interpreted as a boolean True. + - If none of the previous options match the content of the entry, + the parser tries to interpret the entry in order as: - An integer number. - A float number. @@ -68,6 +79,23 @@ def parse_parameters(config): if monitor_params[sect][op].startswith("'") and monitor_params[sect][op].endswith("'"): monitor_params[sect][op] = monitor_params[sect][op][1:-1] continue + if monitor_params[sect][op].startswith('"') and monitor_params[sect][op].endswith('"'): + monitor_params[sect][op] = monitor_params[sect][op][1:-1] + continue + if monitor_params[sect][op].startswith("[") and monitor_params[sect][op].endswith("]"): + try: + monitor_params[sect][op] = json.loads(config.get(sect, op).replace("'", '"')) + continue + except: + raise RuntimeError('Error parsing parameters. The parameter {0}/{1} parameter ' + 'has an invalid type.'.format(sect, op)) + if monitor_params[sect][op].startswith("{") and monitor_params[sect][op].endswith("}"): + try: + monitor_params[sect][op] = json.loads(config.get(sect, op).replace("'", '"')) + continue + except: + raise RuntimeError('Error parsing parameters. The parameter {0}/{1} parameter ' + 'has an invalid type.'.format(sect, op)) if monitor_params[sect][op] == 'None': monitor_params[sect][op] = None continue