1
- '''
1
+ """
2
2
Staticfiles finders for Dash assets
3
3
4
4
Copyright (c) 2018 Gibbs Consulting and others - see CONTRIBUTIONS.md
20
20
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
21
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
22
SOFTWARE.
23
- '''
23
+ """
24
24
25
25
import os
26
26
import importlib
38
38
from django_plotly_dash .dash_wrapper import all_apps
39
39
from django_plotly_dash .util import full_asset_path
40
40
41
+
41
42
class DashComponentFinder (BaseFinder ):
42
- ' Find static files in components'
43
+ " Find static files in components"
43
44
44
- #pylint: disable=abstract-method, redefined-builtin
45
+ # pylint: disable=abstract-method, redefined-builtin
45
46
46
47
def __init__ (self ):
47
-
48
48
self .locations = []
49
49
self .storages = OrderedDict ()
50
50
self .components = {}
51
51
52
- self .ignore_patterns = ["*.py" , "*.pyc" ,]
52
+ self .ignore_patterns = [
53
+ "*.py" ,
54
+ "*.pyc" ,
55
+ ]
53
56
54
57
try :
55
58
components = settings .PLOTLY_COMPONENTS
56
59
except :
57
60
components = []
58
61
59
- built_ins = [('dash' , ['dcc' , 'html' , 'dash_table' , 'deps' , 'dash-renderer' , 'dash-renderer/build' ]),
60
- ('plotly' , ['package_data' ]),
61
- ]
62
+ built_ins = [
63
+ (
64
+ "dash" ,
65
+ [
66
+ "dcc" ,
67
+ "html" ,
68
+ "dash_table" ,
69
+ "deps" ,
70
+ "dash-renderer" ,
71
+ "dash-renderer/build" ,
72
+ ],
73
+ ),
74
+ ("plotly" , ["package_data" ]),
75
+ ]
62
76
63
77
for component_name in components :
64
-
65
- split_name = component_name .split ('/' )
78
+ split_name = component_name .split ("/" )
66
79
try :
67
80
module_name = "." .join (split_name )
68
81
module = importlib .import_module (module_name )
69
82
path_directory = os .path .dirname (module .__file__ )
70
83
except :
71
84
module_name = "." .join (split_name [:- 1 ])
72
85
module = importlib .import_module (module_name )
73
- path_directory = os .path .join (os .path .dirname (module .__file__ ),
74
- split_name [- 1 ])
86
+ path_directory = os .path .join (
87
+ os .path .dirname (module .__file__ ), split_name [- 1 ]
88
+ )
75
89
76
90
root = path_directory
77
91
storage = FileSystemStorage (location = root )
@@ -89,20 +103,18 @@ def __init__(self):
89
103
self .components [path ] = component_name
90
104
91
105
for module_name , component_list in built_ins :
92
-
93
106
module = importlib .import_module (module_name )
94
107
95
108
for specific_component in component_list :
96
-
97
- path_directory = os .path .join ( os . path . dirname (module .__file__ ),
98
- specific_component )
109
+ path_directory = os . path . join (
110
+ os .path .dirname (module .__file__ ), specific_component
111
+ )
99
112
100
113
root = path_directory
101
114
component_name = f"{ module_name } /{ specific_component } "
102
115
path = f"dash/component/{ component_name } "
103
116
104
117
if path not in self .components :
105
-
106
118
storage = FileSystemStorage (location = root )
107
119
storage .prefix = path
108
120
@@ -113,16 +125,19 @@ def __init__(self):
113
125
114
126
super ().__init__ ()
115
127
116
- def find (self , path , all = False ):
128
+ def find (self , path , find_all = False , all = False ):
129
+ all = all or find_all
117
130
matches = []
118
131
for component_name in self .locations :
119
132
storage = self .storages [component_name ]
120
- location = storage .location # dir on disc
133
+ location = storage .location # dir on disc
121
134
122
135
component_path = "dash/component/%s" % component_name
123
- if len (path ) > len (component_path ) and path [:len (component_path )] == component_path :
124
-
125
- matched_path = os .path .join (location , path [len (component_path )+ 1 :])
136
+ if (
137
+ len (path ) > len (component_path )
138
+ and path [: len (component_path )] == component_path
139
+ ):
140
+ matched_path = os .path .join (location , path [len (component_path ) + 1 :])
126
141
if os .path .exists (matched_path ):
127
142
if not all :
128
143
return matched_path
@@ -132,7 +147,7 @@ def find(self, path, all=False):
132
147
133
148
# pylint: disable=inconsistent-return-statements, no-self-use
134
149
def find_location (self , path ):
135
- ' Return location, if it exists'
150
+ " Return location, if it exists"
136
151
if os .path .exists (path ):
137
152
return path
138
153
@@ -142,23 +157,25 @@ def list(self, ignore_patterns):
142
157
for path in get_files (storage , ignore_patterns + self .ignore_patterns ):
143
158
yield path , storage
144
159
160
+
145
161
class DashAppDirectoryFinder (BaseFinder ):
146
- ' Find static fies in application subdirectories'
162
+ " Find static files in application subdirectories"
147
163
148
164
def __init__ (self ):
149
165
# get all registered apps
150
166
151
167
self .locations = []
152
168
self .storages = OrderedDict ()
153
169
154
- self .ignore_patterns = ["*.py" , "*.pyc" ,]
170
+ self .ignore_patterns = [
171
+ "*.py" ,
172
+ "*.pyc" ,
173
+ ]
155
174
156
175
for app_config in apps .get_app_configs ():
157
-
158
- path_directory = os .path .join (app_config .path , 'assets' )
176
+ path_directory = os .path .join (app_config .path , "assets" )
159
177
160
178
if os .path .isdir (path_directory ):
161
-
162
179
storage = FileSystemStorage (location = path_directory )
163
180
164
181
storage .prefix = full_asset_path (app_config .name , "" )
@@ -168,8 +185,8 @@ def __init__(self):
168
185
169
186
super ().__init__ ()
170
187
171
- #pylint: disable=redefined-builtin
172
- def find (self , path , all = False ):
188
+ # pylint: disable=redefined-builtin
189
+ def find (self , path , all = False , ** kwargs ):
173
190
return []
174
191
175
192
def list (self , ignore_patterns ):
@@ -180,12 +197,11 @@ def list(self, ignore_patterns):
180
197
181
198
182
199
class DashAssetFinder (BaseFinder ):
183
- ' Find static files in asset directories'
200
+ " Find static files in asset directories"
184
201
185
- #pylint: disable=unused-import, unused-variable, no-name-in-module, import-error, abstract-method
202
+ # pylint: disable=unused-import, unused-variable, no-name-in-module, import-error, abstract-method
186
203
187
204
def __init__ (self ):
188
-
189
205
# Ensure urls are loaded
190
206
root_urls = settings .ROOT_URLCONF
191
207
importlib .import_module (root_urls )
@@ -197,37 +213,37 @@ def __init__(self):
197
213
self .locations = []
198
214
self .storages = OrderedDict ()
199
215
200
- self .ignore_patterns = ["*.py" , "*.pyc" ,]
216
+ self .ignore_patterns = [
217
+ "*.py" ,
218
+ "*.pyc" ,
219
+ ]
201
220
202
221
added_locations = {}
203
222
204
223
for app_slug , obj in self .apps .items ():
205
-
206
224
caller_module = obj .caller_module
207
225
location = obj .caller_module_location
208
226
subdir = obj .assets_folder
209
227
210
228
path_directory = os .path .join (os .path .dirname (location ), subdir )
211
229
212
230
if os .path .isdir (path_directory ):
213
-
214
231
component_name = app_slug
215
232
storage = FileSystemStorage (location = path_directory )
216
- path = full_asset_path (obj .caller_module .__name__ ,"" )
233
+ path = full_asset_path (obj .caller_module .__name__ , "" )
217
234
storage .prefix = path
218
235
219
236
self .locations .append (component_name )
220
237
self .storages [component_name ] = storage
221
238
222
239
super ().__init__ ()
223
240
224
- #pylint: disable=redefined-builtin
225
- def find (self , path , all = False ):
241
+ # pylint: disable=redefined-builtin
242
+ def find (self , path , all = False , ** kwargs ):
226
243
return []
227
244
228
245
def list (self , ignore_patterns ):
229
246
for component_name in self .locations :
230
247
storage = self .storages [component_name ]
231
248
for path in get_files (storage , ignore_patterns + self .ignore_patterns ):
232
249
yield path , storage
233
-
0 commit comments