|
| 1 | +############################################################################## |
| 2 | +# Copyright by The HDF Group. # |
| 3 | +# All rights reserved. # |
| 4 | +# # |
| 5 | +# This file is part of H5Serv (HDF5 REST Server) Service, Libraries and # |
| 6 | +# Utilities. The full HDF5 REST Server copyright notice, including # |
| 7 | +# terms governing use, modification, and redistribution, is contained in # |
| 8 | +# the file COPYING, which can be found at the root of the source code # |
| 9 | +# distribution tree. If you do not have access to this file, you may # |
| 10 | +# request a copy from help@hdfgroup.org. # |
| 11 | +############################################################################## |
| 12 | + |
| 13 | +import h5py |
| 14 | +import numpy as np |
| 15 | + |
| 16 | +from . import hdf5dtype |
| 17 | + |
| 18 | + |
| 19 | +def is_reference(val): |
| 20 | + """ Return True if the type or value is a Reference """ |
| 21 | + |
| 22 | + if isinstance(val, object) and val.__class__.__name__ == "Reference": |
| 23 | + return True |
| 24 | + elif isinstance(val, type) and val.__name__ == "Reference": |
| 25 | + return True |
| 26 | + else: |
| 27 | + return False |
| 28 | + |
| 29 | + |
| 30 | +def is_regionreference(val): |
| 31 | + """ Return True if the type or value is a RegionReference """ |
| 32 | + |
| 33 | + if isinstance(val, object) and val.__class__.__name__ == "RegionReference": |
| 34 | + return True |
| 35 | + elif isinstance(val, type) and val.__name__ == "RegionReference": |
| 36 | + return True |
| 37 | + |
| 38 | + return False |
| 39 | + |
| 40 | + |
| 41 | +def has_reference(dtype): |
| 42 | + """ return True if the dtype (or a sub-type) is a Reference type """ |
| 43 | + has_ref = False |
| 44 | + if not isinstance(dtype, np.dtype): |
| 45 | + return False |
| 46 | + if len(dtype) > 0: |
| 47 | + for name in dtype.fields: |
| 48 | + item = dtype.fields[name] |
| 49 | + if has_reference(item[0]): |
| 50 | + has_ref = True |
| 51 | + break |
| 52 | + elif dtype.metadata and "ref" in dtype.metadata: |
| 53 | + basedt = dtype.metadata["ref"] |
| 54 | + has_ref = is_reference(basedt) |
| 55 | + elif dtype.metadata and "vlen" in dtype.metadata: |
| 56 | + basedt = dtype.metadata["vlen"] |
| 57 | + has_ref = has_reference(basedt) |
| 58 | + return has_ref |
| 59 | + |
| 60 | + |
| 61 | +def convert_dtype(srcdt, to_h5py=True): |
| 62 | + """Return a dtype based on input dtype, converting any Reference types from |
| 63 | + h5py style to h5json and vice-versa. |
| 64 | + """ |
| 65 | + |
| 66 | + if len(srcdt) > 0: |
| 67 | + fields = [] |
| 68 | + for name in srcdt.fields: |
| 69 | + item = srcdt.fields[name] |
| 70 | + # item is a tuple of dtype and integer offset |
| 71 | + field_dt = convert_dtype(item[0], to_h5py=to_h5py) |
| 72 | + fields.append((name, field_dt)) |
| 73 | + tgt_dt = np.dtype(fields) |
| 74 | + else: |
| 75 | + # check if this a "special dtype" |
| 76 | + if srcdt.metadata and "ref" in srcdt.metadata: |
| 77 | + ref = srcdt.metadata["ref"] |
| 78 | + if is_reference(ref): |
| 79 | + if to_h5py: |
| 80 | + tgt_dt = h5py.special_dtype(ref=h5py.Reference) |
| 81 | + else: |
| 82 | + tgt_dt = hdf5dtype.special_dtype(ref=hdf5dtype.Reference) |
| 83 | + elif is_regionreference(ref): |
| 84 | + if to_h5py: |
| 85 | + tgt_dt = h5py.special_dtype(ref=h5py.RegionReference) |
| 86 | + else: |
| 87 | + tgt_dt = hdf5dtype.special_dtype(ref=hdf5dtype.RegionReference) |
| 88 | + else: |
| 89 | + msg = f"Unexpected ref type: {srcdt}" |
| 90 | + raise TypeError(msg) |
| 91 | + elif srcdt.metadata and "vlen" in srcdt.metadata: |
| 92 | + src_vlen = srcdt.metadata["vlen"] |
| 93 | + if isinstance(src_vlen, np.dtype): |
| 94 | + tgt_base = convert_dtype(src_vlen, to_h5py=to_h5py) |
| 95 | + else: |
| 96 | + tgt_base = src_vlen |
| 97 | + if to_h5py: |
| 98 | + tgt_dt = h5py.special_dtype(vlen=tgt_base) |
| 99 | + else: |
| 100 | + tgt_dt = hdf5dtype.special_dtype(vlen=tgt_base) |
| 101 | + elif srcdt.kind == "U": |
| 102 | + # use vlen for unicode strings |
| 103 | + if to_h5py: |
| 104 | + tgt_dt = h5py.special_dtype(vlen=str) |
| 105 | + else: |
| 106 | + tgt_dt = hdf5dtype.special_dtype(vlen=str) |
| 107 | + else: |
| 108 | + tgt_dt = srcdt |
| 109 | + return tgt_dt |
0 commit comments