Jt.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. from hashlib import md5
  2. from os import listdir
  3. import pickle
  4. import random
  5. import time
  6. class Base:
  7. def __init__(self):
  8. self.create_time = time.localtime()
  9. self.str_time = time.strftime("%Y%m%d%H%M%S", self.create_time)
  10. self.__gene_id()
  11. def __gene_id(self):
  12. self.__id_seed: str = self.str_time + str(random.randint(10000, 99999))
  13. self.id: str = md5(self.__id_seed.encode("UTF-8")).hexdigest()
  14. class Entry(Base):
  15. def __init__(self):
  16. Base.__init__(self)
  17. self.title: str = "未命名事项" + self.str_time[4:8]
  18. Base.__gene_id(self)
  19. self.content: str = "在这里输入事项内容"
  20. self.set_priority()
  21. self.set_deadline()
  22. self.sub_entries_dict: dict = {}
  23. def set_priority(self, priority: float = 0):
  24. if priority < -10:
  25. print("最低优先级为-10,已自动设置为-10")
  26. priority = -10.0
  27. elif priority > 10:
  28. print("最高优先级为10,已自动设置为10")
  29. priority = 10.0
  30. self.priority: float = priority
  31. def set_deadline(self, deadline_date: time.struct_time = time.localtime()):
  32. if deadline_date < time.localtime():
  33. print("\033[1;31mERROR: Let it RIP if it's dead already\033[0m")
  34. return 1
  35. self.deadline = deadline_date
  36. return 0
  37. def set_content(self, content: str):
  38. self.content = content
  39. class Coordinate(Base):
  40. def __init__(self):
  41. Base.__init__(self)
  42. self.name = "未命名坐标系" + self.str_time
  43. # Base.__gene_id(self)
  44. self.entries_dict = {}
  45. self.entries_num = 0
  46. def show_entries(self):
  47. print(self.entries_dict)
  48. def add_entry(self, entry_in: Entry):
  49. self.entries_dict[entry_in.entry_id] = entry_in
  50. self.entries_num += 1
  51. def del_entry(self, entry_in: Entry):
  52. try:
  53. del self.entries_dict[entry_in.entry_id]
  54. self.entries_num -= 1
  55. except KeyError:
  56. print("\033[1;31mERROR: Entry doesn't exist\033[0m")
  57. def save_coord(self):
  58. with open("coord/" + self.name + ".crd", "wb") as f:
  59. pickle.dump(self, f, pickle.HIGHEST_PROTOCOL)
  60. def __str__(self) -> str:
  61. return (
  62. "Title: "
  63. + self.name
  64. + "\nId: "
  65. + self.id
  66. + "\nEntries number: "
  67. + str(self.entries_num)
  68. )
  69. class Interface:
  70. def __init__(self):
  71. self.coordinate_dict: dict = {}
  72. self.exit: bool = False
  73. self.func_dict = {
  74. "exit": self.__exit,
  75. "open": self.__open_coord,
  76. "ls": self.__list_coord,
  77. "show": self.__show_now_coord,
  78. "entry": self.__new_entry,
  79. }
  80. def start(self):
  81. print("Welcome to Jt mission manager")
  82. print("Please open a coordinate")
  83. self.__list_coord()
  84. coord_file = self.files[
  85. int(input("\033[1;32mType in the number of coordinate:\033[0m"))
  86. ]
  87. self.__open_coord("coord/" + coord_file)
  88. while not (self.exit):
  89. command = input("\033[1;32m> \033[0m").split()
  90. func_name = command[0]
  91. try:
  92. func = self.func_dict[func_name]
  93. func(command[1:])
  94. except KeyError:
  95. print("\033[1;31mERROR: Command not found\033[0m")
  96. def __open_coord(self, coord_file: str):
  97. with open(coord_file, "rb") as f:
  98. self.now_coord = pickle.load(f)
  99. def __list_coord(self):
  100. self.files = listdir("coord")
  101. if self.files:
  102. i = 0
  103. for f in self.files:
  104. print(i, f)
  105. i += 1
  106. def __new_coord(self, arg: list):
  107. c = Coordinate()
  108. c.name = arg[0]
  109. raise NotImplementedError
  110. def __show_now_coord(self, arg: list):
  111. print(self.now_coord)
  112. def __add_coordinate(self, c: Coordinate):
  113. if not c.id in self.coordinate_dict:
  114. self.coordinate_dict[c.id] = c
  115. else:
  116. print("\033[1;31mERROR: It's already in there\033[0m")
  117. def __exit(self, arg):
  118. self.exit = True
  119. def main():
  120. i = Interface()
  121. i.start()
  122. if __name__ == "__main__":
  123. main()