Skip to content

Commit 27f66eb

Browse files
committed
Update
1 parent 8c94171 commit 27f66eb

6 files changed

Lines changed: 16 additions & 32 deletions

File tree

src/pyobo/struct/functional/ontology.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from pathlib import Path
99

1010
from curies import Converter
11+
from pystow.utils import safe_open
1112
from rdflib import OWL, RDF, Graph, term
1213

1314
from pyobo.struct.functional.dsl import Annotation, Annotations, Axiom, Box
@@ -16,7 +17,6 @@
1617
FunctionalOWLSerializable,
1718
list_to_funowl,
1819
)
19-
from pyobo.utils.io import safe_open
2020

2121
__all__ = [
2222
"Document",
@@ -108,9 +108,9 @@ def to_rdf(self) -> Graph:
108108
return graph
109109

110110
def write_funowl(self, path: str | Path) -> None:
111-
"""Write functional OWL to a file.."""
111+
"""Write functional OWL to a file."""
112112
path = Path(path).expanduser().resolve()
113-
with safe_open(path, read=False) as file:
113+
with safe_open(path, operation="write") as file:
114114
file.write(self.to_funowl())
115115

116116
def to_funowl(self) -> str:

src/pyobo/struct/obo/reader.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from curies.preprocessing import BlocklistError
1919
from curies.vocabulary import SynonymScope
2020
from more_itertools import pairwise
21+
from pystow.utils import safe_open
2122
from tqdm.auto import tqdm
2223

2324
from .reader_utils import (
@@ -52,7 +53,6 @@
5253
get_rules,
5354
)
5455
from ...utils.cache import write_gzipped_graph
55-
from ...utils.io import safe_open
5656
from ...utils.misc import STATIC_VERSION_REWRITES, cleanup_version
5757

5858
__all__ = [
@@ -90,7 +90,7 @@ def from_obo_path(
9090
)
9191
else:
9292
logger.info("[%s] parsing OBO with obonet from %s", prefix or "<unknown>", path)
93-
with safe_open(path, read=True) as file:
93+
with safe_open(path, operation="read") as file:
9494
graph = _read_obo(file, prefix, ignore_obsolete=ignore_obsolete, use_tqdm=use_tqdm)
9595

9696
if prefix:

src/pyobo/struct/obograph/export.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
import obographs as og
99
from curies import Converter, ReferenceTuple
1010
from curies import vocabulary as v
11+
from pystow.utils import safe_open
1112

1213
from pyobo.identifier_utils.api import get_converter
1314
from pyobo.struct import Obo, OBOLiteral, Stanza, Term, TypeDef
1415
from pyobo.struct import typedef as tdv
15-
from pyobo.utils.io import safe_open
1616

1717
__all__ = [
1818
"to_obograph",
@@ -25,7 +25,7 @@ def write_obograph(obo: Obo, path: str | Path, *, converter: Converter | None =
2525
"""Write an ontology to a file as OBO Graph JSON."""
2626
path = Path(path).expanduser().resolve()
2727
raw_graph = to_obograph(obo, converter=converter)
28-
with safe_open(path, read=False) as file:
28+
with safe_open(path, operation="write") as file:
2929
file.write(raw_graph.model_dump_json(indent=2, exclude_none=True, exclude_unset=True))
3030

3131

src/pyobo/struct/struct.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
from curies import Converter, ReferenceTuple
2626
from curies import vocabulary as _cv
2727
from more_click import force_option, verbose_option
28+
from pystow.utils import safe_open
2829
from tqdm.auto import tqdm
2930
from typing_extensions import Self
3031

@@ -70,7 +71,7 @@
7071
TARGET_PREFIX,
7172
)
7273
from ..utils.cache import write_gzipped_graph
73-
from ..utils.io import multidict, safe_open, write_iterable_tsv
74+
from ..utils.io import multidict, write_iterable_tsv
7475
from ..utils.path import (
7576
CacheArtifact,
7677
get_cache_path,
@@ -997,7 +998,7 @@ def write_obo(
997998
unit="line",
998999
)
9991000
if isinstance(file, str | Path | os.PathLike):
1000-
with safe_open(file, read=False) as fh:
1001+
with safe_open(file, operation="write") as fh:
10011002
self._write_lines(it, fh)
10021003
else:
10031004
self._write_lines(it, file)
@@ -1173,7 +1174,7 @@ def write_metadata(self) -> None:
11731174
metadata = self.get_metadata()
11741175
for path in (self._root_metadata_path, self._get_cache_path(CacheArtifact.metadata)):
11751176
logger.debug("[%s] caching metadata to %s", self._prefix_version, path)
1176-
with safe_open(path, read=False) as file:
1177+
with safe_open(path, operation="write") as file:
11771178
json.dump(metadata, file, indent=2)
11781179

11791180
def write_prefix_map(self) -> None:

src/pyobo/utils/cache.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@
1212
from pystow.cache import CachedDataFrame as cached_df # noqa:N813
1313
from pystow.cache import CachedJSON as cached_json # noqa:N813
1414
from pystow.cache import CachedPickle as cached_pickle # noqa:N813
15+
from pystow.utils import safe_open
1516

16-
from .io import open_map_tsv, open_multimap_tsv, safe_open, write_map_tsv, write_multimap_tsv
17+
from .io import open_map_tsv, open_multimap_tsv, write_map_tsv, write_multimap_tsv
1718

1819
__all__ = [
1920
"cached_collection",
@@ -69,13 +70,13 @@ def dump(self, rv: Mapping[str, str]) -> None:
6970

7071
def get_gzipped_graph(path: str | Path) -> nx.MultiDiGraph:
7172
"""Read a graph that's gzipped nodelink."""
72-
with safe_open(path, read=True) as file:
73+
with safe_open(path, operation="read") as file:
7374
return nx.node_link_graph(json.load(file), edges=NODE_LINK_STYLE)
7475

7576

7677
def write_gzipped_graph(graph: nx.MultiDiGraph, path: str | Path) -> None:
7778
"""Write a graph as gzipped nodelink."""
78-
with safe_open(path, read=False) as file:
79+
with safe_open(path, operation="write") as file:
7980
json.dump(nx.node_link_data(graph, edges=NODE_LINK_STYLE), file)
8081

8182

src/pyobo/utils/io.py

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
"""I/O utilities."""
22

33
import collections.abc
4-
import contextlib
54
import csv
65
import gzip
76
import logging
87
from collections import defaultdict
98
from collections.abc import Generator, Iterable, Mapping
109
from contextlib import contextmanager
1110
from pathlib import Path
12-
from typing import Literal, TextIO, TypeVar, cast
11+
from typing import TypeVar, cast
1312

1413
import pandas as pd
1514
import pystow.utils
@@ -22,7 +21,6 @@
2221
"multisetdict",
2322
"open_map_tsv",
2423
"open_multimap_tsv",
25-
"safe_open",
2624
"safe_open_writer",
2725
"write_iterable_tsv",
2826
"write_map_tsv",
@@ -145,19 +143,3 @@ def write_iterable_tsv(
145143
if header is not None:
146144
writer.writerow(header)
147145
writer.writerows(it)
148-
149-
150-
@contextlib.contextmanager
151-
def safe_open(
152-
path: str | Path, read: bool, encoding: str | None = None
153-
) -> Generator[TextIO, None, None]:
154-
"""Safely open a file for reading or writing text."""
155-
# TODO replace me!
156-
path = Path(path).expanduser().resolve()
157-
mode: Literal["rt", "wt"] = "rt" if read else "wt"
158-
if path.suffix.endswith(".gz"):
159-
with gzip.open(path, mode=mode, encoding=encoding) as file:
160-
yield file
161-
else:
162-
with open(path, mode=mode) as file:
163-
yield file

0 commit comments

Comments
 (0)