# Path: paratemp/tools.py
# class _BlankStream(object):
#     """
#     A class for use when not actually wanting to write to a file.
#     """
# 
#     def write(self, string):
#         pass
# 
#     def fileno(self):
#         return 0  # Not sure if this works. Maybe None would be better
# 
#     def flush(self):
#         pass
# 
# Path: paratemp/exceptions.py
# class InputError(Exception):
#     """Exception raised for errors in the input.
# 
#     Attributes:
#         expr -- input expression in which the error occurred
#         msg  -- explanation of the error
#     """
# 
#     def __init__(self, expr, msg):
#         self.expr = expr
#         self.msg = msg
# 
#     def __str__(self):
#         output = 'Incorrect input "{}". {}'.format(self.expr, self.msg)
#         return repr(output)
# 
# Path: paratemp/tools.py
# @contextmanager
# def cd(new_dir):
#     prev_dir = os.getcwd()
#     os.chdir(os.path.expanduser(new_dir))
#     try:
#         yield
#     finally:
#         os.chdir(prev_dir)
# 
# def copy_no_overwrite(src, dst, silent=False):
#     exists = False
#     if os.path.isdir(src):
#         raise OSError(errno.EISDIR, "Is a directory: {}".format(src))
#     elif os.path.isdir(dst):
#         if os.path.isfile(os.path.join(dst, os.path.basename(src))):
#             exists = True
#     elif os.path.isfile(dst):
#         exists = True
#     if exists:
#         if silent:
#             return dst
#         else:
#             raise OSError(errno.EEXIST, "File already exists", dst)
#     else:
#         return shutil.copy(src, dst)


import errno
import glob
import os
import pathlib
import py
import re
import subprocess
import warnings
from typing import Callable, Iterable, Match
from paratemp.tools import _BlankStream
from paratemp.exceptions import InputError
from paratemp.tools import cd, copy_no_overwrite
    from glob import glob
    from gromacs.tools import Trjconv
    :param str n_top: Default: None. Name (and path) of the topology file. If
        None, folder will be used, but this argument takes priority.
    :param str folder: Default: None. If n_top is not provided, this is the
        folder that will be searched for a file ending in '.top' to be used.
    :param int s_count: Default: 0. The count of solvent molecules to be set
        in the topology file.
    :param str res_name: Default: 'DCM'. Name of the residue to look for in
        the topology file. This is case insensitive (this and the line will
        be made lower case when searching for it).
    :param str prepend: Default: 'unequal-'. The string to prepend to the
        topology file name when copying it (to keep a copy of the original).
    :param bool verbose: Default: True. If True, messages will be printed if
        no changes need to be made or after the changes have successfully been
        made.
    :return: None
    :raises RuntimeError: This is raised if the it is unable to find the
        line with the solvent count. This could also be raised if it cannot find
        the molecules section.
    """
    n_top = _get_n_top(n_top, folder)
    if s_count == get_solv_count_top(n_top=n_top, res_name=res_name):
        if verbose:
            print(
                "Solvent count in {} already set at {}".format(
                    os.path.relpath(n_top), str(s_count)
                )
                + "\nNot copying or changing file."
            )
        return None
    bak_name = os.path.join(os.path.dirname(n_top), prepend + os.path.basename(n_top))

# Path: paratemp/tools.py
# class _BlankStream(object):
#     """
#     A class for use when not actually wanting to write to a file.
#     """
# 
#     def write(self, string):
#         pass
# 
#     def fileno(self):
#         return 0  # Not sure if this works. Maybe None would be better
# 
#     def flush(self):
#         pass
# 
# Path: paratemp/exceptions.py
# class InputError(Exception):
#     """Exception raised for errors in the input.
# 
#     Attributes:
#         expr -- input expression in which the error occurred
#         msg  -- explanation of the error
#     """
# 
#     def __init__(self, expr, msg):
#         self.expr = expr
#         self.msg = msg
# 
#     def __str__(self):
#         output = 'Incorrect input "{}". {}'.format(self.expr, self.msg)
#         return repr(output)
# 
# Path: paratemp/tools.py
# @contextmanager
# def cd(new_dir):
#     prev_dir = os.getcwd()
#     os.chdir(os.path.expanduser(new_dir))
#     try:
#         yield
#     finally:
#         os.chdir(prev_dir)
# 
# def copy_no_overwrite(src, dst, silent=False):
#     exists = False
#     if os.path.isdir(src):
#         raise OSError(errno.EISDIR, "Is a directory: {}".format(src))
#     elif os.path.isdir(dst):
#         if os.path.isfile(os.path.join(dst, os.path.basename(src))):
#             exists = True
#     elif os.path.isfile(dst):
#         exists = True
#     if exists:
#         if silent:
#             return dst
#         else:
#             raise OSError(errno.EEXIST, "File already exists", dst)
#     else:
#         return shutil.copy(src, dst)


# Path: paratemp/sim_setup/sim_setup.py
import errno
import glob
import os
import pathlib
import py
import re
import subprocess
import warnings
from typing import Callable, Iterable, Match
from paratemp.tools import _BlankStream
from paratemp.exceptions import InputError
from paratemp.tools import cd, copy_no_overwrite
    from glob import glob
    from gromacs.tools import Trjconv

    :param str n_top: Default: None. Name (and path) of the topology file. If
        None, folder will be used, but this argument takes priority.
    :param str folder: Default: None. If n_top is not provided, this is the
        folder that will be searched for a file ending in '.top' to be used.
    :param int s_count: Default: 0. The count of solvent molecules to be set
        in the topology file.
    :param str res_name: Default: 'DCM'. Name of the residue to look for in
        the topology file. This is case insensitive (this and the line will
        be made lower case when searching for it).
    :param str prepend: Default: 'unequal-'. The string to prepend to the
        topology file name when copying it (to keep a copy of the original).
    :param bool verbose: Default: True. If True, messages will be printed if
        no changes need to be made or after the changes have successfully been
        made.
    :return: None
    :raises RuntimeError: This is raised if the it is unable to find the
        line with the solvent count. This could also be raised if it cannot find
        the molecules section.
    """
    n_top = _get_n_top(n_top, folder)
    if s_count == get_solv_count_top(n_top=n_top, res_name=res_name):
        if verbose:
            print(
                "Solvent count in {} already set at {}".format(
                    os.path.relpath(n_top), str(s_count)
                )
                + "\nNot copying or changing file."
            )
        return None
    bak_name = os.path.join(os.path.dirname(n_top), prepend + os.path.basename(n_top))

    copy_no_overwrite(n_top, bak_name)