Coverage for ase/config.py: 60.87%

92 statements  

« prev     ^ index     » next       coverage.py v7.16.2, created at 2026-11-27 15:21 +0000

1import configparser 

2import os 

3import shlex 

4import warnings 

5from collections.abc import Mapping 

6from pathlib import Path 

7 

8from ase.calculators.names import builtin, names, templates 

9 

10ASE_CONFIG_FILE = Path.home() / '.config/ase/config.ini' 

11 

12 

13class ASEEnvDeprecationWarning(DeprecationWarning): 

14 def __init__(self, message): 

15 self.message = message 

16 

17 

18class Config(Mapping): 

19 def __init__(self): 

20 def argv_converter(argv): 

21 return shlex.split(argv) 

22 

23 self.parser = configparser.ConfigParser( 

24 converters={'argv': argv_converter}, 

25 interpolation=configparser.ExtendedInterpolation(), 

26 ) 

27 self.paths = [] 

28 

29 def _env(self): 

30 if self.parser.has_section('environment'): 

31 return self.parser['environment'] 

32 else: 

33 return {} 

34 

35 def __iter__(self): 

36 yield from self._env() 

37 

38 def __getitem__(self, item): 

39 # XXX We should replace the mapping behaviour with individual 

40 # methods to get from cfg or environment, or only from cfg. 

41 # 

42 # We cannot be a mapping very correctly without getting trouble 

43 # with mutable state needing synchronization with os.environ. 

44 

45 env = self._env() 

46 try: 

47 return env[item] 

48 except KeyError: 

49 pass 

50 

51 value = os.environ[item] 

52 warnings.warn( 

53 f'Loaded {item} from environment. Please use configfile.', 

54 ASEEnvDeprecationWarning, 

55 ) 

56 

57 return value 

58 

59 def __len__(self): 

60 return len(self._env()) 

61 

62 def check_calculators(self): 

63 print('Calculators') 

64 print('===========') 

65 print() 

66 print('Configured in ASE') 

67 print(' | Installed on machine') 

68 print(' | | Name & version') 

69 print(' | | |') 

70 for name in names: 

71 # configured = False 

72 # installed = False 

73 template = templates.get(name) 

74 # if template is None: 

75 # XXX no template for this calculator. 

76 # We need templates for all calculators somehow, 

77 # but we can probably generate those for old FileIOCalculators 

78 # automatically. 

79 # continue 

80 

81 fullname = name 

82 try: 

83 codeconfig = self[name] 

84 except KeyError: 

85 codeconfig = None 

86 version = None 

87 else: 

88 if template is None: 

89 # XXX we should not be executing this 

90 if codeconfig is not None and 'builtin' in codeconfig: 

91 # builtin calculators 

92 version = 'builtin' 

93 else: 

94 version = None 

95 else: 

96 profile = template.load_profile(codeconfig) 

97 # XXX should be made robust to failure here: 

98 with warnings.catch_warnings(): 

99 warnings.simplefilter('ignore') 

100 version = profile.version() 

101 

102 fullname = name 

103 if version is not None: 

104 fullname += f'--{version}' 

105 

106 def tickmark(thing): 

107 return '[ ]' if thing is None else '[x]' 

108 

109 msg = ' {configured} {installed} {fullname}'.format( 

110 configured=tickmark(codeconfig), 

111 installed=tickmark(version), 

112 fullname=fullname, 

113 ) 

114 print(msg) 

115 

116 def print_header(self): 

117 print('Configuration') 

118 print('-------------') 

119 print() 

120 if not self.paths: 

121 print('No configuration loaded.') 

122 

123 for path in self.paths: 

124 print(f'Loaded: {path}') 

125 

126 def as_dict(self): 

127 return {key: dict(val) for key, val in self.parser.items()} 

128 

129 def _read_paths(self, paths): 

130 self.paths += self.parser.read(paths) 

131 

132 @classmethod 

133 def read(cls, *configfiles): 

134 envpath = os.environ.get('ASE_CONFIG_PATH') 

135 if configfiles: 

136 paths = [f for f in configfiles] 

137 elif envpath is None: 

138 paths = [ 

139 ASE_CONFIG_FILE, 

140 ] 

141 else: 

142 paths = [Path(p) for p in envpath.split(':')] 

143 

144 cfg = cls() 

145 cfg._read_paths(paths) 

146 

147 # add sections for builtin calculators 

148 for name in builtin: 

149 cfg.parser.add_section(name) 

150 cfg.parser[name]['builtin'] = 'True' 

151 return cfg 

152 

153 

154cfg = Config.read()