Skip to content

Commit

Permalink
Some intermediate work of refactoring FileUtils and MDP
Browse files Browse the repository at this point in the history
  • Loading branch information
wehs7661 committed Apr 17, 2024
1 parent 8452bf4 commit 75ee6bb
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 56 deletions.
37 changes: 17 additions & 20 deletions ensemble_md/utils/gmx_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"""
The :code:`gmx_parser` module provides functions for parsing GROMACS files.
"""
import os
import re
import six
import logging
Expand Down Expand Up @@ -169,7 +170,7 @@ def parse_log(log_file):
return weights, counts, wl_delta, equil_time


class MDP(odict, utils.FileUtils):
class MDP(odict):
"""Class that represents a Gromacs mdp run input file.
Modified from `GromacsWrapper <https://github.com/Becksteinlab/GromacsWrapper>`_.
Copyright (c) 2009-2011 Oliver Beckstein <orbeckst@gmail.com>
Expand Down Expand Up @@ -201,7 +202,7 @@ class MDP(odict, utils.FileUtils):
re.VERBOSE,
)

def __init__(self, filename=None, autoconvert=True, **kwargs):
def __init__(self, filename, autoconvert=True, **kwargs):
"""Initialize mdp structure.
:Arguments:
Expand All @@ -218,22 +219,20 @@ def __init__(self, filename=None, autoconvert=True, **kwargs):
super(MDP, self).__init__(
**kwargs
) # can use kwargs to set dict! (but no sanity checks!)

self.autoconvert = autoconvert

if filename is not None:
self._init_filename(filename)
self.read(filename)

def __eq__(self, other):
"""
__eq__ inherited from utils.FileUtils needs to be overridden if new attributes (autoconvert in
this case) are assigned to the instance of the subclass (MDP in our case).
See `this post by LGTM <https://lgtm.com/rules/9990086/>`_ for more details.
"""
if not isinstance(other, MDP):
return False
return utils.FileUtils.__eq__(self, other) and self.autoconvert == other.autoconvert
self.real_filename = os.path.realpath(filename)
self.read(filename)


# def __eq__(self, other):
# """
# __eq__ inherited from utils.FileUtils needs to be overridden if new attributes (autoconvert in
# this case) are assigned to the instance of the subclass (MDP in our case).
# See `this post by LGTM <https://lgtm.com/rules/9990086/>`_ for more details.
# """
# if not isinstance(other, MDP):
# return False
# return utils.FileUtils.__eq__(self, other) and self.autoconvert == other.autoconvert

def _transform(self, value):
if self.autoconvert:
Expand All @@ -243,8 +242,6 @@ def _transform(self, value):

def read(self, filename=None):
"""Read and parse mdp file *filename*."""
self._init_filename(filename)

def BLANK(i):
return "B{0:04d}".format(i)

Expand Down Expand Up @@ -295,7 +292,7 @@ def write(self, filename=None, skipempty=False):
# The line 'if skipempty and (v == "" or v is None):' below could possibly incur FutureWarning
warnings.simplefilter(action='ignore', category=FutureWarning)

with open(self.filename(filename, ext="mdp"), "w") as mdp:
with open(filename, "w") as mdp:
for k, v in self.items():
if k[0] == "B": # blank line
mdp.write("\n")
Expand Down
39 changes: 3 additions & 36 deletions ensemble_md/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,44 +64,11 @@ def flush(self):
pass


"""
class FileUtils:
"""
A utility class for managing file names and extensions within file operations.
Attributes
----------
real_filename : str
The full path of the object for reading and writing I/O.
"""
default_extension = None

def _init_filename(self, filename=None, ext=None):
extension = ext or self.default_extension
filename = self.filename(filename, ext=extension, use_my_ext=True, set_default=True)
def _init_filename(self, filename=None):
self.real_filename = os.path.realpath(filename)

def filename(self, filename=None, ext=None, set_default=False, use_my_ext=False):
if filename is None:
if not hasattr(self, "_filename"):
self._filename = None # add attribute to class
if self._filename:
filename = self._filename
else:
raise ValueError("A file name is required because no default file name was defined.")
my_ext = None
else:
filename, my_ext = os.path.splitext(filename)
if set_default: # replaces existing default file name
self._filename = filename
if my_ext and use_my_ext:
ext = my_ext
if ext is not None:
if ext.startswith(os.extsep):
ext = ext[1:] # strip a dot to avoid annoying mistakes
if ext != "":
filename = filename + os.extsep + ext
return filename

"""

def run_gmx_cmd(arguments, prompt_input=None):
"""
Expand Down

0 comments on commit 75ee6bb

Please sign in to comment.