Skip to content

Commit

Permalink
Marking internal properties as private
Browse files Browse the repository at this point in the history
Providing accessors, to hint that we are accessing mutable state
  • Loading branch information
blast-hardcheese committed Feb 24, 2024
1 parent 67621e4 commit 8ddb318
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/replit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,5 @@ def clear() -> None:
# lazily.
def __getattr__(name: str) -> Any:
if name == "db":
return LazyDB.get_instance().db
return LazyDB.get_db()
raise AttributeError(name)
4 changes: 2 additions & 2 deletions src/replit/database/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
# lazily.
def __getattr__(name: str) -> Any:
if name == "db":
return LazyDB.get_instance().db
return LazyDB.get_db()
if name == "db_url":
return LazyDB.get_instance().db_url
return LazyDB.get_db_url()
raise AttributeError(name)
28 changes: 18 additions & 10 deletions src/replit/database/default_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ class LazyDB:
_instance: Optional["LazyDB"] = None

def __init__(self) -> None:
self.db: Optional[Database] = None
self.db_url = get_db_url()
if self.db_url:
self.db = Database(self.db_url)
self._db: Optional[Database] = None
self._db_url = get_db_url()
if self._db_url:
self._db = Database(self._db_url)
self.refresh_db()
else:
logging.warning(
Expand All @@ -38,11 +38,11 @@ def __init__(self) -> None:

def refresh_db(self) -> None:
"""Refresh the DB URL every hour."""
if not self.db:
if not self._db:
return
self.db_url = get_db_url()
if self.db_url:
self.db.update_db_url(self.db_url)
self._db_url = get_db_url()
if self._db_url:
self._db.update_db_url(self._db_url)
threading.Timer(3600, self.refresh_db).start()

@classmethod
Expand All @@ -52,14 +52,22 @@ def get_instance(cls) -> "LazyDB":
cls._instance = LazyDB()
return cls._instance

@classmethod
def get_db(cls) -> Optional[Database]:
return cls.get_instance()._db

@classmethod
def get_db_url(cls) -> Optional[str]:
return cls.get_instance()._db_url


# Previous versions of this library would just have side-effects and always set
# up a database unconditionally. That is very undesirable, so instead of doing
# that, we are using this egregious hack to get the database / database URL
# lazily.
def __getattr__(name: str) -> Any:
if name == "db":
return LazyDB.get_instance().db
return LazyDB.get_db()
if name == "db_url":
return LazyDB.get_instance().db_url
return LazyDB.get_db_url()
raise AttributeError(name)
8 changes: 4 additions & 4 deletions src/replit/database/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def make_database_proxy_blueprint(view_only: bool, prefix: str = "") -> Blueprin
app = Blueprint("database_proxy" + ("_view_only" if view_only else ""), __name__)

def list_keys() -> Any:
db = LazyDB.get_instance().db
db = LazyDB.get_db()
if db is None:
return "Database is not configured", 500
user_prefix = request.args.get("prefix", "")
Expand All @@ -34,7 +34,7 @@ def list_keys() -> Any:
return "\n".join(keys)

def set_key() -> Any:
db = LazyDB.get_instance().db
db = LazyDB.get_db()
if db is None:
return "Database is not configured", 500
if view_only:
Expand All @@ -50,7 +50,7 @@ def index() -> Any:
return set_key()

def get_key(key: str) -> Any:
db = LazyDB.get_instance().db
db = LazyDB.get_db()
if db is None:
return "Database is not configured", 500
try:
Expand All @@ -59,7 +59,7 @@ def get_key(key: str) -> Any:
return "", 404

def delete_key(key: str) -> Any:
db = LazyDB.get_instance().db
db = LazyDB.get_db()
if db is None:
return "Database is not configured", 500
if view_only:
Expand Down
2 changes: 1 addition & 1 deletion src/replit/web/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@
# lazily.
def __getattr__(name: str) -> Any:
if name == "db":
return LazyDB.get_instance().db
return LazyDB.get_db()
raise AttributeError(name)
6 changes: 3 additions & 3 deletions src/replit/web/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ def set_value(self, value: str) -> None:
Raises:
RuntimeError: Raised if the database is not configured.
"""
db = LazyDB.get_instance().db
db = LazyDB.get_db()
if db is None:
raise RuntimeError("database not configured")
db[self.db_key()] = value

def _ensure_value(self) -> Any:
db = LazyDB.get_instance().db
db = LazyDB.get_db()
if db is None:
raise RuntimeError("database not configured")
try:
Expand Down Expand Up @@ -110,7 +110,7 @@ def __getitem__(self, name: str) -> User:
return User(username=name, prefix=self.prefix)

def __iter__(self) -> Iterator[str]:
db = LazyDB.get_instance().db
db = LazyDB.get_db()
if db is None:
raise RuntimeError("database not configured")
for k in db.keys():
Expand Down

0 comments on commit 8ddb318

Please sign in to comment.