Skip to content

Commit

Permalink
fix AttributeError thrown by str.removeprefix when python < 3.9
Browse files Browse the repository at this point in the history
  • Loading branch information
shtlrs authored and supakeen committed Mar 11, 2024
1 parent db64a8a commit d2098a9
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/pinnwand/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,13 @@ def load_config_file(self, path: Optional[str] = None) -> None:

def load_environment(self):
"""Load configuration from the environment, if any."""
prefix = "PINNWAND_"
for key, value in os.environ.items():
if not key.startswith("PINNWAND_"):
if not key.startswith(prefix):
continue

key = key.removeprefix("PINNWAND_")
key = key[len(prefix) :]
key = key.lower()

try:
value = ast.literal_eval(value)
setattr(self, f"_{key}", value)
Expand Down
26 changes: 26 additions & 0 deletions test/integration/test_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from pinnwand.configuration import Configuration
import tempfile
import toml


def test_load_environment_config(monkeypatch):
monkeypatch.setenv("PINNWAND_SPAMSCORE", "29")
config = Configuration()
config.load_environment()
assert config.spamscore == 29


def test_load_file_config():
with tempfile.NamedTemporaryFile(suffix="", delete=False) as temp:

props = toml.load(temp.name)
url = "sqlite:///database.db"
props["database_uri"] = url

with open(temp.name, "w") as config_file:
toml.dump(props, config_file)
config: Configuration = Configuration()

config.load_config_file(temp.name)
assert config.database_uri == url

0 comments on commit d2098a9

Please sign in to comment.