Stateful.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import Alt
  2. import uuid
  3. import tomlkit, sqlite3
  4. # Place for uid
  5. # ----- Methods binding app command to model -----
  6. # Toml
  7. def GetTomlDoc(tomlName):
  8. try:
  9. with open(tomlName, "rb") as t:
  10. doc = tomlkit.load(t)
  11. if doc == {}:
  12. input("error 0: could not found correct config file")
  13. exit()
  14. return doc
  15. except:
  16. input("error 0: could not found correct config file")
  17. exit()
  18. def MatchTomlKey(tomlName, key, table=None) -> str:
  19. doc = GetTomlDoc(tomlName)
  20. if table == None:
  21. return str(doc.item(key))
  22. elif table != None:
  23. d = doc.unwrap()
  24. return str(d[table][key])
  25. # no differernt between ↑ MatchTomlKey() ↑ except receives and returns in list
  26. def MatchTomlKeys(tomlName, keys, table=None) -> list:
  27. doc = GetTomlDoc(tomlName)
  28. if table == None:
  29. # rl == 'r'eturn 'l'ist
  30. rl = []
  31. for key in keys:
  32. try:
  33. rl.append(doc.item(key))
  34. except:
  35. pass
  36. return rl
  37. elif table != None:
  38. rl = []
  39. d = doc.unwrap()
  40. for key in keys:
  41. try:
  42. rl.append(d[table][key])
  43. except:
  44. pass
  45. return rl
  46. # Sqlite3
  47. def Exist_in_sqlite3(tableName, columnName=None, recordName=None):
  48. if columnName == None:
  49. s = "SELECT name FROM sqlite_master WHERE type='table' AND name='{tableName}';".format(tableName = tableName)
  50. if columnName != None:
  51. s = "SELECT * FROM {tableName} WHERE {columnName}={recordName}".format(tableName = tableName, columnName = columnName, recordName = recordName)
  52. def Operate_sqlite3(dbPath, match_commands):
  53. matchedSyntax = MatchTomlKeys("dev_config.toml", match_commands, "sqlite3")
  54. # 需要检查的类型
  55. # Board, 禁止重复
  56. # Class, 重复就update一个引用关系
  57. # Event, 同一个Board里面不允许重复, 不同Board里可以重复
  58. s = "".join(matchedSyntax).format(tableName = match_commands[1], objName = match_commands[2])
  59. con = sqlite3.connect(dbPath)
  60. cur = con.cursor()
  61. # is_exist()
  62. res = cur.execute(s)
  63. res.fetchone()
  64. # if res = None:
  65. con.close()
  66. # Markdown
  67. # csv
  68. # MongoDB
  69. # ----- Transit Command Handler -----
  70. def PackHandler(app_commands):
  71. dbType = app_commands[-1]
  72. dbPath = app_commands[-2]
  73. if dbType == "sqlite3":
  74. Operate_sqlite3(dbPath, app_commands)
  75. elif dbType == "csv":
  76. pass
  77. elif dbType == "mongodb":
  78. pass
  79. elif dbType == "toml":
  80. pass
  81. elif dbType == "md":
  82. pass
  83. else:
  84. input("error 1: could not found correct Data Base")
  85. exit()
  86. if __name__ == "__main__":
  87. a_c = ['/', 'test.db', 'sqlite3']
  88. a_c1 = ['add', 'board', 'testBoardName', 'test.db', 'sqlite3']
  89. a_c2 = ['edit', 'board', 't_boardName', 'to', 't_newBoardName', 'test.db', 'sqlite3']
  90. e_c = ['add', 'board']
  91. # Operate_sqlite3("test.db", a_c1)
  92. PackHandler(a_c2)