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
14
15
16
17
18
20 if node["type"]=="Convex":
21 return "UNITSPHER"
22 else:
23 return "SPHER2"
24
25
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
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
47 if node["redshiftType"]=="VELOCITY":
48 return "km/s"
49 else:
50 return "nil"
51
52
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 correponding key is not yet
61 present.
62 """
63 for key, value in defaults:
64 if key not in node:
65 if not isinstance(value, (basestring, list)):
66 value = value(node)
67 if value is None:
68 continue
69 node[key] = value
70
71
87
88
90 """returns a defaulting function filling in what is defined in
91 defaults.
92 """
93 def func(node):
94 return _addDefaultsToNode(node, defaults)
95 return func
96
97
99 """returns a function removing values in nodes that have their default
100 values.
101 """
102 def func(node):
103 return _removeDefaultsFromNode(node, dict(defaults))
104 return func
105
106
107 defaults = {
108 "space": [
109 ("flavor", getSpaceFlavor),
110 ("equinox", getEquinox),
111 ("unit", getSpaceUnit)],
112 "time": [
113
114
115 ("unit", "s")],
116 "spectral": [
117 ("unit", "Hz")],
118 "redshift": [
119 ("redshiftType", "REDSHIFT"),
120 ("unit", getRedshiftUnit),
121 ("dopplerdef", "OPTICAL")],
122 "velocity": [
123 ("unit", "m/s"),],
124 }
125
126 defaultingFunctions = dict(
127 (k, _makeDefaulter(v)) for k, v in defaults.iteritems())
128
129 undefaultingFunctions = dict(
130 (k, _makeUndefaulter(v)) for k, v in defaults.iteritems())
131