|
| 1 | +''' |
| 2 | +Staticfiles finders for Dash assets |
| 3 | +
|
| 4 | +Copyright (c) 2018 Gibbs Consulting and others - see CONTRIBUTIONS.md |
| 5 | +
|
| 6 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | +of this software and associated documentation files (the "Software"), to deal |
| 8 | +in the Software without restriction, including without limitation the rights |
| 9 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | +copies of the Software, and to permit persons to whom the Software is |
| 11 | +furnished to do so, subject to the following conditions: |
| 12 | +
|
| 13 | +The above copyright notice and this permission notice shall be included in all |
| 14 | +copies or substantial portions of the Software. |
| 15 | +
|
| 16 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | +SOFTWARE. |
| 23 | +''' |
| 24 | + |
| 25 | +import os |
| 26 | +import importlib |
| 27 | + |
| 28 | +from collections import OrderedDict |
| 29 | + |
| 30 | +from django.contrib.staticfiles.finders import BaseFinder |
| 31 | +from django.contrib.staticfiles.utils import get_files |
| 32 | + |
| 33 | +from django.core.files.storage import FileSystemStorage |
| 34 | + |
| 35 | +from django.conf import settings |
| 36 | +from django.apps import apps |
| 37 | + |
| 38 | +from django_plotly_dash.dash_wrapper import all_apps |
| 39 | + |
| 40 | +class DashComponentFinder(BaseFinder): |
| 41 | + 'Find static files in components' |
| 42 | + |
| 43 | + def __init__(self): |
| 44 | + |
| 45 | + self.locations = [] |
| 46 | + self.storages = OrderedDict() |
| 47 | + self.components = {} |
| 48 | + |
| 49 | + self.ignore_patterns = ["*.py", "*.pyc",] |
| 50 | + |
| 51 | + for component_name in settings.PLOTLY_COMPONENTS: |
| 52 | + |
| 53 | + module = importlib.import_module(component_name) |
| 54 | + path_directory = os.path.dirname(module.__file__) |
| 55 | + |
| 56 | + root = path_directory |
| 57 | + storage = FileSystemStorage(location=root) |
| 58 | + path = "dash/component/%s" % component_name |
| 59 | + |
| 60 | + # Path_directory is where from |
| 61 | + # path is desired url mount point of the content of path_directory |
| 62 | + # component_name is the name of the component |
| 63 | + |
| 64 | + storage.prefix = path |
| 65 | + |
| 66 | + self.locations.append(component_name) |
| 67 | + |
| 68 | + self.storages[component_name] = storage |
| 69 | + self.components[path] = component_name |
| 70 | + |
| 71 | + super(DashComponentFinder, self).__init__() |
| 72 | + |
| 73 | + def find(self, path, all=False): |
| 74 | + matches = [] |
| 75 | + for component_name in self.locations: |
| 76 | + storage = self.storages[component_name] |
| 77 | + location = storage.location # dir on disc |
| 78 | + |
| 79 | + component_path = "dash/component/%s" % component_name |
| 80 | + if len(path) > len(component_path) and path[:len(component_path)] == component_path: |
| 81 | + |
| 82 | + matched_path = os.path.join(location, path[len(component_path)+1:]) |
| 83 | + if os.path.exists(matched_path): |
| 84 | + if not all: |
| 85 | + return matched_path |
| 86 | + matches.append(matched_path) |
| 87 | + |
| 88 | + return matches |
| 89 | + |
| 90 | + def find_location(self, path): |
| 91 | + if os.path.exists(path): |
| 92 | + return path |
| 93 | + |
| 94 | + def list(self, ignore_patterns): |
| 95 | + for component_name in self.locations: |
| 96 | + storage = self.storages[component_name] |
| 97 | + for path in get_files(storage, ignore_patterns + self.ignore_patterns): |
| 98 | + print("DashAssetFinder",path,storage) |
| 99 | + yield path, storage |
| 100 | + |
| 101 | +class DashAssetFinder(BaseFinder): |
| 102 | + 'Find static files in asset directories' |
| 103 | + |
| 104 | + def __init__(self): |
| 105 | + |
| 106 | + # Get all registered apps |
| 107 | + |
| 108 | + import demo.plotly_apps |
| 109 | + import demo.dash_apps |
| 110 | + |
| 111 | + self.apps = all_apps() |
| 112 | + |
| 113 | + self.subdir = 'assets' |
| 114 | + |
| 115 | + self.locations = [] |
| 116 | + self.storages = OrderedDict() |
| 117 | + |
| 118 | + self.ignore_patterns = ["*.py", "*.pyc",] |
| 119 | + |
| 120 | + for app_slug, obj in self.apps.items(): |
| 121 | + caller_module = obj.caller_module |
| 122 | + location = obj.caller_module_location |
| 123 | + path_directory = os.path.join(os.path.dirname(location),self.subdir) |
| 124 | + |
| 125 | + if os.path.isdir(path_directory): |
| 126 | + |
| 127 | + component_name = app_slug |
| 128 | + storage = FileSystemStorage(location=path_directory) |
| 129 | + path = "dash/assets/%s" % component_name |
| 130 | + storage.prefix = path |
| 131 | + |
| 132 | + self.locations.append(component_name) |
| 133 | + self.storages[component_name] = storage |
| 134 | + |
| 135 | + super(DashAssetFinder, self).__init__() |
| 136 | + |
| 137 | + def find(self, path, all=False): |
| 138 | + return [] |
| 139 | + |
| 140 | + def list(self, ignore_patterns): |
| 141 | + for component_name in self.locations: |
| 142 | + storage = self.storages[component_name] |
| 143 | + for path in get_files(storage, ignore_patterns + self.ignore_patterns): |
| 144 | + yield path, storage |
0 commit comments