Coverage for /builds/ase/ase/ase/cli/complete.py: 16.33%

49 statements  

« prev     ^ index     » next       coverage.py v7.5.3, created at 2025-08-02 00:12 +0000

1#!/usr/bin/env python3 

2# fmt: off 

3 

4"""Bash completion for ase. 

5 

6Put this in your .bashrc:: 

7 

8 complete -o default -C /path/to/ase/cli/complete.py ase 

9 

10or run:: 

11 

12 $ ase completion 

13 

14""" 

15 

16import os 

17import sys 

18from glob import glob 

19 

20 

21def match(word, *suffixes): 

22 return [w for w in glob(word + '*') 

23 if any(w.endswith(suffix) for suffix in suffixes)] 

24 

25 

26# Beginning of computer generated data: 

27commands = { 

28 'band-structure': 

29 ['-o', '--output', '-r', '--range'], 

30 'build': 

31 ['-M', '--magnetic-moment', '--modify', '-V', '--vacuum', '-v', 

32 '--vacuum0', '--unit-cell', '--bond-length', '-x', 

33 '--crystal-structure', '-a', '--lattice-constant', 

34 '--orthorhombic', '--cubic', '-r', '--repeat', '-g', 

35 '--gui', '--periodic'], 

36 'completion': 

37 [], 

38 'convert': 

39 ['-v', '--verbose', '-i', '--input-format', '-o', 

40 '--output-format', '-f', '--force', '-n', 

41 '--image-number', '-e', '--exec-code', '-E', 

42 '--exec-file', '-a', '--arrays', '-I', '--info', '-s', 

43 '--split-output', '--read-args', '--write-args'], 

44 'db': 

45 ['-v', '--verbose', '-q', '--quiet', '-n', '--count', '-l', 

46 '--long', '-i', '--insert-into', '-a', 

47 '--add-from-file', '-k', '--add-key-value-pairs', '-L', 

48 '--limit', '--offset', '--delete', '--delete-keys', 

49 '-y', '--yes', '--explain', '-c', '--columns', '-s', 

50 '--sort', '--cut', '-p', '--plot', '--csv', '-w', 

51 '--open-web-browser', '--no-lock-file', '--analyse', 

52 '-j', '--json', '-m', '--show-metadata', 

53 '--set-metadata', '--strip-data', '--progress-bar', 

54 '--show-keys', '--show-values'], 

55 'diff': 

56 ['-r', '--rank-order', '-c', '--calculator-outputs', 

57 '--max-lines', '-t', '--template', '--template-help', 

58 '-s', '--summary-functions', '--log-file', '--as-csv', 

59 '--precision'], 

60 'dimensionality': 

61 ['--display-all', '--no-merge'], 

62 'eos': 

63 ['-p', '--plot', '-t', '--type'], 

64 'exec': 

65 ['-e', '--exec-code', '-E', '--exec-file', '-i', '--input-format', 

66 '-n', '--image-number', '--read-args'], 

67 'find': 

68 ['-v', '--verbose', '-l', '--long', '-i', '--include', '-x', 

69 '--exclude'], 

70 'gui': 

71 ['-n', '--image-number', '-r', '--repeat', '-R', '--rotations', 

72 '-o', '--output', '-g', '--graph', '-t', '--terminal', 

73 '--interpolate', '-b', '--bonds', '-s', '--scale'], 

74 'info': 

75 ['--files', '-v', '--verbose', '--formats', '--calculators'], 

76 'nebplot': 

77 ['--nimages', '--share-x', '--share-y'], 

78 'reciprocal': 

79 [], 

80 'run': 

81 ['-p', '--parameters', '-t', '--tag', '--properties', '-f', 

82 '--maximum-force', '--constrain-tags', '-s', 

83 '--maximum-stress', '-E', '--equation-of-state', 

84 '--eos-type', '-o', '--output', '--modify', '--after'], 

85 'test': 

86 ['-c', '--calculators', '--help-calculators', '--list', 

87 '--list-calculators', '-j', '--jobs', '-v', '--verbose', 

88 '--strict', '--fast', '--coverage', '--nogui', 

89 '--pytest'], 

90 'ulm': 

91 ['-n', '--index', '-d', '--delete', '-v', '--verbose']} 

92# End of computer generated data 

93 

94 

95def complete(word, previous, line, point): 

96 for w in line[:point - len(word)].strip().split()[1:]: 

97 if w[0].isalpha(): 

98 if w in commands: 

99 command = w 

100 break 

101 else: 

102 if word[:1] == '-': 

103 return ['-h', '--help', '--version'] 

104 return list(commands.keys()) + ['-h', '--help', '--verbose'] 

105 

106 if word[:1] == '-': 

107 return commands[command] 

108 

109 words = [] 

110 

111 if command == 'db': 

112 if previous == 'db': 

113 words = match(word, '.db', '.json') 

114 

115 elif command == 'run': 

116 if previous == 'run': 

117 from ase.calculators.calculator import names as words 

118 

119 elif command == 'build': 

120 if previous in ['-x', '--crystal-structure']: 

121 words = ['sc', 'fcc', 'bcc', 'hcp', 'diamond', 'zincblende', 

122 'rocksalt', 'cesiumchloride', 'fluorite', 'wurtzite'] 

123 

124 elif command == 'test': 

125 if previous in ['-c', '--calculators']: 

126 from ase.calculators.calculator import names as words 

127 elif not word.startswith('-'): 

128 from ase.test.testsuite import all_test_modules_and_groups 

129 words = [] 

130 for path in all_test_modules_and_groups(): 

131 path = str(path) 

132 if not path.endswith('.py'): 

133 path += '/' 

134 words.append(path) 

135 

136 return words 

137 

138 

139def main(): 

140 word, previous = sys.argv[2:] 

141 line = os.environ['COMP_LINE'] 

142 point = int(os.environ['COMP_POINT']) 

143 words = complete(word, previous, line, point) 

144 for w in words: 

145 if w.startswith(word): 

146 print(w) 

147 

148 

149if __name__ == '__main__': 

150 main()