Skip to content

Commit

Permalink
Make spglib version check more robust
Browse files Browse the repository at this point in the history
  • Loading branch information
t-reents committed Nov 10, 2024
1 parent d24e1fe commit 27df2ad
Showing 1 changed file with 16 additions and 29 deletions.
45 changes: 16 additions & 29 deletions seekpath/hpkot/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@
import numpy.linalg
from math import sqrt

import sys
from packaging.version import Version

# Use importlib.metadata for version retrieval based on Python version
if sys.version_info < (3, 8):
from importlib_metadata import version # For Python < 3.8
else:
from importlib.metadata import version


def eval_expr_simple(expr, kparam): # pylint=disable: too-many-return-statements
"""
Expand Down Expand Up @@ -222,25 +231,15 @@ def check_spglib_version():
"of the k-paths, but it could not be imported"
)

try:
version = spglib.__version__
except NameError:
version = "1.8.0" # or older, version was introduced only recently

try:
version_pieces = [int(_) for _ in version.split(".")]
if len(version_pieces) < 3:
raise ValueError
except ValueError:
raise ValueError("Unable to parse version number")
spg_version = Version(version("spglib"))

if tuple(version_pieces[:2]) < (1, 9):
raise ValueError("Invalid spglib version, need >= 1.9.4")
min_version = Version("1.9.4")
warning_version = Version("1.13")

if version_pieces[:2] == (1, 9) and version_pieces[2] < 4:
if spg_version < min_version:
raise ValueError("Invalid spglib version, need >= 1.9.4")

if tuple(version_pieces[:2]) < (1, 13):
if spg_version < warning_version:
import warnings

warnings.warn(
Expand All @@ -260,21 +259,9 @@ def get_dot_access_dataset(dataset):
To emulate it for older versions, this function is used.
"""
import spglib

try:
version = spglib.__version__
except NameError:
version = "1.8.0" # or older, version was introduced only recently

version_pieces = tuple(int(v) for v in version.split(".")[:3])
try:
if len(version_pieces) < 3:
raise ValueError
except ValueError:
raise ValueError("Unable to parse version number")
spg_version = Version(version("spglib"))

if version_pieces < (2, 5, 0):
if spg_version < Version("2.5.0"):
from types import SimpleNamespace

return SimpleNamespace(**dataset)
Expand Down

0 comments on commit 27df2ad

Please sign in to comment.