| 1101 | |
| 1102 | # pylint: disable=too-many-branches |
| 1103 | def _collect_and_register_resources( |
| 1104 | self, resources, include_async=True, url_attr="src" |
| 1105 | ): |
| 1106 | # now needs the app context. |
| 1107 | # template in the necessary component suite JS bundles |
| 1108 | # add the version number of the package as a query parameter |
| 1109 | # for cache busting |
| 1110 | def _relative_url_path(relative_package_path="", namespace=""): |
| 1111 | if any( |
| 1112 | relative_package_path.startswith(x + "/") |
| 1113 | for x in ["dcc", "html", "dash_table"] |
| 1114 | ): |
| 1115 | relative_package_path = relative_package_path.replace("dash.", "") |
| 1116 | version = importlib.import_module( |
| 1117 | f"{namespace}.{os.path.split(relative_package_path)[0]}" |
| 1118 | ).__version__ |
| 1119 | else: |
| 1120 | version = importlib.import_module(namespace).__version__ |
| 1121 | |
| 1122 | module_path = os.path.join( # type: ignore[reportCallIssue] |
| 1123 | os.path.dirname(sys.modules[namespace].__file__), # type: ignore[reportCallIssue] |
| 1124 | relative_package_path, |
| 1125 | ) |
| 1126 | |
| 1127 | modified = int(os.stat(module_path).st_mtime) |
| 1128 | |
| 1129 | fingerprint = build_fingerprint(relative_package_path, version, modified) |
| 1130 | return f"{self.config.requests_pathname_prefix}_dash-component-suites/{namespace}/{fingerprint}" |
| 1131 | |
| 1132 | srcs = [] |
| 1133 | for resource in resources: |
| 1134 | is_dynamic_resource = resource.get("dynamic", False) |
| 1135 | is_async = resource.get("async") is not None |
| 1136 | excluded = not include_async and is_async |
| 1137 | |
| 1138 | if "relative_package_path" in resource: |
| 1139 | paths = resource["relative_package_path"] |
| 1140 | paths = [paths] if isinstance(paths, str) else paths |
| 1141 | |
| 1142 | for rel_path in paths: |
| 1143 | if any(x in rel_path for x in ["dcc", "html", "dash_table"]): |
| 1144 | rel_path = rel_path.replace("dash.", "") |
| 1145 | |
| 1146 | self.registered_paths[resource["namespace"]].add(rel_path) |
| 1147 | |
| 1148 | if not is_dynamic_resource and not excluded: |
| 1149 | url = _relative_url_path( |
| 1150 | relative_package_path=rel_path, |
| 1151 | namespace=resource["namespace"], |
| 1152 | ) |
| 1153 | if "attributes" in resource: |
| 1154 | srcs.append({url_attr: url, **resource["attributes"]}) |
| 1155 | else: |
| 1156 | srcs.append(url) |
| 1157 | elif "external_url" in resource: |
| 1158 | if not is_dynamic_resource and not excluded: |
| 1159 | urls = ( |
| 1160 | [resource["external_url"]] |