diff --git a/nf_core/list.py b/nf_core/list.py index 19b0d81e51..4cadadfe83 100644 --- a/nf_core/list.py +++ b/nf_core/list.py @@ -2,9 +2,8 @@ """Lists available nf-core pipelines and versions.""" from __future__ import print_function -from collections import OrderedDict -import datetime +from datetime import datetime import git import json import logging @@ -175,7 +174,7 @@ def filtered_workflows(self): for k in self.keyword_filters: in_name = k in wf.name if wf.name else False in_desc = k in wf.description if wf.description else False - in_topics = any([k in t for t in wf.topics]) + in_topics = any(k in t for t in wf.topics) if not in_name and not in_desc and not in_topics: break else: @@ -250,7 +249,7 @@ def sort_pulled_date(wf): # Handle archived pipelines if wf.archived: rowdata[1] = "archived" - rowdata = [re.sub("\[\w+\]", "", k) for k in rowdata] + rowdata = [re.sub(r"\[\w+\]", "", k) for k in rowdata] table.add_row(*rowdata, style="dim") else: table.add_row(*rowdata) @@ -302,10 +301,10 @@ def __init__(self, data): # Beautify date for release in self.releases: release["published_at_pretty"] = pretty_date( - datetime.datetime.strptime(release.get("published_at"), "%Y-%m-%dT%H:%M:%SZ") + datetime.strptime(release.get("published_at"), "%Y-%m-%dT%H:%M:%SZ") ) release["published_at_timestamp"] = int( - datetime.datetime.strptime(release.get("published_at"), "%Y-%m-%dT%H:%M:%SZ").strftime("%s") + datetime.strptime(release.get("published_at"), "%Y-%m-%dT%H:%M:%SZ").strftime("%s") ) @@ -358,7 +357,7 @@ def get_local_nf_workflow_details(self): self.commit_sha = str(repo.head.commit.hexsha) self.remote_url = str(repo.remotes.origin.url) self.last_pull = os.stat(os.path.join(self.local_path, ".git", "FETCH_HEAD")).st_mtime - self.last_pull_date = datetime.datetime.fromtimestamp(self.last_pull).strftime("%Y-%m-%d %H:%M:%S") + self.last_pull_date = datetime.fromtimestamp(self.last_pull).strftime("%Y-%m-%d %H:%M:%S") self.last_pull_pretty = pretty_date(self.last_pull) # Get the checked out branch if we can @@ -392,7 +391,6 @@ def pretty_date(time): Based on https://stackoverflow.com/a/1551394/713980 Adapted by sven1103 """ - from datetime import datetime now = datetime.now() if isinstance(time, datetime): @@ -402,23 +400,27 @@ def pretty_date(time): second_diff = diff.seconds day_diff = diff.days - pretty_msg = OrderedDict() - pretty_msg[0] = [(float("inf"), 1, "from the future")] - pretty_msg[1] = [ - (10, 1, "just now"), - (60, 1, "{sec:.0f} seconds ago"), - (120, 1, "a minute ago"), - (3600, 60, "{sec:.0f} minutes ago"), - (7200, 1, "an hour ago"), - (86400, 3600, "{sec:.0f} hours ago"), - ] - pretty_msg[2] = [(float("inf"), 1, "yesterday")] - pretty_msg[7] = [(float("inf"), 1, "{days:.0f} day{day_s} ago")] - pretty_msg[31] = [(float("inf"), 7, "{days:.0f} week{day_s} ago")] - pretty_msg[365] = [(float("inf"), 30, "{days:.0f} months ago")] - pretty_msg[float("inf")] = [(float("inf"), 365, "{days:.0f} year{day_s} ago")] - - for days, seconds in pretty_msg.items(): + pretty_msg = ( + (0, ((float("inf"), 1, "from the future"),)), + ( + 1, + ( + (10, 1, "just now"), + (60, 1, "{sec:.0f} seconds ago"), + (120, 1, "a minute ago"), + (3600, 60, "{sec:.0f} minutes ago"), + (7200, 1, "an hour ago"), + (86400, 3600, "{sec:.0f} hours ago"), + ), + ), + (2, ((float("inf"), 1, "yesterday"),)), + (7, ((float("inf"), 1, "{days:.0f} day{day_s} ago"),)), + (31, ((float("inf"), 7, "{days:.0f} week{day_s} ago"),)), + (365, ((float("inf"), 30, "{days:.0f} months ago"),)), + (float("inf"), ((float("inf"), 365, "{days:.0f} year{day_s} ago"),)), + ) + + for days, seconds in pretty_msg: if day_diff < days: for sec in seconds: if second_diff < sec[0]: diff --git a/nf_core/modules/list.py b/nf_core/modules/list.py index 743670908d..537f3bd621 100644 --- a/nf_core/modules/list.py +++ b/nf_core/modules/list.py @@ -1,7 +1,8 @@ import json +import logging from os import pipe + import rich -import logging import nf_core.modules.module_utils diff --git a/tests/test_list.py b/tests/test_list.py index 082c7ffffd..f1c74da90d 100644 --- a/tests/test_list.py +++ b/tests/test_list.py @@ -2,17 +2,24 @@ """ Tests covering the workflow listing code. """ -import nf_core.list - import json -import mock import os -import pytest +import tempfile import time import unittest +from datetime import datetime +from pathlib import Path + +import mock +import pytest from rich.console import Console -from datetime import datetime +import nf_core.list + +# create a temporary directory that can be used by the tests in this file +tmp = Path(tempfile.mkdtemp()) +tmp_nxf = tmp / "nxf" +tmp_nxf_str = str(tmp_nxf) class TestLint(unittest.TestCase): @@ -100,28 +107,28 @@ def test_local_workflows_compare_and_fail_silently(self): rwf_ex.releases = None - @mock.patch.dict(os.environ, {"NXF_ASSETS": "/tmp/nxf"}) + @mock.patch.dict(os.environ, {"NXF_ASSETS": tmp_nxf_str}) @mock.patch("nf_core.list.LocalWorkflow") def test_parse_local_workflow_and_succeed(self, mock_local_wf): - test_path = "/tmp/nxf/nf-core" + test_path = tmp_nxf / "nf-core" if not os.path.isdir(test_path): os.makedirs(test_path) - assert os.environ["NXF_ASSETS"] == "/tmp/nxf" - with open("/tmp/nxf/nf-core/dummy-wf", "w") as f: + assert os.environ["NXF_ASSETS"] == tmp_nxf_str + with open(tmp_nxf / "nf-core/dummy-wf", "w") as f: f.write("dummy") workflows_obj = nf_core.list.Workflows() workflows_obj.get_local_nf_workflows() assert len(workflows_obj.local_workflows) == 1 - @mock.patch.dict(os.environ, {"NXF_ASSETS": "/tmp/nxf"}) + @mock.patch.dict(os.environ, {"NXF_ASSETS": tmp_nxf_str}) @mock.patch("nf_core.list.LocalWorkflow") @mock.patch("subprocess.check_output") def test_parse_local_workflow_home(self, mock_local_wf, mock_subprocess): - test_path = "/tmp/nxf/nf-core" + test_path = tmp_nxf / "nf-core" if not os.path.isdir(test_path): os.makedirs(test_path) - assert os.environ["NXF_ASSETS"] == "/tmp/nxf" - with open("/tmp/nxf/nf-core/dummy-wf", "w") as f: + assert os.environ["NXF_ASSETS"] == tmp_nxf_str + with open(tmp_nxf / "nf-core/dummy-wf", "w") as f: f.write("dummy") workflows_obj = nf_core.list.Workflows() workflows_obj.get_local_nf_workflows() @@ -130,7 +137,7 @@ def test_parse_local_workflow_home(self, mock_local_wf, mock_subprocess): @mock.patch("git.Repo") def test_local_workflow_investigation(self, mock_repo, mock_stat): local_wf = nf_core.list.LocalWorkflow("dummy") - local_wf.local_path = "/tmp" + local_wf.local_path = tmp mock_repo.head.commit.hexsha = "h00r4y" mock_stat.st_mode = 1 local_wf.get_local_nf_workflow_details()