| 1 |
import os |
| 2 |
import tempfile |
| 3 |
from copy import copy, deepcopy |
| 4 |
|
| 5 |
from hdmf.build import BuildManager, ObjectMapper, TypeMap |
| 6 |
from hdmf.common.table import DynamicTable |
| 7 |
from hdmf.container import Container, HERDManager, Data, MultiContainerInterface |
| 8 |
from hdmf.spec import ( |
| 9 |
AttributeSpec, |
| 10 |
DatasetSpec, |
| 11 |
DtypeSpec, |
| 12 |
GroupSpec, |
| 13 |
LinkSpec, |
| 14 |
NamespaceBuilder, |
| 15 |
NamespaceCatalog, |
| 16 |
RefSpec, |
| 17 |
SpecCatalog, |
| 18 |
SpecNamespace, |
| 19 |
) |
| 20 |
from hdmf.spec.spec import ONE_OR_MANY, ZERO_OR_MANY, ZERO_OR_ONE |
| 21 |
from hdmf.utils import docval, get_docval, getargs |
| 22 |
|
| 23 |
CORE_NAMESPACE = "test_core" |
| 24 |
|
| 25 |
|
| 26 |
class CacheSpecTestHelper(object): |
| 27 |
@staticmethod |
| 28 |
def get_types(catalog): |
| 29 |
types = set() |
| 30 |
for ns_name in catalog.namespaces: |
| 31 |
ns = catalog.get_namespace(ns_name) |
| 32 |
for source in ns["schema"]: |
| 33 |
types.update(catalog.get_types(source["source"])) |
| 34 |
return types |
| 35 |
|
| 36 |
|
| 37 |
def get_temp_filepath(): |
| 38 |
# On Windows, h5py cannot truncate an open file in write mode. |
| 39 |
# The temp file will be closed before h5py truncates it and will be removed during the tearDown step. |
| 40 |
temp_file = tempfile.NamedTemporaryFile() |
| 41 |
temp_file.close() |
| 42 |
return temp_file.name |
| 43 |
|
| 44 |
|
| 45 |
############################################ |
| 46 |
# Foo example data containers and specs |
| 47 |
########################################### |
| 48 |
class Foo(Container): |
| 49 |
@docval( |
| 50 |
{"name": "name", "type": str, "doc": "the name of this Foo"}, |
| 51 |
{"name": "my_data", "type": ("array_data", "data"), "doc": "some data"}, |
| 52 |
{"name": "attr1", "type": str, "doc": "an attribute"}, |
| 53 |
{"name": "attr2", "type": int, "doc": "another attribute"}, |
| 54 |
{"name": "attr3", "type": float, "doc": "a third attribute", "default": 3.14}, |
| 55 |
) |
| 56 |
def __init__(self, **kwargs): |
| 57 |
name, my_data, attr1, attr2, attr3 = getargs("name", "my_data", "attr1", "attr2", "attr3", kwargs) |
| 58 |
super().__init__(name=name) |
| 59 |
self.__data = my_data |
| 60 |
self.__attr1 = attr1 |
| 61 |
self.__attr2 = attr2 |
| 62 |
self.__attr3 = attr3 |
| 63 |
|
| 64 |
def __eq__(self, other): |
| 65 |
attrs = ("name", "my_data", "attr1", "attr2", "attr3") |
| 66 |
return all(getattr(self, a) == getattr(other, a) for a in attrs) |
| 67 |
|
| 68 |
def __str__(self): |
| 69 |
attrs = ("name", "my_data", "attr1", "attr2", "attr3") |
| 70 |
return "<" + ",".join("%s=%s" % (a, getattr(self, a)) for a in attrs) + ">" |
| 71 |
|
| 72 |
@property |
| 73 |
def my_data(self): |
| 74 |
return self.__data |
| 75 |
|
| 76 |
@property |
| 77 |
def attr1(self): |
| 78 |
return self.__attr1 |
| 79 |
|
| 80 |
@property |
| 81 |
def attr2(self): |
| 82 |
return self.__attr2 |
| 83 |
|
| 84 |
@property |
| 85 |
def attr3(self): |
| 86 |
return self.__attr3 |
| 87 |
|
| 88 |
def __hash__(self): |
| 89 |
return hash(self.name) |
| 90 |
|
| 91 |
|
| 92 |
class FooBucket(Container): |
| 93 |
@docval( |
| 94 |
{"name": "name", "type": str, "doc": "the name of this bucket"}, |
| 95 |
{"name": "foos", "type": list, "doc": "the Foo objects in this bucket", "default": list()}, |
| 96 |
) |
| 97 |
def __init__(self, **kwargs): |
| 98 |
name, foos = getargs("name", "foos", kwargs) |
| 99 |
super().__init__(name=name) |
| 100 |
self.__foos = {f.name: f for f in foos} # note: collections of groups are unordered in HDF5 |
| 101 |
for f in foos: |
| 102 |
f.parent = self |
| 103 |
|
| 104 |
def __eq__(self, other): |
| 105 |
return self.name == other.name and self.foos == other.foos |
| 106 |
|
| 107 |
def __str__(self): |
| 108 |
return "name=%s, foos=%s" % (self.name, self.foos) |
| 109 |
|
| 110 |
@property |
| 111 |
def foos(self): |
| 112 |
return self.__foos |
| 113 |
|
| 114 |
def remove_foo(self, foo_name): |
| 115 |
foo = self.__foos.pop(foo_name) |
| 116 |
if foo.parent is self: |
| 117 |
self._remove_child(foo) |
| 118 |
return foo |
| 119 |
|
| 120 |
|
| 121 |
class FooFile(Container, HERDManager): |
| 122 |
""" |
| 123 |
NOTE: if the ROOT_NAME for the backend is not 'root' then we must set FooFile.ROOT_NAME before use |
| 124 |
and should be reset to 'root' when use is finished to avoid potential cross-talk between tests. |
| 125 |
""" |
| 126 |
|
| 127 |
ROOT_NAME = "root" # For HDF5 and Zarr this is the root. It should be set before use if different for the backend. |
| 128 |
|
| 129 |
@docval( |
| 130 |
{"name": "buckets", "type": list, "doc": "the FooBuckets in this file", "default": list()}, |
| 131 |
{"name": "foo_link", "type": Foo, "doc": "an optional linked Foo", "default": None}, |
| 132 |
{"name": "foofile_data", "type": "array_data", "doc": "an optional dataset", "default": None}, |
| 133 |
{"name": "foo_ref_attr", "type": Foo, "doc": "a reference Foo", "default": None}, |
| 134 |
) |
| 135 |
def __init__(self, **kwargs): |
| 136 |
buckets, foo_link = getargs("buckets", "foo_link", kwargs) |
| 137 |
foofile_data, foo_ref_attr = getargs("foofile_data", "foo_ref_attr", kwargs) |
| 138 |
super().__init__(name=self.ROOT_NAME) # name is not used - FooFile should be the root container |
| 139 |
self.__buckets = {b.name: b for b in buckets} # note: collections of groups are unordered in HDF5 |
| 140 |
for f in buckets: |
| 141 |
f.parent = self |
| 142 |
self.__foo_link = foo_link |
| 143 |
self.__foofile_data = foofile_data |
| 144 |
self.__foo_ref_attr = foo_ref_attr |
| 145 |
|
| 146 |
def __eq__(self, other): |
| 147 |
return ( |
| 148 |
self.buckets == other.buckets |
| 149 |
and self.foo_link == other.foo_link |
| 150 |
and self.foofile_data == other.foofile_data |
| 151 |
) |
| 152 |
|
| 153 |
def __str__(self): |
| 154 |
return "buckets=%s, foo_link=%s, foofile_data=%s" % (self.buckets, self.foo_link, self.foofile_data) |
| 155 |
|
| 156 |
@property |
| 157 |
def buckets(self): |
| 158 |
return self.__buckets |
| 159 |
|
| 160 |
def add_bucket(self, bucket): |
| 161 |
self.__buckets[bucket.name] = bucket |
| 162 |
bucket.parent = self |
| 163 |
|
| 164 |
def remove_bucket(self, bucket_name): |
| 165 |
bucket = self.__buckets.pop(bucket_name) |
| 166 |
if bucket.parent is self: |
| 167 |
self._remove_child(bucket) |
| 168 |
return bucket |
| 169 |
|
| 170 |
@property |
| 171 |
def foo_link(self): |
| 172 |
return self.__foo_link |
| 173 |
|
| 174 |
@foo_link.setter |
| 175 |
def foo_link(self, value): |
| 176 |
if self.__foo_link is None: |
| 177 |
self.__foo_link = value |
| 178 |
self.set_modified(True) |
| 179 |
else: |
| 180 |
raise ValueError("can't reset foo_link attribute") |
| 181 |
|
| 182 |
@property |
| 183 |
def foofile_data(self): |
| 184 |
return self.__foofile_data |
| 185 |
|
| 186 |
@foofile_data.setter |
| 187 |
def foofile_data(self, value): |
| 188 |
if self.__foofile_data is None: |
| 189 |
self.__foofile_data = value |
| 190 |
self.set_modified(True) |
| 191 |
else: |
| 192 |
raise ValueError("can't reset foofile_data attribute") |
| 193 |
|
| 194 |
@property |
| 195 |
def foo_ref_attr(self): |
| 196 |
return self.__foo_ref_attr |
| 197 |
|
| 198 |
@foo_ref_attr.setter |
| 199 |
def foo_ref_attr(self, value): |
| 200 |
if self.__foo_ref_attr is None: |
| 201 |
self.__foo_ref_attr = value |
| 202 |
self.set_modified(True) |
| 203 |
else: |
| 204 |
raise ValueError("can't reset foo_ref_attr attribute") |
| 205 |
|
| 206 |
|
| 207 |
def get_foo_buildmanager(my_data_dtype="int"): |
| 208 |
""" |
| 209 |
Get a BuildManager (and create all ObjectMappers) for a foofile |
| 210 |
:return: |
| 211 |
""" |
| 212 |
|
| 213 |
foo_spec = GroupSpec( |
| 214 |
"A test group specification with a data type", |
| 215 |
data_type_def="Foo", |
| 216 |
datasets=[ |
| 217 |
DatasetSpec( |
| 218 |
"an example dataset", |
| 219 |
my_data_dtype, |
| 220 |
name="my_data", |
| 221 |
shape=[None], |
| 222 |
attributes=[AttributeSpec("attr2", "an example integer attribute", "int")], |
| 223 |
) |
| 224 |
], |
| 225 |
attributes=[ |
| 226 |
AttributeSpec("attr1", "an example string attribute", "text"), |
| 227 |
AttributeSpec("attr3", "an example float attribute", "float"), |
| 228 |
], |
| 229 |
) |
| 230 |
|
| 231 |
tmp_spec = GroupSpec( |
| 232 |
"A subgroup for Foos", |
| 233 |
name="foo_holder", |
| 234 |
groups=[ |
| 235 |
GroupSpec( |
| 236 |
"the Foos in this bucket", |
| 237 |
data_type_inc="Foo", |
| 238 |
quantity=ZERO_OR_MANY, |
| 239 |
) |
| 240 |
], |
| 241 |
) |
| 242 |
|
| 243 |
bucket_spec = GroupSpec( |
| 244 |
"A test group specification for a data type containing data type", |
| 245 |
data_type_def="FooBucket", |
| 246 |
groups=[tmp_spec], |
| 247 |
) |
| 248 |
|
| 249 |
class FooMapper(ObjectMapper): |
| 250 |
def __init__(self, spec): |
| 251 |
super().__init__(spec) |
| 252 |
my_data_spec = spec.get_dataset("my_data") |
| 253 |
self.map_spec("attr2", my_data_spec.get_attribute("attr2")) |
| 254 |
|
| 255 |
class BucketMapper(ObjectMapper): |
| 256 |
def __init__(self, spec): |
| 257 |
super().__init__(spec) |
| 258 |
foo_holder_spec = spec.get_group("foo_holder") |
| 259 |
self.unmap(foo_holder_spec) |
| 260 |
foo_spec = foo_holder_spec.get_data_type("Foo") |
| 261 |
self.map_spec("foos", foo_spec) |
| 262 |
|
| 263 |
file_links_spec = GroupSpec( |
| 264 |
"Foo link group", |
| 265 |
name="links", |
| 266 |
links=[ |
| 267 |
LinkSpec( |
| 268 |
"Foo link", |
| 269 |
name="foo_link", |
| 270 |
target_type="Foo", |
| 271 |
quantity=ZERO_OR_ONE, |
| 272 |
) |
| 273 |
], |
| 274 |
) |
| 275 |
|
| 276 |
file_spec = GroupSpec( |
| 277 |
"A file of Foos contained in FooBuckets", |
| 278 |
data_type_def="FooFile", |
| 279 |
groups=[ |
| 280 |
GroupSpec( |
| 281 |
"Holds the FooBuckets", |
| 282 |
name="buckets", |
| 283 |
groups=[ |
| 284 |
GroupSpec( |
| 285 |
"One or more FooBuckets", |
| 286 |
data_type_inc="FooBucket", |
| 287 |
quantity=ZERO_OR_MANY, |
| 288 |
) |
| 289 |
], |
| 290 |
), |
| 291 |
file_links_spec, |
| 292 |
], |
| 293 |
datasets=[ |
| 294 |
DatasetSpec( |
| 295 |
"Foo data", |
| 296 |
name="foofile_data", |
| 297 |
dtype="int", |
| 298 |
quantity=ZERO_OR_ONE, |
| 299 |
) |
| 300 |
], |
| 301 |
attributes=[ |
| 302 |
AttributeSpec( |
| 303 |
doc="Foo ref attr", |
| 304 |
name="foo_ref_attr", |
| 305 |
dtype=RefSpec("Foo", "object"), |
| 306 |
required=False, |
| 307 |
) |
| 308 |
], |
| 309 |
) |
| 310 |
|
| 311 |
class FileMapper(ObjectMapper): |
| 312 |
def __init__(self, spec): |
| 313 |
super().__init__(spec) |
| 314 |
bucket_spec = spec.get_group("buckets").get_data_type("FooBucket") |
| 315 |
self.map_spec("buckets", bucket_spec) |
| 316 |
self.unmap(spec.get_group("links")) |
| 317 |
foo_link_spec = spec.get_group("links").get_link("foo_link") |
| 318 |
self.map_spec("foo_link", foo_link_spec) |
| 319 |
|
| 320 |
spec_catalog = SpecCatalog() |
| 321 |
spec_catalog.register_spec(foo_spec, "test.yaml") |
| 322 |
spec_catalog.register_spec(bucket_spec, "test.yaml") |
| 323 |
spec_catalog.register_spec(file_spec, "test.yaml") |
| 324 |
namespace = SpecNamespace( |
| 325 |
"a test namespace", |
| 326 |
CORE_NAMESPACE, |
| 327 |
[{"source": "test.yaml"}], |
| 328 |
version="0.1.0", |
| 329 |
catalog=spec_catalog, |
| 330 |
) |
| 331 |
namespace_catalog = NamespaceCatalog() |
| 332 |
namespace_catalog.add_namespace(CORE_NAMESPACE, namespace) |
| 333 |
type_map = TypeMap(namespace_catalog) |
| 334 |
|
| 335 |
type_map.register_container_type(CORE_NAMESPACE, "Foo", Foo) |
| 336 |
type_map.register_container_type(CORE_NAMESPACE, "FooBucket", FooBucket) |
| 337 |
type_map.register_container_type(CORE_NAMESPACE, "FooFile", FooFile) |
| 338 |
|
| 339 |
type_map.register_map(Foo, FooMapper) |
| 340 |
type_map.register_map(FooBucket, BucketMapper) |
| 341 |
type_map.register_map(FooFile, FileMapper) |
| 342 |
|
| 343 |
manager = BuildManager(type_map) |
| 344 |
return manager |
| 345 |
|
| 346 |
|
| 347 |
############################################ |
| 348 |
# Baz example data containers and specs |
| 349 |
########################################### |
| 350 |
class Baz(Container): |
| 351 |
pass |
| 352 |
|
| 353 |
|
| 354 |
class BazData(Data): |
| 355 |
pass |
| 356 |
|
| 357 |
|
| 358 |
class BazCpdData(Data): |
| 359 |
pass |
| 360 |
|
| 361 |
|
| 362 |
class BazBucket(Container): |
| 363 |
@docval( |
| 364 |
{"name": "name", "type": str, "doc": "the name of this bucket"}, |
| 365 |
{"name": "bazs", "type": list, "doc": "the Baz objects in this bucket"}, |
| 366 |
{"name": "baz_data", "type": BazData, "doc": "dataset of Baz references", "default": None}, |
| 367 |
{"name": "baz_cpd_data", "type": BazCpdData, "doc": "dataset of Baz references", "default": None}, |
| 368 |
) |
| 369 |
def __init__(self, **kwargs): |
| 370 |
name, bazs, baz_data, baz_cpd_data = getargs("name", "bazs", "baz_data", "baz_cpd_data", kwargs) |
| 371 |
super().__init__(name=name) |
| 372 |
self.__bazs = {b.name: b for b in bazs} # note: collections of groups are unordered in HDF5 |
| 373 |
for b in bazs: |
| 374 |
b.parent = self |
| 375 |
self.__baz_data = baz_data |
| 376 |
if self.__baz_data is not None: |
| 377 |
self.__baz_data.parent = self |
| 378 |
self.__baz_cpd_data = baz_cpd_data |
| 379 |
if self.__baz_cpd_data is not None: |
| 380 |
self.__baz_cpd_data.parent = self |
| 381 |
|
| 382 |
@property |
| 383 |
def bazs(self): |
| 384 |
return self.__bazs |
| 385 |
|
| 386 |
@property |
| 387 |
def baz_data(self): |
| 388 |
return self.__baz_data |
| 389 |
|
| 390 |
@property |
| 391 |
def baz_cpd_data(self): |
| 392 |
return self.__baz_cpd_data |
| 393 |
|
| 394 |
def add_baz(self, baz): |
| 395 |
self.__bazs[baz.name] = baz |
| 396 |
baz.parent = self |
| 397 |
|
| 398 |
def remove_baz(self, baz_name): |
| 399 |
baz = self.__bazs.pop(baz_name) |
| 400 |
self._remove_child(baz) |
| 401 |
return baz |
| 402 |
|
| 403 |
|
| 404 |
def get_baz_buildmanager(): |
| 405 |
baz_spec = GroupSpec( |
| 406 |
doc="A test group specification with a data type", |
| 407 |
data_type_def="Baz", |
| 408 |
) |
| 409 |
|
| 410 |
baz_data_spec = DatasetSpec( |
| 411 |
doc="A test dataset of references specification with a data type", |
| 412 |
name="baz_data", |
| 413 |
data_type_def="BazData", |
| 414 |
dtype=RefSpec("Baz", "object"), |
| 415 |
shape=[None], |
| 416 |
) |
| 417 |
|
| 418 |
baz_cpd_data_spec = DatasetSpec( |
| 419 |
doc="A test compound dataset with references specification with a data type", |
| 420 |
name="baz_cpd_data", |
| 421 |
data_type_def="BazCpdData", |
| 422 |
dtype=[ |
| 423 |
DtypeSpec(name="part1", doc="doc", dtype="int"), |
| 424 |
DtypeSpec(name="part2", doc="doc", dtype=RefSpec("Baz", "object")), |
| 425 |
], |
| 426 |
shape=[None], |
| 427 |
) |
| 428 |
|
| 429 |
baz_holder_spec = GroupSpec( |
| 430 |
doc="group of bazs", |
| 431 |
name="bazs", |
| 432 |
groups=[GroupSpec(doc="Baz", data_type_inc="Baz", quantity=ONE_OR_MANY)], |
| 433 |
) |
| 434 |
|
| 435 |
baz_bucket_spec = GroupSpec( |
| 436 |
doc="A test group specification for a data type containing data type", |
| 437 |
data_type_def="BazBucket", |
| 438 |
groups=[baz_holder_spec], |
| 439 |
datasets=[ |
| 440 |
DatasetSpec(doc="doc", data_type_inc="BazData", quantity=ZERO_OR_ONE), |
| 441 |
DatasetSpec(doc="doc", data_type_inc="BazCpdData", quantity=ZERO_OR_ONE), |
| 442 |
], |
| 443 |
) |
| 444 |
|
| 445 |
spec_catalog = SpecCatalog() |
| 446 |
spec_catalog.register_spec(baz_spec, "test.yaml") |
| 447 |
spec_catalog.register_spec(baz_data_spec, "test.yaml") |
| 448 |
spec_catalog.register_spec(baz_cpd_data_spec, "test.yaml") |
| 449 |
spec_catalog.register_spec(baz_bucket_spec, "test.yaml") |
| 450 |
|
| 451 |
namespace = SpecNamespace( |
| 452 |
"a test namespace", |
| 453 |
CORE_NAMESPACE, |
| 454 |
[{"source": "test.yaml"}], |
| 455 |
version="0.1.0", |
| 456 |
catalog=spec_catalog, |
| 457 |
) |
| 458 |
|
| 459 |
namespace_catalog = NamespaceCatalog() |
| 460 |
namespace_catalog.add_namespace(CORE_NAMESPACE, namespace) |
| 461 |
|
| 462 |
type_map = TypeMap(namespace_catalog) |
| 463 |
type_map.register_container_type(CORE_NAMESPACE, "Baz", Baz) |
| 464 |
type_map.register_container_type(CORE_NAMESPACE, "BazData", BazData) |
| 465 |
type_map.register_container_type(CORE_NAMESPACE, "BazCpdData", BazCpdData) |
| 466 |
type_map.register_container_type(CORE_NAMESPACE, "BazBucket", BazBucket) |
| 467 |
|
| 468 |
class BazBucketMapper(ObjectMapper): |
| 469 |
def __init__(self, spec): |
| 470 |
super().__init__(spec) |
| 471 |
baz_holder_spec = spec.get_group("bazs") |
| 472 |
self.unmap(baz_holder_spec) |
| 473 |
baz_spec = baz_holder_spec.get_data_type("Baz") |
| 474 |
self.map_spec("bazs", baz_spec) |
| 475 |
|
| 476 |
type_map.register_map(BazBucket, BazBucketMapper) |
| 477 |
|
| 478 |
manager = BuildManager(type_map) |
| 479 |
return manager |
| 480 |
|
| 481 |
|
| 482 |
def create_test_type_map(specs, container_classes, mappers=None): |
| 483 |
""" |
| 484 |
Create a TypeMap with the specs registered under a test namespace, and classes and mappers registered to type names. |
| 485 |
:param specs: list of specs |
| 486 |
:param container_classes: dict of type name to container class |
| 487 |
:param mappers: (optional) dict of type name to mapper class |
| 488 |
:return: the constructed TypeMap |
| 489 |
""" |
| 490 |
spec_catalog = SpecCatalog() |
| 491 |
schema_file = "test.yaml" |
| 492 |
for s in specs: |
| 493 |
spec_catalog.register_spec(s, schema_file) |
| 494 |
namespace = SpecNamespace( |
| 495 |
doc="a test namespace", |
| 496 |
name=CORE_NAMESPACE, |
| 497 |
schema=[{"source": schema_file}], |
| 498 |
version="0.1.0", |
| 499 |
catalog=spec_catalog, |
| 500 |
) |
| 501 |
namespace_catalog = NamespaceCatalog() |
| 502 |
namespace_catalog.add_namespace(CORE_NAMESPACE, namespace) |
| 503 |
type_map = TypeMap(namespace_catalog) |
| 504 |
for type_name, container_cls in container_classes.items(): |
| 505 |
type_map.register_container_type(CORE_NAMESPACE, type_name, container_cls) |
| 506 |
if mappers: |
| 507 |
for type_name, mapper_cls in mappers.items(): |
| 508 |
container_cls = container_classes[type_name] |
| 509 |
type_map.register_map(container_cls, mapper_cls) |
| 510 |
return type_map |
| 511 |
|
| 512 |
|
| 513 |
def create_load_namespace_yaml(namespace_name, specs, output_dir, incl_types, type_map): |
| 514 |
""" |
| 515 |
Create a TypeMap with the specs loaded from YAML files and dependencies resolved. |
| 516 |
|
| 517 |
This writes namespaces and specs to YAML files, creates an empty TypeMap, and calls load_namespaces on the |
| 518 |
TypeMap, instead of manually creating a SpecCatalog, SpecNamespace, NamespaceCatalog and manually registering |
| 519 |
container types. Importantly, this process resolves dependencies across namespaces. |
| 520 |
|
| 521 |
:param namespace_name: Name of the new namespace. |
| 522 |
:param specs: List of specs of new data types to add. |
| 523 |
:param incl_types: Dict mapping included namespace name to list of data types to include or None to include all. |
| 524 |
:param type_map: The type map to load the namespace into. |
| 525 |
""" |
| 526 |
ns_builder = NamespaceBuilder( |
| 527 |
name=namespace_name, |
| 528 |
doc="a test namespace", |
| 529 |
version="0.1.0", |
| 530 |
) |
| 531 |
ns_filename = ns_builder.name + ".namespace.yaml" |
| 532 |
ext_filename = ns_builder.name + ".extensions.yaml" |
| 533 |
|
| 534 |
for ns, types in incl_types.items(): |
| 535 |
if types is None: # include all types |
| 536 |
ns_builder.include_namespace(ns) |
| 537 |
else: |
| 538 |
for dt in types: |
| 539 |
ns_builder.include_type(dt, namespace=ns) |
| 540 |
|
| 541 |
for data_type in specs: |
| 542 |
ns_builder.add_spec(ext_filename, data_type) |
| 543 |
|
| 544 |
ns_builder.export(ns_filename, outdir=output_dir) |
| 545 |
ns_path = os.path.join(output_dir, ns_filename) |
| 546 |
type_map.load_namespaces(ns_path) |
| 547 |
|
| 548 |
|
| 549 |
# ##### custom spec classes ##### |
| 550 |
|
| 551 |
|
| 552 |
def swap_inc_def(cls, custom_cls): |
| 553 |
args = get_docval(cls.__init__) |
| 554 |
ret = list() |
| 555 |
for arg in args: |
| 556 |
if arg["name"] == "data_type_def": |
| 557 |
ret.append( |
| 558 |
{ |
| 559 |
"name": "my_data_type_def", |
| 560 |
"type": str, |
| 561 |
"doc": "the NWB data type this spec defines", |
| 562 |
"default": None, |
| 563 |
} |
| 564 |
) |
| 565 |
elif arg["name"] == "data_type_inc": |
| 566 |
ret.append( |
| 567 |
{ |
| 568 |
"name": "my_data_type_inc", |
| 569 |
"type": (custom_cls, str), |
| 570 |
"doc": "the NWB data type this spec includes", |
| 571 |
"default": None, |
| 572 |
} |
| 573 |
) |
| 574 |
else: |
| 575 |
ret.append(copy(arg)) |
| 576 |
return ret |
| 577 |
|
| 578 |
|
| 579 |
class BaseStorageOverride: |
| 580 |
__type_key = "my_data_type" |
| 581 |
__inc_key = "my_data_type_inc" |
| 582 |
__def_key = "my_data_type_def" |
| 583 |
|
| 584 |
@classmethod |
| 585 |
def type_key(cls): |
| 586 |
"""Get the key used to store data type on an instance""" |
| 587 |
return cls.__type_key |
| 588 |
|
| 589 |
@classmethod |
| 590 |
def inc_key(cls): |
| 591 |
"""Get the key used to define a data_type include.""" |
| 592 |
return cls.__inc_key |
| 593 |
|
| 594 |
@classmethod |
| 595 |
def def_key(cls): |
| 596 |
"""Get the key used to define a data_type definition.""" |
| 597 |
return cls.__def_key |
| 598 |
|
| 599 |
@classmethod |
| 600 |
def build_const_args(cls, spec_dict): |
| 601 |
"""Extend base functionality to remap data_type_def and data_type_inc keys""" |
| 602 |
spec_dict = copy(spec_dict) |
| 603 |
proxy = super(BaseStorageOverride, cls) |
| 604 |
if proxy.inc_key() in spec_dict: |
| 605 |
spec_dict[cls.inc_key()] = spec_dict.pop(proxy.inc_key()) |
| 606 |
if proxy.def_key() in spec_dict: |
| 607 |
spec_dict[cls.def_key()] = spec_dict.pop(proxy.def_key()) |
| 608 |
ret = proxy.build_const_args(spec_dict) |
| 609 |
return ret |
| 610 |
|
| 611 |
@classmethod |
| 612 |
def _translate_kwargs(cls, kwargs): |
| 613 |
"""Swap mydata_type_def and mydata_type_inc for data_type_def and data_type_inc, respectively""" |
| 614 |
proxy = super(BaseStorageOverride, cls) |
| 615 |
kwargs[proxy.def_key()] = kwargs.pop(cls.def_key()) |
| 616 |
kwargs[proxy.inc_key()] = kwargs.pop(cls.inc_key()) |
| 617 |
return kwargs |
| 618 |
|
| 619 |
|
| 620 |
class CustomGroupSpec(BaseStorageOverride, GroupSpec): |
| 621 |
@docval(*deepcopy(swap_inc_def(GroupSpec, "CustomGroupSpec"))) |
| 622 |
def __init__(self, **kwargs): |
| 623 |
kwargs = self._translate_kwargs(kwargs) |
| 624 |
super().__init__(**kwargs) |
| 625 |
|
| 626 |
@classmethod |
| 627 |
def dataset_spec_cls(cls): |
| 628 |
return CustomDatasetSpec |
| 629 |
|
| 630 |
@docval(*deepcopy(swap_inc_def(GroupSpec, "CustomGroupSpec"))) |
| 631 |
def add_group(self, **kwargs): |
| 632 |
spec = CustomGroupSpec(**kwargs) |
| 633 |
self.set_group(spec) |
| 634 |
return spec |
| 635 |
|
| 636 |
@docval(*deepcopy(swap_inc_def(DatasetSpec, "CustomDatasetSpec"))) |
| 637 |
def add_dataset(self, **kwargs): |
| 638 |
"""Add a new specification for a subgroup to this group specification""" |
| 639 |
spec = CustomDatasetSpec(**kwargs) |
| 640 |
self.set_dataset(spec) |
| 641 |
return spec |
| 642 |
|
| 643 |
|
| 644 |
class CustomDatasetSpec(BaseStorageOverride, DatasetSpec): |
| 645 |
@docval(*deepcopy(swap_inc_def(DatasetSpec, "CustomDatasetSpec"))) |
| 646 |
def __init__(self, **kwargs): |
| 647 |
kwargs = self._translate_kwargs(kwargs) |
| 648 |
super().__init__(**kwargs) |
| 649 |
|
| 650 |
|
| 651 |
class CustomSpecNamespace(SpecNamespace): |
| 652 |
__types_key = "my_data_types" |
| 653 |
|
| 654 |
@classmethod |
| 655 |
def types_key(cls): |
| 656 |
return cls.__types_key |
| 657 |
|
| 658 |
class FooExtendDynamicTable0(DynamicTable): |
| 659 |
""" |
| 660 |
Within PyNWB, PlaneSegmentation extends DynamicTable and sets __columns__. This class is a helper |
| 661 |
class for testing and is directly meant to test __gather_columns, i.e., class generation, downstream. |
| 662 |
""" |
| 663 |
__columns__ = ( |
| 664 |
{'name': 'col1', 'description': '...'}, |
| 665 |
{'name': 'col2', 'description': '...'}, |
| 666 |
) |
| 667 |
|
| 668 |
def __init__(self, **kwargs): |
| 669 |
kwargs['name'] = 'foo0' |
| 670 |
kwargs['description'] = '...' |
| 671 |
super().__init__(**kwargs) |
| 672 |
|
| 673 |
|
| 674 |
class FooExtendDynamicTable1(FooExtendDynamicTable0): |
| 675 |
""" |
| 676 |
In extensions, users can create new classes that inherit from classes that inherit from DynamicTable. |
| 677 |
This is a helper class for testing and is directly meant to test __gather_columns, i.e., |
| 678 |
class generation, downstream. |
| 679 |
""" |
| 680 |
__columns__ = ( |
| 681 |
{'name': 'col3', 'description': '...'}, |
| 682 |
{'name': 'col4', 'description': '...'}, |
| 683 |
) |
| 684 |
|
| 685 |
def __init__(self, **kwargs): |
| 686 |
kwargs['name'] = 'foo1' |
| 687 |
kwargs['description'] = '...' |
| 688 |
super().__init__(**kwargs) |
| 689 |
|
| 690 |
|
| 691 |
class FooExtendDynamicTable2(FooExtendDynamicTable1, MultiContainerInterface): |
| 692 |
__clsconf__ = { |
| 693 |
'add': '...', |
| 694 |
'get': '...', |
| 695 |
'create': '...', |
| 696 |
'type': Container, |
| 697 |
'attr': '...' |
| 698 |
} |
| 699 |
|
| 700 |
def __init__(self, **kwargs): |
| 701 |
kwargs['name'] = 'foo2' |
| 702 |
kwargs['description'] = '...' |
| 703 |
super().__init__(**kwargs) |