diff --git a/.github/workflows/black.yml b/.github/workflows/black_linter.yml similarity index 84% rename from .github/workflows/black.yml rename to .github/workflows/black_linter.yml index 05ccf40..6d34fe3 100644 --- a/.github/workflows/black.yml +++ b/.github/workflows/black_linter.yml @@ -4,11 +4,10 @@ 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 + - uses: psf/black@stable \ No newline at end of file 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 e52ba39..5dbb083 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-11-01 +### Added +- is_registry_path checker function + ## [0.2.0] - 2023-10-02 ### 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