|
1 | 1 | import time |
2 | 2 | from ast import literal_eval |
3 | 3 | import re |
| 4 | +import os |
4 | 5 |
|
5 | | -try: |
6 | | - iport = literal_eval(open("concore.iport").read()) |
7 | | -except: |
8 | | - iport = dict() |
9 | | -try: |
10 | | - oport = literal_eval(open("concore.oport").read()) |
11 | | -except: |
12 | | - oport = dict() |
13 | | - |
| 6 | +def safe_literal_eval(filename, defaultValue): |
| 7 | + try: |
| 8 | + with open(filename, "r") as file: |
| 9 | + return literal_eval(file.read()) |
| 10 | + except (FileNotFoundError, SyntaxError, ValueError, Exception) as e: |
| 11 | + print(f"Error reading {filename}: {e}") |
| 12 | + return defaultValue |
| 13 | + |
| 14 | +iport = safe_literal_eval("concore.iport", {}) |
| 15 | +oport = safe_literal_eval("concore.oport", {}) |
14 | 16 |
|
15 | 17 | s = '' |
16 | 18 | olds = '' |
17 | 19 | delay = 1 |
18 | 20 | retrycount = 0 |
19 | | -inpath = "/in" #must be abs path for docker |
20 | | -outpath = "/out" |
| 21 | +inpath = os.path.abspath("/in") |
| 22 | +outpath = os.path.abspath("/out") |
| 23 | +simtime = 0 |
21 | 24 |
|
22 | 25 | #9/21/22 |
23 | 26 | try: |
|
35 | 38 | print("bad params: "+sparams) |
36 | 39 | except: |
37 | 40 | params = dict() |
| 41 | + |
38 | 42 | #9/30/22 |
39 | | -def tryparam(n,i): |
40 | | - try: |
41 | | - return params[n] |
42 | | - except: |
43 | | - return i |
| 43 | +def tryparam(n, i): |
| 44 | + return params.get(n, i) |
44 | 45 |
|
45 | 46 | #9/12/21 |
46 | 47 | def default_maxtime(default): |
47 | 48 | global maxtime |
48 | | - try: |
49 | | - maxtime = literal_eval(open(inpath+"1/concore.maxtime").read()) |
50 | | - except: |
51 | | - maxtime = default |
| 49 | + maxtime = safe_literal_eval(os.path.join(inpath, "1", "concore.maxtime"), default) |
| 50 | + |
52 | 51 | default_maxtime(100) |
53 | 52 |
|
54 | 53 | def unchanged(): |
55 | | - global olds,s |
56 | | - if olds==s: |
| 54 | + global olds, s |
| 55 | + if olds == s: |
57 | 56 | s = '' |
58 | 57 | return True |
59 | | - else: |
60 | | - olds = s |
61 | | - return False |
| 58 | + olds = s |
| 59 | + return False |
62 | 60 |
|
63 | 61 | def read(port, name, initstr): |
64 | | - global s,simtime,retrycount |
| 62 | + global s, simtime, retrycount |
| 63 | + max_retries=5 |
65 | 64 | time.sleep(delay) |
| 65 | + file_path = os.path.join(inpath+str(port), name) |
| 66 | + |
66 | 67 | try: |
67 | | - infile = open(inpath+str(port)+"/"+name); |
68 | | - ins = infile.read() |
69 | | - except: |
| 68 | + with open(file_path, "r") as infile: |
| 69 | + ins = infile.read() |
| 70 | + except FileNotFoundError: |
| 71 | + print(f"File {file_path} not found, using default value.") |
70 | 72 | ins = initstr |
71 | | - while len(ins)==0: |
| 73 | + except Exception as e: |
| 74 | + print(f"Error reading {file_path}: {e}") |
| 75 | + return initstr |
| 76 | + |
| 77 | + attempts = 0 |
| 78 | + while len(ins) == 0 and attempts < max_retries: |
72 | 79 | time.sleep(delay) |
73 | | - ins = infile.read() |
| 80 | + try: |
| 81 | + with open(file_path, "r") as infile: |
| 82 | + ins = infile.read() |
| 83 | + except Exception as e: |
| 84 | + print(f"Retry {attempts + 1}: Error reading {file_path} - {e}") |
| 85 | + attempts += 1 |
74 | 86 | retrycount += 1 |
| 87 | + |
| 88 | + if len(ins) == 0: |
| 89 | + print(f"Max retries reached for {file_path}, using default value.") |
| 90 | + return initstr |
| 91 | + |
75 | 92 | s += ins |
76 | | - inval = literal_eval(ins) |
77 | | - simtime = max(simtime,inval[0]) |
78 | | - return inval[1:] |
| 93 | + try: |
| 94 | + inval = literal_eval(ins) |
| 95 | + simtime = max(simtime, inval[0]) |
| 96 | + return inval[1:] |
| 97 | + except Exception as e: |
| 98 | + print(f"Error parsing {ins}: {e}") |
| 99 | + return initstr |
| 100 | + |
79 | 101 |
|
80 | 102 | def write(port, name, val, delta=0): |
81 | | - global outpath,simtime |
82 | | - if isinstance(val,str): |
83 | | - time.sleep(2*delay) |
84 | | - elif isinstance(val,list)==False: |
85 | | - print("mywrite must have list or str") |
86 | | - quit() |
| 103 | + global simtime |
| 104 | + file_path = os.path.join(outpath+str(port), name) |
| 105 | + |
| 106 | + if isinstance(val, str): |
| 107 | + time.sleep(2 * delay) |
| 108 | + elif not isinstance(val, list): |
| 109 | + print("write must have list or str") |
| 110 | + return |
| 111 | + |
87 | 112 | try: |
88 | | - with open(outpath+str(port)+"/"+name,"w") as outfile: |
89 | | - if isinstance(val,list): |
90 | | - outfile.write(str([simtime+delta]+val)) |
| 113 | + with open(file_path, "w") as outfile: |
| 114 | + if isinstance(val, list): |
| 115 | + outfile.write(str([simtime + delta] + val)) |
91 | 116 | simtime += delta |
92 | 117 | else: |
93 | 118 | outfile.write(val) |
94 | | - except: |
95 | | - print("skipping"+outpath+str(port)+"/"+name); |
| 119 | + except Exception as e: |
| 120 | + print(f"Error writing to {file_path}: {e}") |
96 | 121 |
|
97 | 122 | def initval(simtime_val): |
98 | 123 | global simtime |
99 | | - val = literal_eval(simtime_val) |
100 | | - simtime = val[0] |
101 | | - return val[1:] |
| 124 | + try: |
| 125 | + val = literal_eval(simtime_val) |
| 126 | + simtime = val[0] |
| 127 | + return val[1:] |
| 128 | + except Exception as e: |
| 129 | + print(f"Error parsing simtime_val: {e}") |
| 130 | + return [] |
102 | 131 |
|
0 commit comments