Coverage for sparc/__init__.py: 62%

24 statements  

« prev     ^ index     » next       coverage.py v7.6.9, created at 2024-12-12 01:13 +0000

1"""Initialization of sparc-x-api 

2 

3For submodules like download_data and api, ase / numpy may be ignored, 

4and run using standard python libaries. This may be useful for cases like 

5conda build and CI where not all dependencies are present 

6""" 

7 

8 

9def _missing_deps_func(*args, **kwargs): 

10 raise ImportError("Importing fails for ase / numpy!") 

11 

12 

13class SPARCMissingDeps: 

14 def __init__(self, *args, **kwargs): 

15 raise ImportError( 

16 "Cannot initialize sparc.SPARC because the required dependencies (ase and numpy) are not available." 

17 ) 

18 

19 def __getattr__(self, name): 

20 raise ImportError( 

21 f"Cannot access '{name}' on sparc.SPARC because the required dependencies (ase and numpy) are not available." 

22 ) 

23 

24 

25try: 

26 import ase 

27 import numpy 

28 

29 _import_complete = True 

30except ImportError: 

31 _import_complete = False 

32 

33if _import_complete: 

34 from packaging import version 

35 

36 from .calculator import SPARC 

37 from .io import read_sparc, register_ase_io_sparc, write_sparc 

38 

39 # If ase version less than 3.23, use manual register function 

40 # Otherwise use the new entry point 

41 if version.parse(ase.__version__) < version.parse("3.23"): 

42 register_ase_io_sparc() 

43 else: 

44 # register calculator class <experimental> 

45 from ase.calculators.calculator import register_calculator_class 

46 

47 register_calculator_class("sparc", SPARC) 

48else: 

49 # If importing is not complete, any code trying to directly import 

50 # the following attributes will raise ImportError 

51 read_sparc = _missing_deps_func 

52 write_sparc = _missing_deps_func 

53 SPARC = SPARCMissingDeps