Skip to content

Commit 0119b7d

Browse files
committed
First version for sync progress bar
1 parent e36ae8d commit 0119b7d

File tree

2 files changed

+35
-18
lines changed

2 files changed

+35
-18
lines changed

mergin/cli.py

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -477,17 +477,36 @@ def sync(ctx):
477477
if mc is None:
478478
return
479479
directory = os.getcwd()
480-
upload_job = None
480+
current_job = None
481+
current_bar = None
481482
try:
482-
483-
def on_progress(increment, push_job):
484-
nonlocal upload_job
485-
upload_job = push_job
486-
487-
# run pull & push cycles until there are no local changes
488-
mc.sync_project(directory, progress_callback=on_progress)
489-
490-
click.secho("Sync complete.", fg="green")
483+
# Iterate over the generator to get updates
484+
for size_change, job in mc.sync_project(directory, upload_progress=True):
485+
# Check if this is a new job (a new push operation)
486+
if job and job != current_job:
487+
# If a previous bar exists, close it
488+
if current_bar:
489+
current_bar.finish()
490+
491+
# A new push job has started. Initialize a new progress bar.
492+
click.echo(f"Starting upload")
493+
current_job = job
494+
495+
# The length of the progress bar should be the total size of the job
496+
# You'll need to get this from your job object (e.g., job.total_size)
497+
total_size = job.total_size
498+
current_bar = click.progressbar(
499+
length=total_size,
500+
label=f"Uploading project",
501+
)
502+
503+
# Update the current progress bar with the size increment
504+
current_bar.update(size_change)
505+
506+
# After the loop finishes, make sure to close the final progress bar
507+
if current_bar:
508+
current_bar.finish()
509+
click.secho("\nProject synced successfully", fg="green")
491510

492511
except InvalidProject as e:
493512
click.secho("Invalid project directory ({})".format(str(e)), fg="red")
@@ -496,8 +515,8 @@ def on_progress(increment, push_job):
496515
return
497516
except KeyboardInterrupt:
498517
click.secho("Cancelling...")
499-
if upload_job:
500-
push_project_cancel(upload_job)
518+
if current_job:
519+
push_project_cancel(current_job)
501520
except Exception as e:
502521
_print_unhandled_exception()
503522

mergin/client.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1512,7 +1512,7 @@ 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(self, project_directory, progress_callback=None):
1515+
def sync_project(self, project_directory, upload_progress=False):
15161516
"""
15171517
Syncs project by loop with these steps:
15181518
1. Pull server version
@@ -1521,7 +1521,7 @@ def sync_project(self, project_directory, progress_callback=None):
15211521
Repeat if there are more local changes.
15221522
15231523
:param project_directory: Project's directory
1524-
:param progress_callback: Optional callback function to report progress during push.
1524+
:param upload_progress: If True, the method will be a generator yielding upload progress as (size_change, job) tuples.
15251525
"""
15261526
mp = MerginProject(project_directory)
15271527
has_changes = True
@@ -1536,16 +1536,14 @@ def sync_project(self, project_directory, progress_callback=None):
15361536
job = push_project_async(self, project_directory)
15371537
if not job:
15381538
break
1539-
if not progress_callback:
1539+
if not upload_progress:
15401540
push_project_wait(job)
15411541
else:
15421542
last_size = 0
15431543
while push_project_is_running(job):
15441544
sleep(SYNC_CALLBACK_WAIT)
15451545
current_size = job.transferred_size
1546-
progress_callback(
1547-
current_size - last_size, job
1548-
) # call callback with transferred size increment
1546+
yield (current_size - last_size, job) # Yields the size change and the job object
15491547
last_size = current_size
15501548
push_project_finalize(job)
15511549
_, has_changes = get_push_changes_batch(self, mp)

0 commit comments

Comments
 (0)