Coverage for ase/cli/info.py: 76.72%

116 statements  

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

1# Note: 

2# Try to avoid module level import statements here to reduce 

3# import time during CLI execution 

4from __future__ import annotations 

5 

6from typing import TYPE_CHECKING 

7 

8from ase.codes import codes, list_codes 

9 

10if TYPE_CHECKING: 

11 from ase._4.plugins.io import IOFormatPlugin 

12 

13 

14class CLICommand: 

15 """Print information about files or system. 

16 

17 Without arguments, show information about ASE installation 

18 and library versions of dependencies. 

19 """ 

20 

21 @staticmethod 

22 def add_arguments(parser): 

23 parser.add_argument( 

24 '--files', 

25 nargs='*', 

26 metavar='PATH', 

27 help='Print information about specified files.', 

28 ) 

29 parser.add_argument( 

30 '-v', 

31 '--verbose', 

32 action='store_true', 

33 help='Show additional information about files.', 

34 ) 

35 parser.add_argument( 

36 '--formats', 

37 action='store_true', 

38 help='List file formats known to ASE.', 

39 ) 

40 parser.add_argument( 

41 '--calculators', 

42 nargs='*', 

43 metavar='NAME', 

44 help=( 

45 'List specified calculators and their configuration, or all ' 

46 'configurable calculators known to ASE.' 

47 ), 

48 ) 

49 parser.add_argument( 

50 '--all-calculators', 

51 action='store_true', 

52 dest='all_calculators', 

53 help=( 

54 'List all calculators known to ASE, and their configuration ' 

55 'if appropriate.' 

56 ), 

57 ) 

58 parser.add_argument( 

59 '--plugins', 

60 action='store_true', 

61 help='List all installed plugin packages', 

62 ) 

63 

64 @staticmethod 

65 def run(args): 

66 names = [] 

67 

68 if args.all_calculators: 

69 names = [*codes] 

70 

71 elif args.calculators is not None: 

72 if args.calculators: 

73 names = args.calculators 

74 else: 

75 names = [ 

76 name 

77 for name, metadata in codes.items() 

78 if metadata.configurable 

79 ] 

80 

81 if names: 

82 list_codes(names) 

83 return 

84 

85 if args.files: 

86 print_file_info(args) 

87 return 

88 

89 print_info() 

90 if args.formats: 

91 print() 

92 print_formats() 

93 

94 if args.plugins: 

95 print_plugins() 

96 

97 

98def print_file_info(args): 

99 from ase.io.bundletrajectory import print_bundletrajectory_info 

100 from ase.io.formats import UnknownFileTypeError, filetype, ioformats 

101 from ase.io.ulm import print_ulm_info 

102 

103 n = max(len(filename) for filename in args.files) + 2 

104 nfiles_not_found = 0 

105 for filename in args.files: 

106 try: 

107 format = filetype(filename) 

108 except FileNotFoundError: 

109 format = '?' 

110 description = 'No such file' 

111 nfiles_not_found += 1 

112 except UnknownFileTypeError: 

113 format = '?' 

114 description = '?' 

115 else: 

116 if format in ioformats: 

117 description = ioformats[format].description 

118 else: 

119 description = '?' 

120 

121 print('{:{}}{} ({})'.format(filename + ':', n, description, format)) 

122 if args.verbose: 

123 if format == 'traj': 

124 print_ulm_info(filename) 

125 elif format == 'bundletrajectory': 

126 print_bundletrajectory_info(filename) 

127 

128 raise SystemExit(nfiles_not_found) 

129 

130 

131def print_info(): 

132 import platform 

133 import sys 

134 

135 from ase.dependencies import all_dependencies 

136 

137 versions = [ 

138 ('platform', platform.platform()), 

139 ('python-' + sys.version.split()[0], sys.executable), 

140 ] 

141 

142 for name, path in versions + all_dependencies(): 

143 print(f'{name:24} {path}') 

144 

145 

146class IOFormatInfo: 

147 """Provide methods for read-only info display of IOFormatPlugin""" 

148 

149 def __init__(self, io_plugin: IOFormatPlugin): 

150 self._fmt = io_plugin 

151 

152 def get_name(self) -> str: 

153 return self._fmt.name 

154 

155 def get_description(self) -> str: 

156 return self._fmt.description 

157 

158 def rw_modes(self) -> str: 

159 "Get rw mode '', 'r', 'w' or 'rw'" 

160 return ( 

161 f'{"r" if self._fmt.can_read else ""}' 

162 f'{"w" if self._fmt.can_write else ""}' 

163 ) 

164 

165 def single_or_multi(self) -> str: 

166 return 'single' if self._fmt.code[0] == '1' else 'multi' 

167 

168 def is_binary(self) -> bool: 

169 return self._fmt.code[1] == 'B' 

170 

171 def encoding(self) -> str | None: 

172 return self._fmt.encoding 

173 

174 def __str__(self) -> str: 

175 rw_info = [self.rw_modes(), self.single_or_multi()] 

176 

177 if self.is_binary(): 

178 rw_info.append('binary') 

179 if (encoding := self.encoding()) is not None: 

180 rw_info.append(encoding) 

181 

182 info = ['/'.join(rw_info)] 

183 

184 if self._fmt.extensions: 

185 info.append('ext={}'.format('|'.join(self._fmt.extensions))) 

186 if self._fmt.globs: 

187 info.append('glob={}'.format('|'.join(self._fmt.globs))) 

188 

189 return ' {} [{}]: {}'.format( 

190 self.get_name(), ', '.join(info), self.get_description() 

191 ) 

192 

193 

194def print_formats(): 

195 from operator import attrgetter 

196 

197 from ase._4.plugins import plugins 

198 

199 print('Supported formats:') 

200 for fmt in sorted(plugins.io_formats, key=attrgetter('name')): 

201 print(IOFormatInfo(fmt)) 

202 

203 

204def _underlined(s: str, style='-') -> str: 

205 return f'{s}\n{style * len(s)}' 

206 

207 

208def print_plugins(): 

209 from ase._4.plugins import plugins 

210 

211 for package_name, package_plugins in plugins.by_package.items(): 

212 plugin = next(iter(package_plugins)) 

213 package_data = plugin.package_data 

214 

215 if package_data: 

216 print('\n') 

217 title = ( 

218 f'{package_name} ' 

219 f'{package_data.get("version")} — ' 

220 f'{package_data.get("Summary")}' 

221 ) 

222 print(_underlined(title, style='=')) 

223 

224 print('\nProvides:') 

225 for plugin_type, plugin_collection in package_plugins.by_type.items(): 

226 if not plugin_collection: 

227 continue 

228 print(f'\n{_underlined(plugin_type.__name__)}') 

229 print(', '.join(plugin.name for plugin in plugin_collection))