11import click
2+ import os
23# import openpathsampling as paths
34
45class AbstractParameter (object ):
@@ -14,6 +15,8 @@ def clicked(self, required=False):
1415# we'll use tests of the -h option in the .travis.yml to ensure that the
1516# .clicked methods work
1617
18+ HELP_MULTIPLE = "; may be used more than once"
19+
1720class Option (AbstractParameter ):
1821 def clicked (self , required = False ): # no-cov
1922 return click .option (* self .args , ** self .kwargs , required = required )
@@ -40,18 +43,38 @@ def __init__(self, param, mode):
4043 super (StorageLoader , self ).__init__ (param )
4144 self .mode = mode
4245
46+ def _workaround (self , name ):
47+ # this is messed up... for some reason, storage doesn't create a new
48+ # file in append mode. That may be a bug
49+ import openpathsampling as paths
50+ if self .mode == 'a' and not os .path .exists (name ):
51+ st = paths .Storage (name , mode = 'w' )
52+ st .close ()
53+
4354 def get (self , name ):
4455 import openpathsampling as paths
56+ self ._workaround (name )
4557 return paths .Storage (name , mode = self .mode )
4658
4759
4860class OPSStorageLoadNames (AbstractLoader ):
61+ """Simple loader that expects its input to be a name or index.
62+ """
4963 def __init__ (self , param , store ):
5064 super (OPSStorageLoadNames , self ).__init__ (param )
5165 self .store = store
5266
5367 def get (self , storage , names ):
54- return [getattr (storage , self .store )[name ] for name in names ]
68+ int_corrected = []
69+ for name in names :
70+ try :
71+ name = int (name )
72+ except ValueError :
73+ pass
74+ int_corrected .append (name )
75+
76+ return [getattr (storage , self .store )[name ]
77+ for name in int_corrected ]
5578
5679
5780class OPSStorageLoadSingle (AbstractLoader ):
@@ -171,16 +194,47 @@ def init_snap_fallback(parameter, storage, name):
171194
172195CVS = OPSStorageLoadNames (
173196 param = Option ('--cv' , type = str , multiple = True ,
174- help = 'name of CV; may select more than once' ),
197+ help = 'name of CV' + HELP_MULTIPLE ),
175198 store = 'cvs'
176199)
177200
201+ MULTI_VOLUME = OPSStorageLoadNames (
202+ param = Option ('--volume' , type = str , multiple = True ,
203+ help = 'name or index of volume' + HELP_MULTIPLE ),
204+ store = 'volumes'
205+ )
206+
207+ MULTI_ENGINE = OPSStorageLoadNames (
208+ param = Option ('--engine' , type = str , multiple = True ,
209+ help = 'name or index of engine' + HELP_MULTIPLE ),
210+ store = 'engines'
211+ )
212+
213+
178214STATES = OPSStorageLoadNames (
179- param = Option ('-s' , '--state' , multiple = True ,
180- help = 'name of state; may select more than once' ),
215+ param = Option ('-s' , '--state' , type = str , multiple = True ,
216+ help = 'name of state' + HELP_MULTIPLE ),
181217 store = 'volumes'
182218)
183219
220+ MULTI_TAG = OPSStorageLoadNames (
221+ param = Option ('--tag' , type = str , multiple = True ,
222+ help = 'tag for object' + HELP_MULTIPLE ),
223+ store = 'tags'
224+ )
225+
226+ MULTI_NETWORK = OPSStorageLoadNames (
227+ param = Option ('--network' , type = str , multiple = True ,
228+ help = 'name or index of network' + HELP_MULTIPLE ),
229+ store = 'networks'
230+ )
231+
232+ MULTI_SCHEME = OPSStorageLoadNames (
233+ param = Option ('--scheme' , type = str , multiple = True ,
234+ help = 'name or index of move scheme' + HELP_MULTIPLE ),
235+ store = 'schemes'
236+ )
237+
184238INPUT_FILE = StorageLoader (
185239 param = Argument ('input_file' ,
186240 type = click .Path (exists = True , readable = True )),
@@ -194,5 +248,14 @@ def init_snap_fallback(parameter, storage, name):
194248 mode = 'w'
195249)
196250
251+ APPEND_FILE = StorageLoader (
252+ param = Option ('-a' , '--append-file' ,
253+ type = click .Path (writable = True , readable = True ),
254+ help = "file to append to" ),
255+ mode = 'a'
256+ )
257+
197258N_STEPS_MC = click .option ('-n' , '--nsteps' , type = int ,
198259 help = "number of Monte Carlo trials to run" )
260+
261+ MULTI_CV = CVS
0 commit comments