diff options
author | Sam Thursfield <sam.thursfield@codethink.co.uk> | 2012-09-04 16:59:45 +0100 |
---|---|---|
committer | Sam Thursfield <sam.thursfield@codethink.co.uk> | 2012-09-04 16:59:45 +0100 |
commit | 9ccfb38951daa3614014b568e6af8af10153fd83 (patch) | |
tree | bb8cb9a6b377653193d0ea45b9224a5c49c3814b /morphlib/morph2.py | |
parent | a0ba0157c77bc3230f9de2b58d3a3fb40db0ab76 (diff) | |
parent | 99e80f647f9ef1c17da203342d932fb5ffc440ee (diff) | |
download | morph-9ccfb38951daa3614014b568e6af8af10153fd83.tar.gz |
Merge branch 'jannis/implement-chunk-strata-lookup-differently'
Diffstat (limited to 'morphlib/morph2.py')
-rw-r--r-- | morphlib/morph2.py | 59 |
1 files changed, 35 insertions, 24 deletions
diff --git a/morphlib/morph2.py b/morphlib/morph2.py index ffb05660..a707030d 100644 --- a/morphlib/morph2.py +++ b/morphlib/morph2.py @@ -44,7 +44,7 @@ class Morphology(object): def __init__(self, text): self._dict = json.loads(text) self._set_defaults() - self._create_child_index() + self._validate_children() def __getitem__(self, key): return self._dict[key] @@ -55,10 +55,40 @@ class Morphology(object): def keys(self): return self._dict.keys() - def lookup_morphology_by_name(self, name): - '''Find child morphology by its morphology name, honouring aliases - defined within this stratum.''' - return self._child_dict[name] + def _validate_children(self): + if self['kind'] == 'system': + names = set() + for info in self['strata']: + name = info.get('alias', info['morph']) + if name in names: + raise ValueError('Duplicate stratum "%s"' % name) + names.add(name) + elif self['kind'] == 'stratum': + names = set() + for info in self['chunks']: + name = info.get('alias', info['name']) + if name in names: + raise ValueError('Duplicate chunk "%s"' % name) + names.add(name) + + def lookup_child_by_name(self, name): + '''Find child reference by its name. + + This lookup honors aliases. + + ''' + + if self['kind'] == 'system': + for info in self['strata']: + source_name = info.get('alias', info['morph']) + if source_name == name: + return info + elif self['kind'] == 'stratum': + for info in self['chunks']: + source_name = info.get('alias', info['name']) + if source_name == name: + return info + raise KeyError('"%s" not found' % name) def _set_defaults(self): if 'max-jobs' in self: @@ -92,22 +122,3 @@ class Morphology(object): source['morph'] = source['name'] if 'build-depends' not in source: source['build-depends'] = None - - def _get_valid_triple(self, info): - return (info['repo'], info['ref'], "%s.morph" % info['morph']) - - def _create_child_index(self): - self._child_dict = {} - if self['kind'] == 'system': - for info in self['strata']: - source_name = info.get('alias', info['morph']) - if source_name in self._child_dict: - raise ValueError("duplicate stratum name: " + source_name) - self._child_dict[source_name] = self._get_valid_triple(info) - elif self['kind'] == 'stratum': - for info in self['chunks']: - # FIXME: in the future, chunks will have an 'alias' field too - source_name = info['name'] - if source_name in self._child_dict: - raise ValueError("duplicate chunk name: " + source_name) - self._child_dict[source_name] = self._get_valid_triple(info) |