Skip to content

Commit b6f3ddb

Browse files
committed
Fix creation of config files and their parent folders when they don't exist
1 parent a0678c7 commit b6f3ddb

2 files changed

Lines changed: 19 additions & 1 deletion

File tree

simulaqron/settings/network_config.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,8 +287,18 @@ def write_to_file(self, file_path: PathLike | str):
287287
if file_path is None:
288288
raise ValueError("Since this networks config was not initialized with a file_path you need to specify one")
289289

290+
# Create the Path object
291+
file_path = Path(str(file_path)).resolve()
292+
293+
# Create all the parent folder if they not exists
294+
if not file_path.parent.exists():
295+
file_path.parent.mkdir(parents=True)
296+
297+
# Poke the file, so it exists before opening
298+
file_path.touch(exist_ok=True)
299+
290300
dictionary = self.to_dict()
291-
with open(file_path, 'w') as f:
301+
with file_path.open('wt') as f:
292302
json.dump(dictionary, f, indent=4)
293303

294304
def read_from_file(self, file_path: PathLike | str):

simulaqron/settings/simulaqron_config.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,14 @@ def default_settings(self):
158158

159159
def save_to_file(self, path: PathLike):
160160
file_path = Path(str(path)).resolve()
161+
162+
# Create all the parent folder if they not exists
163+
if not file_path.parent.exists():
164+
file_path.parent.mkdir(parents=True)
165+
166+
# Poke the file, so it exists before opening
167+
file_path.touch(exist_ok=True)
168+
161169
with file_path.open("wt") as file:
162170
serialized = JSONSerializer.serialize(self)
163171
json.dump(serialized, file, indent=4)

0 commit comments

Comments
 (0)