diff options
author | Sam Thursfield <sam.thursfield@codethink.co.uk> | 2012-09-10 11:09:51 +0100 |
---|---|---|
committer | Sam Thursfield <sam.thursfield@codethink.co.uk> | 2012-09-10 20:04:06 +0100 |
commit | a12cbe6e106c9f62e08a89a3c012a3f3d4a48e9d (patch) | |
tree | 0315b2f2b1835886030922671c5712f3c6ddd866 /morphlib/morph2.py | |
parent | 02f4c93a143b7121c5bd84ec2e20c665149db4c8 (diff) | |
download | morph-a12cbe6e106c9f62e08a89a3c012a3f3d4a48e9d.tar.gz |
Preserve sort order of morphologies, so they can be edited by Morph
Diffstat (limited to 'morphlib/morph2.py')
-rw-r--r-- | morphlib/morph2.py | 62 |
1 files changed, 48 insertions, 14 deletions
diff --git a/morphlib/morph2.py b/morphlib/morph2.py index d99e4a9e..0eca6fed 100644 --- a/morphlib/morph2.py +++ b/morphlib/morph2.py @@ -14,7 +14,10 @@ # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +import collections +import copy import json +import re class Morphology(object): @@ -50,8 +53,15 @@ class Morphology(object): ] } + @staticmethod + def _order_keys(pairs): + result = collections.OrderedDict() + for k,v in pairs: + result[k] = v + return result + def __init__(self, text): - self._dict = json.loads(text) + self._dict = json.loads(text, object_pairs_hook=self._order_keys) self._set_defaults() self._validate_children() @@ -105,19 +115,8 @@ class Morphology(object): if 'disk-size' in self: size = self['disk-size'] - if isinstance(size, basestring): - size = size.lower() - if size.endswith('g'): - size = int(size[:-1]) * 1024 ** 3 - elif size.endswith('m'): # pragma: no cover - size = int(size[:-1]) * 1024 ** 2 - elif size.endswith('k'): # pragma: no cover - size = int(size[:-1]) * 1024 - else: # pragma: no cover - size = int(size) - else: # pragma: no cover - size = int(size) - self._dict['disk-size'] = size + self._dict['_disk-size'] = size + self._dict['disk-size'] = self._parse_size(size) for name, value in self.static_defaults[self['kind']]: if name not in self._dict: @@ -134,3 +133,38 @@ class Morphology(object): source['morph'] = source['name'] if 'build-depends' not in source: source['build-depends'] = None + + def _parse_size(self, size): + if isinstance(size, basestring): + size = size.lower() + if size.endswith('g'): + return int(size[:-1]) * 1024 ** 3 + elif size.endswith('m'): # pragma: no cover + return int(size[:-1]) * 1024 ** 2 + elif size.endswith('k'): # pragma: no cover + return int(size[:-1]) * 1024 + return int(size) + + def write_to_file(self, f): + # Recreate dict without the empty default values, with a few kind + # specific hacks to try and edit standard morphologies as + # non-destructively as possible + as_dict = collections.OrderedDict() + for key in self.keys(): + if self['kind'] == 'stratum' and key == 'chunks': + value = copy.copy(self[key]) + for chunk in value: + if chunk["morph"] == chunk["name"]: + del chunk["morph"] + if self['kind'] == 'system' and key == 'disk-size': + # Use human-readable value (assumes we never programmatically + # change this value within morph) + value = self['_disk-size'] + else: + value = self[key] + if value and key[0] != '_': + as_dict[key] = value + text = json.dumps(as_dict, indent=4) + text = re.sub(" \n", "\n", text) + f.write(text) + f.write('\n') |