Jt.py 5.2 KB

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