11import contextlib
22from collections import deque
33
4+ from flask import current_app
45from flask_frozen import UrlForLogger , Freezer
56
6- absolute_urls_to_freeze = deque ()
7-
87def record_url (url ):
98 """Logs that `url` should be included in the resulting static site"""
10- absolute_urls_to_freeze .append (url )
9+ urls_to_freeze = current_app .config .get ('NAUCSE_ABSOLUTE_URLS_TO_FREEZE' )
10+ if urls_to_freeze is not None :
11+ urls_to_freeze .append (url )
1112
1213
1314class AllLinksLogger (UrlForLogger ):
1415 """Logs ``url_for`` calls, but yields urls from ``absolute_urls_to_freeze`` as well.
1516 """
1617
18+ def __init__ (self , app , urls_to_freeze ):
19+ super ().__init__ (app )
20+ self .naucse_urls_to_freeze = urls_to_freeze
21+
1722 def iter_calls (self ):
1823 """Yield all logged urls and links parsed from content.
1924 """
2025 # Unfortunately, ``yield from`` cannot be used as the queues are
2126 # modified on the go.
22- while self .logged_calls or absolute_urls_to_freeze :
23- if self .logged_calls :
27+ while self .logged_calls or self . naucse_urls_to_freeze :
28+ while self .logged_calls :
2429 yield self .logged_calls .popleft ()
25- # prefer urls from :atrr:`logged_calls` - so, ideally,
26- # cache is populated from the base repository
27- continue
28- if absolute_urls_to_freeze :
29- yield absolute_urls_to_freeze .popleft ()
30+ # Prefer URLs from logged_calls - ideally, cache is populated
31+ # from the base repository.
32+ # That means we only yield from urls_to_freeze
33+ # if there are no logged_calls.
34+ if self .naucse_urls_to_freeze :
35+ yield self .naucse_urls_to_freeze .popleft ()
3036
3137
3238@contextlib .contextmanager
@@ -50,5 +56,10 @@ class NaucseFreezer(Freezer):
5056 def __init__ (self , app ):
5157 super ().__init__ (app )
5258
59+ urls_to_freeze = deque ()
60+
61+ with app .app_context ():
62+ app .config ['NAUCSE_ABSOLUTE_URLS_TO_FREEZE' ] = urls_to_freeze
63+
5364 # override the default url_for_logger with our modified version
54- self .url_for_logger = AllLinksLogger (app )
65+ self .url_for_logger = AllLinksLogger (app , urls_to_freeze )
0 commit comments