Skip to content

Commit

Permalink
update safe delete function.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mariosmsk committed Mar 24, 2024
1 parent 8609404 commit 4d1a052
Showing 1 changed file with 30 additions and 7 deletions.
37 changes: 30 additions & 7 deletions epyt/epanet.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,26 @@ def __init__(self):
MSX_FLOWPACED = 3


def safe_delete(file):
if isinstance(file, list):
for file_path in file:
try:
try:
os.unlink(rf"{file_path}")
except:
os.remove(rf"{file_path}")
except Exception as e:
print(f"Could not delete {file}: {e}")
else:
try:
try:
os.unlink(rf"{file_path}")
except:
os.remove(rf"{file_path}")
except Exception as e:
print(f"Could not delete {file}: {e}")


class EpytValues:

def __init__(self):
Expand Down Expand Up @@ -10340,13 +10360,6 @@ def unload(self):

See also epanet, saveInputFile, closeNetwork().
"""

def safe_delete(file):
try:
os.unlink(file)
except Exception as e:
print(f"Could not delete {file}: {e}")

try:
self.api.ENclose()
finally:
Expand All @@ -10358,6 +10371,13 @@ def safe_delete(file):
safe_delete(file)
safe_delete(self.TempInpFile)

cwd = os.getcwd()
tmp_files = list(filter(lambda f: os.path.isfile(os.path.join(cwd, f))
and f.startswith("s") and 6 <= len(f) <= 8 and "." not in f and "_" in f,
os.listdir(cwd)))
tmp_files_paths = [os.path.join(cwd, f) for f in tmp_files]
safe_delete(tmp_files_paths)

print(f'Close toolkit for the input file "{self.netName[0:-4]}". EPANET Toolkit is unloaded.\n')

def useHydraulicFile(self, hydname):
Expand Down Expand Up @@ -11150,6 +11170,9 @@ def unloadMSX(self):
d.unloadMSX()
"""
self.msx.MSXclose()
msx_temp_files = list(filter(lambda f: os.path.isfile(os.path.join(os.getcwd(), f))
and f.startswith("msx") and "." not in f, os.listdir(os.getcwd())))
safe_delete(msx_temp_files)

def getMSXSpeciesCount(self):
""" Retrieves the number of species.
Expand Down

0 comments on commit 4d1a052

Please sign in to comment.