From 82b8e592dc363037cc954b9a9159b74810fbaf64 Mon Sep 17 00:00:00 2001 From: Thiago Costa Porto Date: Tue, 10 Oct 2023 13:58:12 +0200 Subject: [PATCH 1/5] feat: add login_as_guest shortcut --- packages/hagrid/hagrid/orchestra.py | 13 ++++++++++- packages/syft/src/syft/client/client.py | 30 ++++++++++++++++++++----- 2 files changed, 36 insertions(+), 7 deletions(-) diff --git a/packages/hagrid/hagrid/orchestra.py b/packages/hagrid/hagrid/orchestra.py index 9a9de5fd43b..099bd5e1f91 100644 --- a/packages/hagrid/hagrid/orchestra.py +++ b/packages/hagrid/hagrid/orchestra.py @@ -163,7 +163,7 @@ def __init__( def client(self) -> Any: if self.port: sy = get_syft_client() - return sy.login(url=self.url, port=self.port, verbose=False) # type: ignore + return sy.login(url=self.url, port=self.port) # type: ignore elif self.deployment_type == DeploymentType.PYTHON: return self.python_node.get_guest_client(verbose=False) # type: ignore else: @@ -171,6 +171,16 @@ def client(self) -> Any: f"client not implemented for the deployment type:{self.deployment_type}" ) + def login_as_guest(self, **kwargs: Any) -> Optional[Any]: + client = self.client + + session = client.login_as_guest(**kwargs) + + if isinstance(session, SyftError): + return session + + return session + def login( self, email: Optional[str] = None, password: Optional[str] = None, **kwargs: Any ) -> Optional[Any]: @@ -182,6 +192,7 @@ def login( password = getpass.getpass("Password: ") session = client.login(email=email, password=password, **kwargs) + if isinstance(session, SyftError): return session diff --git a/packages/syft/src/syft/client/client.py b/packages/syft/src/syft/client/client.py index 53687d36381..fe4a4238e0e 100644 --- a/packages/syft/src/syft/client/client.py +++ b/packages/syft/src/syft/client/client.py @@ -805,6 +805,27 @@ def register( ) +@instrument +def login_as_guest( + url: Union[str, GridURL] = DEFAULT_PYGRID_ADDRESS, + node: Optional[AbstractNode] = None, + port: Optional[int] = None, + verbose: bool = True, +): + _client = connect(url=url, node=node, port=port) + + if isinstance(_client, SyftError): + return _client + + if verbose: + print( + f"Logged into <{_client.name}: {_client.metadata.node_side_type.capitalize()}-" + f"side {_client.metadata.node_type.capitalize()}> as GUEST" + ) + + return _client.guest() + + @instrument def login( url: Union[str, GridURL] = DEFAULT_PYGRID_ADDRESS, @@ -828,12 +849,9 @@ def login( login_credentials = UserLoginCredentials(email=email, password=password) if login_credentials is None: - if verbose: - print( - f"Logged into <{_client.name}: {_client.metadata.node_side_type.capitalize()}-" - f"side {_client.metadata.node_type.capitalize()}> as GUEST" - ) - return _client.guest() + return SyftError( + message="If you want to login as guest, use .login_as_guest() instead" + ) if cache and login_credentials: _client_cache = SyftClientSessionCache.get_client( From 0b4f10892e6235013680389e97a2cdfef761b80e Mon Sep 17 00:00:00 2001 From: Julian Cardonnet Date: Tue, 10 Oct 2023 14:27:19 -0300 Subject: [PATCH 2/5] Don't use Rich for confirmation prompt --- packages/syft/src/syft/util/util.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/packages/syft/src/syft/util/util.py b/packages/syft/src/syft/util/util.py index 8f76f6de5be..dd992643233 100644 --- a/packages/syft/src/syft/util/util.py +++ b/packages/syft/src/syft/util/util.py @@ -40,7 +40,6 @@ from nacl.signing import SigningKey from nacl.signing import VerifyKey import requests -from rich.prompt import Confirm # relative from .logger import critical @@ -451,11 +450,15 @@ def prompt_warning_message(message: str, confirm: bool = False) -> bool: warning = SyftWarning(message=message) display(warning) - if confirm: - allowed = Confirm.ask("Would you like to proceed?") - if not allowed: + while confirm: + response = input("Would you like to proceed? [y/n]: ").lower() + if response == "y": + return True + elif response == "n": display("Aborted !!") return False + else: + print("Invalid response. Please enter Y or N.") return True From 663be627c4653680f506884f0d7e90b35cf2d059 Mon Sep 17 00:00:00 2001 From: Thiago Costa Porto Date: Wed, 11 Oct 2023 22:42:08 +0200 Subject: [PATCH 3/5] feat: login_as_guest added to sy and domain client --- packages/syft/src/syft/__init__.py | 1 + packages/syft/src/syft/client/client.py | 21 ++++++++++++++------- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/packages/syft/src/syft/__init__.py b/packages/syft/src/syft/__init__.py index 001c6b81dd4..1dc85763e43 100644 --- a/packages/syft/src/syft/__init__.py +++ b/packages/syft/src/syft/__init__.py @@ -12,6 +12,7 @@ from .abstract_node import NodeType # noqa: F401 from .client.client import connect # noqa: F401 from .client.client import login # noqa: F401 +from .client.client import login_as_guest #noqa: F401 from .client.client import register # noqa: F401 from .client.deploy import Orchestra # noqa: F401 from .client.domain_client import DomainClient # noqa: F401 diff --git a/packages/syft/src/syft/client/client.py b/packages/syft/src/syft/client/client.py index fe4a4238e0e..4e0fae32932 100644 --- a/packages/syft/src/syft/client/client.py +++ b/packages/syft/src/syft/client/client.py @@ -593,6 +593,17 @@ def me(self) -> Optional[Union[UserView, SyftError]]: return self.api.services.user.get_current_user() return None + def login_as_guest(self) -> Self: + _guest_client = self.guest() + + print( + f"Logged into <{self.name}: {self.metadata.node_side_type.capitalize()}-side " + f"{self.metadata.node_type.capitalize()}> as GUEST" + ) + + return _guest_client + + def login( self, email: str, password: str, cache: bool = True, register=False, **kwargs ) -> Self: @@ -833,12 +844,13 @@ def login( port: Optional[int] = None, email: Optional[str] = None, password: Optional[str] = None, - cache: bool = True, - verbose: bool = True, + cache: bool = True ) -> SyftClient: _client = connect(url=url, node=node, port=port) + if isinstance(_client, SyftError): return _client + connection = _client.connection login_credentials = None @@ -848,11 +860,6 @@ def login( password = getpass("Password: ") login_credentials = UserLoginCredentials(email=email, password=password) - if login_credentials is None: - return SyftError( - message="If you want to login as guest, use .login_as_guest() instead" - ) - if cache and login_credentials: _client_cache = SyftClientSessionCache.get_client( login_credentials.email, From 6a509238db7070b64a54c0ddca5d3eb4668e26f2 Mon Sep 17 00:00:00 2001 From: Thiago Costa Porto Date: Wed, 11 Oct 2023 22:42:50 +0200 Subject: [PATCH 4/5] lint: tox pass --- packages/syft/src/syft/__init__.py | 2 +- packages/syft/src/syft/client/client.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/syft/src/syft/__init__.py b/packages/syft/src/syft/__init__.py index 1dc85763e43..1f95ce2cfa3 100644 --- a/packages/syft/src/syft/__init__.py +++ b/packages/syft/src/syft/__init__.py @@ -12,7 +12,7 @@ from .abstract_node import NodeType # noqa: F401 from .client.client import connect # noqa: F401 from .client.client import login # noqa: F401 -from .client.client import login_as_guest #noqa: F401 +from .client.client import login_as_guest # noqa: F401 from .client.client import register # noqa: F401 from .client.deploy import Orchestra # noqa: F401 from .client.domain_client import DomainClient # noqa: F401 diff --git a/packages/syft/src/syft/client/client.py b/packages/syft/src/syft/client/client.py index 4e0fae32932..f3abd97fb4b 100644 --- a/packages/syft/src/syft/client/client.py +++ b/packages/syft/src/syft/client/client.py @@ -844,7 +844,7 @@ def login( port: Optional[int] = None, email: Optional[str] = None, password: Optional[str] = None, - cache: bool = True + cache: bool = True, ) -> SyftClient: _client = connect(url=url, node=node, port=port) From 74e428c1bb6f3a10cf50abe7e92041d325bb75ea Mon Sep 17 00:00:00 2001 From: Thiago Costa Porto Date: Wed, 11 Oct 2023 23:08:17 +0200 Subject: [PATCH 5/5] lint: tox pass --- packages/syft/src/syft/client/client.py | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/syft/src/syft/client/client.py b/packages/syft/src/syft/client/client.py index f3abd97fb4b..38bc3e6d2ac 100644 --- a/packages/syft/src/syft/client/client.py +++ b/packages/syft/src/syft/client/client.py @@ -603,7 +603,6 @@ def login_as_guest(self) -> Self: return _guest_client - def login( self, email: str, password: str, cache: bool = True, register=False, **kwargs ) -> Self: