-
Notifications
You must be signed in to change notification settings - Fork 32
Fix fragile parameter parsing in concore.py and concoredocker.py #201
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -156,6 +156,33 @@ def safe_literal_eval(filename, defaultValue): | |
| # =================================================================== | ||
| # Parameter Parsing | ||
| # =================================================================== | ||
| def parse_params(sparams: str) -> dict: | ||
| """Parse semicolon-delimited key=value pairs into a dictionary. | ||
|
|
||
| Args: | ||
| sparams: String in format "key1=value1;key2=value2" | ||
|
|
||
| Returns: | ||
| Dictionary with parsed key-value pairs | ||
| """ | ||
| params = {} | ||
| if not sparams: | ||
| return params | ||
|
|
||
| for item in sparams.split(";"): | ||
| if "=" in item: | ||
| key, value = item.split("=", 1) # split only once | ||
| key=key.strip() | ||
| value=value.strip() | ||
| #try to convert to python type (int, float, list, etc.) | ||
| # Use literal_eval to preserve backward compatibility (integers/lists) | ||
| # Fallback to string for unquoted values (paths, URLs) | ||
| try: | ||
| params[key] = literal_eval(value) | ||
| except (ValueError, SyntaxError): | ||
| params[key] = value | ||
| return params | ||
|
GREENRAT-K405 marked this conversation as resolved.
GREENRAT-K405 marked this conversation as resolved.
Comment on lines
+175
to
+187
|
||
|
|
||
| try: | ||
| sparams_path = concore_params_file | ||
| if os.path.exists(sparams_path): | ||
|
|
@@ -166,16 +193,10 @@ def safe_literal_eval(filename, defaultValue): | |
| if sparams[0] == '"' and sparams[-1] == '"': #windows keeps "" need to remove | ||
| sparams = sparams[1:-1] | ||
|
|
||
| # Convert key=value;key2=value2 to Python dict format | ||
| if sparams != '{' and not (sparams.startswith('{') and sparams.endswith('}')): # Check if it needs conversion | ||
| logging.debug("converting sparams: "+sparams) | ||
| sparams = "{'"+re.sub(';',",'",re.sub('=',"':",re.sub(' ','',sparams)))+"}" | ||
| logging.debug("converted sparams: " + sparams) | ||
| try: | ||
| params = literal_eval(sparams) | ||
| except Exception as e: | ||
| logging.warning(f"bad params content: {sparams}, error: {e}") | ||
| params = dict() | ||
| # Parse params using clean function instead of regex | ||
| logging.debug("parsing sparams: "+sparams) | ||
| params = parse_params(sparams) | ||
|
GREENRAT-K405 marked this conversation as resolved.
|
||
| logging.debug("parsed params: " + str(params)) | ||
|
GREENRAT-K405 marked this conversation as resolved.
|
||
| else: | ||
| params = dict() | ||
| else: | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,20 +25,41 @@ def safe_literal_eval(filename, defaultValue): | |
| concore_maxtime_file = os.path.join(inpath, "1", "concore.maxtime") | ||
|
|
||
| #9/21/22 | ||
| def parse_params(sparams): | ||
| params = {} | ||
| if not sparams: | ||
| return params | ||
|
|
||
| # keep backward compatibility: comma-separated params | ||
| for item in sparams.split(","): | ||
| if "=" in item: | ||
| key, value = item.split("=", 1) | ||
| key = key.strip() | ||
| value = value.strip() | ||
| #try to convert to python type (int, float, list, etc.) | ||
| # Use literal_eval to preserve backward compatibility (integers/lists) | ||
| # Fallback to string for unquoted values (paths, URLs) | ||
| try: | ||
| params[key] = literal_eval(value) | ||
| except (ValueError, SyntaxError): | ||
| params[key] = value | ||
|
GREENRAT-K405 marked this conversation as resolved.
Comment on lines
+44
to
+56
|
||
| return params | ||
|
|
||
| try: | ||
| sparams = open(concore_params_file).read() | ||
| if sparams[0] == '"': #windows keeps "" need to remove | ||
| sparams = open(concore_params_file).read().strip() | ||
|
GREENRAT-K405 marked this conversation as resolved.
Outdated
|
||
|
|
||
| if sparams and sparams[0] == '"': # windows keeps quotes | ||
| sparams = sparams[1:] | ||
| sparams = sparams[0:sparams.find('"')] | ||
| if sparams != '{': | ||
| print("converting sparams: "+sparams) | ||
| sparams = "{'"+re.sub(',',",'",re.sub('=',"':",re.sub(' ','',sparams)))+"}" | ||
| print("converted sparams: " + sparams) | ||
| try: | ||
| params = literal_eval(sparams) | ||
| except: | ||
| print("bad params: "+sparams) | ||
| except: | ||
| if '"' in sparams: | ||
| sparams = sparams[:sparams.find('"')] | ||
|
|
||
| if sparams: | ||
| print("parsing sparams:", sparams) | ||
| params = parse_params(sparams) | ||
| else: | ||
| params = dict() | ||
| except Exception as e: | ||
| print(f"Error reading concore.params: {e}") | ||
| params = dict() | ||
|
|
||
| #9/30/22 | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.