summaryrefslogtreecommitdiff
path: root/morphlib/morph2.py
diff options
context:
space:
mode:
authorJannis Pohlmann <jannis.pohlmann@codethink.co.uk>2012-09-04 15:52:07 +0000
committerSam Thursfield <sam.thursfield@codethink.co.uk>2012-09-04 16:59:09 +0100
commit99e80f647f9ef1c17da203342d932fb5ffc440ee (patch)
treebb8cb9a6b377653193d0ea45b9224a5c49c3814b /morphlib/morph2.py
parenta0ba0157c77bc3230f9de2b58d3a3fb40db0ab76 (diff)
downloadmorph-99e80f647f9ef1c17da203342d932fb5ffc440ee.tar.gz
Rework morphology child lookup to be more useful for branch/merge
In order to modify morphologies in place and then write them back to disk in system branches (e.g. when running "morph edit"), we need to have access to the dicts that store references to strata in system morphologies and chunks in stratum morphologies, respectively. Therefor, the previous triplet-returning child lookup is replaced with a new internal method to ensure uniqueness of names in morphologies and a new method to lookup children in this commit. The unit tests are adjusted to cover everything in appropriate ways.
Diffstat (limited to 'morphlib/morph2.py')
-rw-r--r--morphlib/morph2.py59
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)