Skip to content

Commit

Permalink
leftover
Browse files Browse the repository at this point in the history
  • Loading branch information
calad0i committed Dec 16, 2024
1 parent d3c8881 commit 738e5b0
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions hls4ml/utils/dependency.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import sys
from functools import wraps
from importlib.metadata import metadata
from inspect import ismethod

extra_requires: dict[str, list[str]] = {}
subpackage = None
for k, v in metadata('hls4ml')._headers: # type: ignore
if k != 'Requires-Dist':
continue
if '; extra == ' not in v:
continue

req, pkg = v.split('; extra == ')
pkg = pkg.strip('"')

extra_requires.setdefault(pkg, []).append(req)


def requires(pkg: str):
"""Mark a function or method as requiring a package to be installed.
'name': requires hls4ml[name] to be installed.
'_name': requires name to be installed.
Parameters
----------
pkg : str
The package to require.
"""

def deco(f):
if ismethod(f):
qualifier = f"Method {f.__self__.__class__.__name__}.{f.__name__}"
else:
qualifier = f"Function {f.__name__}"

if not pkg.startswith("_"):
reqs = ", ".join(extra_requires[pkg])
msg = f"{qualifier} requires {reqs}, but package {{ename}} is missing"
"Please consider install it with `pip install hls4ml[{pkg}]` for full functionality with {pkg}."
else:
msg = f"{qualifier} requires {pkg[1:]}, but package {{ename}} is missing."
"Consider install it with `pip install {pkg}`."

@wraps(f)
def inner(*args, **kwargs):
try:
return f(*args, **kwargs)
except ImportError as e:
print(msg.format(ename=e.name), file=sys.stderr)
raise e

return inner

return deco

0 comments on commit 738e5b0

Please sign in to comment.