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

List urls #988

Merged
merged 2 commits into from
Sep 26, 2023
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
16 changes: 13 additions & 3 deletions tests/unit_tests/test_tethys_cli/test_list_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def tearDown(self):
@mock.patch("tethys_cli.list_command.print")
@mock.patch("tethys_cli.list_command.get_installed_tethys_items")
def test_list_command_installed_apps(self, mock_installed_items, mock_print):
mock_args = mock.MagicMock()
mock_args = mock.MagicMock(urls=None)
mock_installed_items.side_effect = [{"foo": "/foo", "bar": "/bar"}, {}]

list_command(mock_args)
Expand All @@ -45,7 +45,7 @@ def test_list_command_installed_apps(self, mock_installed_items, mock_print):
@mock.patch("tethys_cli.list_command.print")
@mock.patch("tethys_cli.list_command.get_installed_tethys_items")
def test_list_command_installed_extensions(self, mock_installed_items, mock_print):
mock_args = mock.MagicMock()
mock_args = mock.MagicMock(urls=None)
mock_installed_items.side_effect = [{}, {"baz": "/baz"}]

list_command(mock_args)
Expand All @@ -62,7 +62,7 @@ def test_list_command_installed_extensions(self, mock_installed_items, mock_prin
@mock.patch("tethys_cli.list_command.print")
@mock.patch("tethys_cli.list_command.get_installed_tethys_items")
def test_list_command_installed_both(self, mock_installed_items, mock_print):
mock_args = mock.MagicMock()
mock_args = mock.MagicMock(urls=None)
mock_installed_items.side_effect = [
{"foo": "/foo", "bar": "/bar"},
{"baz": "/baz"},
Expand All @@ -82,3 +82,13 @@ def test_list_command_installed_both(self, mock_installed_items, mock_print):
self.assertIn(" foo", check_list)
self.assertIn(" bar", check_list)
self.assertIn(" baz", check_list)

@mock.patch("tethys_cli.list_command.write_msg")
def test_list_command_urls(self, mock_msg):
mock_args = mock.MagicMock(urls=True)

list_command(mock_args)

# Check if print is called correctly
self.mock_write_info.assert_called_with("test_app")
self.assertEqual(mock_msg.call_count, 2)
19 changes: 19 additions & 0 deletions tethys_apps/base/url_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,25 @@ def __repr__(self):
f"handler={self.handler}, handler_type={self.handler_type}>"
)

@staticmethod
def _get_function_dot_path(func):
return func if isinstance(func, str) else f"{func.__module__}.{func.__name__}"

def display(self, prefix=""):
value = (
f"{prefix}UrlMap:\n"
f"{prefix} name: {self.name}\n"
f"{prefix} url: {self.url}\n"
f"{prefix} protocol: {self.protocol}\n"
f"{prefix} controller: {self._get_function_dot_path(self.controller)}\n"
)
if self.handler:
value += (
f"{prefix} handler: {self._get_function_dot_path(self.handler)}\n"
f"{prefix} handler_type: {self.handler_type}\n"
)
return value


def url_map_maker(root_url):
"""
Expand Down
3 changes: 2 additions & 1 deletion tethys_apps/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,8 @@ def get_installed_tethys_items(apps=False, extensions=False):
for name, module in items.items():
try:
item = __import__(module, fromlist=[""])
paths[name] = item.__path__[0]
value = item.__path__[0]
paths[name] = value
except (IndexError, ImportError):
"""DO NOTHING"""

Expand Down
20 changes: 16 additions & 4 deletions tethys_cli/list_command.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
from tethys_apps.utilities import get_installed_tethys_items
from tethys_apps.utilities import get_installed_tethys_items, SingletonHarvester
from tethys_cli.cli_helpers import load_apps
from tethys_cli.cli_colors import write_info
from tethys_cli.cli_colors import write_info, write_msg


def add_list_parser(subparsers):
# Setup list command
list_parser = subparsers.add_parser(
"list", help="List installed apps and extensions."
)
list_parser.set_defaults(func=list_command)
list_parser.add_argument(
"--urls",
action="store_true",
help="List URLMaps for apps",
)
list_parser.set_defaults(func=list_command, urls=False)


def list_command(args):
Expand All @@ -19,9 +24,16 @@ def list_command(args):
installed_apps = get_installed_tethys_items(apps=True)
installed_extensions = get_installed_tethys_items(extensions=True)

if args.urls:
for app in SingletonHarvester().apps:
write_info(f"{app.package}")
for url_map in app.registered_url_maps:
write_msg(f"{url_map.display(' ')}")
return

if installed_apps:
write_info("Apps:")
for item in installed_apps:
for item, p in installed_apps.items():
print(f" {item}")

if installed_extensions:
Expand Down
Loading