Skip to content
Snippets Groups Projects
Verified Commit 19b40540 authored by James Graham's avatar James Graham
Browse files

fix: backup forcefield directories instead of overwrite

parent a9fdae85
Branches
No related tags found
No related merge requests found
...@@ -8,7 +8,7 @@ import shutil ...@@ -8,7 +8,7 @@ import shutil
import typing import typing
from .parsers import CFG 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] PathLike = typing.Union[pathlib.Path, str]
...@@ -32,6 +32,7 @@ class ForceField: ...@@ -32,6 +32,7 @@ class ForceField:
:param str name: Forcefield name to open/create :param str name: Forcefield name to open/create
""" """
self.directory = pathlib.Path(dir_path).joinpath(f'ff{name}.ff') self.directory = pathlib.Path(dir_path).joinpath(f'ff{name}.ff')
backup_file(self.directory)
self.directory.mkdir(parents=True, exist_ok=True) self.directory.mkdir(parents=True, exist_ok=True)
with open(self.directory.joinpath('forcefield.itp'), 'w') as itp: with open(self.directory.joinpath('forcefield.itp'), 'w') as itp:
......
import unittest
import os
import collections import collections
import pathlib
import tempfile
import unittest
from pycgtool.forcefield import ForceField from pycgtool.forcefield import ForceField
...@@ -57,13 +58,18 @@ class ForceFieldTest(unittest.TestCase): ...@@ -57,13 +58,18 @@ class ForceFieldTest(unittest.TestCase):
self.bondset = DummyBondSet(self.bonds, "Dummy") self.bondset = DummyBondSet(self.bonds, "Dummy")
def test_create(self): def test_create(self):
name = "test" with tempfile.TemporaryDirectory() as t:
dirname = "fftest.ff" 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) # Makes a backup of the existing ff and replaces it
self.assertTrue(os.path.exists(dirname)) ForceField(name, dir_path=tmp_dir)
self.assertTrue(os.path.isdir(dirname))
ForceField(name)
def test_bond_section(self): def test_bond_section(self):
expected = [ expected = [
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment