Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for pathlib.Path to OmegaConf.load() and OmegaConf.save() #158

Merged
merged 4 commits into from
Feb 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions news/159.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add support for loading/saving config files by using native python pathlib.Path
9 changes: 5 additions & 4 deletions omegaconf/omegaconf.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import copy
import io
import os
import pathlib
import re
import sys
from contextlib import contextmanager
Expand Down Expand Up @@ -180,10 +181,10 @@ def create( # noqa F811
)

@staticmethod
def load(file_: Union[str, IO[bytes]]) -> Union[DictConfig, ListConfig]:
def load(file_: Union[str, pathlib.Path, IO[bytes]]) -> Union[DictConfig, ListConfig]:
from ._utils import get_yaml_loader

if isinstance(file_, str):
if isinstance(file_, (str, pathlib.Path)):
with io.open(os.path.abspath(file_), "r", encoding="utf-8") as f:
obj = yaml.load(f, Loader=get_yaml_loader())
assert isinstance(obj, (list, dict, str))
Expand All @@ -196,15 +197,15 @@ def load(file_: Union[str, IO[bytes]]) -> Union[DictConfig, ListConfig]:
raise TypeError("Unexpected file type")

@staticmethod
def save(config: Container, f: Union[str, IO[str]], resolve: bool = False) -> None:
def save(config: Container, f: Union[str, pathlib.Path, IO[str]], resolve: bool = False) -> None:
"""
Save as configuration object to a file
:param config: omegaconf.Config object (DictConfig or ListConfig).
:param f: filename or file object
:param resolve: True to save a resolved config (defaults to False)
"""
data = config.pretty(resolve=resolve)
if isinstance(f, str):
if isinstance(f, (str, pathlib.Path)):
with io.open(os.path.abspath(f), "w", encoding="utf-8") as file:
file.write(data)
elif hasattr(f, "write"):
Expand Down
27 changes: 15 additions & 12 deletions tests/test_serialization.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# -*- coding: utf-8 -*-
import io
import os
import pathlib
import tempfile
from typing import Any, Dict
from typing import Any, Dict, Type

import pytest

Expand All @@ -24,14 +25,15 @@ def save_load_from_file(conf: Container, resolve: bool, expected: Any) -> None:
os.unlink(fp.name)


def save_load_from_filename(conf: Container, resolve: bool, expected: Any) -> None:
def save_load_from_filename(conf: Container, resolve: bool, expected: Any, file_class: Type) -> None:
if expected is None:
expected = conf
# note that delete=False here is a work around windows incompetence.
try:
with tempfile.NamedTemporaryFile(delete=False) as fp:
OmegaConf.save(conf, fp.name, resolve=resolve)
c2 = OmegaConf.load(fp.name)
filepath = file_class(fp.name)
OmegaConf.save(conf, filepath, resolve=resolve)
c2 = OmegaConf.load(filepath)
assert c2 == expected
finally:
os.unlink(fp.name)
Expand All @@ -43,26 +45,27 @@ def test_load_from_invalid() -> None:


@pytest.mark.parametrize(
"input_,resolve,expected",
"input_,resolve,expected,file_class",
[
(dict(a=10), False, None),
({"foo": 10, "bar": "${foo}"}, False, None),
({"foo": 10, "bar": "${foo}"}, False, {"foo": 10, "bar": 10}),
([u"שלום"], False, None),
(dict(a=10), False, None, str),
({"foo": 10, "bar": "${foo}"}, False, None, str),
({"foo": 10, "bar": "${foo}"}, False, None, pathlib.Path),
({"foo": 10, "bar": "${foo}"}, False, {"foo": 10, "bar": 10}, str),
([u"שלום"], False, None, str),
],
)
class TestSaveLoad:
def test_save_load__from_file(
self, input_: Dict[str, Any], resolve: bool, expected: Any
self, input_: Dict[str, Any], resolve: bool, expected: Any, file_class: Type
) -> None:
cfg = OmegaConf.create(input_)
save_load_from_file(cfg, resolve, expected)

def test_save_load__from_filename(
self, input_: Dict[str, Any], resolve: bool, expected: Any
self, input_: Dict[str, Any], resolve: bool, expected: Any, file_class: Type
) -> None:
cfg = OmegaConf.create(input_)
save_load_from_filename(cfg, resolve, expected)
save_load_from_filename(cfg, resolve, expected, file_class)


def test_save_illegal_type() -> None:
Expand Down