Stateful.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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():
  48. pass
  49. def Operate_sqlite3(dbPath, match_commands):
  50. matchedSyntax = MatchTomlKeys("dev_config.toml", match_commands, "sqlite3")
  51. s = "".join(matchedSyntax).format(tableName = match_commands[1], objName = match_commands[2])
  52. con = sqlite3.connect(dbPath)
  53. cur = con.cursor()
  54. res = cur.execute(s)
  55. res.fetchone()
  56. # if res = None:
  57. con.close()
  58. # Markdown
  59. # csv
  60. # MongoDB
  61. # ----- Transit Command Handler -----
  62. def PackHandler(app_commands):
  63. dbType = app_commands[-1]
  64. dbPath = app_commands[-2]
  65. if dbType == "sqlite3":
  66. Operate_sqlite3(dbPath, app_commands)
  67. elif dbType == "csv":
  68. pass
  69. elif dbType == "mongodb":
  70. pass
  71. elif dbType == "toml":
  72. pass
  73. elif dbType == "md":
  74. pass
  75. else:
  76. input("error 1: could not found correct Data Base")
  77. exit()
  78. if __name__ == "__main__":
  79. a_c = ['/', 'test.db', 'sqlite3']
  80. a_c1 = ['add', 'board', 'testBoardName', 'test.db', 'sqlite3']
  81. a_c2 = ['edit', 'board', 't_boardName', 'to', 't_newBoardName', 'test.db', 'sqlite3']
  82. e_c = ['add', 'board']
  83. # Operate_sqlite3("test.db", a_c1)
  84. PackHandler(a_c2)