Stateful.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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="", after_to:str=""):
  109. if addType == "board":
  110. ie = IsExist("Board", addName)
  111. if ie == True:
  112. print("err <Code>: Board already exist")
  113. elif ie == False:
  114. sqls = "INSERT INTO Board VALUES(null, '{name}', 'alive');".format(name=self.name)
  115. elif addType == "class":
  116. # 1. 检查cl存在
  117. ie = IsExist("Class", addName)
  118. # 2. 有没有指定目标(after_to)
  119. if ie == False:
  120. if after_to == "" and len(self.cp) == 1:
  121. print("err <Code>: syntax error")
  122. elif
  123. elif ie == True:
  124. pass
  125. # 3. 没有指定目标的 情况下, 当前路径(dp)有没有可以输入的; 没有就err
  126. elif addType == "event":
  127. pass
  128. res = Exec_one(self.dp, sqls)
  129. def delete(self):
  130. sqls = "UPDATE {table} SET status='deleted' WHERE name='{name}';".format(table=self.table, name=self.name)
  131. res = Exec_one(self.dp, sqls)
  132. def edit(self):
  133. # 感觉应该调二级响应去做吧...
  134. # edit boardName
  135. # edit className
  136. # edit eventName
  137. # edit event dscrp
  138. # edit event blabla
  139. pass
  140. def move(self, moveObj):
  141. if moveObj == "KB":
  142. print("err <Code>: syntax error")
  143. elif moveObj == "CL":
  144. pass
  145. elif moveObj == "EV":
  146. pass
  147. def back(self, backType, backPath=""):
  148. if backType == "home":
  149. sqls = "SELECT name FROM sqlite_master WHERE type='table' AND name is NOT 'sqlite_sequence';"
  150. elif backType == "previous":
  151. sqls = "SELECT name FROM {table} WHERE name='{}'"
  152. Exec_one(dbPath, sqls)
  153. def Handler():
  154. pass
  155. if __name__ == "__main__":
  156. dbPath = "dev.db"