Skip to content

Commit 1a5b456

Browse files
Cyanoxygenliushuyu
andcommitted
ab3cfg: improve file reading and parsing logic
- Replace try...catch block in file reading with 'with open()' statement Co-Authored-By: liushuyu <[email protected]>
1 parent 67d2bc7 commit 1a5b456

File tree

1 file changed

+9
-12
lines changed

1 file changed

+9
-12
lines changed

acbs/ab3cfg.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,24 @@ def __init__(self, path) -> None:
1111
:param path: path to the ab3cfg.sh file, with the filename
1212
'''
1313
self.cfgpath = path
14-
self.file = None
1514
self.vars = None
1615
self.stage2 = False
1716
try:
18-
self.file = open(self.cfgpath, 'rt')
19-
except IOError:
17+
with open(self.cfgpath, 'rt') as cfgfile:
18+
self.parse(cfgfile)
19+
except Exception as e:
2020
# You run ACBS then you should have AB3, eh?
2121
raise RuntimeError(f'Autobuild3 config file {self.cfgpath} does not exist\n' +
22-
'Unable to read Autobuild3 config file.')
23-
if self.file:
24-
self.parse()
22+
'Unable to read Autobuild3 config file.') from e
2523

26-
def parse(self):
24+
def parse(self, file):
2725
'''
2826
Parse the ab3cfg.sh file.
2927
'''
30-
if self.file:
31-
try:
32-
self.vars = bashvar.eval_bashvar(self.file.read(), filename=self.cfgpath)
33-
except Exception as e:
34-
raise RuntimeError(f'Error parsing autobuild3 config file: {e}.')
28+
try:
29+
self.vars = bashvar.eval_bashvar(file.read(), filename=self.cfgpath)
30+
except Exception as e:
31+
raise RuntimeError(f'Error parsing autobuild3 config file: {e}.') from e
3532

3633
def is_in_stage2(self) -> bool:
3734
'''

0 commit comments

Comments
 (0)