Jt.py 6.5 KB

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