2021-05-06 00:59:07 +12:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# --!-- coding: utf8 --!--
|
|
|
|
|
2022-11-19 11:32:08 +13:00
|
|
|
import collections
|
2021-05-06 00:59:07 +12:00
|
|
|
import os
|
|
|
|
|
2023-02-05 04:11:18 +13:00
|
|
|
from manuskript.data.abstractData import AbstractData
|
2021-05-06 00:59:07 +12:00
|
|
|
from manuskript.io.textFile import TextFile
|
|
|
|
|
|
|
|
|
|
|
|
class Status:
|
|
|
|
|
2022-11-20 02:59:51 +13:00
|
|
|
def __init__(self, host, index: int, name: str):
|
2021-05-06 00:59:07 +12:00
|
|
|
self.host = host
|
2022-11-20 02:59:51 +13:00
|
|
|
self.ID = index
|
2021-05-06 00:59:07 +12:00
|
|
|
|
|
|
|
self.name = name
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
|
|
|
def load(self):
|
|
|
|
self.host.load()
|
|
|
|
|
|
|
|
def save(self):
|
|
|
|
self.host.save()
|
|
|
|
|
|
|
|
|
2023-02-05 04:11:18 +13:00
|
|
|
class StatusHost(AbstractData):
|
2021-05-06 00:59:07 +12:00
|
|
|
|
|
|
|
def __init__(self, path):
|
2023-02-05 04:11:18 +13:00
|
|
|
AbstractData.__init__(self, os.path.join(path, "status.txt"))
|
|
|
|
self.file = TextFile(self.dataPath)
|
2022-11-19 11:32:08 +13:00
|
|
|
self.statuses = collections.OrderedDict()
|
2021-05-06 00:59:07 +12:00
|
|
|
|
2023-03-22 11:37:17 +13:00
|
|
|
def changePath(self, path: str):
|
|
|
|
AbstractData.changePath(self, os.path.join(path, "status.txt"))
|
|
|
|
self.file = TextFile(self.dataPath)
|
|
|
|
|
2022-11-20 02:36:57 +13:00
|
|
|
def addStatus(self, name: str = None) -> Status:
|
2022-11-19 11:32:08 +13:00
|
|
|
if name is None:
|
|
|
|
name = "New Status"
|
|
|
|
|
2022-11-20 02:59:51 +13:00
|
|
|
status = Status(self, 1+len(self.statuses), name)
|
2022-11-19 11:32:08 +13:00
|
|
|
self.statuses[name] = status
|
|
|
|
return status
|
2021-05-06 00:59:07 +12:00
|
|
|
|
2022-11-20 02:36:57 +13:00
|
|
|
def removeStatus(self, name: str) -> Status:
|
|
|
|
return self.statuses.pop(name)
|
2021-05-06 00:59:07 +12:00
|
|
|
|
|
|
|
def renameStatus(self, oldName: str, newName: str):
|
2022-11-20 02:36:57 +13:00
|
|
|
match = False
|
|
|
|
drop = [oldName]
|
2021-05-06 00:59:07 +12:00
|
|
|
|
2022-11-20 02:36:57 +13:00
|
|
|
for name, status in self.statuses.items():
|
|
|
|
if match:
|
|
|
|
drop.append(name)
|
|
|
|
elif name == oldName:
|
|
|
|
status.name = newName
|
|
|
|
match = True
|
|
|
|
|
|
|
|
if not match:
|
|
|
|
return
|
|
|
|
|
|
|
|
statuses = list()
|
|
|
|
for name in drop:
|
|
|
|
statuses.append(self.statuses.pop(name))
|
|
|
|
|
|
|
|
for status in statuses:
|
|
|
|
self.statuses[status.name] = status
|
|
|
|
|
|
|
|
def getStatus(self, name: str) -> Status:
|
2021-05-06 00:59:07 +12:00
|
|
|
return self.statuses.get(name)
|
|
|
|
|
2022-11-20 02:59:51 +13:00
|
|
|
def getStatusByID(self, ID: int | None) -> Status | None:
|
|
|
|
if ID is None:
|
|
|
|
return None
|
2022-11-20 02:36:57 +13:00
|
|
|
|
2022-11-20 02:59:51 +13:00
|
|
|
index = 1
|
2022-11-20 02:36:57 +13:00
|
|
|
for status in self.statuses.values():
|
2022-11-20 02:59:51 +13:00
|
|
|
if index == ID:
|
|
|
|
assert status.ID == ID
|
2022-11-20 02:36:57 +13:00
|
|
|
return status
|
|
|
|
|
|
|
|
index += 1
|
|
|
|
|
|
|
|
return None
|
|
|
|
|
2021-05-06 00:59:07 +12:00
|
|
|
def __iter__(self):
|
|
|
|
return self.statuses.values().__iter__()
|
|
|
|
|
|
|
|
def load(self):
|
2023-02-05 04:11:18 +13:00
|
|
|
self.statuses.clear()
|
|
|
|
|
|
|
|
AbstractData.load(self)
|
|
|
|
|
2021-05-06 03:53:58 +12:00
|
|
|
try:
|
|
|
|
text = self.file.load()
|
|
|
|
except FileNotFoundError:
|
2023-02-05 04:11:18 +13:00
|
|
|
self.complete(False)
|
2021-05-06 03:53:58 +12:00
|
|
|
return
|
2021-05-06 00:59:07 +12:00
|
|
|
|
2022-11-01 01:00:21 +13:00
|
|
|
if (len(text) <= 1) or (text[len(text) - 1] != "\n"):
|
2023-02-05 04:11:18 +13:00
|
|
|
self.complete(False)
|
2021-05-06 00:59:07 +12:00
|
|
|
return
|
|
|
|
|
|
|
|
text = text[:-1]
|
|
|
|
if len(text) <= 0:
|
2023-02-05 04:11:18 +13:00
|
|
|
self.complete(False)
|
2021-05-06 00:59:07 +12:00
|
|
|
return
|
|
|
|
|
|
|
|
for name in text.split("\n"):
|
|
|
|
self.addStatus(name)
|
|
|
|
|
2023-02-05 04:11:18 +13:00
|
|
|
self.complete()
|
|
|
|
|
2021-05-06 00:59:07 +12:00
|
|
|
def save(self):
|
2023-02-05 04:11:18 +13:00
|
|
|
AbstractData.save(self)
|
|
|
|
|
2021-05-06 00:59:07 +12:00
|
|
|
self.file.save("\n".join(self.statuses.keys()) + "\n")
|
2023-02-05 04:11:18 +13:00
|
|
|
self.complete()
|