Coverage for /builds/hweiske/ase/ase/cli/info.py: 86.11%

72 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2024-04-22 11:22 +0000

1# Note: 

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

3# import time during CLI execution 

4 

5 

6class CLICommand: 

7 """Print information about files or system. 

8 

9 Without any filename(s), informations about the ASE installation will be 

10 shown (Python version, library versions, ...). 

11 

12 With filename(s), the file format will be determined for each file. 

13 """ 

14 

15 @staticmethod 

16 def add_arguments(parser): 

17 parser.add_argument('filename', nargs='*', 

18 help='Name of file to determine format for.') 

19 parser.add_argument('-v', '--verbose', action='store_true', 

20 help='Show more information about files.') 

21 parser.add_argument('--formats', action='store_true', 

22 help='List file formats known to ASE.') 

23 parser.add_argument('--config', action='store_true', 

24 help='List configured calculators') 

25 parser.add_argument('--calculators', action='store_true', 

26 help='List all calculators known to ASE ' 

27 'and whether/how each is installed. Also, ' 

28 'attempt to determine version numbers by ' 

29 'running binaries or importing packages as ' 

30 'appropriate.') 

31 

32 @staticmethod 

33 def run(args): 

34 from ase.config import cfg 

35 from ase.io.bundletrajectory import print_bundletrajectory_info 

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

37 from ase.io.ulm import print_ulm_info 

38 if not args.filename: 

39 print_info() 

40 if args.formats: 

41 print() 

42 print_formats() 

43 if args.config: 

44 print() 

45 cfg.print_everything() 

46 if args.calculators: 

47 print() 

48 cfg.check_calculators() 

49 # print() 

50 # from ase.calculators.autodetect import (detect_calculators, 

51 # format_configs) 

52 # configs = detect_calculators() 

53 # print('Calculators:') 

54 # for message in format_configs(configs): 

55 # print(' {}'.format(message)) 

56 # print() 

57 # print('Available: {}'.format(','.join(sorted(configs)))) 

58 return 

59 

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

61 nfiles_not_found = 0 

62 for filename in args.filename: 

63 try: 

64 format = filetype(filename) 

65 except FileNotFoundError: 

66 format = '?' 

67 description = 'No such file' 

68 nfiles_not_found += 1 

69 except UnknownFileTypeError: 

70 format = '?' 

71 description = '?' 

72 else: 

73 if format in ioformats: 

74 description = ioformats[format].description 

75 else: 

76 description = '?' 

77 

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

79 description, format)) 

80 if args.verbose: 

81 if format == 'traj': 

82 print_ulm_info(filename) 

83 elif format == 'bundletrajectory': 

84 print_bundletrajectory_info(filename) 

85 

86 raise SystemExit(nfiles_not_found) 

87 

88 

89def print_info(): 

90 import platform 

91 import sys 

92 

93 from ase.dependencies import all_dependencies 

94 

95 versions = [('platform', platform.platform()), 

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

97 

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

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

100 

101 

102def print_formats(): 

103 from ase.io.formats import ioformats 

104 

105 print('Supported formats:') 

106 for fmtname in sorted(ioformats): 

107 fmt = ioformats[fmtname] 

108 

109 infos = [fmt.modes, 'single' if fmt.single else 'multi'] 

110 if fmt.isbinary: 

111 infos.append('binary') 

112 if fmt.encoding is not None: 

113 infos.append(fmt.encoding) 

114 infostring = '/'.join(infos) 

115 

116 moreinfo = [infostring] 

117 if fmt.extensions: 

118 moreinfo.append('ext={}'.format('|'.join(fmt.extensions))) 

119 if fmt.globs: 

120 moreinfo.append('glob={}'.format('|'.join(fmt.globs))) 

121 

122 print(' {} [{}]: {}'.format(fmt.name, 

123 ', '.join(moreinfo), 

124 fmt.description))