Skip to content

Commit

Permalink
Merge 2e34162 into 5d35bda
Browse files Browse the repository at this point in the history
  • Loading branch information
Schlepptop committed Mar 14, 2022
2 parents 5d35bda + 2e34162 commit 5e4af4a
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 36 deletions.
6 changes: 3 additions & 3 deletions leaflet/__init__.py
Expand Up @@ -6,15 +6,15 @@
from django.core.exceptions import ImproperlyConfigured
from django.templatetags.static import static
from django.utils.translation import gettext_lazy as _
from django.utils.functional import lazy

from .utils import memoized_lazy_function, ListWithLazyItems, ListWithLazyItemsRawIterator
from .utils import ListWithLazyItems, ListWithLazyItemsRawIterator


DEFAULT_TILES = [(_('OSM'), '//{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
'© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors')]

LEAFLET_CONFIG = getattr(settings, 'LEAFLET_CONFIG', {})

app_settings = dict({
'TILES': DEFAULT_TILES,
'OVERLAYS': [],
Expand Down Expand Up @@ -168,7 +168,7 @@ def _normalize_plugins_config():
pass
else:
# pass relative URL through django.contrib.staticfiles
urls[i] = memoized_lazy_function(static, url) # lazy variant of `static(url)`
urls[i] = lazy(static, str)(url) # lazy variant of `static(url)`

urls = ListWithLazyItems(urls)
plugin_dict[resource_type] = urls
Expand Down
35 changes: 2 additions & 33 deletions leaflet/utils.py
Expand Up @@ -3,30 +3,6 @@
from django.utils.functional import Promise


class memoized_lazy_function(Promise):
"""
Represents a lazy value which is calculated by calling
func(*args, **kwargs) and then is memoized.
>>> f = memoized_lazy_function(lambda a: print('.') or a, 'hello')
>>> f()
.
'hello'
>>> f()
'hello'
"""

def __init__(self, func, *args, **kwargs):
self._func = func
self._args = args
self._kwargs = kwargs

def __call__(self):
if not hasattr(self, '_result'):
self._result = self._func(*self._args, **self._kwargs)
return self._result


class ListWithLazyItems(Sequence):
"""
Mimics a lazy list.
Expand All @@ -45,13 +21,13 @@ def __init__(self, iterable=()):

def __iter__(self):
for item in self._list:
yield self._resolve_lazy_item(item)
yield item

def __len__(self):
return len(self._list)

def __getitem__(self, index):
return self._resolve_lazy_item(self._list[index])
return self._list[index]

def extend(self, iterable):
if isinstance(iterable, ListWithLazyItems):
Expand All @@ -68,13 +44,6 @@ def __add__(self, iterable):
lazy_list.extend(iterable)
return lazy_list

@classmethod
def _resolve_lazy_item(cls, item):
if cls.is_lazy_item(item):
item = item()

return item

@classmethod
def is_lazy_item(cls, item):
return isinstance(item, Promise)
Expand Down

0 comments on commit 5e4af4a

Please sign in to comment.