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 committed Mar 11, 2024
1 parent 693e501 commit e286d00
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 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
1 change: 0 additions & 1 deletion src/pinnwand/handler/website.py
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,6 @@ def initialize(self, path: str) -> None:
self.path = path

async def get(self) -> None:

try:
with open(self.path, "rb") as f:
self.write(f.read())
Expand Down
25 changes: 25 additions & 0 deletions test/integration/test_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from pinnwand.configuration import Configuration, ConfigurationProvider
import tempfile
import toml


def test_load_environment_config(monkeypatch):
monkeypatch.setenv("PINNWAND_SPAMSCORE", "29")
config: Configuration = ConfigurationProvider.get_config(load_env=True)
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 = ConfigurationProvider.get_config()

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

0 comments on commit e286d00

Please sign in to comment.