From bc433f235f6d0c00aa7b7db483b8a41ba58135b1 Mon Sep 17 00:00:00 2001 From: Danielle McLean Date: Fri, 22 Jun 2018 12:31:03 +1000 Subject: [PATCH] Use a mypy-friendly approach to loading and caching the package.json file --- lemoncurry/utils.py | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/lemoncurry/utils.py b/lemoncurry/utils.py index 6b5b3ad..5f6f786 100644 --- a/lemoncurry/utils.py +++ b/lemoncurry/utils.py @@ -6,12 +6,27 @@ from django.http import HttpResponse, JsonResponse from django.http import HttpResponseForbidden, HttpResponseBadRequest from django.utils.html import strip_tags from os.path import join -from types import SimpleNamespace +from typing import Any, Dict, Optional from urllib.parse import urlencode, urljoin, urlparse from .templatetags.markdown import markdown -cache = SimpleNamespace(package_json=None) + +class PackageJson: + data: Optional[Dict[str, Any]] + + def __init__(self) -> None: + self.data = None + + def load(self) -> Dict[str, Any]: + if self.data is None: + with open(join(settings.BASE_DIR, 'package.json')) as f: + self.data = json.load(f) + assert self.data is not None + return self.data + + +PACKAGE = PackageJson() def friendly_url(url): @@ -19,12 +34,8 @@ def friendly_url(url): return netloc + path -def load_package_json(): - if cache.package_json: - return cache.package_json - with open(join(settings.BASE_DIR, 'package.json')) as f: - cache.package_json = json.load(f) - return cache.package_json +def load_package_json() -> Dict[str, Any]: + return PACKAGE.load() def origin(request):