Skip to content

Commit

Permalink
get_config compat for h5py
Browse files Browse the repository at this point in the history
  • Loading branch information
jreadey committed Dec 25, 2024
1 parent c0f9ea7 commit 5e99816
Show file tree
Hide file tree
Showing 14 changed files with 368 additions and 220 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
103 changes: 0 additions & 103 deletions h5pyd/_hl/config.py

This file was deleted.

18 changes: 12 additions & 6 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 @@ -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,7 +320,8 @@ 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
Expand All @@ -341,7 +342,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 Down Expand Up @@ -406,6 +407,9 @@ def __init__(
if swmr:
use_cache = False # disable metadata caching in swmr mode

if track_order is None:
track_order = cfg.track_order

http_conn = HttpConn(
domain,
endpoint=endpoint,
Expand Down Expand Up @@ -433,8 +437,6 @@ 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

Expand Down Expand Up @@ -487,6 +489,10 @@ def __init__(
body["owner"] = owner
if linked_domain:
body["linked_domain"] = linked_domain
if 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
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
64 changes: 48 additions & 16 deletions h5pyd/_hl/group.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from .table import Table
from .datatype import Datatype
from . import h5type
from .. import config


def isUUID(name):
Expand All @@ -49,7 +50,7 @@ class Group(HLObject, MutableMappingHDF5):
""" Represents an HDF5 group.
"""

def __init__(self, bind, track_order=False, **kwargs):
def __init__(self, bind, track_order=None, **kwargs):
# print "group init, bind:", bind

""" Create a new Group object by binding to a low-level GroupID.
Expand All @@ -58,7 +59,20 @@ def __init__(self, bind, track_order=False, **kwargs):
if not isinstance(bind, GroupID):
raise ValueError(f"{bind} is not a GroupID")
HLObject.__init__(self, bind, **kwargs)
self._track_order = track_order

if track_order is None:
# set order based on group creation props
gcpl = self.id.gcpl_json
if "CreateOrder" in gcpl:
createOrder = gcpl["CreateOrder"]
if not createOrder or createOrder == "0":
self._track_order = False
else:
self._track_order = True
else:
self._track_order = False
else:
self._track_order = track_order
self._req_prefix = "/groups/" + self.id.uuid
self._link_db = {} # cache for links

Expand Down Expand Up @@ -182,9 +196,10 @@ def _get_objdb_links(self):
group_json = objdb[self.id.id]
return group_json["links"]

def _make_group(self, parent_id=None, parent_name=None, link=None):
def _make_group(self, parent_id=None, parent_name=None, link=None, track_order=None):
""" helper function to make a group """

cfg = config.get_config()
link_json = {}
if parent_id:
link_json["id"] = parent_id
Expand All @@ -195,6 +210,9 @@ def _make_group(self, parent_id=None, parent_name=None, link=None):
body = {}
if link_json:
body["link"] = link_json
if track_order or cfg.track_order:
body["creationProperties"] = {"CreateOrder": 1}

self.log.debug(f"create group with body: {body}")
rsp = self.POST('/groups', body=body)

Expand All @@ -211,7 +229,7 @@ def _make_group(self, parent_id=None, parent_name=None, link=None):

return sub_group

def create_group(self, h5path, track_order=False):
def create_group(self, h5path, track_order=None):
""" Create and return a new subgroup.
Name may be absolute or relative. Fails if the target name already
Expand All @@ -223,8 +241,7 @@ def create_group(self, h5path, track_order=False):

if h5path is None:
# anonymous group
sub_group = self._make_group()
sub_group._track_order = track_order
sub_group = self._make_group(track_order=track_order)
return sub_group

if h5path[-1] == '/':
Expand Down Expand Up @@ -514,7 +531,7 @@ def require_group(self, name):
raise TypeError(f"Incompatible object ({grp.__class__.__name__}) already exists")
return grp

def getObjByUuid(self, uuid, collection_type=None, track_order=False):
def getObjByUuid(self, uuid, collection_type=None, track_order=None):
""" Utility method to get an obj based on collection type and uuid """
self.log.debug(f"getObjByUuid({uuid})")
obj_json = None
Expand Down Expand Up @@ -549,7 +566,10 @@ def getObjByUuid(self, uuid, collection_type=None, track_order=False):
# will need to get JSON from server
req = f"/{collection_type}/{uuid}"
# make server request
obj_json = self.GET(req, params={"CreateOrder": "1" if track_order else "0"})
params = {}
if track_order is not None:
params["CreateOrder"] = "1" if track_order else "0"
obj_json = self.GET(req, params=params)

if collection_type == 'groups':
tgt = Group(GroupID(self, obj_json), track_order=track_order)
Expand All @@ -568,7 +588,7 @@ def getObjByUuid(self, uuid, collection_type=None, track_order=False):

return tgt

def __getitem__(self, name, track_order=False):
def __getitem__(self, name, track_order=None):
""" Open an object in the file """
# convert bytes to str for PY3
if isinstance(name, bytes):
Expand Down Expand Up @@ -655,7 +675,7 @@ def _objectify_link_Json(self, link_json):

return link_obj

def get(self, name, default=None, getclass=False, getlink=False, track_order=False, **kwds):
def get(self, name, default=None, getclass=False, getlink=False, track_order=None, **kwds):
""" Retrieve an item or other information.
"name" given only:
Expand Down Expand Up @@ -739,8 +759,8 @@ def get(self, name, default=None, getclass=False, getlink=False, track_order=Fal
params["pattern"] = pattern
if follow_links:
params["follow_links"] = 1
if track_order:
params["CreateOrder"] = 1
if track_order is not None:
params["CreateOrder"] = "1" if track_order else "0"

if name:
body = {}
Expand Down Expand Up @@ -848,7 +868,10 @@ def __setitem__(self, name, obj):
raise IOError("cannot create subgroup of softlink")
parent_uuid = link_json["id"]
req = "/groups/" + parent_uuid
group_json = self.GET(req, params={"CreateOrder": "1" if self._track_order else "0"})
params = {}
if self._track_order is not None:
params["CreateOrder"] = "1" if self._track_order else "0"
group_json = self.GET(req, params=params)
tgt = Group(GroupID(self, group_json))
tgt[basename] = obj

Expand Down Expand Up @@ -946,7 +969,10 @@ def __len__(self):
return len(links_json)

req = "/groups/" + self.id.uuid
rsp_json = self.GET(req, params={"CreateOrder": "1" if self._track_order else "0"})
params = {}
if self._track_order is not None:
params["CreateOrder"] = "1" if self._track_order else "0"
rsp_json = self.GET(req, params=params)
return rsp_json['linkCount']

def __iter__(self):
Expand All @@ -955,7 +981,10 @@ def __iter__(self):

if links is None:
req = "/groups/" + self.id.uuid + "/links"
rsp_json = self.GET(req, params={"CreateOrder": "1" if self._track_order else "0"})
params = {}
if self._track_order is not None:
params["CreateOrder"] = "1" if self._track_order else "0"
rsp_json = self.GET(req, params=params)
links = rsp_json['links']

# reset the link cache
Expand Down Expand Up @@ -1180,7 +1209,10 @@ def visititems(self, func):
else:
# request from server
req = "/groups/" + parent.id.uuid + "/links"
rsp_json = self.GET(req, params={"CreateOrder": "1" if self._track_order else "0"})
params = {}
if self._track_order is not None:
params["CreateOrder"] = "1" if self._track_order else "0"
rsp_json = self.GET(req, params=params)
links = rsp_json['links']
for link in links:
obj = None
Expand Down
Loading

0 comments on commit 5e99816

Please sign in to comment.