summaryrefslogtreecommitdiffstats
path: root/gamechestcli/gamechest/processor.py
blob: 794deb8e914617bcf45b459f0fed47c8a99b4eb9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import contextlib
import selectors

from rich.progress import Progress as RichProgress
from rich import print


def process_task(title, task):
    with contextlib.ExitStack() as stack:
        runner = stack.enter_context(task())
        selector = stack.enter_context(selectors.DefaultSelector())
        selector.register(runner.get_read_fd(), selectors.EVENT_READ)
        rich_progress = stack.enter_context(RichProgress())
        known_total = 100
        global_id = rich_progress.add_task(title, total=known_total)
        last_step = None
        while (rc := runner.poll()) is None:
            if selector.select():
                progress = runner.progress_read()
                #rich_progress.console.log(progress)
                known_total = progress.steps_count*100
                rich_progress.update(global_id,
                                     completed=progress.step*100+progress.percent,
                                     total=known_total)
                if last_step != progress.step:
                    if last_step is not None:
                        rich_progress.update(step_id, completed=100)
                    else:
                        rich_progress.console.print('Total steps:',
                                                    progress.steps_count)
                    last_step = progress.step
                    step_id = rich_progress.add_task(
                        f'Step {progress.step}', total=100)
                rich_progress.update(step_id, completed=progress.percent)
        rich_progress.update(step_id, completed=100)
        rich_progress.update(global_id, completed=known_total)
        #rich_progress.console.print('installation ended with code:', rc)
        if rc != 0:
            # success, update db to say installed
            status_db.set_installed(game_info)
    print('ended with code:', rc)