aboutsummaryrefslogtreecommitdiff
path: root/mcwrapper/status.py
blob: 42f8ed81ed00b1547cc78ad465a6c1172e76e4aa (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
56
57
58
# vim: expandtab ft=python ts=4 sw=4 sts=4:
import os

from . import prints

__STATUSSTRINGS__ = {
    0: "Not running",
    1: "Starting",
    2: "Running",
    3: "Stopping",
    }
__STATUSFILE__ = 'status'


class MCStatus:
    "Tracks server status"
    def __init__(self, wrapper, file_export=False):
        self.wrapper = wrapper
        self.status = 0
        wrapper.hook_start(self.__server_start__)
        wrapper.hook_stop(self.__server_stop__)
        wrapper.hook_line(': Done', self.__server_started__)
        wrapper.hook_line(': Stopping the server', self.__server_stopping__)
        self.file_export = file_export
        if file_export:
            with open(__STATUSFILE__, 'w') as file:
                file.write(__STATUSSTRINGS__[0] + '\n')

    def clean(self):
        try:
            os.remove(__STATUSFILE__)
        except FileNotFoundError:
            pass

    def __server_start__(self):
        self.status = 1
        if self.file_export:
            with open(__STATUSFILE__, 'w') as file:
                file.write(__STATUSSTRINGS__[1] + '\n')

    def __server_stop__(self):
        if self.file_export:
            with open(__STATUSFILE__, 'w') as file:
                file.write(__STATUSSTRINGS__[0] + '\n')

    def __server_started__(self, line):
        prints.info("Server start.")
        self.status = 2
        if self.file_export:
            with open(__STATUSFILE__, 'w') as file:
                file.write(__STATUSSTRINGS__[2] + '\n')

    def __server_stopping__(self, line):
        prints.info("Server stop.")
        self.status = 3
        if self.file_export:
            with open(__STATUSFILE__, 'w') as file:
                file.write(__STATUSSTRINGS__[3] + '\n')