diff options
author | Richard Maw <richard.maw@codethink.co.uk> | 2013-08-29 11:25:36 +0000 |
---|---|---|
committer | Richard Maw <richard.maw@codethink.co.uk> | 2013-08-30 10:34:53 +0000 |
commit | b0096f15bb3624207b1df71b22070b4780b03658 (patch) | |
tree | ffcf474dd043d404b0a9e791c7a8cd69f6fb5e1e /morphlib/morphloader.py | |
parent | 4ee24ddb0c89723a3e6c1c15ccdea9c8d454d084 (diff) | |
download | morph-b0096f15bb3624207b1df71b22070b4780b03658.tar.gz |
morphloader: Add method to unset default values
The original contents of the morphology is not generally trackable,
comments are lost for example, and it's simpler to output a canonical
format than attempt to preserve everything of the original.
However, we don't want to clutter the output with fields that have
been filled out to be the defaults, so provide a method to remove
fields that are the same as their default.
This also removes a check in set_defaults that it is a valid kind,
since it explicitly declares it assumes the morphology is valid.
Diffstat (limited to 'morphlib/morphloader.py')
-rw-r--r-- | morphlib/morphloader.py | 25 |
1 files changed, 23 insertions, 2 deletions
diff --git a/morphlib/morphloader.py b/morphlib/morphloader.py index 70f46064..c94078f9 100644 --- a/morphlib/morphloader.py +++ b/morphlib/morphloader.py @@ -332,8 +332,22 @@ class MorphologyLoader(object): self._set_stratum_defaults(morphology) elif kind == 'chunk': self._set_chunk_defaults(morphology) - else: - assert kind == 'cluster' + + def unset_defaults(self, morphology): + '''If a field is equal to its default, delete it. + + The morphology is assumed to be valid. + + ''' + + kind = morphology['kind'] + defaults = self._static_defaults[kind] + for key in defaults: + if key in morphology and morphology[key] == defaults[key]: + del morphology[key] + + if kind == 'stratum': + self._unset_stratum_defaults(morphology) def _set_system_defaults(self, morph): pass @@ -345,6 +359,13 @@ class MorphologyLoader(object): if 'morph' not in spec: spec['morph'] = spec['name'] + def _unset_stratum_defaults(self, morph): + for spec in morph['chunks']: + if 'repo' in spec and spec['repo'] == spec['name']: + del spec['repo'] + if 'morph' in spec and spec['morph'] == spec['name']: + del spec['morph'] + def _set_chunk_defaults(self, morph): if morph['max-jobs'] is not None: morph['max-jobs'] = int(morph['max-jobs']) |