From: W. Trevor King Date: Thu, 28 Jul 2011 17:35:12 +0000 (-0400) Subject: Split out h5_create_group so it can be used by other modules. X-Git-Tag: v0.1^0 X-Git-Url: http://git.tremily.us/?p=h5config.git;a=commitdiff_plain;h=0dab14f71e9387d9c44f1d42f886ca834ec0d03f Split out h5_create_group so it can be used by other modules. My initial break from pypiezo was not from the pypiezo tip. This commit pulls in following pypiezo changes: commit 67d6dca84988d5d4271835522f4697d9e0a2c6f2 Split out h5_create_group so it can be used by other modules. commit 9afcf604e967123b3cdf2665827afe24a74225f2 Return the group created in h5_create_group(). commit 5d84f6b5392236e8d2b1513178063bfc2a5f5893 Special case for '/' group in h5_create_group. It also strips trailing whitespace from h5config/hdf5.py. --- diff --git a/h5config/hdf5.py b/h5config/hdf5.py index 4e06567..996fadf 100644 --- a/h5config/hdf5.py +++ b/h5config/hdf5.py @@ -35,7 +35,7 @@ def pformat_HDF5(filename, group='/'): def _pformat_hdf5(cwg, depth=0): lines = [] lines.append(' '*depth + cwg.name) - depth += 1 + depth += 1 for key,value in cwg.iteritems(): if isinstance(value, _h5py.Group): lines.extend(_pformat_hdf5(value, depth)) @@ -45,7 +45,21 @@ def _pformat_hdf5(cwg, depth=0): else: lines.append(' '*depth + str(value)) return lines - + +def h5_create_group(cwg, path): + "Create the group where the settings are stored (if necessary)." + if path == '/': + return cwg + gpath = [''] + for group in path.strip('/').split('/'): + gpath.append(group) + if group not in cwg.keys(): + _LOG.debug('creating group %s in %s' + % ('/'.join(gpath), cwg.file)) + cwg.create_group(group) + cwg = cwg[group] + return cwg + class _HDF5Config (_config.BackedConfig): """Mixin to back a `_Config` class with an HDF5 file. @@ -109,18 +123,7 @@ class _HDF5Config (_config.BackedConfig): def _setup_file(self): f = _h5py.File(self.filename, 'a') cwg = f # current working group - - # Create the group where the settings are stored (if necessary) - gpath = [''] - for group in self.group.strip('/').split('/'): - if not group: - break - gpath.append(group) - if group not in cwg.keys(): - _LOG.debug('creating group %s in %s' - % ('/'.join(gpath), self.filename)) - cwg.create_group(group) - cwg = cwg[group] + h5_create_group(cwg, self.group) f.close() def dump(self, help=False, from_file=False):