lsst.daf.persistence  16.0-3-g3806c63+7
baseYaml.py
Go to the documentation of this file.
1 # This file is part of daf_persistence
2 #
3 # Developed for the LSST Data Management System.
4 # This product includes software developed by the LSST Project
5 # (http://www.lsst.org/).
6 # See the COPYRIGHT file at the top-level directory of this distribution
7 # for details of code ownership.
8 #
9 # This program is free software: you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation, either version 3 of the License, or
12 # (at your option) any later version.
13 #
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License
20 # along with this program. If not, see <http://www.gnu.org/licenses/>.
21 
22 """This module defines YAML I/O for key lsst.daf.base classes."""
23 
24 import yaml
25 
26 import lsst.daf.base
27 
28 
29 # YAML representers for key lsst.daf.base classes
30 
31 
32 def dt_representer(dumper, data):
33  """Represent an lsst.daf.base.DateTime (as ISO8601-formatted string in TAI)
34  """
35  return dumper.represent_scalar(u'lsst.daf.base.DateTime',
36  data.toString(lsst.daf.base.DateTime.TAI))
37 
38 
39 yaml.add_representer(lsst.daf.base.DateTime, dt_representer)
40 
41 
42 def pl_representer(dumper, data):
43  """Represent an lsst.daf.base.PropertyList as an ordered sequence of
44  name/type/value/comment tuples)"""
45  result = lsst.daf.base.getPropertyListState(data)
46  return dumper.represent_sequence(u'lsst.daf.base.PropertyList', result,
47  flow_style=None)
48 
49 
50 yaml.add_representer(lsst.daf.base.PropertyList, pl_representer)
51 
52 
53 def ps_representer(dumper, data):
54  """Represent an lsst.daf.base.PropertySet as a mapping from names to
55  type/value pairs."""
56  result = lsst.daf.base.getPropertySetState(data)
57  return dumper.represent_sequence(u'lsst.daf.base.PropertySet', result,
58  flow_style=None)
59 
60 
61 yaml.add_representer(lsst.daf.base.PropertySet, ps_representer)
62 
63 
64 
65 # YAML constructors for key lsst.daf.base classes
66 
67 
68 def dt_constructor(loader, node):
69  """Construct an lsst.daf.base.DateTime from an ISO8601-formatted string in
70  TAI"""
71  dt = loader.construct_scalar(node)
72  return lsst.daf.base.DateTime(str(dt), lsst.daf.base.DateTime.TAI)
73 
74 
75 yaml.add_constructor(u'lsst.daf.base.DateTime', dt_constructor)
76 
77 
78 def pl_constructor(loader, node):
79  """Construct an lsst.daf.base.PropertyList from a pickle-state."""
81  yield pl
82  state = loader.construct_sequence(node, deep=True)
83  lsst.daf.base.setPropertyListState(pl, state)
84 
85 
86 yaml.add_constructor(u'lsst.daf.base.PropertyList', pl_constructor)
87 
88 
89 def ps_constructor(loader, node):
90  """Construct an lsst.daf.base.PropertyList from a pickle-state."""
92  yield ps
93  state = loader.construct_sequence(node, deep=True)
94  lsst.daf.base.setPropertySetState(ps, state)
95 
96 
97 yaml.add_constructor(u'lsst.daf.base.PropertySet', ps_constructor)
def dt_representer(dumper, data)
Definition: baseYaml.py:32
def dt_constructor(loader, node)
Definition: baseYaml.py:68
def pl_constructor(loader, node)
Definition: baseYaml.py:78
def pl_representer(dumper, data)
Definition: baseYaml.py:42
def ps_constructor(loader, node)
Definition: baseYaml.py:89
def ps_representer(dumper, data)
Definition: baseYaml.py:53