Coverage for eminus/io/__init__.py: 80.95%

42 statements  

« prev     ^ index     » next       coverage.py v7.6.4, created at 2024-11-01 11:47 +0000

1# SPDX-FileCopyrightText: 2021 The eminus developers 

2# SPDX-License-Identifier: Apache-2.0 

3"""File input and output functionalities.""" 

4 

5from .cube import read_cube, write_cube 

6from .gth import read_gth 

7from .json import read_json, write_json 

8from .pdb import create_pdb_str, write_pdb 

9from .poscar import read_poscar, write_poscar 

10from .traj import read_traj, write_traj 

11from .xyz import read_xyz, write_xyz 

12 

13__all__ = [ 

14 "create_pdb_str", 

15 "read", 

16 "read_cube", 

17 "read_gth", 

18 "read_json", 

19 "read_poscar", 

20 "read_traj", 

21 "read_xyz", 

22 "write", 

23 "write_cube", 

24 "write_json", 

25 "write_pdb", 

26 "write_poscar", 

27 "write_traj", 

28 "write_xyz", 

29] 

30 

31 

32def read(filename, *args, **kwargs): 

33 """Unified file reader function. 

34 

35 Args: 

36 filename: Input file path/name. 

37 *args: Pass-through arguments. 

38 

39 Keyword Args: 

40 **kwargs: Pass-through keyword arguments. 

41 

42 Returns: 

43 Read file information. 

44 """ 

45 if filename.endswith(".json"): 

46 return read_json(filename, *args, **kwargs) 

47 if filename.endswith(".hdf5"): 

48 from ..extras import read_hdf5 

49 

50 return read_hdf5(filename, *args, **kwargs) 

51 if filename.endswith(".xyz"): 

52 return read_xyz(filename, *args, **kwargs) 

53 if "POSCAR" in filename: 

54 return read_poscar(filename, *args, **kwargs) 

55 if filename.endswith((".trj", ".traj")): 

56 return read_traj(filename, *args, **kwargs) 

57 if filename.endswith((".cub", ".cube")): 

58 return read_cube(filename, *args, **kwargs) 

59 msg = "File ending not recognized." 

60 raise NotImplementedError(msg) 

61 

62 

63def write(obj, filename, *args, **kwargs): # noqa: PLR0911 

64 """Unified file writer function. 

65 

66 Args: 

67 obj: Class object. 

68 filename: Input file path/name. 

69 *args: Pass-through arguments. 

70 

71 Keyword Args: 

72 **kwargs: Pass-through keyword arguments. 

73 

74 Returns: 

75 None. 

76 """ 

77 if filename.endswith(".json"): 

78 return write_json(obj, filename, *args, **kwargs) 

79 if filename.endswith(".hdf5"): 

80 from ..extras import write_hdf5 

81 

82 return write_hdf5(obj, filename, *args, **kwargs) 

83 if filename.endswith(".xyz"): 

84 return write_xyz(obj, filename, *args, **kwargs) 

85 if "POSCAR" in filename: 

86 return write_poscar(obj, filename, *args, **kwargs) 

87 if filename.endswith((".trj", ".traj")): 

88 return write_traj(obj, filename, *args, **kwargs) 

89 if filename.endswith((".cub", ".cube")): 

90 return write_cube(obj, filename, *args, **kwargs) 

91 if filename.endswith(".pdb"): 

92 return write_pdb(obj, filename, *args, **kwargs) 

93 msg = "File ending not recognized." 

94 raise NotImplementedError(msg)