|
6 | 6 |
|
7 | 7 | import os
|
8 | 8 | import json
|
| 9 | +from json import JSONDecodeError |
| 10 | +import yaml |
9 | 11 |
|
10 |
| -root_directory = os.path.join(os.getcwd(), "..") |
| 12 | +current_directory = os.getcwd() |
11 | 13 |
|
12 | 14 |
|
| 15 | +tag_by_file_type = {"json": "objective-function", "yaml": "rao-objective-function"} |
| 16 | + |
13 | 17 | def rao_parameters_file(file_path):
|
14 | 18 | correct_version = False
|
15 |
| - obj_fun = False |
16 |
| - if file_path.endswith(".json") or file_path.endswith(".yml"): |
| 19 | + score = 0 |
| 20 | + if "target" not in file_path and (file_path.endswith(".json") or file_path.endswith(".yml")): |
| 21 | + ftype = "json" if file_path.endswith(".json") else "yaml" |
17 | 22 | with open(os.path.join(dirpath, filename), 'r') as file:
|
18 | 23 | for line in file:
|
19 |
| - if "objective-function" in line: |
20 |
| - obj_fun = True |
21 | 24 | if '"version" : "2.4"' in line or '"version" : "2.5"' in line:
|
22 | 25 | correct_version = True
|
23 |
| - if correct_version and obj_fun: |
| 26 | + if tag_by_file_type[ftype] in line: |
| 27 | + score += 1 |
| 28 | + if "MAX_MIN_MARGIN_IN" in line or "MAX_MIN_RELATIVE_MARGIN_IN" in line: |
| 29 | + score += 1 |
| 30 | + if correct_version and score >= 2: |
24 | 31 | return True
|
25 | 32 | return False
|
26 | 33 |
|
| 34 | +def read_data(file_path) -> tuple[dict, str]: |
| 35 | + if file_path.endswith(".json"): |
| 36 | + with open(file_path, 'r') as file: |
| 37 | + try: |
| 38 | + return json.load(file), "json" |
| 39 | + except JSONDecodeError as je: |
| 40 | + print("in file " + file_path) |
| 41 | + raise je |
| 42 | + if file_path.endswith(".yml"): |
| 43 | + with open(file_path, 'r') as file: |
| 44 | + return yaml.safe_load(file), "yaml" # ["rao-parameters"] |
| 45 | + |
| 46 | +def extract_leading_whitespace(line): |
| 47 | + leading_whitespace = "" |
| 48 | + for char in line: |
| 49 | + if char.isspace(): |
| 50 | + leading_whitespace += char |
| 51 | + else: |
| 52 | + break |
| 53 | + return leading_whitespace |
| 54 | + |
| 55 | +def write_data(new_data, file_path, file_type): |
| 56 | + with open(file_path, 'r') as file: |
| 57 | + lines = file.readlines() |
| 58 | + lines_to_write = [] |
| 59 | + inside_obj_fun_to_replace = False |
| 60 | + for line in lines: |
| 61 | + if tag_by_file_type[file_type] in line and tag_by_file_type[file_type] in new_data: |
| 62 | + leading_whitespace = extract_leading_whitespace(line) |
| 63 | + inside_obj_fun_to_replace = True |
| 64 | + if inside_obj_fun_to_replace and ((file_type == "json" and "}" in line) or (file_type == "yaml" and line == "\n")): |
| 65 | + obj_fun_str = f'"{tag_by_file_type[file_type]}" : ' + obj_function_as_str_lines(new_data, file_type) |
| 66 | + for new_line in obj_fun_str.splitlines(True): |
| 67 | + lines_to_write.append(leading_whitespace + new_line) |
| 68 | + inside_obj_fun_to_replace = False |
| 69 | + elif not inside_obj_fun_to_replace: |
| 70 | + lines_to_write.append(line) |
| 71 | + with open(file_path, 'w') as file: |
| 72 | + file.writelines(lines_to_write) |
| 73 | + |
| 74 | + |
| 75 | +def obj_function_as_str_lines(new_data, file_type): |
| 76 | + if file_type == "json": |
| 77 | + return json.dumps(new_data[tag_by_file_type[file_type]], indent=2, separators=(',', ' : ')) + ',\n' |
| 78 | + else: |
| 79 | + return yaml.dump(new_data[tag_by_file_type[file_type]], default_flow_style=False) + '\n' |
| 80 | + |
| 81 | + |
| 82 | + |
| 83 | +def new_rao_param(data: dict, file_path: str, file_type: str) -> dict: |
| 84 | + try: |
| 85 | + old_obj_fun = data[tag_by_file_type[file_type]] |
| 86 | + new_obj_fun = {} |
| 87 | + for key in old_obj_fun: |
| 88 | + if key not in ("curative-stop-criterion", "optimize-curative-if-preventive-unsecure"): |
| 89 | + new_obj_fun[key] = old_obj_fun[key] |
| 90 | + prev_secure = "preventive-stop-criterion" not in old_obj_fun or old_obj_fun["preventive-stop-criterion"] == "SECURE" |
| 91 | + if prev_secure: |
| 92 | + if "optimize-curative-if-preventive-unsecure" in old_obj_fun: |
| 93 | + new_obj_fun["enforce-curative-security"] = old_obj_fun["optimize-curative-if-preventive-unsecure"] |
| 94 | + else: |
| 95 | + cur_secure = "curative-stop-criterion" in old_obj_fun and old_obj_fun["curative-stop-criterion"] in ("SECURE", "PREVENTIVE_OBJECTIVE_AND_SECURE", "MIN_OBJECTIVE") |
| 96 | + if cur_secure: |
| 97 | + new_obj_fun["enforce-curative-security"] = True |
| 98 | + else: |
| 99 | + new_obj_fun["enforce-curative-security"] = False |
| 100 | + except KeyError as ke: |
| 101 | + raise KeyError("in file " + file_path) from ke |
| 102 | + data[tag_by_file_type[file_type]] = new_obj_fun |
| 103 | + return data |
| 104 | + |
| 105 | + |
27 | 106 |
|
28 |
| -for dirpath, dirnames, filenames in os.walk(root_directory): |
29 |
| - for filename in filenames: |
30 |
| - file_path = os.path.join(dirpath, filename) |
31 |
| - if rao_parameters_file(file_path): |
32 |
| - print("file to change : " + file_path) |
33 |
| - lines = None |
34 |
| - if file_path.endswith(".json"): |
35 |
| - with open(file_path, 'r') as file: |
36 |
| - lines = file.readlines() |
37 |
| - lines = [line.replace('"version" : "2.4"', '"version" : "2.5"') for line in lines if "forbid-cost-increase" not in line] |
38 |
| - if lines is not None: |
39 |
| - with open(file_path, 'w') as file: |
40 |
| - file.writelines(lines) |
| 107 | +if __name__ == "__main__": |
| 108 | + base_dir = os.path.join(current_directory, "..") |
| 109 | + print(base_dir) |
| 110 | + for dirpath, dirnames, filenames in os.walk(base_dir): |
| 111 | + for filename in filenames: |
| 112 | + file_path = os.path.join(dirpath, filename) |
| 113 | + if rao_parameters_file(file_path): |
| 114 | + data, file_type = read_data(file_path) |
| 115 | + new_rao_params = new_rao_param(data, file_path, file_type) |
| 116 | + write_data(new_rao_params, file_path, file_type) |
0 commit comments