Package gavo :: Package stc :: Module stcsdefaults
[frames] | no frames]

Source Code for Module gavo.stc.stcsdefaults

  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-2019, the GAVO project 
 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 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
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.iteritems(): 79 if key in defaults: 80 default = defaults[key] 81 if not isinstance(default, (basestring, list)): 82 default = default(node) 83 if value==default: 84 defaultedKeys.append(key) 85 for key in defaultedKeys: 86 del node[key]
87 88
89 -def _makeDefaulter(defaults):
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
98 -def _makeUndefaulter(defaults):
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 # For time, the default is the unit of errors and such. 114 # The values are either in days or they're MJD. 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