/usr/share/pyshared/dap/parsers/dds.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 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 | """DDS parser.
This module implements a DDS parser based on ``shlex.shlex``.
"""
__author__ = "Roberto De Almeida <rob@pydap.org>"
from shlex import shlex
from dap.dtypes import *
from dap import proxy
from dap.parsers import BaseParser
# string.digits + string.ascii_letters + string.punctuation - {};[]:="
WORDCHARS = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!#$%&\'()*+-./<>?@\\^_`|~'
atomic_types = ('byte', 'int', 'uint', 'int16', 'uint16', 'int32', 'uint32', 'float32', 'float64', 'string', 'url')
constructors = ('grid', 'sequence', 'structure')
class DDSParser(BaseParser):
"""A parser for Dataset Descriptor Structure.
First we create a dataset and get its DDS:
>>> dataset = DatasetType(name='test')
>>> dataset['a'] = BaseType(name='a', data=1)
>>> from dap.server import SimpleHandler
>>> headers, output = SimpleHandler(dataset).dds()
>>> dds = ''.join(output)
>>> print dds
Dataset {
Int32 a;
} test;
<BLANKLINE>
Now we try to parse it. We'll get a second dataset that should have the
same DDS:
>>> dataset2 = DDSParser(dds, '').parse()
>>> headers, output = SimpleHandler(dataset2).dds()
>>> print ''.join(output)
Dataset {
Int32 a;
} test;
<BLANKLINE>
QED.
"""
def __init__(self, dds, url, cache=None, username=None, password=None):
shlex.__init__(self, dds)
self.url = url[:-4] # strip .dds from url
# Info for the proxy.
self.cache = cache
self.username = username
self.password = password
# Add punctuation to words.
self.wordchars = WORDCHARS
def parse(self):
"""Parse the DDS and return a ``DatasetType`` object."""
return self._dataset()
def _dataset(self):
self._consume('Dataset')
self._consume('{')
dataset = DatasetType()
# Scan type declarations.
while self._check(*(atomic_types + constructors)):
var = self._declaration()
dataset[var.name] = var
self._consume('}')
# Scan the filename.
dataset.name = self.get_token()
self._consume(';')
# Fix all ids.
dataset._set_id()
# Add Proxy to all BaseType instances.
def walk(dapvar):
for var in dapvar.walk():
if isinstance(var, BaseType):
# Set the data.
var.data = proxy.Proxy(self.url,
var.id,
getattr(var, 'shape', ()),
var.type,
cache=self.cache,
username=self.username,
password=self.password)
else:
walk(var)
walk(dataset)
return dataset
def _declaration(self):
if self._check('grid'): return self._grid()
elif self._check('sequence'): return self._sequence()
elif self._check('structure'): return self._structure()
else: return self._base_declaration()
def _base_declaration(self):
type_ = self.get_token()
name = self.get_token()
# Get the dimensions, if any.
shape, dimensions = self._dimensions()
self._consume(';')
if shape:
var = ArrayType(name=name, shape=shape, dimensions=dimensions, type=type_)
else:
var = BaseType(name=name, type=type_)
return var
def _dimensions(self):
shape = []
names = []
while not self._check(';'):
self._consume('[')
_token = self.get_token()
if self._check('='):
names.append(_token)
self._consume('=')
shape.append(int(self.get_token()))
else:
shape.append(int(_token))
self._consume(']')
return tuple(shape), tuple(names)
def _sequence(self):
sequence = SequenceType()
self._consume('sequence')
self._consume('{')
while 1:
var = self._declaration()
sequence[var.name] = var
if self._check('}'): break
self._consume('}')
# Get the sequence name.
sequence.name = self.get_token()
self._consume(';')
return sequence
def _structure(self):
structure = StructureType()
self._consume('structure')
self._consume('{')
while 1:
var = self._declaration()
structure[var.name] = var
if self._check('}'): break
self._consume('}')
# Get structure name.
structure.name = self.get_token()
self._consume(';')
return structure
def _grid(self):
grid = GridType()
self._consume('grid')
self._consume('{')
# Scan the array.
self._consume('array')
self._consume(':')
var = self._base_declaration()
grid.array = var
# Scan the maps.
self._consume('maps')
self._consume(':')
while 1:
var = self._base_declaration()
grid.maps[var.name] = var
if self._check('}'): break
self._consume('}')
grid.name = self.get_token()
self._consume(';')
return grid
def _test():
import doctest
doctest.testmod()
if __name__ == "__main__":
_test()
|