Skip to content

Commit

Permalink
initial
Browse files Browse the repository at this point in the history
  • Loading branch information
TomHodson committed Oct 11, 2024
1 parent 410760f commit acc8308
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 6 deletions.
18 changes: 12 additions & 6 deletions pyfdb/pyfdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ class PatchedLib:
and patches the accessors with automatic python-C error handling.
"""

def __init__(self):
self.path = findlibs.find("fdb5")
def __init__(self, lib_path=None):
self.path = lib_path or findlibs.find("fdb5")

if self.path is None:
raise RuntimeError("FDB5 library not found")
Expand Down Expand Up @@ -102,9 +102,8 @@ def __repr__(self):
return f"<pyfdb.pyfdb.PatchedLib FDB5 version {self.version} from {self.path}>"


# Bootstrap the library

lib = PatchedLib()
# Lazy load the library to avoid loading it when the module is imported
lib = None


class Key:
Expand Down Expand Up @@ -296,7 +295,14 @@ class FDB:

__fdb = None

def __init__(self, config=None, user_config=None):
def __init__(self, config=None, user_config=None, lib_path=None):
global lib
print("lib", lib)
if lib is None:
print("loading library")
lib = PatchedLib(lib_path)
print("lib", lib)

fdb = ffi.new("fdb_handle_t**")

if config is not None or user_config is not None:
Expand Down
40 changes: 40 additions & 0 deletions tests/test_lib.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from pathlib import Path
from tempfile import TemporaryDirectory

import findlibs

import pyfdb


def test_lib_path():
# pyfdb uses findlibs internally to locate the library
# So this looks a bit circular but it's just to check that we can pass a path explicitly
# The `libpath` argument allows the user to hard code the path to the library.
# This should not be necessary in normal usage but it's a useful backup.
lib_path = findlibs.find("fdb5")

with TemporaryDirectory() as tmp_root:
tests_dir = Path(__file__).parent

config = dict(
type="local",
engine="toc",
schema=str(tests_dir / "default_fdb_schema"),
spaces=[
dict(
handler="Default",
roots=[
{"path": tmp_root, "write": True},
],
)
],
)

fdb = pyfdb.FDB(config=config)
data = open(tests_dir / "x138-300.grib", "rb").read()
fdb.archive(data)
fdb.flush()
list_output = list(fdb.list(keys=True))
assert len(list_output) == 1
print("external lib", pyfdb.lib)
assert pyfdb.lib.path == lib_path

0 comments on commit acc8308

Please sign in to comment.