Skip to content

Commit

Permalink
fix: backup forcefield directories instead of overwrite
Browse files Browse the repository at this point in the history
  • Loading branch information
jag1g13 committed Oct 9, 2021
1 parent a9fdae8 commit 19b4054
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
3 changes: 2 additions & 1 deletion pycgtool/forcefield.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import typing

from .parsers import CFG
from .util import any_starts_with, file_write_lines
from .util import any_starts_with, backup_file, file_write_lines

PathLike = typing.Union[pathlib.Path, str]

Expand All @@ -32,6 +32,7 @@ def __init__(self, name: str, dir_path: PathLike = pathlib.Path('.')):
:param str name: Forcefield name to open/create
"""
self.directory = pathlib.Path(dir_path).joinpath(f'ff{name}.ff')
backup_file(self.directory)
self.directory.mkdir(parents=True, exist_ok=True)

with open(self.directory.joinpath('forcefield.itp'), 'w') as itp:
Expand Down
22 changes: 14 additions & 8 deletions tests/test_forcefield.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import unittest
import os
import collections
import pathlib
import tempfile
import unittest

from pycgtool.forcefield import ForceField

Expand Down Expand Up @@ -57,13 +58,18 @@ def setUp(self):
self.bondset = DummyBondSet(self.bonds, "Dummy")

def test_create(self):
name = "test"
dirname = "fftest.ff"
with tempfile.TemporaryDirectory() as t:
tmp_dir = pathlib.Path(t)

name = "test"
ff_dir = tmp_dir.joinpath('fftest.ff')

ForceField(name, dir_path=tmp_dir)
self.assertTrue(ff_dir.exists())
self.assertTrue(ff_dir.is_dir())

ForceField(name)
self.assertTrue(os.path.exists(dirname))
self.assertTrue(os.path.isdir(dirname))
ForceField(name)
# Makes a backup of the existing ff and replaces it
ForceField(name, dir_path=tmp_dir)

def test_bond_section(self):
expected = [
Expand Down

0 comments on commit 19b4054

Please sign in to comment.