From e2b47f047e634e5f5f1f177c0a53125b8c6cbed5 Mon Sep 17 00:00:00 2001 From: Patrick Labatut Date: Mon, 13 Jul 2020 12:03:01 -0700 Subject: [PATCH] Finish extracting _open_file() Summary: Finish extracting _open_file() to a separate utils module (started with D20754859 (https://github.com/facebookresearch/pytorch3d/commit/c9267ab7af0f72217a1ee1a0b37941a5c8fdb325) / previous version of this diff). Reviewed By: nikhilaravi Differential Revision: D20720344 fbshipit-source-id: 77ef201ff37a8f2a0cd19be162cb97ee99480158 --- pytorch3d/io/obj_io.py | 11 ++--------- pytorch3d/io/ply_io.py | 18 +++--------------- pytorch3d/io/utils.py | 7 ++++--- 3 files changed, 9 insertions(+), 27 deletions(-) diff --git a/pytorch3d/io/obj_io.py b/pytorch3d/io/obj_io.py index bf5a0f85..2affff9b 100644 --- a/pytorch3d/io/obj_io.py +++ b/pytorch3d/io/obj_io.py @@ -3,7 +3,6 @@ """This module implements utility functions for loading and saving meshes.""" import os -import pathlib import warnings from collections import namedtuple from typing import Optional @@ -216,7 +215,7 @@ def load_obj( # pyre-fixme[6]: Expected `_PathLike[Variable[typing.AnyStr <: [str, # bytes]]]` for 1st param but got `Union[_PathLike[typing.Any], bytes, str]`. data_dir = os.path.dirname(f_obj) - f_obj, new_f = _open_file(f_obj) + f_obj, new_f = _open_file(f_obj, "r") try: return _load( f_obj, @@ -524,13 +523,7 @@ def save_obj(f, verts, faces, decimal_places: Optional[int] = None): message = "Argument 'faces' should either be empty or of shape (num_faces, 3)." raise ValueError(message) - new_f = False - if isinstance(f, str): - new_f = True - f = open(f, "w") - elif isinstance(f, pathlib.Path): - new_f = True - f = f.open("w") + f, new_f = _open_file(f, "w") try: return _save(f, verts, faces, decimal_places) finally: diff --git a/pytorch3d/io/ply_io.py b/pytorch3d/io/ply_io.py index f0a652a6..4afc46ee 100644 --- a/pytorch3d/io/ply_io.py +++ b/pytorch3d/io/ply_io.py @@ -4,7 +4,6 @@ """This module implements utility functions for loading and saving meshes.""" -import pathlib import struct import sys import warnings @@ -13,6 +12,7 @@ from typing import Optional, Tuple import numpy as np import torch +from pytorch3d.io.utils import _open_file _PlyTypeData = namedtuple("_PlyTypeData", "size struct_char np_type") @@ -603,13 +603,7 @@ def _load_ply_raw(f) -> Tuple[_PlyHeader, dict]: uniformly-sized list, then the value will be a 2D numpy array. If not, it is a list of the relevant property values. """ - new_f = False - if isinstance(f, str): - new_f = True - f = open(f, "rb") - elif isinstance(f, pathlib.Path): - new_f = True - f = f.open("rb") + f, new_f = _open_file(f, "rb") try: header, elements = _load_ply_raw_stream(f) finally: @@ -806,13 +800,7 @@ def save_ply( message = "Argument 'verts_normals' should either be empty or of shape (num_verts, 3)." raise ValueError(message) - new_f = False - if isinstance(f, str): - new_f = True - f = open(f, "w") - elif isinstance(f, pathlib.Path): - new_f = True - f = f.open("w") + f, new_f = _open_file(f, "w") try: _save_ply(f, verts, faces, verts_normals, decimal_places) finally: diff --git a/pytorch3d/io/utils.py b/pytorch3d/io/utils.py index a66da0f0..88bbcc65 100644 --- a/pytorch3d/io/utils.py +++ b/pytorch3d/io/utils.py @@ -7,14 +7,15 @@ from fvcore.common.file_io import PathManager from PIL import Image -def _open_file(f): +# TODO(plabatut): Replace with a context manager +def _open_file(f, mode="r"): new_f = False if isinstance(f, str): new_f = True - f = open(f, "r") + f = open(f, mode) elif isinstance(f, pathlib.Path): new_f = True - f = f.open("r") + f = f.open(mode) return f, new_f