Stateful.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. # from Controller import kbc_config
  2. import Alt
  3. import tomlkit, sqlite3
  4. # ----- Operating Cursor -----
  5. oc = {
  6. "dt":str,
  7. "dp":str,
  8. "cp":list,
  9. "pp":list,
  10. "next_move":str,
  11. "tp":list,
  12. "tp_in":list,
  13. "tp_to":list,
  14. "tp_attr":str
  15. }
  16. # some thoughts:
  17. # class oc():
  18. # def get_oc(), def move_oc()
  19. # ----- Toml Methods -----
  20. def GetTomlDoc(tomlName:str):
  21. try:
  22. with open(tomlName, "rb") as t:
  23. doc = tomlkit.load(t)
  24. if doc == {}:
  25. input("error 0: could not found correct config file")
  26. exit()
  27. return doc
  28. except:
  29. input("error 0: could not found correct config file")
  30. exit()
  31. def MatchTomlKey(tomlName:str, key:str, table:str="none") -> str:
  32. doc = GetTomlDoc(tomlName)
  33. if table == "none":
  34. return str(doc.item(key))
  35. elif table != "none":
  36. d = doc.unwrap()
  37. return str(d[table][key])
  38. # no differernt between ↑ MatchTomlKey() ↑ except receives and returns in list
  39. def MatchTomlKeys(tomlName:str, keys:list, table:str="none") -> list:
  40. doc = GetTomlDoc(tomlName)
  41. if table == "none":
  42. # rl == 'r'eturn 'l'ist
  43. rl = []
  44. for key in keys:
  45. try:
  46. rl.append(doc.item(key))
  47. except:
  48. pass
  49. return rl
  50. elif table != "none":
  51. rl = []
  52. d = doc.unwrap()
  53. for key in keys:
  54. try:
  55. rl.append(d[table][key])
  56. except:
  57. pass
  58. return rl
  59. def MatchTomlTable(tomlName:str, tableName:str, returnType:str="list"):
  60. d = GetTomlDoc(tomlName).unwrap()
  61. if returnType == "list":
  62. return list(d.get(tableName).values())
  63. elif returnType == "dict":
  64. return d
  65. # ----- Sqlite Methods -----
  66. def Exec_one(dbPath:str, commands:list):
  67. con = sqlite3.connect(dbPath)
  68. cur = con.cursor()
  69. cur.execute(commands)
  70. con.commit()
  71. re = cur.fetchall()
  72. cur.close()
  73. con.close()
  74. return re
  75. def IsExist(dbPath:str, tableName:str, itemName:str, returnBool:bool=True):
  76. # [todo 4] 这里面的.capitalize()后面需要根据config.toml里面的内容判断
  77. # 可能也不用, 因为KBCLEV的表名和本身并无关系
  78. tableName = tableName.capitalize()
  79. sqls = "SELECT name FROM {table} WHERE name='{name}'".format(table=tableName, name=itemName)
  80. ie = Exec_one(dbPath, sqls)
  81. if ie != [] and returnBool == False:
  82. return ie
  83. elif ie != [] and returnBool == True:
  84. return True
  85. elif ie == []:
  86. return False
  87. else:
  88. # Alt.Err(errCode)
  89. print("err <Code>: unexpected error in existence check")
  90. # ----- Record_main(DB record as a class) -----
  91. class RM():
  92. def __init__(self, type:str="", name:str="", dscrp:str="", creator:str="", relatedBoard:str="", relatedClass:str="", status:int=10) -> None:
  93. self.id = "null"
  94. self.type = type
  95. self.name = name
  96. self.dscrp = dscrp
  97. self.creator = creator
  98. self.createdTime = "datetime('now')"
  99. self.relatedBoard = relatedBoard
  100. self.relatedClass = relatedClass
  101. self.status = status
  102. def select(self, selectColumn:str="name", aliveOnly:bool=True):
  103. if aliveOnly == True:
  104. sqls = "SELECT {sc} FROM compact_main WHERE type='{s.type}' AND name='{s.name}' AND realatedBoard='{s.relatedBoard}' AND relatedClass='{s.relatedClass}' AND status={s.status};".format(sc=selectColumn, s=self)
  105. elif aliveOnly == False:
  106. sqls = "SELECT {sc} FROM compact_main WHERE type='{s.type}' AND name='{s.name}' AND realatedBoard='{s.relatedBoard}' AND relatedClass='{s.relatedClass}';".format(sc=selectColumn, s=self)
  107. return sqls
  108. def add(self):
  109. sqls = "INSERT INTO compact_main VALUES({a.id}, '{a.type}', '{a.name}', '{a.dscrp}', '{a.creator}', '{a.createdTime}', '{a.relatedBoard}', '{a.relatedClass}', {a.status});".format(a=self)
  110. return sqls
  111. def delete(self): # 1. withStatus:int=10? 2. withStatement >= or > or <?
  112. sqls = "UPDATE compact_main SET status=-10 WHERE type='{d.type}' AND name='{d.name}' AND relatedBoard='{d.relatedBoard}' AND relatedClass='{d.relatedClass}';".format(d=self)
  113. return sqls
  114. def edit(self):
  115. pass
  116. def move(self):
  117. pass
  118. def back(self):
  119. pass
  120. def export():
  121. pass
  122. # ----- Record_log_action(DB record as a class) -----
  123. class RLA():
  124. pass
  125. def GenModel():
  126. # model IE?
  127. # over write?
  128. # exec
  129. pass
  130. # ----- Interactions Handler(with other .py) -----
  131. def Handler():
  132. pass
  133. if __name__ == "__main__":
  134. asd = RM("board", "KB", "", "n.HE", "", "", )
  135. print(asd.select())
  136. input("hold: ")