lsst.pex.config  18.1.0-2-g919ecaf
convert.py
Go to the documentation of this file.
1 # This file is part of pex_config.
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 __all__ = ('makePropertySet', 'makePolicy')
23 
24 try:
25  import lsst.pex.policy as pexPolicy
26 except ImportError:
27  pexPolicy = None
28 
29 try:
30  import lsst.daf.base as dafBase
31 except ImportError:
32  dafBase = None
33 
34 
35 def makePropertySet(config):
36  """Convert a configuration into a `lsst.daf.base.PropertySet`.
37 
38  Parameters
39  ----------
40  config : `lsst.pex.config.Config`
41  Configuration instance.
42 
43  Returns
44  -------
45  propertySet : `lsst.daf.base.PropertySet`
46  A `~lsst.daf.base.PropertySet` that is equivalent to the ``config``
47  instance. If ``config`` is `None` then this return value is also
48  `None`.
49 
50  See also
51  --------
52  makePolicy
53  lsst.daf.base.PropertySet
54  """
55  if dafBase is None:
56  raise RuntimeError("lsst.daf.base is not available")
57 
58  def _helper(ps, prefix, dict_):
59  for k, v in dict_.items():
60  name = prefix + "." + k if prefix is not None else k
61  if isinstance(v, dict):
62  _helper(ps, name, v)
63  elif v is not None:
64  ps.set(name, v)
65 
66  if config is not None:
67  ps = dafBase.PropertySet()
68  _helper(ps, None, config.toDict())
69  return ps
70  else:
71  return None
72 
73 
74 def makePolicy(config):
75  """Convert a configuration into a `lsst.pex.policy.Policy`.
76 
77  Parameters
78  ----------
79  config : `lsst.pex.config.Config`
80  Configuration instance.
81 
82  Returns
83  -------
84  policy : `lsst.pex.policy.Policy`
85  A `~lsst.pex.policy.Policy` that is equivalent to the ``config``
86  instance. If ``config`` is `None` then return value is also `None`.
87 
88  See also
89  --------
90  makePropertySet
91  lsst.pex.policy.Policy
92  """
93  if pexPolicy is None:
94  raise RuntimeError("lsst.pex.policy is not available")
95 
96  def _helper(dict_):
97  p = pexPolicy.Policy()
98  for k, v in dict_.items():
99  if isinstance(v, dict):
100  p.set(k, _helper(v))
101  elif isinstance(v, list):
102  for vi in v:
103  p.add(k, vi)
104  elif v is not None:
105  p.set(k, v)
106  return p
107  if config:
108  return _helper(config.toDict())
109  else:
110  return None
def makePropertySet(config)
Definition: convert.py:35
def makePolicy(config)
Definition: convert.py:74