# 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
            elif not done and res_name.lower() in line.lower():
                line = re.sub(r"\d+", str(s_count), line)
                done = True
            out_top.write(line)
    if not done:
        # Not the right error, but fine for now
        # Also, this should not be accessible: if anything, get_solv_count_top
        # will fail with a RuntimeError first for any issue.
        raise RuntimeError(
            "Did not find a line with the solvent count" " in {}".format(n_top)
        )
    elif verbose:
        print(
            "Solvent count in {} set at {}".format(os.path.relpath(n_top), str(s_count))
            + "\nOriginal copied to {}.".format(os.path.relpath(bak_name))
        )
    return None


def copy_topology(f_from, f_to, overwrite=False):
    try:
        os.makedirs(f_to)
    except OSError as e:
        if e.errno == 17:
            pass  # Ignore FileExistsError
        else:
            raise
    to_copy = glob.glob(f_from + "/*.top")
    to_copy += glob.glob(f_from + "/*.itp")
    for path in to_copy:

# 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

            elif not done and res_name.lower() in line.lower():
                line = re.sub(r"\d+", str(s_count), line)
                done = True
            out_top.write(line)
    if not done:
        # Not the right error, but fine for now
        # Also, this should not be accessible: if anything, get_solv_count_top
        # will fail with a RuntimeError first for any issue.
        raise RuntimeError(
            "Did not find a line with the solvent count" " in {}".format(n_top)
        )
    elif verbose:
        print(
            "Solvent count in {} set at {}".format(os.path.relpath(n_top), str(s_count))
            + "\nOriginal copied to {}.".format(os.path.relpath(bak_name))
        )
    return None


def copy_topology(f_from, f_to, overwrite=False):
    try:
        os.makedirs(f_to)
    except OSError as e:
        if e.errno == 17:
            pass  # Ignore FileExistsError
        else:
            raise
    to_copy = glob.glob(f_from + "/*.top")
    to_copy += glob.glob(f_from + "/*.itp")
    for path in to_copy:

        copy_no_overwrite(path, f_to, silent=overwrite)