Stateful.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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(exec_commands:list, returnBool:bool=True):
  61. # [todo 4] 这里面的.capitalize()后面需要根据config.toml里面的内容判断
  62. # 可能也不用, 因为KBCLEV的表名和本身并无关系
  63. tableName = str(exec_commands[1]).capitalize()
  64. ItemName = str(exec_commands[2])
  65. sqls = "SELECT name FROM {table} WHERE name='{name}'".format(table=tableName, name=ItemName)
  66. ie = Exec_one(dbPath, sqls)
  67. if ie != [] and returnBool == False:
  68. return ie
  69. elif ie != [] and returnBool == True:
  70. return True
  71. elif ie == []:
  72. return False
  73. else:
  74. # Alt.Err(errCode)
  75. print("err <Code>: unexpected error in existence check")
  76. class objBoard():
  77. pass
  78. class objClass():
  79. pass
  80. class objEvent():
  81. pass
  82. # 把config里面的DB config写成对象?
  83. def BuildObj():
  84. pass
  85. def GenModel():# Controller里面还有一个一样的方法
  86. # model IE?
  87. # over write?
  88. # exec
  89. pass
  90. # ↓ 'OC' == 'Operating Cursor'
  91. class OC():
  92. def __init__(self, currentPath, previousPath, dbType, dbPath, tableName, columnName, newColumnName) -> None:
  93. self.cp = currentPath
  94. self.pp = previousPath
  95. self.dt = dbType
  96. self.dp = dbPath
  97. self.table = tableName
  98. self.name = columnName
  99. self.newName = newColumnName
  100. def select(self, aliveOnly=True):
  101. if aliveOnly == True:
  102. sqls = "SELECT name FROM {table} WHERE name='{name}' AND status='alive';".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="board"):
  109. if addType == "board":
  110. sqls = "INSERT INTO Board VALUES(null, '{name}', 'alive');".format(name=self.name)
  111. elif addType == "class":
  112. sqls = "INSERT INTO Class VALUES(null, '{name}', '{usingBoard}', 'alive');".format(name=self.name, usingBoard=self.pc)
  113. elif addType == "event":
  114. sqls = "INSERT INTO Event VALUES(null, '{name}', );"
  115. res = Exec_one(self.dp, sqls)
  116. def delete(self):
  117. sqls = "UPDATE {table} SET status='deleted' WHERE name='{name}';".format(table=self.table, name=self.name)
  118. res = Exec_one(self.dp, sqls)
  119. def edit(self):
  120. # 感觉应该调二级响应去做吧...
  121. # edit boardName
  122. # edit className
  123. # edit eventName
  124. # edit event dscrp
  125. # edit event blabla
  126. pass
  127. def move(self, moveObj):
  128. if moveObj == "KB":
  129. print("err <Code>: syntax error")
  130. elif moveObj == "CL":
  131. pass
  132. elif moveObj == "EV":
  133. pass
  134. def back(self, backType, backPath=""):
  135. if backType == "home":
  136. sqls = "SELECT name FROM sqlite_master WHERE type='table' AND name is NOT 'sqlite_sequence';"
  137. elif backType == "previous":
  138. sqls = "SELECT name FROM {table} WHERE name='{}'"
  139. Exec_one(dbPath, sqls)
  140. def Handler():
  141. pass
  142. if __name__ == "__main__":
  143. dbPath = "dev.db"