1
1
import contextlib
2
2
from collections import deque
3
3
4
+ from flask import current_app
4
5
from flask_frozen import UrlForLogger , Freezer
5
6
6
- absolute_urls_to_freeze = deque ()
7
-
8
7
def record_url (url ):
9
8
"""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 )
11
12
12
13
13
14
class AllLinksLogger (UrlForLogger ):
14
15
"""Logs ``url_for`` calls, but yields urls from ``absolute_urls_to_freeze`` as well.
15
16
"""
16
17
18
+ def __init__ (self , app , urls_to_freeze ):
19
+ super ().__init__ (app )
20
+ self .naucse_urls_to_freeze = urls_to_freeze
21
+
17
22
def iter_calls (self ):
18
23
"""Yield all logged urls and links parsed from content.
19
24
"""
20
25
# Unfortunately, ``yield from`` cannot be used as the queues are
21
26
# 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 :
24
29
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 ()
30
36
31
37
32
38
@contextlib .contextmanager
@@ -50,5 +56,10 @@ class NaucseFreezer(Freezer):
50
56
def __init__ (self , app ):
51
57
super ().__init__ (app )
52
58
59
+ urls_to_freeze = deque ()
60
+
61
+ with app .app_context ():
62
+ app .config ['NAUCSE_ABSOLUTE_URLS_TO_FREEZE' ] = urls_to_freeze
63
+
53
64
# 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