Skip to content

Commit

Permalink
Config (#245)
Browse files Browse the repository at this point in the history
* Added h5pyd.get_config for compat with h5py
* support link/attribute ordering with cfg.track_order
  • Loading branch information
jreadey authored Dec 27, 2024
1 parent c0f9ea7 commit 880721b
Show file tree
Hide file tree
Showing 18 changed files with 507 additions and 286 deletions.
3 changes: 2 additions & 1 deletion h5pyd/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
from . import h5ds


from .config import Config
from .config import get_config

__version__ = version.version


Expand Down
9 changes: 7 additions & 2 deletions h5pyd/_hl/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import posixpath
import os
import sys
import json
import numpy as np
import logging
Expand All @@ -28,6 +29,10 @@
numpy_float_types = (np.float16, np.float32, np.float64)


def eprint(*args, **kwargs):
print(*args, file=sys.stderr, **kwargs)


class FakeLock():
def __init__(self):
pass
Expand Down Expand Up @@ -506,7 +511,7 @@ def readElement(buffer, offset, arr, index, dt):
e = np.frombuffer(bytes(e_buffer), dtype=dt)
arr[index] = e[0]
except ValueError:
print(f"ERROR: ValueError setting {e_buffer} and dtype: {dt}")
eprint(f"ERROR: ValueError setting {e_buffer} and dtype: {dt}")
raise
else:
# variable length element
Expand All @@ -533,7 +538,7 @@ def readElement(buffer, offset, arr, index, dt):
try:
e = np.frombuffer(bytes(e_buffer), dtype=vlen)
except ValueError:
print("ValueError -- e_buffer:", e_buffer, "dtype:", vlen)
eprint("ValueError -- e_buffer:", e_buffer, "dtype:", vlen)
raise
arr[index] = e

Expand Down
103 changes: 0 additions & 103 deletions h5pyd/_hl/config.py

This file was deleted.

32 changes: 16 additions & 16 deletions h5pyd/_hl/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from .objectid import GroupID
from .group import Group
from .httpconn import HttpConn
from .config import Config
from .. import config

VERBOSE_REFRESH_TIME = 1.0 # 1 second

Expand Down Expand Up @@ -49,7 +49,7 @@ class H5Image(io.RawIOBase):
def __init__(self, domain_path, h5path="h5image", chunks_per_page=1, logger=None):
""" verify dataset can be accessed and set logger if supplied """
self._cursor = 0
if domain_path.startswith("hdf5::/"):
if domain_path and domain_path.startswith("hdf5::/"):
self._domain_path = domain_path
else:
self._domain_path = "hdf5:/" + domain_path
Expand Down Expand Up @@ -276,7 +276,7 @@ def __init__(
logger=None,
owner=None,
linked_domain=None,
track_order=False,
track_order=None,
retries=10,
timeout=180,
**kwds,
Expand Down Expand Up @@ -320,13 +320,13 @@ def __init__(
Create new domain using the root of the linked domain
track_order
Whether to track dataset/group/attribute creation order within this file. Objects will be iterated
in ascending creation order if this is enabled, otherwise in ascending alphanumeric order.
in ascending creation order if this is True, if False in ascending alphanumeric order.
If None use global default get_config().track_order.
retries
Number of retry attempts to be used if a server request fails
timeout
Timeout value in seconds
"""

groupid = None
dn_ids = []
# if we're passed a GroupId as domain, just initialize the file object
Expand All @@ -341,7 +341,7 @@ def __init__(
if mode is None:
mode = "r"

cfg = Config() # pulls in state from a .hscfg file (if found).
cfg = config.get_config() # pulls in state from a .hscfg file (if found).

# accept domain values in the form:
# http://server:port/home/user/myfile.h5
Expand All @@ -354,7 +354,7 @@ def __init__(
#
# For http prefixed values, extract the endpont and use the rest as domain path
for protocol in ("http://", "https://", "hdf5://", "http+unix://"):
if domain.startswith(protocol):
if domain and domain.startswith(protocol):
if protocol.startswith("http"):
domain = domain[len(protocol):]
# extract the endpoint
Expand Down Expand Up @@ -383,7 +383,7 @@ def __init__(
endpoint = cfg["hs_endpoint"]

# remove the trailing slash on endpoint if it exists
if endpoint.endswith('/'):
if endpoint and endpoint.endswith('/'):
endpoint = endpoint.strip('/')

if username is None:
Expand Down Expand Up @@ -433,12 +433,10 @@ def __init__(
if bucket:
params["bucket"] = bucket

params["CreateOrder"] = "1" if track_order else "0"

# need some special logic for the first request in local mode
# to give the sockets time to initialize

if endpoint.startswith("local"):
if endpoint and endpoint.startswith("local"):
connect_backoff = [0.5, 1, 2, 4, 8, 16]
else:
connect_backoff = []
Expand Down Expand Up @@ -487,6 +485,10 @@ def __init__(
body["owner"] = owner
if linked_domain:
body["linked_domain"] = linked_domain
if track_order or cfg.track_order:
create_props = {"CreateOrder": 1}
group_body = {"creationProperties": create_props}
body["group"] = group_body
rsp = http_conn.PUT(req, params=params, body=body)
if rsp.status_code != 201:
http_conn.close()
Expand Down Expand Up @@ -552,22 +554,20 @@ def __init__(

groupid = GroupID(None, group_json, http_conn=http_conn)
# end else

self._name = "/"
self._id = groupid
self._verboseInfo = None # aditional state we'll get when requested
self._verboseInfo = None # additional state we'll get when requested
self._verboseUpdated = None # when the verbose data was fetched
self._lastScan = None # when summary stats where last updated by server
self._dn_ids = dn_ids
self._track_order = track_order
self._swmr_mode = swmr

Group.__init__(self, self._id, track_order=track_order)

def _getVerboseInfo(self):
now = time.time()
if (
self._verboseUpdated is None or now - self._verboseUpdated > VERBOSE_REFRESH_TIME
):
if (self._verboseUpdated is None or now - self._verboseUpdated > VERBOSE_REFRESH_TIME):
# resynch the verbose data
req = "/?verbose=1"
rsp_json = self.GET(req, use_cache=False, params={"CreateOrder": "1" if self._track_order else "0"})
Expand Down
4 changes: 2 additions & 2 deletions h5pyd/_hl/folders.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import time
import logging
from .httpconn import HttpConn
from .config import Config
from .. import config


class Folder:
Expand Down Expand Up @@ -143,7 +143,7 @@ def __init__(
if mode is None:
mode = "r"

cfg = Config() # pulls in state from a .hscfg file (if found).
cfg = config.get_config() # pulls in state from a .hscfg file (if found).

if endpoint is None and "hs_endpoint" in cfg:
endpoint = cfg["hs_endpoint"]
Expand Down
Loading

0 comments on commit 880721b

Please sign in to comment.