Skip to content

Commit

Permalink
initial
Browse files Browse the repository at this point in the history
  • Loading branch information
TomHodson committed Oct 14, 2024
1 parent 410760f commit d1e4753
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
28 changes: 23 additions & 5 deletions pyfdb/pyfdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
import io
import json
import os
import sys
from functools import wraps
from pathlib import Path

import cffi
import findlibs
Expand All @@ -40,12 +42,20 @@ class PatchedLib:
and patches the accessors with automatic python-C error handling.
"""

loaded: bool = False

def __init__(self):
self.path = findlibs.find("fdb5")
pass

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

if self.path is None:
raise RuntimeError("FDB5 library not found")

if not Path(self.path).exists():
raise FileNotFoundError(f"Could not find the FDB5 library at {self.path}")

ffi.cdef(self.__read_header())
self.__lib = ffi.dlopen(self.path)

Expand Down Expand Up @@ -76,6 +86,7 @@ def __init__(self):
f"This version of pyfdb ({__version__}) requires fdb version {__fdb_version__} or greater."
f"You have fdb version {self.version} loaded from {self.path}"
)
self.loaded = True

def __read_header(self):
with open(os.path.join(os.path.dirname(__file__), "processed_fdb.h"), "r") as f:
Expand All @@ -102,8 +113,8 @@ def __repr__(self):
return f"<pyfdb.pyfdb.PatchedLib FDB5 version {self.version} from {self.path}>"


# Bootstrap the library

# This doe no actually load the library
# It is only loaded when the FDB class is instantiated by calling lib.load()
lib = PatchedLib()


Expand Down Expand Up @@ -296,7 +307,11 @@ 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
if not lib.loaded:
lib.load(lib_path)

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

if config is not None or user_config is not None:
Expand Down Expand Up @@ -368,7 +383,7 @@ def ctype(self):
return self.__fdb


fdb = None
fdb = 0


# Use functools.wraps to copy over the docstring from FDB.xxx to the module level functions
Expand All @@ -383,8 +398,11 @@ def archive(data) -> None:
@wraps(FDB.list)
def list(request, duplicates=False, keys=False) -> ListIterator:
global fdb
print("internal", id(fdb))
if not fdb:
fdb = FDB()
print("internal", id(fdb))
print("sys.modules", id(sys.modules[__name__].fdb))
return ListIterator(fdb, request, duplicates, keys)


Expand Down
13 changes: 13 additions & 0 deletions tests/test_lib.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from pytest import raises

import pyfdb


def test_direct_config_2():
# 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 = "made up path"
assert raises(FileNotFoundError, pyfdb.FDB, lib_path=lib_path)
assert pyfdb.lib.path == lib_path

0 comments on commit d1e4753

Please sign in to comment.