summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTiago Gomes <tiago.gomes@codethink.co.uk>2015-11-12 09:37:52 (GMT)
committerPedro Alvarez <pedro.alvarez@codethink.co.uk>2016-03-25 20:47:55 (GMT)
commit0ee859ada56e13b3c3a5d789372ba8e7e16c05ad (patch)
treef5aba4f9dd3dc6130883addd6198227c922e5856
parentd58d8e8f7a4ec03ff14021a4515c8283dad52573 (diff)
downloadmorph-0ee859ada56e13b3c3a5d789372ba8e7e16c05ad.tar.gz
morphloader: simplify API
Use an optional parameter to control whether the defaults should be set instead of a separate function. Change-Id: Idbbd5a08e8b16d8e01bb9539274092978b64f6f0
-rw-r--r--morphlib/morphloader.py46
-rw-r--r--morphlib/morphloader_tests.py22
-rw-r--r--morphlib/plugins/diff_plugin.py4
3 files changed, 31 insertions, 41 deletions
diff --git a/morphlib/morphloader.py b/morphlib/morphloader.py
index 668b133..66f4763 100644
--- a/morphlib/morphloader.py
+++ b/morphlib/morphloader.py
@@ -388,49 +388,32 @@ class MorphologyLoader(object):
self._predefined_build_systems['manual'] = \
morphlib.buildsystem.ManualBuildSystem()
- def parse_morphology_text(self, text, morph_filename):
- '''Parse a textual morphology.
-
- The text may be a string, or an open file handle.
+ def load_from_string(self, string, filename='string',
+ set_defaults=True): # pragma: no cover
+ '''Load a morphology from a string.
- Return the new Morphology object, or raise an error indicating
- the problem. This method does minimal validation: a syntactically
- correct morphology is fine, even if none of the fields are
- valid. It also does not set any default values for any of the
- fields. See validate and set_defaults.
+ Return the Morphology object.
'''
try:
- obj = yaml.safe_load(text)
+ obj = yaml.safe_load(string)
except yaml.error.YAMLError as e:
- raise MorphologyNotYamlError(morph_filename, e)
+ raise MorphologyNotYamlError(filename, e)
if not isinstance(obj, dict):
- raise NotADictionaryError(morph_filename)
+ raise NotADictionaryError(filename)
m = morphlib.morphology.Morphology(obj)
- m.filename = morph_filename
- return m
-
- def load_from_string(self, string,
- filename='string'): # pragma: no cover
- '''Load a morphology from a string.
-
- Return the Morphology object.
-
- '''
-
- if string is None:
- return None
-
- m = self.parse_morphology_text(string, filename)
+ m.filename = filename
self.validate(m)
- self.set_commands(m)
- self.set_defaults(m)
+
+ if set_defaults:
+ self.set_commands(m)
+ self.set_defaults(m)
return m
- def load_from_file(self, filename):
+ def load_from_file(self, filename, set_defaults=True):
'''Load a morphology from a named file.
Return the Morphology object.
@@ -439,7 +422,8 @@ class MorphologyLoader(object):
with open(filename) as f:
text = f.read()
- return self.load_from_string(text, filename=filename)
+ return self.load_from_string(text, filename=filename,
+ set_defaults=set_defaults)
def save_to_string(self, morphology):
'''Return normalised textual form of morphology.'''
diff --git a/morphlib/morphloader_tests.py b/morphlib/morphloader_tests.py
index 3d9e5fb..db22264 100644
--- a/morphlib/morphloader_tests.py
+++ b/morphlib/morphloader_tests.py
@@ -61,7 +61,7 @@ name: foo
kind: chunk
build-system: manual
'''
- morph = self.loader.parse_morphology_text(string, 'test')
+ morph = self.loader.load_from_string(string, 'test')
self.assertEqual(morph['kind'], 'chunk')
self.assertEqual(morph['name'], 'foo')
self.assertEqual(morph['build-system'], 'manual')
@@ -69,12 +69,12 @@ build-system: manual
def test_fails_to_parse_utter_garbage(self):
self.assertRaises(
morphlib.morphloader.MorphologySyntaxError,
- self.loader.parse_morphology_text, ',,,', 'test')
+ self.loader.load_from_string, ',,,', 'test')
def test_fails_to_parse_non_dict(self):
self.assertRaises(
morphlib.morphloader.NotADictionaryError,
- self.loader.parse_morphology_text, '- item1\n- item2\n', 'test')
+ self.loader.load_from_string, '- item1\n- item2\n', 'test')
def test_fails_to_validate_dict_without_kind(self):
m = morphlib.morphology.Morphology({
@@ -689,9 +689,10 @@ build-system: manual
name: foo
kind: cluster
systems:
- - morph: bar
+ - morph: bar
+ deploy: {}
'''
- m = self.loader.parse_morphology_text(string, 'test')
+ m = self.loader.load_from_string(string, 'test')
self.loader.set_defaults(m)
self.loader.validate(m)
self.assertEqual(m['name'], 'foo')
@@ -740,12 +741,17 @@ build-system: manual
def test_multi_line_round_trip(self):
s = ('name: foo\n'
- 'kind: bar\n'
+ 'kind: system\n'
'description: |\n'
' 1 2 3\n'
' 4 5 6\n'
- ' 7 8 9\n')
- m = self.loader.parse_morphology_text(s, 'string')
+ ' 7 8 9\n'
+ 'arch: x86_64\n'
+ 'strata:\n'
+ '- name: le-chunk\n'
+ ' morph: le-chunk\n'
+ 'configuration-extensions: []\n')
+ m = self.loader.load_from_string(s, 'string')
self.assertEqual(s, self.loader.save_to_string(m))
def test_smoketest_multi_line_unicode(self):
diff --git a/morphlib/plugins/diff_plugin.py b/morphlib/plugins/diff_plugin.py
index 24a6d69..de4ca0b 100644
--- a/morphlib/plugins/diff_plugin.py
+++ b/morphlib/plugins/diff_plugin.py
@@ -108,8 +108,8 @@ class DiffPlugin(cliapp.Plugin):
definitions = mf.list_morphologies()
system_paths = set()
for definition in definitions:
- m = ml.parse_morphology_text(mf.read_file(definition),
- definition)
+ m = ml.load_from_string(mf.read_file(definition), definition,
+ False)
if m.get('kind') == 'system' or 'strata' in m:
system_paths.add(definition)
return reponame, ref, system_paths