Skip to content

Commit

Permalink
Revert "zopen: explicit binary/text mode , and explicit `encoding…
Browse files Browse the repository at this point in the history
…` as UTF…" (#4221)

This reverts commit b2a5e0f.
  • Loading branch information
shyuep authored Dec 11, 2024
1 parent ac663b4 commit 9ff725d
Show file tree
Hide file tree
Showing 47 changed files with 170 additions and 168 deletions.
2 changes: 1 addition & 1 deletion dev_scripts/potcar_scrambler.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def scramble_single_potcar(self, potcar: PotcarSingle) -> str:
return scrambled_potcar_str

def to_file(self, filename: str) -> None:
with zopen(filename, mode="wt", encoding="utf-8") as file:
with zopen(filename, mode="wt") as file:
file.write(self.scrambled_potcars_str)

@classmethod
Expand Down
2 changes: 1 addition & 1 deletion src/pymatgen/apps/borg/hive.py
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ def _get_transformation_history(path: PathLike):
"""Check for a transformations.json* file and return the history."""
if trans_json := glob(f"{path!s}/transformations.json*"):
try:
with zopen(trans_json[0], mode="rt", encoding="utf-8") as file:
with zopen(trans_json[0]) as file:
return json.load(file)["history"]
except Exception:
return None
Expand Down
4 changes: 2 additions & 2 deletions src/pymatgen/apps/borg/queen.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,12 @@ def save_data(self, filename: PathLike) -> None:
that if the filename ends with gz or bz2, the relevant gzip
or bz2 compression will be applied.
"""
with zopen(filename, mode="wt", encoding="utf-8") as file:
with zopen(filename, mode="wt") as file:
json.dump(list(self._data), file, cls=MontyEncoder)

def load_data(self, filename: PathLike) -> None:
"""Load assimilated data from a file."""
with zopen(filename, mode="rt", encoding="utf-8") as file:
with zopen(filename, mode="rt") as file:
self._data = json.load(file, cls=MontyDecoder)


Expand Down
18 changes: 9 additions & 9 deletions src/pymatgen/core/structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -2953,15 +2953,15 @@ def to(self, filename: PathLike = "", fmt: FileFormats = "", **kwargs) -> str:
elif fmt == "json" or fnmatch(filename.lower(), "*.json*"):
json_str = json.dumps(self.as_dict())
if filename:
with zopen(filename, mode="wt", encoding="utf-8") as file:
with zopen(filename, mode="wt") as file:
file.write(json_str)
return json_str
elif fmt == "xsf" or fnmatch(filename.lower(), "*.xsf*"):
from pymatgen.io.xcrysden import XSF

res_str = XSF(self).to_str()
if filename:
with zopen(filename, mode="wt", encoding="utf-8") as file:
with zopen(filename, mode="wt", encoding="utf8") as file:
file.write(res_str)
return res_str
elif (
Expand All @@ -2987,15 +2987,15 @@ def to(self, filename: PathLike = "", fmt: FileFormats = "", **kwargs) -> str:
yaml.dump(self.as_dict(), str_io)
yaml_str = str_io.getvalue()
if filename:
with zopen(filename, mode="wt", encoding="utf-8") as file:
with zopen(filename, mode="wt") as file:
file.write(yaml_str)
return yaml_str
elif fmt == "aims" or fnmatch(filename, "geometry.in"):
from pymatgen.io.aims.inputs import AimsGeometryIn

geom_in = AimsGeometryIn.from_structure(self)
if filename:
with zopen(filename, mode="wt", encoding="utf-8") as file:
with zopen(filename, mode="w") as file:
file.write(geom_in.get_header(filename))
file.write(geom_in.content)
file.write("\n")
Expand All @@ -3010,7 +3010,7 @@ def to(self, filename: PathLike = "", fmt: FileFormats = "", **kwargs) -> str:

res_str = ResIO.structure_to_str(self)
if filename:
with zopen(filename, mode="wt", encoding="utf-8") as file:
with zopen(filename, mode="wt", encoding="utf8") as file:
file.write(res_str)
return res_str
elif fmt == "pwmat" or fnmatch(filename.lower(), "*.pwmat") or fnmatch(filename.lower(), "*.config"):
Expand Down Expand Up @@ -3173,7 +3173,7 @@ def from_file(
return struct

fname = os.path.basename(filename)
with zopen(filename, mode="rt", errors="replace", encoding="utf-8") as file:
with zopen(filename, mode="rt", errors="replace") as file:
contents = file.read()
if fnmatch(fname.lower(), "*.cif*") or fnmatch(fname.lower(), "*.mcif*"):
return cls.from_str(
Expand Down Expand Up @@ -3919,7 +3919,7 @@ def to(self, filename: str = "", fmt: str = "") -> str | None:
elif fmt == "json" or fnmatch(filename, "*.json*") or fnmatch(filename, "*.mson*"):
json_str = json.dumps(self.as_dict())
if filename:
with zopen(filename, mode="wt", encoding="utf-8") as file:
with zopen(filename, mode="wt", encoding="utf8") as file:
file.write(json_str)
return json_str
elif fmt in {"yaml", "yml"} or fnmatch(filename, "*.yaml*") or fnmatch(filename, "*.yml*"):
Expand All @@ -3928,7 +3928,7 @@ def to(self, filename: str = "", fmt: str = "") -> str | None:
yaml.dump(self.as_dict(), str_io)
yaml_str = str_io.getvalue()
if filename:
with zopen(filename, mode="wt", encoding="utf-8") as file:
with zopen(filename, mode="wt", encoding="utf8") as file:
file.write(yaml_str)
return yaml_str
else:
Expand Down Expand Up @@ -4010,7 +4010,7 @@ def from_file(cls, filename: PathLike) -> Self | None:
"""
filename = str(filename)

with zopen(filename, mode="rt", encoding="utf-8") as file:
with zopen(filename) as file:
contents = file.read()
fname = filename.lower()
if fnmatch(fname, "*.xyz*"):
Expand Down
2 changes: 1 addition & 1 deletion src/pymatgen/core/trajectory.py
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ def write_Xdatcar(

xdatcar_str = "\n".join(lines) + "\n"

with zopen(filename, mode="wt", encoding="utf-8") as file:
with zopen(filename, mode="wt") as file:
file.write(xdatcar_str)

def as_dict(self) -> dict:
Expand Down
2 changes: 1 addition & 1 deletion src/pymatgen/io/adf.py
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,7 @@ def _parse_logfile(self, logfile):
# The last non-empty line of the logfile must match the end pattern.
# Otherwise the job has some internal failure. The TAPE13 part of the
# ADF manual has a detailed explanation.
with zopen(logfile, mode="rt", encoding="utf-8") as file:
with zopen(logfile, mode="rt") as file:
for line in reverse_readline(file):
if line == "":
continue
Expand Down
4 changes: 2 additions & 2 deletions src/pymatgen/io/aims/inputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def from_file(cls, filepath: str | Path) -> Self:
Returns:
AimsGeometryIn: The input object represented in the file
"""
with zopen(filepath, mode="rt", encoding="utf-8") as in_file:
with zopen(filepath, mode="rt") as in_file:
content = in_file.read()
return cls.from_str(content)

Expand Down Expand Up @@ -753,7 +753,7 @@ def from_file(cls, filename: str, label: str | None = None) -> Self:
Returns:
AimsSpeciesFile
"""
with zopen(filename, mode="rt", encoding="utf-8") as file:
with zopen(filename, mode="rt") as file:
return cls(data=file.read(), label=label)

@classmethod
Expand Down
8 changes: 4 additions & 4 deletions src/pymatgen/io/cif.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ def from_file(cls, filename: PathLike) -> Self:
Returns:
CifFile
"""
with zopen(filename, mode="rt", errors="replace", encoding="utf-8") as file:
with zopen(filename, mode="rt", errors="replace") as file:
return cls.from_str(file.read())


Expand Down Expand Up @@ -1760,9 +1760,9 @@ def cif_file(self) -> CifFile:

def write_file(
self,
filename: PathLike,
mode: Literal["wt", "at"] = "wt",
filename: str | Path,
mode: Literal["w", "a", "wt", "at"] = "w",
) -> None:
"""Write the CIF file."""
with zopen(filename, mode=mode, encoding="utf-8") as file:
with zopen(filename, mode=mode) as file:
file.write(str(self))
6 changes: 3 additions & 3 deletions src/pymatgen/io/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ def to_cube(self, filename, comment: str = ""):
filename (str): Name of the cube file to be written.
comment (str): If provided, this will be added to the second comment line
"""
with zopen(filename, mode="wt", encoding="utf-8") as file:
with zopen(filename, mode="wt") as file:
file.write(f"# Cube file for {self.structure.formula} generated by Pymatgen\n")
file.write(f"# {comment}\n")
file.write(f"\t {len(self.structure)} 0.000000 0.000000 0.000000\n")
Expand Down Expand Up @@ -386,7 +386,7 @@ def from_cube(cls, filename: str | Path) -> Self:
Args:
filename (str): of the cube to read
"""
file = zopen(filename, mode="rt", encoding="utf-8")
file = zopen(filename, mode="rt")

# skip header lines
file.readline()
Expand Down Expand Up @@ -529,7 +529,7 @@ def __getitem__(self, item):
f"No parser defined for {item}. Contents are returned as a string.",
stacklevel=2,
)
with zopen(fpath, mode="rt", encoding="utf-8") as f:
with zopen(fpath, "rt") as f:
return f.read()

def get_files_by_name(self, name: str) -> dict[str, Any]:
Expand Down
6 changes: 3 additions & 3 deletions src/pymatgen/io/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def write_file(self, filename: PathLike) -> None:
Args:
filename: The filename to output to, including path.
"""
with zopen(Path(filename), mode="wt", encoding="utf-8") as file:
with zopen(Path(filename), mode="wt") as file:
file.write(self.get_str())

@classmethod
Expand Down Expand Up @@ -102,7 +102,7 @@ def from_file(cls, path: PathLike) -> None:
Returns:
InputFile
"""
with zopen(Path(path), mode="rt", encoding="utf-8") as file:
with zopen(Path(path), mode="rt") as file:
return cls.from_str(file.read()) # from_str not implemented


Expand Down Expand Up @@ -218,7 +218,7 @@ def write_input(
if isinstance(contents, InputFile):
contents.write_file(file_path)
else:
with zopen(file_path, mode="wt", encoding="utf-8") as file:
with zopen(file_path, mode="wt") as file:
file.write(str(contents))

if zip_inputs:
Expand Down
2 changes: 1 addition & 1 deletion src/pymatgen/io/cp2k/inputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -692,7 +692,7 @@ def _from_dict(cls, dct: dict):
@classmethod
def from_file(cls, filename: str | Path) -> Self:
"""Initialize from a file."""
with zopen(filename, mode="rt", encoding="utf-8") as file:
with zopen(filename, mode="rt") as file:
txt = preprocessor(file.read(), os.path.dirname(file.name))
return cls.from_str(txt)

Expand Down
16 changes: 8 additions & 8 deletions src/pymatgen/io/cp2k/outputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ def parse_initial_structure(self):
)

coord_table = []
with zopen(self.filename, mode="rt", encoding="utf-8") as file:
with zopen(self.filename, mode="rt") as file:
while True:
line = file.readline()
if re.search(r"Atom\s+Kind\s+Element\s+X\s+Y\s+Z\s+Z\(eff\)\s+Mass", line):
Expand Down Expand Up @@ -789,7 +789,7 @@ def parse_atomic_kind_info(self):
except (TypeError, IndexError, ValueError):
atomic_kind_info[kind]["total_pseudopotential_energy"] = None

with zopen(self.filename, mode="rt", encoding="utf-8") as file:
with zopen(self.filename, mode="rt") as file:
j = -1
lines = file.readlines()
for k, line in enumerate(lines):
Expand Down Expand Up @@ -1010,7 +1010,7 @@ def parse_mo_eigenvalues(self):
eigenvalues = []
efermi = []

with zopen(self.filename, mode="rt", encoding="utf-8") as file:
with zopen(self.filename, mode="rt") as file:
lines = iter(file.readlines())
for line in lines:
try:
Expand Down Expand Up @@ -1349,7 +1349,7 @@ def parse_hyperfine(self, hyperfine_filename=None):
else:
return None

with zopen(hyperfine_filename, mode="rt", encoding="utf-8") as file:
with zopen(hyperfine_filename, mode="rt") as file:
lines = [line for line in file.read().split("\n") if line]

hyperfine = [[] for _ in self.ionic_steps]
Expand All @@ -1370,7 +1370,7 @@ def parse_gtensor(self, gtensor_filename=None):
else:
return None

with zopen(gtensor_filename, mode="rt", encoding="utf-8") as file:
with zopen(gtensor_filename, mode="rt") as file:
lines = [line for line in file.read().split("\n") if line]

data = {}
Expand Down Expand Up @@ -1407,7 +1407,7 @@ def parse_chi_tensor(self, chi_filename=None):
else:
return None

with zopen(chi_filename, mode="rt", encoding="utf-8") as file:
with zopen(chi_filename, mode="rt") as file:
lines = [line for line in file.read().split("\n") if line]

data = {k: [] for k in "chi_soft chi_local chi_total chi_total_ppm_cgs PV1 PV2 PV3 ISO ANISO".split()}
Expand Down Expand Up @@ -1554,7 +1554,7 @@ def read_table_pattern(
row_pattern, or a dict in case that named capturing groups are defined by
row_pattern.
"""
with zopen(self.filename, mode="rt", encoding="utf-8") as file:
with zopen(self.filename, mode="rt") as file:
if strip:
lines = file.readlines()
text = "".join(
Expand Down Expand Up @@ -1691,7 +1691,7 @@ def parse_pdos(dos_file=None, spin_channel=None, total=False):
"""
spin = Spin(spin_channel) if spin_channel else Spin.down if "BETA" in os.path.split(dos_file)[-1] else Spin.up

with zopen(dos_file, mode="rt", encoding="utf-8") as file:
with zopen(dos_file, mode="rt") as file:
lines = file.readlines()
kind = re.search(r"atomic kind\s(.*)\sat iter", lines[0]) or re.search(r"list\s(\d+)\s(.*)\sat iter", lines[0])
kind = kind.groups()[0]
Expand Down
2 changes: 1 addition & 1 deletion src/pymatgen/io/cp2k/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def preprocessor(data: str, dir: str = ".") -> str: # noqa: A002
raise ValueError(f"length of inc should be 2, got {len(inc)}")
inc = inc[1].strip("'")
inc = inc.strip('"')
with zopen(os.path.join(dir, inc), mode="rt", encoding="utf-8") as file:
with zopen(os.path.join(dir, inc)) as file:
data = re.sub(rf"{incl}", file.read(), data)
variable_sets = re.findall(r"(@SET.+)", data, re.IGNORECASE)
for match in variable_sets:
Expand Down
4 changes: 2 additions & 2 deletions src/pymatgen/io/cssr.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def write_file(self, filename):
Args:
filename (str): Filename to write to.
"""
with zopen(filename, mode="wt", encoding="utf-8") as file:
with zopen(filename, mode="wt") as file:
file.write(str(self) + "\n")

@classmethod
Expand Down Expand Up @@ -98,5 +98,5 @@ def from_file(cls, filename: str | Path) -> Self:
Returns:
Cssr object.
"""
with zopen(filename, mode="rt", encoding="utf-8") as file:
with zopen(filename, mode="rt") as file:
return cls.from_str(file.read())
2 changes: 1 addition & 1 deletion src/pymatgen/io/exciting/inputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ def from_file(cls, filename: str | Path) -> Self:
Returns:
ExcitingInput
"""
with zopen(filename, mode="rt", encoding="utf-8") as file:
with zopen(filename, mode="rt") as file:
data = file.read().replace("\n", "")
return cls.from_str(data)

Expand Down
Loading

0 comments on commit 9ff725d

Please sign in to comment.