-
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
base: dev
Are you sure you want to change the base?
Changes from all commits
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,26 +156,50 @@ def safe_literal_eval(filename, defaultValue): | |
| # =================================================================== | ||
| # Parameter Parsing | ||
| # =================================================================== | ||
| def parse_params(sparams: str) -> dict: | ||
| params = {} | ||
| if not sparams: | ||
| return params | ||
|
|
||
| s = sparams.strip() | ||
|
|
||
| #full dict literal | ||
| if s.startswith("{") and s.endswith("}"): | ||
| try: | ||
| val = literal_eval(s) | ||
| if isinstance(val, dict): | ||
| return val | ||
| except (ValueError, SyntaxError): | ||
| pass | ||
|
|
||
| for item in s.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 | ||
|
Comment on lines
+175
to
+187
|
||
|
|
||
| try: | ||
| sparams_path = concore_params_file | ||
| if os.path.exists(sparams_path): | ||
| with open(sparams_path, "r") as f: | ||
| sparams = f.read() | ||
| sparams = f.read().strip() | ||
| if sparams: # Ensure sparams is not empty | ||
| # Windows sometimes keeps quotes | ||
| 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) | ||
| logging.debug("parsed params: " + str(params)) | ||
GREENRAT-K405 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| else: | ||
| params = dict() | ||
| else: | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,20 +25,53 @@ 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 | ||
|
|
||
| s = sparams.strip() | ||
|
|
||
| # full dict literal | ||
| if s.startswith("{") and s.endswith("}"): | ||
| try: | ||
| val = literal_eval(s) | ||
| if isinstance(val, dict): | ||
| return val | ||
| except (ValueError, SyntaxError): | ||
GREENRAT-K405 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| pass | ||
|
|
||
| # keep backward compatibility: comma-separated params | ||
| for item in s.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 | ||
|
Comment on lines
+44
to
+56
|
||
| return params | ||
|
|
||
| try: | ||
| sparams = open(concore_params_file).read() | ||
| if sparams[0] == '"': #windows keeps "" need to remove | ||
| with open(concore_params_file, "r") as f: | ||
| sparams = f.read().strip() | ||
|
|
||
| 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.