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
« 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
8from ase.calculators.names import builtin, names, templates
10ASE_CONFIG_FILE = Path.home() / '.config/ase/config.ini'
13class ASEEnvDeprecationWarning(DeprecationWarning):
14 def __init__(self, message):
15 self.message = message
18class Config(Mapping):
19 def __init__(self):
20 def argv_converter(argv):
21 return shlex.split(argv)
23 self.parser = configparser.ConfigParser(
24 converters={'argv': argv_converter},
25 interpolation=configparser.ExtendedInterpolation(),
26 )
27 self.paths = []
29 def _env(self):
30 if self.parser.has_section('environment'):
31 return self.parser['environment']
32 else:
33 return {}
35 def __iter__(self):
36 yield from self._env()
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.
45 env = self._env()
46 try:
47 return env[item]
48 except KeyError:
49 pass
51 value = os.environ[item]
52 warnings.warn(
53 f'Loaded {item} from environment. Please use configfile.',
54 ASEEnvDeprecationWarning,
55 )
57 return value
59 def __len__(self):
60 return len(self._env())
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
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()
102 fullname = name
103 if version is not None:
104 fullname += f'--{version}'
106 def tickmark(thing):
107 return '[ ]' if thing is None else '[x]'
109 msg = ' {configured} {installed} {fullname}'.format(
110 configured=tickmark(codeconfig),
111 installed=tickmark(version),
112 fullname=fullname,
113 )
114 print(msg)
116 def print_header(self):
117 print('Configuration')
118 print('-------------')
119 print()
120 if not self.paths:
121 print('No configuration loaded.')
123 for path in self.paths:
124 print(f'Loaded: {path}')
126 def as_dict(self):
127 return {key: dict(val) for key, val in self.parser.items()}
129 def _read_paths(self, paths):
130 self.paths += self.parser.read(paths)
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(':')]
144 cfg = cls()
145 cfg._read_paths(paths)
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
154cfg = Config.read()