/usr/lib/python3/dist-packages/pydap/responses/ascii.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 | """The ASCII response.
The ASCII response is an unnoficial response used to return the data as ASCII.
Pydap's implementation is reverse engineered from the official server.
"""
try:
from functools import singledispatch
except ImportError:
from singledispatch import singledispatch
import copy
import numpy as np
from six.moves import zip
from ..model import (BaseType,
SequenceType, StructureType)
from ..lib import encode, __version__
from .lib import BaseResponse
from .dds import dds
class ASCIIResponse(BaseResponse):
"""The ASCII response."""
__version__ = __version__
def __init__(self, dataset):
BaseResponse.__init__(self, dataset)
self.headers.extend([
('Content-description', 'dods_ascii'),
('Content-type', 'text/plain; charset=ascii'),
])
def __iter__(self):
for line in dds(self.dataset):
yield line.encode('ascii')
yield (45 * '-' + '\n').encode('ascii')
for line in ascii(self.dataset):
yield line.encode('ascii')
@singledispatch
def ascii(var, printname=True):
"""A single dispatcher for the ASCII response."""
raise StopIteration
@ascii.register(SequenceType)
def _sequenctype(var, printname=True):
yield ', '.join([child.id for child in var.children()])
yield '\n'
for rec in var.iterdata():
out = copy.copy(var)
out.__class__ = StructureType
out.data = rec
for i, line in enumerate(ascii(out, printname=False)):
line = line.strip()
if line and i > 0:
yield ', '
yield line
yield '\n'
@ascii.register(StructureType)
def _structuretype(var, printname=True):
for child in var.children():
for line in ascii(child, printname):
yield line
yield '\n'
@ascii.register(BaseType)
def _basetype(var, printname=True):
if printname:
yield var.id
yield '\n'
if not getattr(var, "shape", ()):
yield encode(var.data)
else:
for indexes, value in zip(np.ndindex(var.shape), var.data.flat):
yield "{indexes} {value}\n".format(
indexes="[" + "][".join([str(idx) for idx in indexes]) + "]",
value=encode(value))
|