aboutsummaryrefslogtreecommitdiff
path: root/tools/python/maps_generator/generator/status.py
blob: 6dbd84c97b4a8a5e13f4323e591dbcbef4ce409c (plain) (blame)
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
import os
from typing import AnyStr
from typing import Optional


def with_stat_ext(country: AnyStr):
    return f"{country}.status"


def without_stat_ext(status: AnyStr):
    return status.replace(".status", "")


class Status:
    """Status is used for recovering and continuation maps generation."""

    def __init__(
        self, stat_path: Optional[AnyStr] = None, stat_next: Optional[AnyStr] = None
    ):
        self.stat_path = stat_path
        self.stat_next = stat_next
        self.stat_saved = None
        self.find = False

    def init(self, stat_path: AnyStr, stat_next: AnyStr):
        self.stat_path = stat_path
        self.stat_next = stat_next
        self.stat_saved = self.status()
        if not self.find:
            self.find = not self.stat_saved or not self.need_skip()

    def need_skip(self) -> bool:
        if self.find:
            return False
        return self.stat_saved and self.stat_next != self.stat_saved

    def update_status(self):
        with open(self.stat_path, "w") as status:
            status.write(self.stat_next)

    def finish(self):
        with open(self.stat_path, "w") as status:
            status.write("finish")

    def is_finished(self):
        return self.status() == "finish"

    def status(self):
        try:
            with open(self.stat_path) as status:
                return status.read()
        except IOError:
            return None