archived_Stateful.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import Alt, WithSqlite
  2. import tomlkit
  3. # uid related
  4. # Toml Methods
  5. def GetTomlDoc(tomlName):
  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, key, table=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, keys, table=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. # ----- Transit Command Handler -----
  45. def Handler(app_commands):
  46. dbType = app_commands[-1]
  47. dbPath = app_commands[-2]
  48. # [todo 3] 检查dbPath是否对应dbType, 否的话报错并exit()
  49. exec_commands = app_commands[0:-2]
  50. if dbType == "sqlite3":
  51. WithSqlite.Regular(dbPath, exec_commands)
  52. elif dbType == "csv":
  53. pass
  54. elif dbType == "mongodb":
  55. pass
  56. elif dbType == "toml":
  57. pass
  58. elif dbType == "md":
  59. pass
  60. else:
  61. input("error 1: could not found correct Data Base")
  62. exit()
  63. # ----- Docker related -----
  64. if __name__ == "__main__":
  65. a_c = ['/', 'test.db', 'sqlite3']
  66. a_c1 = ['add', 'board', 'testBoardName', 'test.db', 'sqlite3']
  67. a_c2 = ['edit', 'board', 't_boardName', 'to', 't_newBoardName', 'test.db', 'sqlite3']
  68. e_c = ['add', 'board']
  69. # Operate_sqlite3("test.db", a_c1)
  70. Handler(a_c2)