Coverage for ase/cli/db.py: 100.00%

38 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 

4 

5 

6class CLICommand: 

7 """Manipulate and query ASE database. 

8 

9 Query is a comma-separated list of 

10 selections where each selection is of the type "ID", "key" or 

11 "key=value". Instead of "=", one can also use "<", "<=", ">=", ">" 

12 and "!=" (these must be protected from the shell by using quotes). 

13 Special keys: 

14 

15 * id 

16 * user 

17 * calculator 

18 * age 

19 * natoms 

20 * energy 

21 * magmom 

22 * charge 

23 

24 Chemical symbols can also be used to select number of 

25 specific atomic species (H, He, Li, ...). Selection examples: 

26 

27 calculator=nwchem 

28 age<1d 

29 natoms=1 

30 user=alice 

31 2.2<bandgap<4.1 

32 Cu>=10 

33 

34 See also: https://docs.ase-lib.org/ase/db/db.html. 

35 """ 

36 

37 @staticmethod 

38 def add_arguments(parser): 

39 add = parser.add_argument 

40 add('database', help='SQLite3 file, JSON file or postgres URL.') 

41 add('query', nargs='*', help='Query string.') 

42 add('-v', '--verbose', action='store_true', help='More output.') 

43 add('-q', '--quiet', action='store_true', help='Less output.') 

44 add( 

45 '-n', 

46 '--count', 

47 action='store_true', 

48 help='Count number of selected rows.', 

49 ) 

50 add( 

51 '-l', 

52 '--long', 

53 action='store_true', 

54 help='Long description of selected row', 

55 ) 

56 add( 

57 '-i', 

58 '--insert-into', 

59 metavar='db-name', 

60 help='Insert selected rows into another database.', 

61 ) 

62 add( 

63 '-a', 

64 '--add-from-file', 

65 metavar='filename', 

66 help='Add configuration(s) from file. ' 

67 'If the file contains more than one configuration then you can ' 

68 'use the syntax filename@: to add all of them. Default is to ' 

69 'only add the last.', 

70 ) 

71 add( 

72 '-k', 

73 '--add-key-value-pairs', 

74 metavar='key1=val1,key2=val2,...', 

75 help='Add key-value pairs to selected rows. Values must ' 

76 'be numbers or strings and keys must follow the same rules as ' 

77 'keywords.', 

78 ) 

79 add( 

80 '-L', 

81 '--limit', 

82 type=int, 

83 default=-1, 

84 metavar='N', 

85 help='Show only first N rows. Use --limit=0 ' 

86 'to show all. Default is 20 rows when listing rows and no ' 

87 'limit when --insert-into is used.', 

88 ) 

89 add( 

90 '--offset', 

91 type=int, 

92 default=0, 

93 metavar='N', 

94 help='Skip first N rows. By default, no rows are skipped', 

95 ) 

96 add('--delete', action='store_true', help='Delete selected rows.') 

97 add( 

98 '--delete-keys', 

99 metavar='key1,key2,...', 

100 help='Delete keys for selected rows.', 

101 ) 

102 add('-y', '--yes', action='store_true', help='Say yes.') 

103 add('--explain', action='store_true', help='Explain query plan.') 

104 add( 

105 '-c', 

106 '--columns', 

107 metavar='col1,col2,...', 

108 help='Specify columns to show. Precede the column specification ' 

109 'with a "+" in order to add columns to the default set of ' 

110 'columns. Precede by a "-" to remove columns. Use "++" for all.', 

111 ) 

112 add( 

113 '-s', 

114 '--sort', 

115 metavar='column', 

116 default='id', 

117 help='Sort rows using "column". Use "column-" for a descending ' 

118 'sort. Default is to sort after id.', 

119 ) 

120 add( 

121 '--cut', 

122 type=int, 

123 default=35, 

124 help='Cut keywords and key-value ' 

125 'columns after CUT characters. Use --cut=0 to disable cutting. ' 

126 'Default is 35 characters', 

127 ) 

128 add( 

129 '-p', 

130 '--plot', 

131 metavar='x,y1,y2,...', 

132 help='Example: "-p x,y": plot y row against x row. Use ' 

133 '"-p a:x,y" to make a plot for each value of a.', 

134 ) 

135 add( 

136 '--csv', 

137 action='store_true', 

138 help='Write comma-separated-values file.', 

139 ) 

140 add( 

141 '-w', 

142 '--open-web-browser', 

143 action='store_true', 

144 help='Open results in web-browser.', 

145 ) 

146 add('--no-lock-file', action='store_true', help="Don't use lock-files") 

147 add( 

148 '--analyse', 

149 action='store_true', 

150 help='Gathers statistics about tables and indices to help make ' 

151 'better query planning choices.', 

152 ) 

153 add( 

154 '-j', 

155 '--json', 

156 action='store_true', 

157 help='Write json representation of selected row.', 

158 ) 

159 add( 

160 '-m', 

161 '--show-metadata', 

162 action='store_true', 

163 help='Show metadata as json.', 

164 ) 

165 add( 

166 '--set-metadata', 

167 metavar='something.json', 

168 help='Set metadata from a json file.', 

169 ) 

170 add( 

171 '--strip-data', 

172 action='store_true', 

173 help='Strip data when using --insert-into.', 

174 ) 

175 add( 

176 '--progress-bar', 

177 action='store_true', 

178 help='Show a progress bar when using --insert-into.', 

179 ) 

180 add('--show-keys', action='store_true', help='Show all keys.') 

181 add( 

182 '--show-values', 

183 metavar='key1,key2,...', 

184 help='Show values for key(s).', 

185 ) 

186 

187 @staticmethod 

188 def run(args): 

189 from ase.db.cli import main 

190 

191 main(args)