6
6
7
7
8
8
def import_string (dotted_path ):
9
- '''
9
+ """
10
10
This is a rough copy of django's import_string, which wasn't introduced until Django 1.7
11
11
12
12
Once this package's support for Django 1.6 has been removed, this can be safely replaced with
13
13
`from django.utils.module_loading import import_string`
14
- '''
14
+ """
15
15
try :
16
- module_path , class_name = dotted_path .rsplit ('.' , 1 )
16
+ module_path , class_name = dotted_path .rsplit ("." , 1 )
17
17
module = import_module (module_path )
18
18
return getattr (module , class_name )
19
19
except (ValueError , AttributeError , ImportError ):
20
- raise ImportError (' %s doesn\ ' t look like a valid module path' % dotted_path )
20
+ raise ImportError (" %s doesn't look like a valid module path" % dotted_path )
21
21
22
22
23
23
def get_loader (config_name ):
24
24
if config_name not in _loaders :
25
25
config = load_config (config_name )
26
- loader_class = import_string (config [' LOADER_CLASS' ])
26
+ loader_class = import_string (config [" LOADER_CLASS" ])
27
27
_loaders [config_name ] = loader_class (config_name , config )
28
28
return _loaders [config_name ]
29
29
@@ -33,13 +33,13 @@ def get_skip_common_chunks(config_name):
33
33
# The global default is currently False, whenever that is changed, change
34
34
# this fallback value as well which is present to provide backwards
35
35
# compatibility.
36
- return loader .config .get (' SKIP_COMMON_CHUNKS' , False )
36
+ return loader .config .get (" SKIP_COMMON_CHUNKS" , False )
37
37
38
38
39
39
def _filter_by_extension (bundle , extension ):
40
- ''' Return only files with the given extension'''
40
+ """ Return only files with the given extension"""
41
41
for chunk in bundle :
42
- if chunk [' name' ].endswith (' .{0}' .format (extension )):
42
+ if chunk [" name" ].endswith (" .{0}" .format (extension )):
43
43
yield chunk
44
44
45
45
@@ -50,63 +50,64 @@ def _get_bundle(loader, bundle_name, extension):
50
50
return bundle
51
51
52
52
53
- def get_files (bundle_name , extension = None , config = ' DEFAULT' ):
54
- ''' Returns list of chunks from named bundle'''
53
+ def get_files (bundle_name , extension = None , config = " DEFAULT" ):
54
+ """ Returns list of chunks from named bundle"""
55
55
loader = get_loader (config )
56
56
return list (_get_bundle (loader , bundle_name , extension ))
57
57
58
58
59
- def get_as_tags (bundle_name , extension = None , config = 'DEFAULT' , suffix = '' , attrs = '' , is_preload = False ):
60
- '''
59
+ def get_as_tags (
60
+ bundle_name , extension = None , config = "DEFAULT" , suffix = "" , attrs = "" , is_preload = False
61
+ ):
62
+ """
61
63
Get a list of formatted <script> & <link> tags for the assets in the
62
64
named bundle.
63
65
64
66
:param bundle_name: The name of the bundle
65
67
:param extension: (optional) filter by extension, eg. 'js' or 'css'
66
68
:param config: (optional) the name of the configuration
67
69
:return: a list of formatted tags as strings
68
- '''
70
+ """
69
71
70
72
loader = get_loader (config )
71
73
bundle = _get_bundle (loader , bundle_name , extension )
72
74
tags = []
73
75
74
76
for chunk in bundle :
75
- if chunk [' name' ].endswith ((' .js' , ' .js.gz' )):
77
+ if chunk [" name" ].endswith ((" .js" , " .js.gz" )):
76
78
if is_preload :
77
- tags .append ((
78
- '<link rel="preload" as="script" href="{0}" {1}/>'
79
- ).format ('' .join ([chunk ['url' ], suffix ]), attrs ))
79
+ tags .append (
80
+ ('<link rel="preload" as="script" href="{0}" {1}/>' ).format (
81
+ "" .join ([chunk ["url" ], suffix ]), attrs
82
+ )
83
+ )
80
84
else :
81
- tags .append ((
82
- '<script src="{0}"{2}{1}></script>'
83
- ).format (
84
- '' .join ([chunk ['url' ], suffix ]),
85
+ tags .append (
86
+ ('<script src="{0}"{2}{1}></script>' ).format (
87
+ "" .join ([chunk ["url" ], suffix ]),
88
+ attrs ,
89
+ loader .get_integrity_attr (chunk ),
90
+ )
91
+ )
92
+ elif chunk ["name" ].endswith ((".css" , ".css.gz" )):
93
+ tags .append (
94
+ ('<link href="{0}" rel={2}{3}{1}/>' ).format (
95
+ "" .join ([chunk ["url" ], suffix ]),
85
96
attrs ,
97
+ '"stylesheet"' if not is_preload else '"preload" as="style"' ,
86
98
loader .get_integrity_attr (chunk ),
87
- ))
88
- elif chunk ['name' ].endswith (('.css' , '.css.gz' )):
89
- tags .append ((
90
- '<link href="{0}" rel={2}{3}{1}/>'
91
- ).format (
92
- '' .join ([chunk ['url' ], suffix ]),
93
- attrs ,
94
- '"stylesheet"' if not is_preload else '"preload" as="style"' ,
95
- loader .get_integrity_attr (chunk ),
96
- ))
99
+ )
100
+ )
97
101
return tags
98
102
99
103
100
- def get_static (asset_name , config = ' DEFAULT' ):
101
- '''
104
+ def get_static (asset_name , config = " DEFAULT" ):
105
+ """
102
106
Equivalent to Django's 'static' look up but for webpack assets.
103
107
104
108
:param asset_name: the name of the asset
105
109
:param config: (optional) the name of the configuration
106
110
:return: path to webpack asset as a string
107
- '''
108
- public_path = get_loader (config ).get_assets ().get ('publicPath' )
109
- if not public_path or public_path == 'auto' :
110
- public_path = getattr (settings , 'STATIC_URL' )
111
-
112
- return '{0}{1}' .format (public_path , asset_name )
111
+ """
112
+ loader = get_loader (config )
113
+ return loader .get_asset_url (asset_name )
0 commit comments