Skip to content

Commit c51d270

Browse files
authored
Merge pull request #141 from saksham-gera/dev
Fixing Issue #137 partially for concore.py and concoredocker.py
2 parents 7637d8d + 96fe4fb commit c51d270

2 files changed

Lines changed: 150 additions & 97 deletions

File tree

concore.py

Lines changed: 73 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,24 @@
1212
with open("concorekill.bat","w") as fpid:
1313
fpid.write("taskkill /F /PID "+str(os.getpid())+"\n")
1414

15-
try:
16-
iport = literal_eval(open("concore.iport").read())
17-
except:
18-
iport = dict()
19-
try:
20-
oport = literal_eval(open("concore.oport").read())
21-
except:
22-
oport = dict()
23-
15+
def safe_literal_eval(filename, defaultValue):
16+
try:
17+
with open(filename, "r") as file:
18+
return literal_eval(file.read())
19+
except (FileNotFoundError, SyntaxError, ValueError, Exception) as e:
20+
print(f"Error reading {filename}: {e}")
21+
return defaultValue
22+
23+
iport = safe_literal_eval("concore.iport", {})
24+
oport = safe_literal_eval("concore.oport", {})
2425

2526
s = ''
2627
olds = ''
2728
delay = 1
2829
retrycount = 0
2930
inpath = "./in" #must be rel path for local
3031
outpath = "./out"
32+
simtime = 0
3133

3234
#9/21/22
3335
try:
@@ -46,70 +48,92 @@
4648
except:
4749
params = dict()
4850
#9/30/22
49-
def tryparam(n,i):
50-
try:
51-
return params[n]
52-
except:
53-
return i
51+
def tryparam(n, i):
52+
return params.get(n, i)
5453

5554

5655
#9/12/21
5756
def default_maxtime(default):
5857
global maxtime
59-
try:
60-
maxtime = literal_eval(open(inpath+"1/concore.maxtime").read())
61-
except:
62-
maxtime = default
58+
maxtime = safe_literal_eval(os.path.join(inpath + "1", "concore.maxtime"), default)
59+
6360
default_maxtime(100)
6461

6562
def unchanged():
66-
global olds,s
67-
if olds==s:
63+
global olds, s
64+
if olds == s:
6865
s = ''
6966
return True
70-
else:
71-
olds = s
72-
return False
67+
olds = s
68+
return False
7369

7470
def read(port, name, initstr):
75-
global s,simtime,retrycount
71+
global s, simtime, retrycount
72+
max_retries=5
7673
time.sleep(delay)
74+
file_path = os.path.join(inpath+str(port), name)
75+
7776
try:
78-
infile = open(inpath+str(port)+"/"+name);
79-
ins = infile.read()
80-
infile.close()
81-
except:
77+
with open(file_path, "r") as infile:
78+
ins = infile.read()
79+
except FileNotFoundError:
80+
print(f"File {file_path} not found, using default value.")
8281
ins = initstr
83-
while len(ins)==0:
82+
except Exception as e:
83+
print(f"Error reading {file_path}: {e}")
84+
return initstr
85+
86+
attempts = 0
87+
while len(ins) == 0 and attempts < max_retries:
8488
time.sleep(delay)
85-
infile = open(inpath+str(port)+"/"+name);
86-
ins = infile.read()
87-
infile.close()
89+
try:
90+
with open(file_path, "r") as infile:
91+
ins = infile.read()
92+
except Exception as e:
93+
print(f"Retry {attempts + 1}: Error reading {file_path} - {e}")
94+
attempts += 1
8895
retrycount += 1
96+
97+
if len(ins) == 0:
98+
print(f"Max retries reached for {file_path}, using default value.")
99+
return initstr
100+
89101
s += ins
90-
inval = literal_eval(ins)
91-
simtime = max(simtime,inval[0])
92-
return inval[1:]
102+
try:
103+
inval = literal_eval(ins)
104+
simtime = max(simtime, inval[0])
105+
return inval[1:]
106+
except Exception as e:
107+
print(f"Error parsing {ins}: {e}")
108+
return initstr
109+
93110

94111
def write(port, name, val, delta=0):
95-
global outpath,simtime
96-
if isinstance(val,str):
97-
time.sleep(2*delay)
98-
elif isinstance(val,list)==False:
99-
print("mywrite must have list or str")
100-
quit()
112+
global simtime
113+
file_path = os.path.join(outpath+str(port), name)
114+
115+
if isinstance(val, str):
116+
time.sleep(2 * delay)
117+
elif not isinstance(val, list):
118+
print("write must have list or str")
119+
return
120+
101121
try:
102-
with open(outpath+str(port)+"/"+name,"w") as outfile:
103-
if isinstance(val,list):
104-
outfile.write(str([simtime+delta]+val))
122+
with open(file_path, "w") as outfile:
123+
if isinstance(val, list):
124+
outfile.write(str([simtime + delta] + val))
105125
simtime += delta
106126
else:
107127
outfile.write(val)
108-
except:
109-
print("skipping"+outpath+str(port)+"/"+name);
128+
except Exception as e:
129+
print(f"Error writing to {file_path}: {e}")
110130

111131
def initval(simtime_val):
112132
global simtime
113-
val = literal_eval(simtime_val)
114-
simtime = val[0]
115-
return val[1:]
133+
try:
134+
val = literal_eval(simtime_val)
135+
simtime = val[0]
136+
return val[1:]
137+
except Exception as e:
138+
print(f"Error parsing simtime_val: {e}")
139+
return []

concoredocker.py

Lines changed: 77 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,26 @@
11
import time
22
from ast import literal_eval
33
import re
4+
import os
45

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", {})
1416

1517
s = ''
1618
olds = ''
1719
delay = 1
1820
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
2124

2225
#9/21/22
2326
try:
@@ -35,68 +38,94 @@
3538
print("bad params: "+sparams)
3639
except:
3740
params = dict()
41+
3842
#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)
4445

4546
#9/12/21
4647
def default_maxtime(default):
4748
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+
5251
default_maxtime(100)
5352

5453
def unchanged():
55-
global olds,s
56-
if olds==s:
54+
global olds, s
55+
if olds == s:
5756
s = ''
5857
return True
59-
else:
60-
olds = s
61-
return False
58+
olds = s
59+
return False
6260

6361
def read(port, name, initstr):
64-
global s,simtime,retrycount
62+
global s, simtime, retrycount
63+
max_retries=5
6564
time.sleep(delay)
65+
file_path = os.path.join(inpath+str(port), name)
66+
6667
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.")
7072
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:
7279
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
7486
retrycount += 1
87+
88+
if len(ins) == 0:
89+
print(f"Max retries reached for {file_path}, using default value.")
90+
return initstr
91+
7592
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+
79101

80102
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+
87112
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))
91116
simtime += delta
92117
else:
93118
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}")
96121

97122
def initval(simtime_val):
98123
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 []
102131

0 commit comments

Comments
 (0)