Skip to content

Commit 3d00631

Browse files
committed
Introduce generator for getting progress
- final commit probably
1 parent 2e38b84 commit 3d00631

File tree

4 files changed

+20
-34
lines changed

4 files changed

+20
-34
lines changed

mergin/cli.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,7 @@ def sync(ctx):
481481
current_bar = None
482482
try:
483483
# Iterate over the generator to get updates
484-
for size_change, job in mc._sync_project_generator(directory):
484+
for size_change, job in mc.sync_project_generator(directory):
485485
# Check if this is a new job (a new push operation)
486486
if job and job != current_job:
487487
# If a previous bar exists, close it

mergin/client.py

Lines changed: 12 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1512,9 +1512,13 @@ def create_invitation(self, workspace_id: int, email: str, workspace_role: Works
15121512
ws_inv = self.post(f"v2/workspaces/{workspace_id}/invitations", params, json_headers)
15131513
return json.load(ws_inv)
15141514

1515-
def _sync_project_generator(self, project_directory):
1515+
def sync_project_generator(self, project_directory):
15161516
"""
1517-
See `sync_project` for details. This method is a generator yielding upload progress as (size_change, job) tuples.
1517+
Syncs project by loop with these steps:
1518+
1. Pull server version
1519+
2. Get local changes
1520+
3. Push first change batch
1521+
Repeat if there are more local changes.
15181522
15191523
:param project_directory: Project's directory
15201524
"""
@@ -1527,6 +1531,7 @@ def _sync_project_generator(self, project_directory):
15271531
job = push_project_async(self, project_directory)
15281532
if not job:
15291533
break
1534+
# waiting for progress
15301535
last_size = 0
15311536
while push_project_is_running(job):
15321537
sleep(SYNC_CALLBACK_WAIT)
@@ -1549,35 +1554,11 @@ def _sync_project_generator(self, project_directory):
15491554

15501555
def sync_project(self, project_directory):
15511556
"""
1552-
Syncs project by loop with these steps:
1553-
1. Pull server version
1554-
2. Get local changes
1555-
3. Push first change batch
1556-
Repeat if there are more local changes.
1557+
See description of _sync_project_generator().
15571558
15581559
:param project_directory: Project's directory
1559-
:param upload_progress: If True, the method will be a generator yielding upload progress as (size_change, job) tuples.
15601560
"""
1561-
mp = MerginProject(project_directory)
1562-
has_changes = True
1563-
server_conflict_attempts = 0
1564-
while has_changes:
1565-
self.pull_project(project_directory)
1566-
try:
1567-
job = push_project_async(self, project_directory)
1568-
if not job:
1569-
break
1570-
push_project_wait(job)
1571-
push_project_finalize(job)
1572-
_, has_changes = get_push_changes_batch(self, mp)
1573-
server_conflict_attempts = 0
1574-
except ClientError as e:
1575-
if e.is_retryable_sync() and server_conflict_attempts < PUSH_ATTEMPTS - 1:
1576-
# retry on conflict, e.g. when server has changes that we do not have yet
1577-
mp.log.info(
1578-
f"Restarting sync process (conflict on server) - {server_conflict_attempts + 1}/{PUSH_ATTEMPTS}"
1579-
)
1580-
server_conflict_attempts += 1
1581-
sleep(PUSH_ATTEMPT_WAIT)
1582-
continue
1583-
raise e
1561+
# walk through the generator to perform the sync
1562+
# in this method we do not yield anything to the caller
1563+
for _ in self.sync_project_generator(project_directory):
1564+
pass

mergin/common.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ class ErrorCode(Enum):
3939

4040

4141
class ClientError(Exception):
42-
def __init__(self, detail: str, url=None, server_code=None, server_response=None, http_error=None, http_method=None):
42+
def __init__(
43+
self, detail: str, url=None, server_code=None, server_response=None, http_error=None, http_method=None
44+
):
4345
self.detail = detail
4446
self.url = url
4547
self.http_error = http_error

mergin/test/test_common.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from ..common import ClientError, ErrorCode
22

3+
34
def test_client_error_is_blocked_sync():
45
"""Test the is_blocked_sync method of ClientError."""
56
error = ClientError(detail="", server_code=None)
@@ -12,6 +13,7 @@ def test_client_error_is_blocked_sync():
1213
error.server_code = ErrorCode.ProjectVersionExists.value
1314
assert error.is_blocking_sync() is True
1415

16+
1517
def test_client_error_is_rate_limit():
1618
"""Test the is_rate_limit method of ClientError."""
1719
error = ClientError(detail="", http_error=None)
@@ -21,6 +23,7 @@ def test_client_error_is_rate_limit():
2123
error.http_error = 429
2224
assert error.is_rate_limit() is True
2325

26+
2427
def test_client_error_is_retryable_sync():
2528
"""Test the is_retryable_sync method of ClientError."""
2629
error = ClientError(detail="", server_code=None, http_error=None)
@@ -43,4 +46,4 @@ def test_client_error_is_retryable_sync():
4346
error.http_error = 500
4447
assert error.is_retryable_sync() is False
4548
error.http_error = 429
46-
assert error.is_retryable_sync() is True
49+
assert error.is_retryable_sync() is True

0 commit comments

Comments
 (0)