Skip to content

settings

pymonzo settings related code.

PyMonzoSettings

Bases: BaseSettings

pymonzo settings schema.

Attributes:

Name Type Description
token Dict[str, Union[str, int]]

OAuth token. For more information see pymonzo.MonzoAPI.authorize.

client_id Optional[str]

OAuth client ID.

client_secret Optional[str]

OAuth client secret.

Source code in pymonzo/settings.py
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
class PyMonzoSettings(BaseSettings):
    """pymonzo settings schema.

    Attributes:
        token: OAuth token. For more information see [`pymonzo.MonzoAPI.authorize`][].
        client_id: OAuth client ID.
        client_secret: OAuth client secret.
    """

    model_config = SettingsConfigDict(env_prefix="pymonzo_")

    token: Dict[str, Union[str, int]]
    client_id: Optional[str] = None
    client_secret: Optional[str] = None

    @classmethod
    def load_from_disk(cls, settings_path: Path) -> Self:
        """Load pymonzo settings from disk.

        Arguments:
            settings_path: Settings file path.

        Returns:
            Loaded pymonzo settings.
        """
        with open(settings_path) as f:
            settings = json.load(f)

        return cls(**settings)

    def save_to_disk(self, settings_path: Path) -> None:
        """Save pymonzo settings on disk.

        Arguments:
            settings_path: Settings file path.
        """
        # Make sure the file is not publicly accessible
        # Source: https://github.com/python/cpython/issues/73400
        os.umask(0o077)
        with open(settings_path, "w", opener=partial(os.open, mode=0o600)) as f:
            f.write(self.model_dump_json(indent=2))

load_from_disk(settings_path) classmethod

Load pymonzo settings from disk.

Parameters:

Name Type Description Default
settings_path Path

Settings file path.

required

Returns:

Type Description
Self

Loaded pymonzo settings.

Source code in pymonzo/settings.py
33
34
35
36
37
38
39
40
41
42
43
44
45
46
@classmethod
def load_from_disk(cls, settings_path: Path) -> Self:
    """Load pymonzo settings from disk.

    Arguments:
        settings_path: Settings file path.

    Returns:
        Loaded pymonzo settings.
    """
    with open(settings_path) as f:
        settings = json.load(f)

    return cls(**settings)

save_to_disk(settings_path)

Save pymonzo settings on disk.

Parameters:

Name Type Description Default
settings_path Path

Settings file path.

required
Source code in pymonzo/settings.py
48
49
50
51
52
53
54
55
56
57
58
def save_to_disk(self, settings_path: Path) -> None:
    """Save pymonzo settings on disk.

    Arguments:
        settings_path: Settings file path.
    """
    # Make sure the file is not publicly accessible
    # Source: https://github.com/python/cpython/issues/73400
    os.umask(0o077)
    with open(settings_path, "w", opener=partial(os.open, mode=0o600)) as f:
        f.write(self.model_dump_json(indent=2))