Skip to content

Commit 1a3f04d

Browse files
Merge pull request #436 from pieces-app/fix-auth-commands
fix auth commands
2 parents e3b2da3 + cf446d1 commit 1a3f04d

File tree

5 files changed

+434
-21
lines changed

5 files changed

+434
-21
lines changed

src/pieces/_vendor/pieces_os_client/wrapper/basic_identifier/user.py

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from typing import TYPE_CHECKING, Optional
2-
2+
from threading import Thread
33
from .basic import Basic
44

55
from pieces._vendor.pieces_os_client.models.allocation_status_enum import AllocationStatusEnum
@@ -53,20 +53,27 @@ def _on_login_connect(self):
5353
thread: The thread handling the login process.
5454
timeout: The maximum time to wait for the login process.
5555
"""
56-
self.connect(True)
56+
self.connect()
5757

5858
def login(self, connect_after_login=True, timeout=120):
5959
"""
6060
Logs the user into the OS and optionally connects to the cloud.
6161
6262
Args:
63-
connect_after_login: A flag indicating if the user should connect to the cloud after login (default is True).
64-
timeout: The maximum time to wait for the login process (default is 120 seconds).
63+
connect_after_login: A flag indicating if the user should connect to the cloud after login (default is True).
64+
timeout: The maximum time to wait for the login process (default is 120 seconds).
6565
"""
66-
thread = self.pieces_client.os_api.sign_into_os(async_req=True)
66+
result = {}
67+
68+
def target():
69+
result['user'] = self.pieces_client.os_api.sign_into_os()
70+
71+
thread = Thread(target=target)
72+
thread.start()
73+
thread.join(timeout)
74+
6775
if connect_after_login:
68-
user = thread.get(timeout)
69-
self.user_profile = user
76+
self.user_profile = result.get('user')
7077
self._on_login_connect()
7178

7279
def logout(self):
@@ -84,8 +91,20 @@ def connect(self, async_req = False):
8491
"""
8592
if not self.user_profile:
8693
raise PermissionError("You must be logged in to use this feature")
87-
self.on_user_callback(self.user_profile, True) # Set the connecting to cloud bool to true
88-
self.pieces_client.allocations_api.allocations_connect_new_cloud(self.user_profile,async_req=async_req)
94+
self.on_user_callback(
95+
self.user_profile, True
96+
) # Set the connecting to cloud bool to true
97+
if async_req:
98+
thread = Thread(
99+
target=self.pieces_client.allocations_api.allocations_connect_new_cloud,
100+
args=(self.user_profile,),
101+
)
102+
thread.start()
103+
else:
104+
self.pieces_client.allocations_api.allocations_connect_new_cloud(
105+
self.user_profile
106+
)
107+
89108

90109
def disconnect(self):
91110
"""

src/pieces/_vendor/pieces_os_client/wrapper/client.py

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ def model_id(self):
188188
def model_name(self, model):
189189
models = self.get_models()
190190
if model not in models:
191-
raise ValueError("Not a vaild model name, the available models are"
191+
raise ValueError("Not a valid model name, the available models are"
192192
f"{', '.join(models.keys())}")
193193
self._model_name = model
194194
self._model_id = models[model]
@@ -282,15 +282,6 @@ def pieces_os_installer(self, callback: Callable[[DownloadModel], None]) -> PosI
282282
"""
283283
return PosInstaller(callback, self.app_name)
284284

285-
def pool(self, api_call, args):
286-
"""
287-
call the api async without stopping the main thread
288-
Create thread pool on first request
289-
avoids instantiating unused threadpool for blocking clients.
290-
return the ThreadPool created
291-
"""
292-
return self.api_client.pool.apply_async(api_call, args)
293-
294285

295286
# Register the function to be called on exit
296287
atexit.register(PiecesClient.close)

src/pieces/command_interface/auth_commands.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
import argparse
2+
from pieces._vendor.pieces_os_client.models.allocation_status_enum import (
3+
AllocationStatusEnum,
4+
)
25
from pieces.base_command import BaseCommand
36
from pieces.urls import URLs
47
from pieces.settings import Settings
@@ -40,9 +43,13 @@ def execute(self, **kwargs) -> int:
4043
Settings.pieces_client.user_api.user_snapshot().user
4144
)
4245
if Settings.pieces_client.user.user_profile:
46+
status = Settings.pieces_client.user.cloud_status or AllocationStatusEnum.DISCONNECTED
4347
Settings.logger.print(
44-
f"Signed in as {Settings.pieces_client.user.name}\nemail: {Settings.pieces_client.user.email}"
48+
f"Signed in as {Settings.pieces_client.user.name}\nEmail: {Settings.pieces_client.user.email}\nCloud status: {status.value.title()}"
4549
)
50+
if status == AllocationStatusEnum.DISCONNECTED:
51+
Settings.logger.print("Connecting to the Pieces Cloud...")
52+
Settings.pieces_client.user.connect()
4653
return 0
4754
try:
4855
Settings.pieces_client.user.login(True)

src/pieces/core/assets_command.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,11 @@ def wrapper(*args, **kwargs):
4747
try:
4848
if AssetsCommands.current_asset is None:
4949
raise ValueError("No material selected")
50-
AssetsCommands.current_asset.asset # Check if the current asset is vaild
50+
AssetsCommands.current_asset.asset # Check if the current asset is valid
5151
except (ValueError, NotFoundException):
5252
ListCommand.list_assets()
53+
if AssetsCommands.current_asset is None:
54+
return
5355
return func(asset=AssetsCommands.current_asset, *args, **kwargs)
5456

5557
return wrapper

0 commit comments

Comments
 (0)