From 717450e82cbdd3a4a73ba3a98c56a8c333e549c9 Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Fri, 16 Mar 2012 00:06:12 -0400 Subject: [PATCH] Create missing ~/.config directory (and other) if necessary. --- h5config/storage/__init__.py | 5 +++++ h5config/storage/hdf5.py | 1 + h5config/storage/yaml.py | 1 + 3 files changed, 7 insertions(+) diff --git a/h5config/storage/__init__.py b/h5config/storage/__init__.py index a62c473..a9dc2d4 100644 --- a/h5config/storage/__init__.py +++ b/h5config/storage/__init__.py @@ -46,3 +46,8 @@ class FileStorage (Storage): def __init__(self, filename=None): self._filename = filename + + def _create_basedir(self, filename): + dirname = _os_path.dirname(filename) + if not _os_path.isdir(dirname): + _os.makedirs(dirname) diff --git a/h5config/storage/hdf5.py b/h5config/storage/hdf5.py index b8384bc..65b2ba7 100644 --- a/h5config/storage/hdf5.py +++ b/h5config/storage/hdf5.py @@ -146,6 +146,7 @@ class HDF5_Storage (_FileStorage): self._file_checked = True def _setup_file(self): + self._create_basedir(filename=self._filename) with _h5py.File(self._filename, 'a') as f: cwg = f # current working group h5_create_group(cwg, self.group) diff --git a/h5config/storage/yaml.py b/h5config/storage/yaml.py index 61200d4..7999d20 100644 --- a/h5config/storage/yaml.py +++ b/h5config/storage/yaml.py @@ -134,6 +134,7 @@ class YAML_Storage (_FileStorage): return config def _save(self, config): + self._create_basedir(filename=self._filename) data = self._to_dict(config) with open(self._filename, 'w') as f: _yaml.dump(data, stream=f, Dumper=self.dumper, -- 2.26.2