| 1 |
""" |
| 2 |
Functions for adding defaults to STC-S concrete syntax trees. |
| 3 |
|
| 4 |
Default addition is governed by the two dicts at the bottom of |
| 5 |
the module: |
| 6 |
|
| 7 |
- pathFunctions -- maps path tuples to handling functions. If there |
| 8 |
is a match here, no name-based defaulting is done |
| 9 |
- nodeNameFunctions -- maps the last element of a path tuple to |
| 10 |
handling functions. |
| 11 |
""" |
| 12 |
|
| 13 |
#c Copyright 2008-2025, the GAVO project <[email protected]> |
| 14 |
#c |
| 15 |
#c This program is free software, covered by the GNU GPL. See the |
| 16 |
#c COPYING file in the source distribution. |
| 17 |
|
| 18 |
|
| 19 |
def getSpaceFlavor(node): |
| 20 |
if node["type"]=="Convex": |
| 21 |
return "UNITSPHER" |
| 22 |
else: |
| 23 |
return "SPHER2" |
| 24 |
|
| 25 |
|
| 26 |
def getSpaceUnit(node): |
| 27 |
if node["frame"] and node["frame"].startswith("GEO"): |
| 28 |
return "deg deg m" |
| 29 |
elif node["flavor"].startswith("CART"): |
| 30 |
return "m" |
| 31 |
elif node["flavor"]=="UNITSPHER": |
| 32 |
return "" |
| 33 |
else: |
| 34 |
return "deg" |
| 35 |
|
| 36 |
|
| 37 |
def getEquinox(node): |
| 38 |
if node["frame"]=="FK4": |
| 39 |
return "B1950.0" |
| 40 |
elif node["frame"]=="FK5": |
| 41 |
return "J2000.0" |
| 42 |
else: |
| 43 |
return None |
| 44 |
|
| 45 |
|
| 46 |
def getRedshiftUnit(node): |
| 47 |
if node["redshiftType"]=="VELOCITY": |
| 48 |
return ["km/s"] |
| 49 |
else: |
| 50 |
return ["nil"] |
| 51 |
|
| 52 |
|
| 53 |
def _addDefaultsToNode(node, defaults): |
| 54 |
"""adds defaults to node. |
| 55 |
|
| 56 |
defaults is a sequence of (key, default) pairs, where default is either |
| 57 |
a string (which gets added in a list node), a list (which gets added |
| 58 |
directly) or a function(node) -> string or list to obtain the default. |
| 59 |
|
| 60 |
Values are only added to a node if the corresponding key is not yet |
| 61 |
present. |
| 62 |
""" |
| 63 |
for key, value in defaults: |
| 64 |
if key not in node: |
| 65 |
if not isinstance(value, (str, list)): |
| 66 |
value = value(node) |
| 67 |
if value is None: |
| 68 |
continue |
| 69 |
node[key] = value |
| 70 |
|
| 71 |
|
| 72 |
def _removeDefaultsFromNode(node, defaults): |
| 73 |
"""removes defaults from node. |
| 74 |
|
| 75 |
See _addDefaultsToNode for details. |
| 76 |
""" |
| 77 |
defaultedKeys = [] |
| 78 |
for key, value in node.items(): |
| 79 |
if key in defaults: |
| 80 |
default = defaults[key] |
| 81 |
if not isinstance(default, (str, list)): |
| 82 |
default = default(node) |
| 83 |
if value==default: |
| 84 |
defaultedKeys.append(key) |
| 85 |
|
| 86 |
# redshift unit defaults are in lists because the grammar wants |
| 87 |
# it this way; remove them, too: |
| 88 |
if isinstance(default, list) and value==default[0]: |
| 89 |
defaultedKeys.append(key) |
| 90 |
|
| 91 |
for key in defaultedKeys: |
| 92 |
del node[key] |
| 93 |
|
| 94 |
|
| 95 |
def _makeDefaulter(defaults): |
| 96 |
"""returns a defaulting function filling in what is defined in |
| 97 |
defaults. |
| 98 |
""" |
| 99 |
def func(node): |
| 100 |
return _addDefaultsToNode(node, defaults) |
| 101 |
return func |
| 102 |
|
| 103 |
|
| 104 |
def _makeUndefaulter(defaults): |
| 105 |
"""returns a function removing values in nodes that have their default |
| 106 |
values. |
| 107 |
""" |
| 108 |
def func(node): |
| 109 |
return _removeDefaultsFromNode(node, dict(defaults)) |
| 110 |
return func |
| 111 |
|
| 112 |
|
| 113 |
defaults = { |
| 114 |
"space": [ |
| 115 |
("flavor", getSpaceFlavor), |
| 116 |
("equinox", getEquinox), |
| 117 |
("unit", getSpaceUnit)], |
| 118 |
"time": [ |
| 119 |
# For time, the default is the unit of errors and such. |
| 120 |
# The values are either in days or they're MJD. |
| 121 |
("unit", "s")], |
| 122 |
"spectral": [ |
| 123 |
("unit", "Hz")], |
| 124 |
"redshift": [ |
| 125 |
("redshiftType", "REDSHIFT"), |
| 126 |
("unit", getRedshiftUnit), |
| 127 |
("dopplerdef", "OPTICAL")], |
| 128 |
"velocity": [ |
| 129 |
("unit", "m/s"),], |
| 130 |
} |
| 131 |
|
| 132 |
defaultingFunctions = dict( |
| 133 |
(k, _makeDefaulter(v)) for k, v in defaults.items()) |
| 134 |
|
| 135 |
undefaultingFunctions = dict( |
| 136 |
(k, _makeUndefaulter(v)) for k, v in defaults.items()) |