Skip to content

Commit 6e7c091

Browse files
committed
Add APPEND_FILE parameter
1 parent 59c437e commit 6e7c091

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

paths_cli/parameters.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import click
2+
import os
23
# import openpathsampling as paths
34

45
class AbstractParameter(object):
@@ -40,8 +41,17 @@ def __init__(self, param, mode):
4041
super(StorageLoader, self).__init__(param)
4142
self.mode = mode
4243

44+
def workaround(self, name):
45+
# this is messed up... for some reason, storage doesn't create a new
46+
# file in append mode. That may be a bug
47+
import openpathsampling as paths
48+
if self.mode == 'a' and not os.path.exists(name):
49+
st = paths.Storage(name, mode='w')
50+
st.close()
51+
4352
def get(self, name):
4453
import openpathsampling as paths
54+
self.workaround(name)
4555
return paths.Storage(name, mode=self.mode)
4656

4757

@@ -194,5 +204,12 @@ def init_snap_fallback(parameter, storage, name):
194204
mode='w'
195205
)
196206

207+
APPEND_FILE = StorageLoader(
208+
param=Option('-a', '--append-file',
209+
type=click.Path(writable=True, readable=True),
210+
help="file to append to"),
211+
mode='a'
212+
)
213+
197214
N_STEPS_MC = click.option('-n', '--nsteps', type=int,
198215
help="number of Monte Carlo trials to run")

paths_cli/tests/test_parameters.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,3 +319,23 @@ def test_OUTPUT_FILE():
319319
os.remove(filename)
320320
os.rmdir(tempdir)
321321

322+
def test_APPEND_FILE():
323+
tempdir = tempfile.mkdtemp()
324+
filename = os.path.join(tempdir, "test_append_file.nc")
325+
assert not os.path.exists(filename)
326+
storage = APPEND_FILE.get(filename)
327+
print(storage)
328+
assert os.path.exists(filename)
329+
traj = make_1d_traj([0.0, 1.0])
330+
storage.tags['first_save'] = traj[0]
331+
storage.close()
332+
storage = APPEND_FILE.get(filename)
333+
assert storage.tags['first_save'] == traj[0]
334+
storage.tags['second_save'] = traj[1]
335+
storage.close()
336+
storage = APPEND_FILE.get(filename)
337+
assert len(storage.tags) == 2
338+
storage.close()
339+
os.remove(filename)
340+
os.rmdir(tempdir)
341+

0 commit comments

Comments
 (0)