Skip to content

Commit

Permalink
add compatibility with pathlib.Path type from stdlib.
Browse files Browse the repository at this point in the history
  • Loading branch information
publicmatt committed Apr 5, 2024
1 parent a6adce7 commit 72e9eba
Showing 1 changed file with 27 additions and 26 deletions.
53 changes: 27 additions & 26 deletions src/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from importlib.abc import InspectLoader
from types import ModuleType
from typing import Any, Dict, Iterable, List, Mapping, Optional, TextIO, Union, cast
from pathlib import Path

try:
import yaml
Expand Down Expand Up @@ -349,7 +350,7 @@ class FileConfiguration(Configuration):

def __init__(
self,
data: Union[str, TextIO],
data: Union[str, Path, TextIO],
read_from_file: bool = False,
*,
lowercase_keys: bool = False,
Expand All @@ -370,13 +371,13 @@ def __init__(
interpolate=interpolate,
interpolate_type=interpolate_type,
)
self._filename = data if read_from_file and isinstance(data, str) else None
self._filename = data if read_from_file and isinstance(data, (str, Path)) else None
self._ignore_missing_paths = ignore_missing_paths
self._reload_with_check(data, read_from_file)

def _reload_with_check(
self,
data: Union[str, TextIO],
data: Union[str, Path, TextIO],
read_from_file: bool = False,
) -> None: # pragma: no cover
try:
Expand All @@ -388,7 +389,7 @@ def _reload_with_check(

def _reload(
self,
data: Union[str, TextIO],
data: Union[str, Path, TextIO],
read_from_file: bool = False,
) -> None: # pragma: no cover
raise NotImplementedError()
Expand All @@ -404,12 +405,12 @@ class JSONConfiguration(FileConfiguration):

def _reload(
self,
data: Union[str, TextIO],
data: Union[str, Path, TextIO],
read_from_file: bool = False,
) -> None:
"""Reload the JSON data."""
if read_from_file:
if isinstance(data, str):
if isinstance(data, (str, Path)):
with open(data, "rt") as f:
result = json.load(f)
else:
Expand All @@ -420,7 +421,7 @@ def _reload(


def config_from_json(
data: Union[str, TextIO],
data: Union[str, Path, TextIO],
read_from_file: bool = False,
*,
lowercase_keys: bool = False,
Expand Down Expand Up @@ -456,7 +457,7 @@ class INIConfiguration(FileConfiguration):

def __init__(
self,
data: Union[str, TextIO],
data: Union[str, Path, TextIO],
read_from_file: bool = False,
*,
section_prefix: str = "",
Expand All @@ -476,7 +477,7 @@ def __init__(
ignore_missing_paths=ignore_missing_paths,
)

def _reload(self, data: Union[str, TextIO], read_from_file: bool = False) -> None:
def _reload(self, data: Union[str, Path, TextIO], read_from_file: bool = False) -> None:
"""Reload the INI data."""
import configparser

Expand All @@ -487,7 +488,7 @@ def optionxform(self, optionstr: str) -> str:
return super().optionxform(optionstr) if lowercase else optionstr

if read_from_file:
if isinstance(data, str):
if isinstance(data, (str, Path)):
with open(data, "rt") as f:
data = f.read()
else:
Expand All @@ -505,7 +506,7 @@ def optionxform(self, optionstr: str) -> str:


def config_from_ini(
data: Union[str, TextIO],
data: Union[str, Path, TextIO],
read_from_file: bool = False,
*,
section_prefix: str = "",
Expand Down Expand Up @@ -543,7 +544,7 @@ class DotEnvConfiguration(FileConfiguration):

def __init__(
self,
data: Union[str, TextIO],
data: Union[str, Path, TextIO],
read_from_file: bool = False,
prefix: str = "",
separator: str = "__",
Expand All @@ -567,12 +568,12 @@ def __init__(

def _reload(
self,
data: Union[str, TextIO],
data: Union[str, Path, TextIO],
read_from_file: bool = False,
) -> None:
"""Reload the .env data."""
if read_from_file:
if isinstance(data, str):
if isinstance(data, (str, Path)):
with open(data, "rt") as f:
data = f.read()
else:
Expand All @@ -594,7 +595,7 @@ def _reload(


def config_from_dotenv(
data: Union[str, TextIO],
data: Union[str, Path, TextIO],
read_from_file: bool = False,
prefix: str = "",
separator: str = "__",
Expand Down Expand Up @@ -634,7 +635,7 @@ class PythonConfiguration(Configuration):

def __init__(
self,
module: Union[str, ModuleType],
module: Union[str, Path, ModuleType],
prefix: str = "",
separator: str = "_",
*,
Expand All @@ -651,7 +652,7 @@ def __init__(
lowercase_keys: whether to convert every key to lower case.
"""
try:
if isinstance(module, str):
if isinstance(module, (str, Path)):
if module.endswith(".py"):
import importlib.util
from importlib import machinery
Expand Down Expand Up @@ -708,7 +709,7 @@ def reload(self) -> None:


def config_from_python(
module: Union[str, ModuleType],
module: Union[str, Path, ModuleType],
prefix: str = "",
separator: str = "_",
*,
Expand Down Expand Up @@ -796,7 +797,7 @@ class YAMLConfiguration(FileConfiguration):

def __init__(
self,
data: Union[str, TextIO],
data: Union[str, Path, TextIO],
read_from_file: bool = False,
*,
lowercase_keys: bool = False,
Expand All @@ -818,9 +819,9 @@ def __init__(
ignore_missing_paths=ignore_missing_paths,
)

def _reload(self, data: Union[str, TextIO], read_from_file: bool = False) -> None:
def _reload(self, data: Union[str, Path, TextIO], read_from_file: bool = False) -> None:
"""Reload the YAML data."""
if read_from_file and isinstance(data, str):
if read_from_file and isinstance(data, (str, Path)):
with open(data, "rt") as f:
loaded = yaml.load(f, Loader=yaml.FullLoader)
else:
Expand All @@ -831,7 +832,7 @@ def _reload(self, data: Union[str, TextIO], read_from_file: bool = False) -> Non


def config_from_yaml(
data: Union[str, TextIO],
data: Union[str, Path, TextIO],
read_from_file: bool = False,
*,
lowercase_keys: bool = False,
Expand Down Expand Up @@ -866,7 +867,7 @@ class TOMLConfiguration(FileConfiguration):

def __init__(
self,
data: Union[str, TextIO],
data: Union[str, Path, TextIO],
read_from_file: bool = False,
*,
section_prefix: str = "",
Expand All @@ -891,10 +892,10 @@ def __init__(
ignore_missing_paths=ignore_missing_paths,
)

def _reload(self, data: Union[str, TextIO], read_from_file: bool = False) -> None:
def _reload(self, data: Union[str, Path, TextIO], read_from_file: bool = False) -> None:
"""Reload the TOML data."""
if read_from_file:
if isinstance(data, str):
if isinstance(data, (str, Path)):
with open(data, "rb") as f:
loaded = toml.load(f)
else:
Expand All @@ -914,7 +915,7 @@ def _reload(self, data: Union[str, TextIO], read_from_file: bool = False) -> Non


def config_from_toml(
data: Union[str, TextIO],
data: Union[str, Path, TextIO],
read_from_file: bool = False,
*,
section_prefix: str = "",
Expand Down

0 comments on commit 72e9eba

Please sign in to comment.