feluelle commented on a change in pull request #6696: [AIRFLOW-6128] Simplify 
plugins_manager and webserver plugin code
URL: https://github.com/apache/airflow/pull/6696#discussion_r363355724
 
 

 ##########
 File path: airflow/plugins_manager.py
 ##########
 @@ -159,161 +143,195 @@ def is_valid_plugin(plugin_obj, existing_plugins):
     return False
 
 
-plugins = []  # type: List[AirflowPlugin]
+plugins: List[AirflowPlugin] = []
+stat_name_handlers: List[Callable[[str], str]] = []
+
+
+def load_entrypoint_plugins(entry_points: Generator[EntryPoint, None, None]) 
-> None:
+    """
+    Load AirflowPlugin subclasses from the entrypoints
+    provided. The entry_point group should be 'airflow.plugins'.
+
+    :param entry_points: A collection of entrypoints to search for plugins
+    :type entry_points: Generator[setuptools.EntryPoint, None, None]
+    """
+    for entry_point in entry_points:
+        log.debug('Importing entry_point plugin %s', entry_point.name)
+        plugin_obj = entry_point.load()
+        if is_valid_plugin(plugin_obj, plugins):
+            if callable(getattr(plugin_obj, 'on_load', None)):
+                plugin_obj.on_load()
+                plugins.append(plugin_obj)
+
 
-norm_pattern = re.compile(r'[/|.]')
+load_entrypoint_plugins(pkg_resources.iter_entry_points('airflow.plugins'))
 
-assert settings.PLUGINS_FOLDER, "Plugins folder is not set"
 
-# Crawl through the plugins folder to find AirflowPlugin derivatives
-for root, dirs, files in os.walk(settings.PLUGINS_FOLDER, followlinks=True):
-    for f in files:
-        filepath = os.path.join(root, f)
-        try:
-            if not os.path.isfile(filepath):
-                continue
-            mod_name, file_ext = os.path.splitext(
-                os.path.split(filepath)[-1])
-            if file_ext != '.py':
-                continue
+def import_plugin(filepath: str, mod_name: str, root: str) -> None:
+    """Imports plugin module."""
+    log.debug('Importing plugin module %s', filepath)
+    # normalize root path as namespace
+    namespace = '_'.join([re.sub(NORM_PATTERN, '__', root), mod_name])
+    # noinspection PyDeprecation
+    module = imp.load_source(namespace, filepath)
+    for obj in list(module.__dict__.values()):
+        if is_valid_plugin(obj, plugins):
+            plugins.append(obj)
 
-            log.debug('Importing plugin module %s', filepath)
-            # normalize root path as namespace
-            namespace = '_'.join([re.sub(norm_pattern, '__', root), mod_name])
 
-            m = imp.load_source(namespace, filepath)
-            for obj in list(m.__dict__.values()):
-                if is_valid_plugin(obj, plugins):
-                    plugins.append(obj)
-        except Exception as e:  # pylint: disable=broad-except
-            log.exception(e)
-            path = filepath or str(f)
-            log.error('Failed to import plugin %s', path)
-            import_errors[path] = str(e)
+def try_to_import_plugin(filepath: str, root: str) -> None:
+    """Tries to import plugin from the file specified. Skips it if not a valid 
python file."""
+    if not os.path.isfile(filepath):
+        return
+    mod_name, file_ext = os.path.splitext(os.path.split(filepath)[-1])
+    if file_ext != '.py':
+        return
+    import_plugin(filepath=filepath, mod_name=mod_name, root=root)
 
-plugins = load_entrypoint_plugins(
-    pkg_resources.iter_entry_points('airflow.plugins'),
-    plugins
-)
+
+def find_airflow_plugins() -> None:
+    """Crawl through the plugins folder to find AirflowPlugin derivatives"""
+    assert settings.PLUGINS_FOLDER, "Plugins folder is not set"
 
 Review comment:
   I thought we did decide to not use `assert` for raising exceptions.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to