Coverage for ase/cli/complete.py: 14.89%

47 statements  

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

1#!/usr/bin/env python3 

2 

3"""Bash completion for ase. 

4 

5Put this in your .bashrc:: 

6 

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

8 

9or run:: 

10 

11 $ ase completion 

12 

13""" 

14 

15import os 

16import sys 

17from glob import glob 

18 

19 

20def match(word, *suffixes): 

21 return [ 

22 w 

23 for w in glob(word + '*') 

24 if any(w.endswith(suffix) for suffix in suffixes) 

25 ] 

26 

27 

28# Beginning of computer generated data: 

29commands = { 

30 'band-structure': [ 

31 '-o', 

32 '--output', 

33 '-r', 

34 '--range', 

35 ], 

36 'build': [ 

37 '-M', 

38 '--magnetic-moment', 

39 '--modify', 

40 '-V', 

41 '--vacuum', 

42 '-v', 

43 '--vacuum0', 

44 '--unit-cell', 

45 '--bond-length', 

46 '-x', 

47 '--crystal-structure', 

48 '-a', 

49 '--lattice-constant', 

50 '--orthorhombic', 

51 '--cubic', 

52 '-r', 

53 '--repeat', 

54 '-g', 

55 '--gui', 

56 '--periodic', 

57 ], 

58 'completion': [], 

59 'convert': [ 

60 '-v', 

61 '--verbose', 

62 '-i', 

63 '--input-format', 

64 '-o', 

65 '--output-format', 

66 '-f', 

67 '--force', 

68 '-n', 

69 '--image-number', 

70 '-e', 

71 '--exec-code', 

72 '-E', 

73 '--exec-file', 

74 '-a', 

75 '--arrays', 

76 '-I', 

77 '--info', 

78 '-s', 

79 '--split-output', 

80 '--read-args', 

81 '--write-args', 

82 ], 

83 'db': [ 

84 '-v', 

85 '--verbose', 

86 '-q', 

87 '--quiet', 

88 '-n', 

89 '--count', 

90 '-l', 

91 '--long', 

92 '-i', 

93 '--insert-into', 

94 '-a', 

95 '--add-from-file', 

96 '-k', 

97 '--add-key-value-pairs', 

98 '-L', 

99 '--limit', 

100 '--offset', 

101 '--delete', 

102 '--delete-keys', 

103 '-y', 

104 '--yes', 

105 '--explain', 

106 '-c', 

107 '--columns', 

108 '-s', 

109 '--sort', 

110 '--cut', 

111 '-p', 

112 '--plot', 

113 '--csv', 

114 '-w', 

115 '--open-web-browser', 

116 '--no-lock-file', 

117 '--analyse', 

118 '-j', 

119 '--json', 

120 '-m', 

121 '--show-metadata', 

122 '--set-metadata', 

123 '--strip-data', 

124 '--progress-bar', 

125 '--show-keys', 

126 '--show-values', 

127 ], 

128 'diff': [ 

129 '-r', 

130 '--rank-order', 

131 '-c', 

132 '--calculator-outputs', 

133 '--max-lines', 

134 '-t', 

135 '--template', 

136 '--template-help', 

137 '-s', 

138 '--summary-functions', 

139 '--log-file', 

140 '--as-csv', 

141 '--precision', 

142 ], 

143 'dimensionality': [ 

144 '--display-all', 

145 '--no-merge', 

146 ], 

147 'eos': [ 

148 '-p', 

149 '--plot', 

150 '-t', 

151 '--type', 

152 ], 

153 'exec': [ 

154 '-e', 

155 '--exec-code', 

156 '-E', 

157 '--exec-file', 

158 '-i', 

159 '--input-format', 

160 '-n', 

161 '--image-number', 

162 '--read-args', 

163 ], 

164 'find': [ 

165 '-v', 

166 '--verbose', 

167 '-l', 

168 '--long', 

169 '-i', 

170 '--include', 

171 '-x', 

172 '--exclude', 

173 ], 

174 'gui': [ 

175 '-n', 

176 '--image-number', 

177 '-r', 

178 '--repeat', 

179 '-R', 

180 '--rotations', 

181 '-o', 

182 '--output', 

183 '-g', 

184 '--graph', 

185 '-t', 

186 '--terminal', 

187 '--interpolate', 

188 '-b', 

189 '--bonds', 

190 '-s', 

191 '--scale', 

192 '--configfile', 

193 ], 

194 'info': [ 

195 '--files', 

196 '-v', 

197 '--verbose', 

198 '--formats', 

199 '--calculators', 

200 '--all-calculators', 

201 '--plugins', 

202 ], 

203 'nebplot': [ 

204 '--nimages', 

205 '--share-x', 

206 '--share-y', 

207 ], 

208 'parallel': [ 

209 '-m', 

210 '--module', 

211 '-c', 

212 '--cmd', 

213 ], 

214 'reciprocal': [], 

215 'run': [ 

216 '-p', 

217 '--parameters', 

218 '-t', 

219 '--tag', 

220 '--properties', 

221 '-f', 

222 '--maximum-force', 

223 '--constrain-tags', 

224 '-s', 

225 '--maximum-stress', 

226 '-E', 

227 '--equation-of-state', 

228 '--eos-type', 

229 '-o', 

230 '--output', 

231 '--modify', 

232 '--after', 

233 ], 

234 'test': [ 

235 '-c', 

236 '--calculators', 

237 '--help-calculators', 

238 '--list', 

239 '--list-calculators', 

240 '-j', 

241 '--jobs', 

242 '-v', 

243 '--verbose', 

244 '--strict', 

245 '--fast', 

246 '--coverage', 

247 '--nogui', 

248 '--pytest', 

249 ], 

250 'ulm': [ 

251 '-n', 

252 '--index', 

253 '-d', 

254 '--delete', 

255 '-v', 

256 '--verbose', 

257 ], 

258} 

259# End of computer generated data 

260 

261 

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

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

264 if w[0].isalpha(): 

265 if w in commands: 

266 command = w 

267 break 

268 else: 

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

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

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

272 

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

274 return commands[command] 

275 

276 words = [] 

277 

278 if command == 'db': 

279 if previous == 'db': 

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

281 

282 elif command == 'run': 

283 if previous == 'run': 

284 from ase.calculators.calculator import names as words 

285 

286 elif command == 'build': 

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

288 words = [ 

289 'sc', 

290 'fcc', 

291 'bcc', 

292 'hcp', 

293 'diamond', 

294 'zincblende', 

295 'rocksalt', 

296 'cesiumchloride', 

297 'fluorite', 

298 'wurtzite', 

299 ] 

300 

301 elif command == 'test': 

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

303 from ase.calculators.calculator import names as words 

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

305 from ase.test.testsuite import all_test_modules_and_groups 

306 

307 words = [] 

308 for path in all_test_modules_and_groups(): 

309 path = str(path) 

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

311 path += '/' 

312 words.append(path) 

313 

314 return words 

315 

316 

317def main(): 

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

319 line = os.environ['COMP_LINE'] 

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

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

322 for w in words: 

323 if w.startswith(word): 

324 print(w) 

325 

326 

327if __name__ == '__main__': 

328 main()