From a36606e8c6b94cc72cb04a170f3a925fa29dbf4b Mon Sep 17 00:00:00 2001 From: Manuela Kuhn <manuela.kuhn@desy.de> Date: Tue, 17 Jan 2017 18:24:27 +0100 Subject: [PATCH] Added lists and dict as interpreted types in parameter parsing --- cfel_optarg.py | 40 ++++++++++++++++++++++++++++++++++------ 1 file changed, 34 insertions(+), 6 deletions(-) diff --git a/cfel_optarg.py b/cfel_optarg.py index 40ec420..885565a 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 -- GitLab