Stateful.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. import Alt
  2. import tomlkit, sqlite3
  3. # uid related
  4. # ----- Toml Methods -----
  5. def GetTomlDoc(tomlName:str):
  6. try:
  7. with open(tomlName, "rb") as t:
  8. doc = tomlkit.load(t)
  9. if doc == {}:
  10. input("error 0: could not found correct config file")
  11. exit()
  12. return doc
  13. except:
  14. input("error 0: could not found correct config file")
  15. exit()
  16. def MatchTomlKey(tomlName:str, key:str, table:str="none") -> str:
  17. doc = GetTomlDoc(tomlName)
  18. if table == "none":
  19. return str(doc.item(key))
  20. elif table != "none":
  21. d = doc.unwrap()
  22. return str(d[table][key])
  23. # no differernt between ↑ MatchTomlKey() ↑ except receives and returns in list
  24. def MatchTomlKeys(tomlName:str, keys:list, table:str="none") -> list:
  25. doc = GetTomlDoc(tomlName)
  26. if table == "none":
  27. # rl == 'r'eturn 'l'ist
  28. rl = []
  29. for key in keys:
  30. try:
  31. rl.append(doc.item(key))
  32. except:
  33. pass
  34. return rl
  35. elif table != "none":
  36. rl = []
  37. d = doc.unwrap()
  38. for key in keys:
  39. try:
  40. rl.append(d[table][key])
  41. except:
  42. pass
  43. return rl
  44. def MatchTomlTable(tomlName:str, tableName:str, returnType:str="list"):
  45. d = GetTomlDoc(tomlName).unwrap()
  46. if returnType == "list":
  47. return list(d.get(tableName).values())
  48. elif returnType == "dict":
  49. return d
  50. # ----- Sqlite Methods -----
  51. def Exec_one(dbPath:str, commands:list):
  52. con = sqlite3.connect(dbPath)
  53. cur = con.cursor()
  54. cur.execute(commands)
  55. con.commit()
  56. re = cur.fetchall()
  57. cur.close()
  58. con.close()
  59. return re
  60. def IsExist(tableName:str, itemName:str, returnBool:bool=True):
  61. # [todo 4] 这里面的.capitalize()后面需要根据config.toml里面的内容判断
  62. # 可能也不用, 因为KBCLEV的表名和本身并无关系
  63. tableName = tableName.capitalize()
  64. sqls = "SELECT name FROM {table} WHERE name='{name}'".format(table=tableName, name=itemName)
  65. ie = Exec_one(dbPath, sqls)
  66. if ie != [] and returnBool == False:
  67. return ie
  68. elif ie != [] and returnBool == True:
  69. return True
  70. elif ie == []:
  71. return False
  72. else:
  73. # Alt.Err(errCode)
  74. print("err <Code>: unexpected error in existence check")
  75. class objBoard():
  76. pass
  77. class objClass():
  78. pass
  79. class objEvent():
  80. pass
  81. # 把config里面的DB config写成对象?
  82. def BuildObj():
  83. pass
  84. def GenModel():# Controller里面还有一个一样的方法
  85. # model IE?
  86. # over write?
  87. # exec
  88. pass
  89. # ↓ 'OC' == 'Operating Cursor'
  90. class OC():
  91. def __init__(self, currentPath:list, previousPath:list, dbType:str, dbPath:str, tableName:str, columnName:str, newColumnName:str) -> None:
  92. self.cp = currentPath
  93. self.pp = previousPath
  94. self.dt = dbType
  95. self.dp = dbPath
  96. self.table = tableName
  97. self.name = columnName
  98. self.newName = newColumnName
  99. def select(self, aliveOnly:bool=True):# KAO, what about unclassified...
  100. if aliveOnly == True:
  101. # → ↓不对啊这样的话分不出来事件的分类了
  102. sqls = "SELECT name FROM {table} WHERE name='{name}' AND status='alive' or status='unclassified';".format(table=self.table, name=self.name)
  103. elif aliveOnly == False:
  104. sqls = "SELECT name FROM {table} WHERE name='{name}';".format(table=self.table, name=self.name)
  105. res = Exec_one(self.dp, sqls)
  106. # 也许需要一个后处理res = [('a'),('b')]这样的格式问题
  107. return res
  108. def add(self, addType:str="board", addName:str="none", after_to:str="none"):
  109. # Logic see also: /else/addBoardLogic.png
  110. if addType == "board":
  111. ie = IsExist("Board", addName)
  112. if ie == True:
  113. print("err <Code>: Board already exist")
  114. elif ie == False:
  115. sqls = "INSERT INTO Board VALUES(null, '{name}', 'alive');".format(name=self.name)
  116. # Logic see also: /else/addClassLogic.png
  117. elif addType == "class":
  118. ie = IsExist("Class", addName)
  119. if ie == True:
  120. if after_to != "none":
  121. res = IsExist("Board", after_to)
  122. if res == True:
  123. sqls = "SELECT usingBoard FROM Class WHERE name='{name}';".format(name=addName)
  124. # 'ub' == 'usingBoard'
  125. ub = Exec_one(self.dp, ub)
  126. ub = ub + after_to
  127. sqls = "UPDATE Class SET usingBoard='{usingBoard}' WHERE name='{name}';".format(usingBoard=ub, name=addName)
  128. elif res == False:
  129. print("err <Code>: syntax error")
  130. elif after_to == "none":
  131. if len(self.cp) < 2:
  132. print("err <Code>: syntax error")
  133. elif len(self.cp) >= 2:
  134. sqls = "SELECT usingBoard FROM Class WHERE name='{name}';".format(name=addName)
  135. # 'ub' == 'usingBoard'
  136. ub = Exec_one(self.dp, ub)
  137. ub = ub + self.cp[1]
  138. sqls = "UPDATE Class SET usingBoard='{usingBoard}' WHERE name='{name}';".format(usingBoard=ub, name=addName)
  139. elif ie == False:
  140. if after_to != "none":
  141. res = IsExist("Board", after_to)
  142. if res == True:
  143. sqls = "INSERT INTO Class VALUES(null, '{name}', '{usingBoard}', 'alive');".format(name=addName, usingBoard=after_to)
  144. elif res == False:
  145. print("err <Code>: syntax error")
  146. elif after_to == "none":
  147. if len(self.cp) < 2:
  148. print("err <Code>: syntax error")
  149. elif len(self.cp) >= 2:
  150. ub = self.cp[1]
  151. sqls = "INSERT INTO Class VALUES(null, '{name}', '{usingBoard}', 'alive');".format(name=addName, usingBoard=ub)
  152. # Logic see also: /else/addEventLogic.png
  153. elif addType == "event":
  154. pass
  155. res = Exec_one(self.dp, sqls)
  156. def delete(self):
  157. sqls = "UPDATE {table} SET status='deleted' WHERE name='{name}';".format(table=self.table, name=self.name)
  158. res = Exec_one(self.dp, sqls)
  159. def edit(self):
  160. # 感觉应该调二级响应去做吧...
  161. # edit boardName
  162. # edit className
  163. # edit eventName
  164. # edit event dscrp
  165. # edit event blabla
  166. pass
  167. def move(self, moveObj):
  168. if moveObj == "KB":
  169. print("err <Code>: syntax error")
  170. elif moveObj == "CL":
  171. pass
  172. elif moveObj == "EV":
  173. pass
  174. def back(self, backType, backPath=""):
  175. if backType == "home":
  176. sqls = "SELECT name FROM sqlite_master WHERE type='table' AND name is NOT 'sqlite_sequence';"
  177. elif backType == "previous":
  178. sqls = "SELECT name FROM {table} WHERE name='{}'"
  179. Exec_one(dbPath, sqls)
  180. def Handler():
  181. pass
  182. if __name__ == "__main__":
  183. dbPath = "dev.db"