-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #94 from lsst/tickets/DM-14503
DM-14503: Implement YAML I/O for PropertySet/List.
- Loading branch information
Showing
3 changed files
with
291 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
# This file is part of daf_persistence | ||
# | ||
# Developed for the LSST Data Management System. | ||
# This product includes software developed by the LSST Project | ||
# (http://www.lsst.org/). | ||
# See the COPYRIGHT file at the top-level directory of this distribution | ||
# for details of code ownership. | ||
# | ||
# This program is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
"""This module defines YAML I/O for key lsst.daf.base classes.""" | ||
|
||
import yaml | ||
|
||
import lsst.daf.base | ||
|
||
|
||
# YAML representers for key lsst.daf.base classes | ||
|
||
|
||
def dt_representer(dumper, data): | ||
"""Represent an lsst.daf.base.DateTime (as ISO8601-formatted string in TAI) | ||
""" | ||
return dumper.represent_scalar(u'lsst.daf.base.DateTime', | ||
data.toString(lsst.daf.base.DateTime.TAI)) | ||
|
||
|
||
yaml.add_representer(lsst.daf.base.DateTime, dt_representer) | ||
|
||
|
||
def pl_representer(dumper, data): | ||
"""Represent an lsst.daf.base.PropertyList as an ordered sequence of | ||
name/type/value/comment tuples)""" | ||
result = lsst.daf.base.getstate(data) | ||
return dumper.represent_sequence(u'lsst.daf.base.PropertyList', result, | ||
flow_style=None) | ||
|
||
|
||
yaml.add_representer(lsst.daf.base.PropertyList, pl_representer) | ||
|
||
|
||
def ps_representer(dumper, data): | ||
"""Represent an lsst.daf.base.PropertySet as a mapping from names to | ||
type/value pairs.""" | ||
result = lsst.daf.base.getstate(data) | ||
return dumper.represent_sequence(u'lsst.daf.base.PropertySet', result, | ||
flow_style=None) | ||
|
||
|
||
yaml.add_representer(lsst.daf.base.PropertySet, ps_representer) | ||
|
||
############################################################################### | ||
|
||
# YAML constructors for key lsst.daf.base classes | ||
|
||
|
||
def dt_constructor(loader, node): | ||
"""Construct an lsst.daf.base.DateTime from an ISO8601-formatted string in | ||
TAI""" | ||
dt = loader.construct_scalar(node) | ||
return lsst.daf.base.DateTime(str(dt), lsst.daf.base.DateTime.TAI) | ||
|
||
|
||
yaml.add_constructor(u'lsst.daf.base.DateTime', dt_constructor) | ||
|
||
|
||
def pl_constructor(loader, node): | ||
"""Construct an lsst.daf.base.PropertyList from a pickle-state.""" | ||
pl = lsst.daf.base.PropertyList() | ||
yield pl | ||
state = loader.construct_sequence(node, deep=True) | ||
lsst.daf.base.setstate(pl, state) | ||
|
||
|
||
yaml.add_constructor(u'lsst.daf.base.PropertyList', pl_constructor) | ||
|
||
|
||
def ps_constructor(loader, node): | ||
"""Construct an lsst.daf.base.PropertyList from a pickle-state.""" | ||
ps = lsst.daf.base.PropertySet() | ||
yield ps | ||
state = loader.construct_sequence(node, deep=True) | ||
lsst.daf.base.setstate(ps, state) | ||
|
||
|
||
yaml.add_constructor(u'lsst.daf.base.PropertySet', ps_constructor) |
Oops, something went wrong.