Code refactor

This commit is contained in:
WQY\qiong
2022-11-19 18:30:45 +08:00
parent 25b5de9e2d
commit 94e35851cd

View File

@@ -16,17 +16,15 @@ def _verify_platform():
class Output:
def __init__(self, name: str) -> None:
self._name = name
def __init__(self, path: str) -> None:
self._path = path
self._lib = ctypes.CDLL(os.path.join(os.getcwd(), 'epanet-output.dll'))
self._handle = ctypes.c_void_p()
self._check(self._lib.ENR_init(ctypes.byref(self._handle)))
dir = os.path.dirname(os.getcwd())
self.opt = os.path.join(os.path.join(dir, 'temp'), self._name + '.db.opt')
self._check(self._lib.ENR_open(self._handle, ctypes.c_char_p(self.opt.encode())))
self._check(self._lib.ENR_open(self._handle, ctypes.c_char_p(self._path.encode())))
def _check(self, result):
@@ -35,7 +33,7 @@ class Output:
code = self._lib.ENR_checkError(self._handle, ctypes.byref(msg))
assert code == result
error = f'Failed to read project [{self._name}] output, message [{msg.value.decode()}]'
error = f'Failed to read project [{self._path}] output, message [{msg.value.decode()}]'
self._lib.ENR_free(ctypes.byref(msg))
@@ -147,8 +145,8 @@ class Output:
self._check(self._lib.ENR_close(ctypes.byref(self._handle)))
def _read_output(name: str) -> str:
opt = Output(name)
def dump_output(path: str) -> str:
opt = Output(path)
return opt.dump()
@@ -170,11 +168,16 @@ def run_project(name: str) -> str:
result = os.system(command)
if result != 0:
raise Exception("Failed to run simulation for project [{name}]")
msg = f'Failed to run simulation for project [{name}]'
raise Exception(msg)
return _read_output(name)
return dump_output(opt)
def run_inp(inp: str) -> str:
return ''
if __name__ == '__main__':
_verify_platform()
print(_read_output('net3'))
print(run_project('net3'))