summaryrefslogtreecommitdiff
path: root/tools/libdol2asm/context.py
blob: 82c4d09ad8b6b4c11f43b658a4421aef497bca02 (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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
from multiprocessing import Queue
from dataclasses import dataclass, field
from typing import Any

from .globals import *

@dataclass
class Context:
    """
    The context is used to provide ways for multiprocessing 
    code to print and talk to the main process.
    """

    index: int
    output: Queue

    def send_command(self, command, *args):
        self.output.put((command, (*args,)))

    def debug(self, *args):
        self.send_command('debug', *args)

    def warning(self, *args):
        self.send_command('warning', *args)

    def error(self, *args):
        self.send_command('error', *args)

    def info(self, *args):
        self.send_command('info', *args)

    def complete(self, result=None):
        self.send_command('complete', self.index, result)

    def exception(self, traceback):
        self.send_command('exception', self.index, traceback)

    def exit(self):
        self.send_command('exit', 0)

@dataclass
class MainContext(Context):
    """ Context that is ONLY used on the main process. """
    
    def send_command(self, command, *args):
        if command == 'debug':
            debug(*args)
        elif command == 'warning':
            warning(*args)
        elif command == 'info':
            info(*args)
        elif command == 'error':
            error(*args)
        elif self.output:
            self.output.put((command, (*args,)))