/usr/share/pyshared/dap/responses/das.py is in python-dap 2.2.6.7-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 | """DAS DAP response.
This module implements the DAS DAP response, building it
dynamically from datasets objects.
"""
__author__ = "Roberto De Almeida <rob@pydap.org>"
from dap.lib import INDENT, __dap__, encode_atom, to_list
from dap.dtypes import *
from dap.dtypes import _basetypes
def build(self, constraints=None):
dataset = self._parseconstraints(None)
headers = [('Content-description', 'dods_das'),
('XDODS-Server', 'dods/%s' % '.'.join([str(i) for i in __dap__])),
('Content-type', 'text/plain'),
]
output = _dispatch(dataset)
return headers, output
# Type conversion between Python and DODS, for the DAS.
typeconvert = {basestring: 'String',
unicode : 'String',
str : 'String',
float : 'Float64',
long : 'Int32',
int : 'Int32',
}
def _recursive_build(attr, values, level=0):
"""
Recursive function to build the DAS.
This function checks for attribute nodes that do not belong to any
variable, and append them as metadata.
"""
# Check for metadata.
if isinstance(values, dict):
yield '%s%s {\n' % ((level+1) * INDENT, attr)
for k,v in values.items():
for line in _recursive_build(k, v, level+1):
yield line
yield '%s}\n' % ((level+1) * INDENT)
else:
# Convert values to list.
values = to_list(values) # this takes care of numpy arrays
if not isinstance(values, list): values = [values]
# Get value type and encode properly.
attrtype = typeconvert[type(values[0])]
values = [encode_atom(v) for v in values]
values = ', '.join(values)
yield '%s%s %s %s;\n' % ((level+1) * INDENT, attrtype, attr.replace(' ', '_'), values)
def _dispatch(dapvar, level=0):
func = {DatasetType : _dataset,
StructureType: _structure,
SequenceType : _sequence,
GridType : _grid,
ArrayType : _array,
BaseType : _base,
}[type(dapvar)]
return func(dapvar, level)
def _dataset(dapvar, level=0):
yield '%sAttributes {\n' % (level * INDENT)
# Global attributes and metadata.
for attr,values in dapvar.attributes.items():
for line in _recursive_build(attr, values, level):
yield line
# Get the DAS from stored variables.
for var in dapvar.walk():
for line in _dispatch(var, level=level+1):
yield line
yield '%s}\n' % (level * INDENT)
def _structure(dapvar, level=0):
yield '%s%s {\n' % (level * INDENT, dapvar.name)
# Global attributes and metadata.
for attr,values in dapvar.attributes.items():
for line in _recursive_build(attr, values, level):
yield line
# Get the DAS from stored variables.
for var in dapvar.walk():
for line in _dispatch(var, level=level+1):
yield line
yield '%s}\n' % (level * INDENT)
_sequence = _structure
#_grid = _structure
def _grid(dapvar, level=0):
yield '%s%s {\n' % (level * INDENT, dapvar.name)
# Global attributes and metadata.
for attr,values in dapvar.attributes.items():
for line in _recursive_build(attr, values, level):
yield line
yield '%s}\n' % (level * INDENT)
def _array(dapvar, level=0):
yield '%s%s {\n' % (level * INDENT, dapvar.name)
# Global attributes and metadata.
for attr,values in dapvar.attributes.items():
for line in _recursive_build(attr, values, level):
yield line
yield '%s}\n' % (level * INDENT)
_base = _array
def _test():
import doctest
doctest.testmod()
if __name__ == "__main__":
_test()
|