From af67f0f5fa5d564a8deb027de387bc2946230733 Mon Sep 17 00:00:00 2001 From: Daniel D'Avella Date: Wed, 21 Nov 2018 12:08:12 -0500 Subject: [PATCH] Support use of pathlib.Path for asdf.open and write_to --- CHANGES.rst | 3 +++ asdf/generic_io.py | 9 +++++---- asdf/tests/test_api.py | 4 ++-- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index 1d8d2acea..a9b04b3a2 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -12,6 +12,9 @@ - Deprecate the ``asdf.asdftypes`` module in favor of ``asdf.types``. [#611] +- Support use of ``pathlib.Path`` with ``asdf.open`` and ``AsdfFile.write_to``. + [#617] + 2.2.1 (2018-11-15) ------------------ diff --git a/asdf/generic_io.py b/asdf/generic_io.py index 0f43c2571..7a4edfd45 100644 --- a/asdf/generic_io.py +++ b/asdf/generic_io.py @@ -10,12 +10,13 @@ """ import io -import math import os -import platform import re import sys +import math +import pathlib import tempfile +import platform from distutils.version import LooseVersion from os import SEEK_SET, SEEK_CUR, SEEK_END @@ -1171,8 +1172,8 @@ def get_file(init, mode='r', uri=None, close=False): init.mode, mode)) return GenericWrapper(init) - elif isinstance(init, str): - parsed = urlparse.urlparse(init) + elif isinstance(init, (str, pathlib.Path)): + parsed = urlparse.urlparse(str(init)) if parsed.scheme in ['http', 'https']: if 'w' in mode: raise ValueError( diff --git a/asdf/tests/test_api.py b/asdf/tests/test_api.py index b7a78ae7a..77cdf8db8 100644 --- a/asdf/tests/test_api.py +++ b/asdf/tests/test_api.py @@ -245,8 +245,8 @@ def test_open_pathlib_path(tmpdir): with asdf.AsdfFile(tree) as af: af.write_to(path) - with asdf.open(tree) as af: - assert af['data'] == tree['data'] + with asdf.open(path) as af: + assert (af['data'] == tree['data']).all() @pytest.mark.skip(reason='Until inline_threshold is added as a write option')