From 22b2ce6e4c7d0fdced171ea588e661e4c0cd1438 Mon Sep 17 00:00:00 2001 From: Khoroshevskyi Date: Wed, 1 Nov 2023 17:31:24 +0100 Subject: [PATCH 1/3] added reg_path check in helpers --- .pre-commit-config.yaml | 10 +++++++ Makefile | 3 +- docs/changelog.md | 4 +++ pephubclient/__init__.py | 2 +- pephubclient/helpers.py | 19 +++++++++++++ requirements/requirements-all.txt | 1 + requirements/requirements-test.txt | 4 ++- tests/test_pephubclient.py | 45 +++++++++++++++++++++++++++++- 8 files changed, 83 insertions(+), 5 deletions(-) create mode 100644 .pre-commit-config.yaml diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..20df14e --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,10 @@ +repos: + # Run the Ruff linter. + - repo: https://github.com/astral-sh/ruff-pre-commit + # Ruff version. + rev: v0.1.3 + hooks: + # Run the Ruff linter. + - id: ruff + # Run the Ruff formatter. + - id: ruff-format diff --git a/Makefile b/Makefile index 19130c8..5033ebb 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,5 @@ lint: - # black should be last in the list, as it lint the code. Tests can fail if order will be different - flake8 && isort . && black . + ruff format . run-coverage: coverage run -m pytest diff --git a/docs/changelog.md b/docs/changelog.md index 5d1ebd2..c3ff70d 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -2,6 +2,10 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html) and [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) format. +## [0.2.1] - 2023-10-01 +### Added +- is_registry_path checker function + ## [0.2.0] - 2023-08-28 ### Added - Project search functionality diff --git a/pephubclient/__init__.py b/pephubclient/__init__.py index 5d45d35..60771ac 100644 --- a/pephubclient/__init__.py +++ b/pephubclient/__init__.py @@ -1,7 +1,7 @@ from pephubclient.pephubclient import PEPHubClient __app_name__ = "pephubclient" -__version__ = "0.2.0" +__version__ = "0.2.1" __author__ = "Oleksandr Khoroshevskyi, Rafal Stepien" diff --git a/pephubclient/helpers.py b/pephubclient/helpers.py index 4e42c4c..467a8dd 100644 --- a/pephubclient/helpers.py +++ b/pephubclient/helpers.py @@ -4,7 +4,11 @@ import requests from requests.exceptions import ConnectionError +from ubiquerg import parse_registry_path +from pydantic.error_wrappers import ValidationError + from pephubclient.exceptions import PEPExistsError, ResponseError +from pephubclient.constants import RegistryPath class RequestManager: @@ -84,3 +88,18 @@ def call_client_func(func: Callable[..., Any], **kwargs) -> Any: MessageHandler.print_warning(f"PEP already exists. {err}") except OSError as err: MessageHandler.print_error(f"{err}") + + +def is_registry_path(input_string: str) -> bool: + """ + Check if input is a registry path to pephub + :param str input_string: path to the PEP (or registry path) + :return bool: True if input is a registry path + """ + if input_string.endswith(".yaml"): + return False + try: + RegistryPath(**parse_registry_path(input_string)) + except (ValidationError, TypeError): + return False + return True diff --git a/requirements/requirements-all.txt b/requirements/requirements-all.txt index a2413ea..8163295 100644 --- a/requirements/requirements-all.txt +++ b/requirements/requirements-all.txt @@ -3,3 +3,4 @@ peppy>=0.35.7 requests>=2.28.2 pydantic<2.0 pandas>=2.0.0 +ubiquerg>=0.6.3 \ No newline at end of file diff --git a/requirements/requirements-test.txt b/requirements/requirements-test.txt index 1825cf5..241ddcd 100644 --- a/requirements/requirements-test.txt +++ b/requirements/requirements-test.txt @@ -1,7 +1,9 @@ black +ruff pytest python-dotenv pytest-mock flake8 coveralls -pytest-cov \ No newline at end of file +pytest-cov +pre-commit \ No newline at end of file diff --git a/tests/test_pephubclient.py b/tests/test_pephubclient.py index a273fc5..c35c8ac 100644 --- a/tests/test_pephubclient.py +++ b/tests/test_pephubclient.py @@ -5,6 +5,7 @@ from pephubclient.exceptions import ResponseError from pephubclient.pephubclient import PEPHubClient +from pephubclient.helpers import is_registry_path SAMPLE_PEP = os.path.join( os.path.dirname(os.path.dirname(os.path.abspath(__file__))), @@ -150,7 +151,7 @@ def test_push_with_pephub_error_response( def test_search_prj(self, mocker): return_value = b'{"count":1,"limit":100,"offset":0,"items":[{"namespace":"namespace1","name":"basic","tag":"default","is_private":false,"number_of_samples":2,"description":"None","last_update_date":"2023-08-27 19:07:31.552861+00:00","submission_date":"2023-08-27 19:07:31.552858+00:00","digest":"08cbcdbf4974fc84bee824c562b324b5","pep_schema":"random_schema_name"}],"session_info":null,"can_edit":false}' - requests_mock = mocker.patch( + mocker.patch( "requests.request", return_value=Mock(content=return_value, status_code=200), ) @@ -158,3 +159,45 @@ def test_search_prj(self, mocker): return_value = PEPHubClient().find_project(namespace="namespace1") assert return_value.count == 1 assert len(return_value.items) == 1 + + +class TestHelpers: + @pytest.mark.parametrize( + "input_str, expected_output", + [ + ( + "databio/pep:default", + True, + ), + ( + "pephub.databio.org::databio/pep:default", + True, + ), + ( + "pephub.databio.org://databio/pep:default", + True, + ), + ( + "databio/pep", + True, + ), + ( + "databio/pep/default", + False, + ), + ( + "some/random/path/to.yaml", + False, + ), + ( + "path_to.csv", + False, + ), + ( + "this/is/path/to.csv", + False, + ), + ], + ) + def test_is_registry_path(self, input_str, expected_output): + assert is_registry_path(input_str) is expected_output From 67119ad1df3f0eec751c27a8bc0a9dd2642b6c56 Mon Sep 17 00:00:00 2001 From: Khoroshevskyi Date: Wed, 1 Nov 2023 17:33:11 +0100 Subject: [PATCH 2/3] added linter --- .github/workflows/black.yml | 14 -------------- .github/workflows/ruff_linter.yml | 12 ++++++++++++ 2 files changed, 12 insertions(+), 14 deletions(-) delete mode 100644 .github/workflows/black.yml create mode 100644 .github/workflows/ruff_linter.yml diff --git a/.github/workflows/black.yml b/.github/workflows/black.yml deleted file mode 100644 index 05ccf40..0000000 --- a/.github/workflows/black.yml +++ /dev/null @@ -1,14 +0,0 @@ -name: Lint - -on: - pull_request: - branches: [main] - - -jobs: - lint: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2 - - uses: actions/setup-python@v2 - - uses: psf/black@stable diff --git a/.github/workflows/ruff_linter.yml b/.github/workflows/ruff_linter.yml new file mode 100644 index 0000000..5419d74 --- /dev/null +++ b/.github/workflows/ruff_linter.yml @@ -0,0 +1,12 @@ +name: Ruff + +on: + pull_request: + branches: [main] + +jobs: + lint: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: chartboost/ruff-action@v1 From 7a89e24b2d72ad9b7e0c4fa94ef6c07cb8ef3884 Mon Sep 17 00:00:00 2001 From: Khoroshevskyi Date: Mon, 13 Nov 2023 02:51:10 +0100 Subject: [PATCH 3/3] rolled back to black checker --- .github/workflows/black_linter.yml | 13 +++++++++++++ .github/workflows/ruff_linter.yml | 12 ------------ 2 files changed, 13 insertions(+), 12 deletions(-) create mode 100644 .github/workflows/black_linter.yml delete mode 100644 .github/workflows/ruff_linter.yml diff --git a/.github/workflows/black_linter.yml b/.github/workflows/black_linter.yml new file mode 100644 index 0000000..6d34fe3 --- /dev/null +++ b/.github/workflows/black_linter.yml @@ -0,0 +1,13 @@ +name: Lint + +on: + pull_request: + branches: [main] + +jobs: + lint: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - uses: actions/setup-python@v2 + - uses: psf/black@stable \ No newline at end of file diff --git a/.github/workflows/ruff_linter.yml b/.github/workflows/ruff_linter.yml deleted file mode 100644 index 5419d74..0000000 --- a/.github/workflows/ruff_linter.yml +++ /dev/null @@ -1,12 +0,0 @@ -name: Ruff - -on: - pull_request: - branches: [main] - -jobs: - lint: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v3 - - uses: chartboost/ruff-action@v1