summaryrefslogtreecommitdiff
path: root/morphlib/morphloader.py
diff options
context:
space:
mode:
authorTiago Gomes <tiago.gomes@codethink.co.uk>2015-11-12 09:37:52 +0000
committerPedro Alvarez <pedro.alvarez@codethink.co.uk>2016-03-25 20:47:55 +0000
commit0ee859ada56e13b3c3a5d789372ba8e7e16c05ad (patch)
treef5aba4f9dd3dc6130883addd6198227c922e5856 /morphlib/morphloader.py
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
Diffstat (limited to 'morphlib/morphloader.py')
-rw-r--r--morphlib/morphloader.py46
1 files changed, 15 insertions, 31 deletions
diff --git a/morphlib/morphloader.py b/morphlib/morphloader.py
index 668b133e..66f4763a 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.'''