Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 12
introduce helper to normalize workspace/project roles from strings#284
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
0f70940c62dbadff671d00ec31a543ef278File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -12,4 +12,4 @@ htmlcov | ||
| .pytest_cache | ||
| deps | ||
| venv | ||
| .vscode/settings.json | ||
| .vscode/ | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -38,6 +38,7 @@ | ||
| unique_path_name, | ||
| conflicted_copy_file_name, | ||
| edit_conflict_file_name, | ||
| normalize_role, | ||
| ) | ||
| from ..merginproject import pygeodiff | ||
| from ..report import create_report | ||
| @@ -3026,3 +3027,25 @@ def test_server_type(mc): | ||
| mock_client_get.side_effect = ClientError(detail="Service unavailable", http_error=503) | ||
| with pytest.raises(ClientError, match="Service unavailable"): | ||
| mc.server_type() | ||
| @pytest.mark.parametrize( | ||
| "value, role_enum, expected", | ||
| [ | ||
| ("guest", WorkspaceRole, WorkspaceRole.GUEST), | ||
| (" GuEsT ", WorkspaceRole, WorkspaceRole.GUEST), | ||
| ("writer", ProjectRole, ProjectRole.WRITER), | ||
| (" WRITER ", ProjectRole, ProjectRole.WRITER), | ||
| ("guuuest", WorkspaceRole, None), | ||
| ("ownerr", ProjectRole, None), | ||
| ("", WorkspaceRole, None), | ||
| (None, WorkspaceRole, None), | ||
| (123, WorkspaceRole, None), | ||
| ], | ||
| ) | ||
| def test_normalize_role_parametrized(value, role_enum, expected): | ||
| result = normalize_role(value, role_enum) | ||
| if expected is None: | ||
| assert result is None | ||
| else: | ||
| assert result == expected | ||
Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this should be enough for all cases | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -7,7 +7,9 @@ | ||
| from datetime import datetime | ||
| from pathlib import Path | ||
| import tempfile | ||
| from .common import ClientError | ||
| from enum import Enum | ||
| from typing import Optional, Type, Union | ||
| from .common import ClientError, WorkspaceRole | ||
| def generate_checksum(file, chunk_size=4096): | ||
| @@ -309,3 +311,20 @@ def cleanup_tmp_dir(mp, tmp_dir: tempfile.TemporaryDirectory): | ||
| mp.log.warning(f"Permission error during tmp dir cleanup: {tmp_dir.name}") | ||
| except Exception as e: | ||
| mp.log.error(f"Error during tmp dir cleanup: {tmp_dir.name}: {e}") | ||
| def normalize_role(role: Union[str, Enum], enum_cls: Type[Enum]) -> Optional[Enum]: | ||
varmar05 marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| """ | ||
| Takes a role as a string or an Enum member and returns the corresponding Enum member | ||
| from the given enum class. Returns None if the input is invalid or no match is found. | ||
| """ | ||
| if isinstance(role, enum_cls): | ||
| return role | ||
| if isinstance(role, str): | ||
| try: | ||
| return enum_cls(role.strip().lower()) | ||
| except ValueError: | ||
| return None | ||
| return None | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if we want to get rid of repeated code you can try to wrap this up in some decorator which would make sure that arg would be always
Enum