/usr/lib/python3/dist-packages/pydap/responses/das.py is in python3-pydap 3.2.2+ds1-1ubuntu1.
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 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 | """The DAS response.
The DAS response describes the attributes associated with a dataset and its
variables. Together with the DDS the DAS response completely describes the
metadata of a dataset, allowing it to be introspected and data to be
downloaded.
"""
try:
from functools import singledispatch
except ImportError:
from singledispatch import singledispatch
from collections import Iterable
from six import string_types, integer_types
from six.moves import map
from ..model import (DatasetType, BaseType,
StructureType, SequenceType,
GridType)
from ..lib import encode, quote, __version__, NUMPY_TO_DAP2_TYPEMAP
from .lib import BaseResponse
INDENT = ' ' * 4
class DASResponse(BaseResponse):
"""The DAS response."""
__version__ = __version__
def __init__(self, dataset):
BaseResponse.__init__(self, dataset)
self.headers.extend([
('Content-description', 'dods_das'),
('Content-type', 'text/plain; charset=ascii'),
])
def __iter__(self):
for line in das(self.dataset):
yield line.encode('ascii')
@singledispatch
def das(var, level=0):
"""Single dispatcher that generates the DAS response."""
raise StopIteration
@das.register(DatasetType)
def _datasettype(var, level=0):
yield '{indent}Attributes {{\n'.format(indent=level*INDENT)
for attr in sorted(var.attributes.keys()):
values = var.attributes[attr]
for line in build_attributes(attr, values, level+1):
yield line
for child in var.children():
for line in das(child, level=level+1):
yield line
yield '{indent}}}\n'.format(indent=level*INDENT)
@das.register(StructureType)
@das.register(SequenceType)
def _structuretype(var, level=0):
yield '{indent}{name} {{\n'.format(indent=level*INDENT, name=var.name)
for attr in sorted(var.attributes.keys()):
values = var.attributes[attr]
for line in build_attributes(attr, values, level+1):
yield line
for child in var.children():
for line in das(child, level=level+1):
yield line
yield '{indent}}}\n'.format(indent=level*INDENT)
@das.register(BaseType)
@das.register(GridType)
def _basetypegridtype(var, level=0):
yield '{indent}{name} {{\n'.format(indent=level*INDENT, name=var.name)
for attr in sorted(var.attributes.keys()):
values = var.attributes[attr]
for line in build_attributes(attr, values, level+1):
yield line
yield '{indent}}}\n'.format(indent=level*INDENT)
def build_attributes(attr, values, level=0):
"""Recursive function to build the DAS."""
# check for metadata
if isinstance(values, dict):
yield '{indent}{attr} {{\n'.format(indent=(level)*INDENT, attr=attr)
for k, v in values.items():
for line in build_attributes(k, v, level+1):
yield line
yield '{indent}}}\n'.format(indent=(level)*INDENT)
else:
# get type
type = get_type(values)
# encode values
if (isinstance(values, string_types) or
not isinstance(values, Iterable) or
getattr(values, 'shape', None) == ()):
values = [encode(values)]
else:
values = map(encode, values)
yield '{indent}{type} {attr} {values};\n'.format(
indent=(level)*INDENT,
type=type,
attr=quote(attr),
values=', '.join(values))
def get_type(values):
"""Extract the type of a variable.
This function tries to determine the DAP type of a Python variable using
several methods. Returns the DAP type as a string.
"""
if hasattr(values, 'dtype'):
return NUMPY_TO_DAP2_TYPEMAP[values.dtype.char]
elif isinstance(values, string_types) or not isinstance(values, Iterable):
return type_convert(values)
else:
# if there are several values, they may have different types, so we
# need to convert all of them and use a precedence table
types = [type_convert(val) for val in values]
precedence = ['String', 'Float64', 'Int32']
types.sort(key=precedence.index)
return types[0]
def type_convert(obj):
"""Map Python objects to the corresponding Opendap types.
Returns the DAP representation of the type as a string.
"""
if isinstance(obj, float):
return 'Float64'
elif isinstance(obj, integer_types):
return 'Int32'
else:
return 'String'
|