Finish extracting _open_file()

Summary: Finish extracting _open_file() to a separate utils module (started with D20754859 (c9267ab7af) / previous version of this diff).

Reviewed By: nikhilaravi

Differential Revision: D20720344

fbshipit-source-id: 77ef201ff37a8f2a0cd19be162cb97ee99480158
This commit is contained in:
Patrick Labatut 2020-07-13 12:03:01 -07:00 committed by Facebook GitHub Bot
parent 20ef9195f0
commit e2b47f047e
3 changed files with 9 additions and 27 deletions

View File

@ -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:

View File

@ -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:

View File

@ -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