|
3 | 3 | import os |
4 | 4 | import sys |
5 | 5 | import traceback |
| 6 | +import inspect |
6 | 7 | from importlib import util |
7 | 8 | from typing import Callable, Dict, List, Union |
8 | 9 |
|
9 | 10 | from flask import url_for |
10 | 11 |
|
11 | 12 | from .utilities import camel_to_snake, get_docstring, snake_to_spine |
| 13 | +from .views import View as BaseView |
12 | 14 | from .views.builder import static_from |
13 | 15 |
|
14 | 16 |
|
@@ -109,6 +111,10 @@ def add_view(self, view_class, *urls, endpoint=None, **kwargs): |
109 | 111 | for url in cleaned_urls: |
110 | 112 | self._rules[url] = self._views[endpoint] |
111 | 113 |
|
| 114 | + # Store this extension name as the View owner |
| 115 | + if issubclass(view_class, BaseView): |
| 116 | + view_class.set_extension(self.name) |
| 117 | + |
112 | 118 | def on_register(self, function, args=None, kwargs=None): |
113 | 119 | """ |
114 | 120 |
|
@@ -262,11 +268,37 @@ def find_extensions_in_file(extension_path: str, module_name="extensions") -> li |
262 | 268 | ) |
263 | 269 | return [] |
264 | 270 | else: |
265 | | - if hasattr(mod, "__extensions__"): |
| 271 | + # TODO: Add documentation links to warnings |
| 272 | + if hasattr(mod, "LABTHINGS_EXTENSIONS"): |
| 273 | + ext_objects = [] |
| 274 | + for ext_element in getattr(mod, "LABTHINGS_EXTENSIONS"): |
| 275 | + if inspect.isclass(ext_element) and issubclass( |
| 276 | + ext_element, BaseExtension |
| 277 | + ): |
| 278 | + ext_objects.append(ext_element()) |
| 279 | + elif isinstance(ext_element, BaseExtension): |
| 280 | + logging.warning( |
| 281 | + "%s: Extension instance passed instead of class. LABTHINGS_EXTENSIONS should contain classes, not instances.", |
| 282 | + ext_element, |
| 283 | + ) |
| 284 | + ext_objects.append(ext_element) |
| 285 | + else: |
| 286 | + logging.error( |
| 287 | + "Unsupported extension type %s. Skipping.", type(ext_element) |
| 288 | + ) |
| 289 | + return ext_objects |
| 290 | + elif hasattr(mod, "__extensions__"): |
| 291 | + logging.warning( |
| 292 | + "Explicit extension list using the __extensions__ global is deprecated. Please use LABTHINGS_EXTENSIONS instead." |
| 293 | + ) |
266 | 294 | return [ |
267 | 295 | getattr(mod, ext_name) for ext_name in getattr(mod, "__extensions__") |
268 | 296 | ] |
269 | 297 | else: |
| 298 | + logging.warning( |
| 299 | + "No LTX_MANIFEST found for %s. Searching for implicit extension objects.", |
| 300 | + extension_path, |
| 301 | + ) |
270 | 302 | return find_instances_in_module(mod, BaseExtension) |
271 | 303 |
|
272 | 304 |
|
|
0 commit comments