Skip to content

Commit

Permalink
Merge pull request #47 from lsst/tickets/DM-18488-daf_base
Browse files Browse the repository at this point in the history
DM-18488: Support pyyaml 5.1
  • Loading branch information
timj committed Mar 21, 2019
2 parents bdf3636 + 1c03cf2 commit aef33af
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,10 @@ def _propertyContainerElementTypeName(container, name):
"""Return name of the type of a particular element"""
try:
t = container.typeOf(name)
except LookupError:
except (LookupError, RuntimeError) as e:
# KeyError is more commonly expected when asking for an element
# from a mapping.
raise KeyError
raise KeyError(str(e))
for checkType in ("Bool", "Short", "Int", "Long", "LongLong", "Float", "Double", "String", "DateTime",
"PropertySet"):
if t == getattr(container, "TYPE_" + checkType):
Expand Down
26 changes: 17 additions & 9 deletions python/lsst/daf/base/yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,19 @@
from .propertyContainer import PropertyList, getPropertyListState, setPropertyListState, \
getPropertySetState, setPropertySetState, PropertySet

# For YAML >= 5.1 need a different Loader for the constructor
loaderList = []
if yaml:
loaderList = [yaml.Loader, yaml.CLoader]
try:
loaderList.append(yaml.FullLoader)
except AttributeError:
pass
try:
loaderList.append(yaml.UnsafeLoader)
except AttributeError:
pass


# YAML representers for key lsst.daf.base classes

Expand Down Expand Up @@ -83,10 +96,6 @@ def dt_constructor(loader, node):
return DateTime(str(dt), DateTime.TAI)


if yaml:
yaml.add_constructor('lsst.daf.base.DateTime', dt_constructor)


def pl_constructor(loader, node):
"""Construct an lsst.daf.base.PropertyList from a YAML pickle-like structure."""
pl = PropertyList()
Expand All @@ -95,10 +104,6 @@ def pl_constructor(loader, node):
setPropertyListState(pl, state)


if yaml:
yaml.add_constructor('lsst.daf.base.PropertyList', pl_constructor)


def ps_constructor(loader, node):
"""Construct an lsst.daf.base.PropertyList from a YAML pickle-like structure."""
ps = PropertySet()
Expand All @@ -108,4 +113,7 @@ def ps_constructor(loader, node):


if yaml:
yaml.add_constructor('lsst.daf.base.PropertySet', ps_constructor)
for loader in loaderList:
yaml.add_constructor('lsst.daf.base.PropertyList', pl_constructor, Loader=loader)
yaml.add_constructor('lsst.daf.base.PropertySet', ps_constructor, Loader=loader)
yaml.add_constructor('lsst.daf.base.DateTime', dt_constructor, Loader=loader)
22 changes: 13 additions & 9 deletions tests/test_yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ class YAMLTestCase(unittest.TestCase):

@unittest.skipIf(yaml is None, "yaml module not installed")
def setUp(self):
pass
# In pyyaml >= 5.1 we prefer to use FullLoader
try:
self.yamlLoader = yaml.FullLoader
except AttributeError:
self.yamlLoader = yaml.Loader

def testYamlPS(self):
ps = lsst.daf.base.PropertySet()
Expand All @@ -56,7 +60,7 @@ def testYamlPS(self):
ps.set("dt", lsst.daf.base.DateTime("20090402T072639.314159265Z", lsst.daf.base.DateTime.UTC))
ps.set("blank", "")

ps2 = yaml.load(yaml.dump(ps))
ps2 = yaml.load(yaml.dump(ps), Loader=self.yamlLoader)
self.assertIsInstance(ps2, lsst.daf.base.PropertySet)
self.assertEqual(ps, ps2)

Expand All @@ -73,7 +77,7 @@ def testYamlPL(self):
apl.set("int2", 2009)
apl.set("dt", lsst.daf.base.DateTime("20090402T072639.314159265Z", lsst.daf.base.DateTime.UTC))

apl2 = yaml.load(yaml.dump(apl))
apl2 = yaml.load(yaml.dump(apl), Loader=self.yamlLoader)
self.assertIsInstance(apl2, lsst.daf.base.PropertyList)
self.assertEqual(apl, apl2)

Expand All @@ -90,7 +94,7 @@ def testYamlNest(self):

ps.setPropertySet("ps", ps2)

ps3 = yaml.load(yaml.dump(ps))
ps3 = yaml.load(yaml.dump(ps), Loader=self.yamlLoader)
self.assertEqual(ps3, ps)
self.assertEqual(ps3.getPropertySet("ps"), ps.getPropertySet("ps"))

Expand All @@ -102,28 +106,28 @@ def testYamlNest(self):
apl.add("withcom", "string", "a comment")
apl.setPropertySet("ps", ps3)

apl2 = yaml.load(yaml.dump(apl))
apl2 = yaml.load(yaml.dump(apl), Loader=self.yamlLoader)
self.assertEqual(apl2, apl)

# Add the PropertyList to the PropertySet to ensure that the
# correct type is returned and no loss of comments
ps.setPropertySet("newpl", apl)
apl3 = yaml.load(yaml.dump(ps))
apl3 = yaml.load(yaml.dump(ps), Loader=self.yamlLoader)
self.assertEqual(apl3, ps)

def testYamlDateTime(self):
ts = lsst.daf.base.DateTime("2004-03-01T12:39:45.1Z", lsst.daf.base.DateTime.UTC)
ts2 = yaml.load(yaml.dump(ts))
ts2 = yaml.load(yaml.dump(ts), Loader=self.yamlLoader)
self.assertIsInstance(ts2, lsst.daf.base.DateTime)
self.assertEqual(ts, ts2)

def testLoader(self):
"""Test loading of reference YAML files"""
# Old and new serialization of a propertyList
with open(os.path.join(TESTDIR, "data", "fitsheader-tuple.yaml")) as fd:
old = yaml.load(fd)
old = yaml.load(fd, Loader=self.yamlLoader)
with open(os.path.join(TESTDIR, "data", "fitsheader.yaml")) as fd:
new = yaml.load(fd)
new = yaml.load(fd, Loader=self.yamlLoader)
self.assertIsInstance(new, lsst.daf.base.PropertyList)
self.assertIsInstance(old, lsst.daf.base.PropertyList)
self.assertEqual(old, new)
Expand Down

0 comments on commit aef33af

Please sign in to comment.