Skip to content

Commit

Permalink
fix(fal): fix 3.8 support and deserialization errors (#435)
Browse files Browse the repository at this point in the history
* fix: support 3.8

* fix: fix deserialization

* fix typing

* lazy import tomli
  • Loading branch information
efiop authored Feb 28, 2025
1 parent 7893bad commit 2042021
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
4 changes: 2 additions & 2 deletions projects/fal/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ dependencies = [
"pyjwt[crypto]>=2.8.0,<3",
"uvicorn>=0.29.0,<1",
"cookiecutter",
"tomli==2.2.1",
"tomli-w==1.2.0",
"tomli>2,<3",
"tomli-w>=1,<2",
]

[project.optional-dependencies]
Expand Down
1 change: 1 addition & 0 deletions projects/fal/src/fal/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
"starlette_exporter",
"structlog",
"tomli",
"tomli-w",
]


Expand Down
25 changes: 13 additions & 12 deletions projects/fal/src/fal/config.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
import os
from typing import Optional

import tomli
import tomli_w
from typing import Dict, List, Optional

SETTINGS_SECTION = "__internal__"


class Config:
_config: dict[str, dict[str, str]]
_config: Dict[str, Dict[str, str]]
_profile: Optional[str]

DEFAULT_CONFIG_PATH = "~/.fal/config.toml"

def __init__(self):
import tomli

self.config_path = os.path.expanduser(
os.getenv("FAL_CONFIG_PATH", self.DEFAULT_CONFIG_PATH)
)
Expand All @@ -35,21 +34,23 @@ def profile(self) -> Optional[str]:
return self._profile

@profile.setter
def profile(self, value: Optional[str]):
def profile(self, value: Optional[str]) -> None:
if value and value not in self._config:
self._config[value] = {}

self._profile = value

def profiles(self):
def profiles(self) -> List[str]:
keys = []
for key in self._config:
if key != SETTINGS_SECTION:
keys.append(key)

return keys

def save(self):
def save(self) -> None:
import tomli_w

with open(self.config_path, "wb") as file:
tomli_w.dump(self._config, file)

Expand All @@ -59,19 +60,19 @@ def get(self, key: str) -> Optional[str]:

return self._config.get(self.profile, {}).get(key)

def set(self, key: str, value: str):
def set(self, key: str, value: str) -> None:
if not self.profile:
raise ValueError("No profile set.")

self._config[self.profile][key] = value

def get_internal(self, key):
def get_internal(self, key: str) -> Optional[str]:
if SETTINGS_SECTION not in self._config:
self._config[SETTINGS_SECTION] = {}

return self._config[SETTINGS_SECTION].get(key)

def set_internal(self, key, value):
def set_internal(self, key: str, value: Optional[str]) -> None:
if SETTINGS_SECTION not in self._config:
self._config[SETTINGS_SECTION] = {}

Expand All @@ -80,5 +81,5 @@ def set_internal(self, key, value):
else:
self._config[SETTINGS_SECTION][key] = value

def delete(self, profile):
def delete(self, profile: str) -> None:
del self._config[profile]

0 comments on commit 2042021

Please sign in to comment.