Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More friendly output of the airflow plugins command + add timetables #19298

Merged
merged 1 commit into from
Oct 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 24 additions & 5 deletions airflow/plugins_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
"appbuilder_menu_items",
"global_operator_extra_links",
"operator_extra_links",
"timetables",
"source",
}

Expand Down Expand Up @@ -372,10 +373,7 @@ def initialize_extra_operators_links_plugins():
operator_extra_links.extend(list(plugin.operator_extra_links))

registered_operator_link_classes.update(
{
f"{link.__class__.__module__}.{link.__class__.__name__}": link.__class__
for link in plugin.operator_extra_links
}
{as_importable_string(link.__class__): link.__class__ for link in plugin.operator_extra_links}
)


Expand Down Expand Up @@ -478,6 +476,27 @@ def get_plugin_info(attrs_to_dump: Optional[List[str]] = None) -> List[Dict[str,
if plugins:
for plugin in plugins:
info = {"name": plugin.name}
info.update({n: getattr(plugin, n) for n in attrs_to_dump})
for attr in attrs_to_dump:
if attr in ('global_operator_extra_links', 'operator_extra_links'):
info[attr] = [
f'<{as_importable_string(d.__class__)} object>' for d in getattr(plugin, attr)
]
elif attr in ('macros', 'timetables', 'hooks', 'executors'):
info[attr] = [as_importable_string(d) for d in getattr(plugin, attr)]
elif attr == 'appbuilder_views':
info[attr] = [
{**d, 'view': as_importable_string(d['view'].__class__) if 'view' in d else None}
for d in getattr(plugin, attr)
]
elif attr == 'flask_blueprints':
info[attr] = [
(
f"<{as_importable_string(d.__class__)}: "
f"name={d.name!r} import_name={d.import_name!r}>"
)
for d in getattr(plugin, attr)
]
else:
info[attr] = getattr(plugin, attr)
plugins_info.append(info)
return plugins_info
50 changes: 38 additions & 12 deletions tests/cli/commands/test_plugins_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from airflow.cli.commands import plugins_command
from airflow.hooks.base import BaseHook
from airflow.plugins_manager import AirflowPlugin
from tests.plugins.test_plugin import AirflowTestPlugin as ComplexAirflowPlugin
from tests.test_utils.mock_plugins import mock_plugin_manager


Expand All @@ -49,24 +50,49 @@ def test_should_display_no_plugins(self):
stdout = temp_stdout.getvalue()
assert 'No plugins loaded' in stdout

@mock_plugin_manager(plugins=[TestPlugin])
@mock_plugin_manager(plugins=[ComplexAirflowPlugin])
def test_should_display_one_plugins(self):
with redirect_stdout(io.StringIO()) as temp_stdout:
plugins_command.dump_plugins(self.parser.parse_args(['plugins', '--output=json']))
stdout = temp_stdout.getvalue()
print(stdout)
info = json.loads(stdout)
assert info == [
{
'name': TestPlugin.name,
'name': 'test_plugin',
'macros': ['tests.plugins.test_plugin.plugin_macro'],
'executors': ['tests.plugins.test_plugin.PluginExecutor'],
'flask_blueprints': [
"<flask.blueprints.Blueprint: name='test_plugin' import_name='tests.plugins.test_plugin'>"
],
'appbuilder_views': [
{
'name': 'Test View',
'category': 'Test Plugin',
'view': 'tests.plugins.test_plugin.PluginTestAppBuilderBaseView',
}
],
'global_operator_extra_links': [
'<tests.test_utils.mock_operators.AirflowLink object>',
'<tests.test_utils.mock_operators.GithubLink object>',
],
'timetables': ['tests.plugins.test_plugin.CustomCronDataIntervalTimetable'],
'operator_extra_links': [
'<tests.test_utils.mock_operators.GoogleLink object>',
'<tests.test_utils.mock_operators.AirflowLink2 object>',
'<tests.test_utils.mock_operators.CustomOpLink object>',
'<tests.test_utils.mock_operators.CustomBaseIndexOpLink object>',
],
'hooks': ['tests.plugins.test_plugin.PluginHook'],
'source': None,
'hooks': [PluginHook.__name__],
'executors': [],
'macros': [],
'flask_blueprints': [],
'appbuilder_views': [],
'appbuilder_menu_items': [],
'global_operator_extra_links': [],
'operator_extra_links': [],
'appbuilder_menu_items': [
{'name': 'Google', 'href': 'https://www.google.com', 'category': 'Search'},
{
'name': 'apache',
'href': 'https://www.apache.org/',
'label': 'The Apache Software Foundation',
},
],
}
]

Expand All @@ -83,8 +109,8 @@ def test_should_display_one_plugins_as_table(self):
expected_output = textwrap.dedent(
"""\
name | hooks
================+===========
test-plugin-cli | PluginHook
================+===================================================
test-plugin-cli | tests.cli.commands.test_plugins_command.PluginHook
"""
)
self.assertEqual(stdout, expected_output)
7 changes: 7 additions & 0 deletions tests/plugins/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
# This is the class you derive to create a plugin
from airflow.plugins_manager import AirflowPlugin
from airflow.sensors.base import BaseSensorOperator
from airflow.timetables.interval import CronDataIntervalTimetable
from tests.test_utils.mock_operators import (
AirflowLink,
AirflowLink2,
Expand Down Expand Up @@ -99,6 +100,11 @@ def test(self):
)


# Extend an existing class to avoid the need to implement the full interface
class CustomCronDataIntervalTimetable(CronDataIntervalTimetable):
pass


# Defining the plugin class
class AirflowTestPlugin(AirflowPlugin):
name = "test_plugin"
Expand All @@ -115,6 +121,7 @@ class AirflowTestPlugin(AirflowPlugin):
GithubLink(),
]
operator_extra_links = [GoogleLink(), AirflowLink2(), CustomOpLink(), CustomBaseIndexOpLink(1)]
timetables = [CustomCronDataIntervalTimetable]


class MockPluginA(AirflowPlugin):
Expand Down
1 change: 1 addition & 0 deletions tests/test_utils/mock_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"global_operator_extra_links",
"operator_extra_links",
"registered_operator_link_classes",
"timetable_classes",
]


Expand Down