Skip to content

Commit

Permalink
Merge pull request #2038 from jkerhin/make-zip-safe
Browse files Browse the repository at this point in the history
Update Config to use importlib for defaults
  • Loading branch information
bboe authored Nov 23, 2024
2 parents 108e4ee + 1fd51b0 commit 42f5236
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 9 deletions.
3 changes: 2 additions & 1 deletion AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -84,5 +84,6 @@ Source Contributors
- Josh Kim `@jsk56143 <https://github.com/jsk56143>`_
- Rolf Campbell `@endlisnis <https://github.com/endlisnis>`_
- zacc `@zacc <https://github.com/zacc>`_
- c0d3rman `@c0d3rman <https://github.com/c0d3rman>`
- c0d3rman `@c0d3rman <https://github.com/c0d3rman>`_
- Joe Kerhin `@jkerhin <https://github.com/jkerhin>`_
- Add "Name <email (optional)> and github profile link" above this line.
10 changes: 6 additions & 4 deletions praw/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
from __future__ import annotations

import configparser
import importlib
import importlib.resources
import os
import sys
from pathlib import Path
from threading import Lock
from typing import Any
Expand Down Expand Up @@ -48,7 +49,8 @@ def _load_config(cls, *, config_interpolation: str | None = None):
interpolator_class = None

config = configparser.ConfigParser(interpolation=interpolator_class)
module_dir = Path(sys.modules[__name__].__file__).parent
with importlib.resources.open_text(__package__, "praw.ini") as hdl:
config.read_file(hdl)

if "APPDATA" in os.environ: # Windows
os_config_path = Path(os.environ["APPDATA"])
Expand All @@ -59,10 +61,10 @@ def _load_config(cls, *, config_interpolation: str | None = None):
else:
os_config_path = None

locations = [str(module_dir / "praw.ini"), "praw.ini"]
locations = ["praw.ini"]

if os_config_path is not None:
locations.insert(1, str(os_config_path / "praw.ini"))
locations.insert(0, str(os_config_path / "praw.ini"))

config.read(locations)
cls.CONFIG = config
Expand Down
5 changes: 1 addition & 4 deletions tests/unit/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,10 @@ def _assert_config_read(environment, mock_config):
del os.environ[env_name]
os.environ[environment] = "/MOCK"

module_dir = Path(sys.modules["praw"].__file__).parent
environ_path = (
Path("/MOCK") / (".config" if environment == "HOME" else "") / "praw.ini"
)
locations = [
str(module_dir / "praw.ini"),
str(environ_path),
"praw.ini",
]
Expand Down Expand Up @@ -83,8 +81,7 @@ def test_load_ini_with_no_config_directory(self, mock_config):
prev_environment[key] = os.environ[key]
del os.environ[key]

module_dir = os.path.dirname(sys.modules["praw"].__file__)
locations = [os.path.join(module_dir, "praw.ini"), "praw.ini"]
locations = ["praw.ini"]

try:
Config._load_config()
Expand Down

0 comments on commit 42f5236

Please sign in to comment.