Skip to content

Commit de541d1

Browse files
committed
refactor: Make config and storage a singleton
Removes the need for a util method to implement a singleton-like structure.
1 parent 57d0e2e commit de541d1

File tree

5 files changed

+25
-33
lines changed

5 files changed

+25
-33
lines changed

src/pyddns/config.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,17 @@
1313
import os
1414

1515

16+
from typing import Optional
17+
18+
1619
class Config:
20+
_instance: Optional["Config"] = None
21+
22+
def __new__(cls, *args, **kwargs):
23+
if cls._instance is None:
24+
cls._instance = super(Config, cls).__new__(cls)
25+
return cls._instance
26+
1727
"""
1828
A class to manage configuration settings using ConfigParser.
1929

src/pyddns/services/cloudflare_service.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
)
2121
from cloudflare.types.dns import RecordResponse
2222

23-
from pyddns.utils import _get_config, _get_storage
23+
from pyddns.config import Config
24+
from pyddns.storage import Storage
2425
from pyddns.client import DDNSClient
2526

2627

@@ -37,8 +38,8 @@ def __init__(
3738
) -> None:
3839
logging.debug("CloudFlare DNS: Initializing Cloudflare_DDNS client.")
3940
self.service_name = "Cloudflare"
40-
self.config = _get_config()
41-
self.storage = _get_storage()
41+
self.config = Config()
42+
self.storage = Storage()
4243

4344
self.zone_id: str = zone_id or self.config.get(
4445
self.service_name, "zone_id"

src/pyddns/services/duckdns_service.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414

1515
import requests
1616

17-
from pyddns.utils import _get_config, _get_storage
17+
from pyddns.config import Config
18+
from pyddns.storage import Storage
1819
from pyddns.client import DDNSClient
1920

2021

@@ -31,8 +32,8 @@ def __init__(self, token: Optional[str] = None):
3132
self.url = "https://www.duckdns.org/update"
3233
self.service_name = "Duckdns"
3334

34-
self.config = _get_config()
35-
self.storage = _get_storage()
35+
self.config = Config()
36+
self.storage = Storage()
3637
self.token = token or self.config.get(self.service_name, "token")
3738

3839
def _obtain_record(

src/pyddns/cache.py renamed to src/pyddns/storage.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@
1313

1414

1515
class Storage:
16+
_instance: Optional["Storage"] = None
17+
18+
def __new__(cls, *args, **kwargs):
19+
if cls._instance is None:
20+
cls._instance = super(Storage, cls).__new__(cls)
21+
return cls._instance
22+
1623
"""
1724
A class to manage SQLite database operations for DDNS services.
1825

src/pyddns/utils.py

Lines changed: 0 additions & 27 deletions
This file was deleted.

0 commit comments

Comments
 (0)